【问题标题】:Render a scene with openGL使用 openGL 渲染场景
【发布时间】:2023-03-23 19:04:01
【问题描述】:

我必须使用 openGL 渲染一个包含各种网格的场景。网格定义如下:

struct Mesh {
frame3f         frame;      // frame
vector<vec3f>   pos;        // vertex position
vector<vec3f>   norm;       // vertex normal
vector<vec2f>   texcoord;   // vertex texcture coordinates
vector<vec3i>   triangle;   // triangle
vector<vec4i>   quad;       // quad
Material*       mat;        // material}

网格可以由三角形和四边形组成,我尝试使用以下代码渲染顶点:

for (auto mesh : scene->meshes)
{
    // bind material kd, ks, n
    glVertexAttrib3f(material_kd_loc, mesh->mat->kd.x, mesh->mat->kd.y, mesh->mat->kd.z);
    glVertexAttrib3f(material_ks_loc, mesh->mat->ks.x, mesh->mat->ks.y, mesh->mat->ks.z);
    glVertexAttrib1f(material_n_loc, mesh->mat->n);

    // bind mesh frame - use frame_to_matrix
    mat4f mesh_mat = frame_to_matrix(mesh->frame);
    glUniformMatrix4fv(mesh_frame_loc, 1, GL_TRUE, &mesh_mat[0][0]);

    // enable vertex attributes arrays
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    //and set up pointers to the mesh data
    glVertexAttribPointer(vertex_pos_loc, mesh->pos.size(), GL_FLOAT, GL_FALSE, 0, mesh->pos.data());
    glVertexAttribPointer(vertex_norm_loc, mesh->norm.size(), GL_FLOAT, GL_TRUE, 0, mesh->norm.data());

    // draw triangles and quads
    if (mesh->triangle.size() != 0){

        glDrawElements(GL_TRIANGLES, mesh->pos.size(), GL_UNSIGNED_INT, mesh->triangle.data());
    }
    if (mesh->quad.size() != 0){
        glDrawElements(GL_QUADS, mesh->pos.size(), GL_UNSIGNED_INT, mesh->quad.data());

    }
    // disable vertex attribute arrays
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

} 这就是我画的(不要关心颜色,因为我还没有计算它们)

如果我的代码正确,这就是我应该看到的

有人知道错误在哪里吗?

【问题讨论】:

  • 发布您的实际(取自正在运行的程序,而不是某些输入文件)位置、三角形和四边形数组的内容(在给定的示例中它们并不大)。
  • 相机以 0,0,6 为中心这是创建网格的代码:
  • 新网格 { frame3f{ {-offset, offset,0}, x3f, y3f, z3f }, { {0,1,0}, {-1,-1,0}, {1 ,-1,0} }, { z3f, z3f, z3f }, { zero2f, y2f, x2f }, { {0,1,2} }, {}, 新材质{one3f,zero3f,100} }, '
  • 新网格 { frame3f{ { offset, offset,0}, x3f, y3f, z3f }, { {1,1,0}, {-1, 1,0}, {-1, -1,0}, { 1,-1,0} }, { z3f, z3f, z3f, z3f }, { one2f, y2f, zero2f, x2f }, {}, { {0,1,2,3} } , 新材质{one3f,zero3f,100} },
  • 新网格 { identity_frame3f, { {-offset,1-offset,0}, {-1-offset,-1-offset,0}, {1-offset,-1-offset,0 } }, { z3f, z3f, z3f }, { zero2f, y2f, x2f }, { {0,1,2} }, {}, 新材质{one3f,zero3f,100} },

标签: opengl c++11 vertex-array


【解决方案1】:
glDrawElements(GL_TRIANGLES, mesh->pos.size(), GL_UNSIGNED_INT, mesh->triangle.data());
glDrawElements(GL_QUADS, mesh->quad.size(), GL_UNSIGNED_INT, mesh->quad.data());

看看两个调用的第二个参数。四边形是不正确的。

glVertexAttribPointer(vertex_pos_loc, mesh->pos.size(), GL_FLOAT, GL_FALSE, 0, mesh->pos.data());

也不正确 - 第二个参数是每个顶点的组件数,而不是数组的大小。

【讨论】:

  • 抱歉,我粘贴了一个旧版本,现在它已在问题上进行了编辑,但是我还有其他(更复杂的)场景要渲染,它们完全是一团糟,顶点漂浮在任何地方,所以我猜我能正确渲染两个三角形只是运气
  • 谢谢,现在第一个场景看起来不错。但其他场景看起来很奇怪:看起来有些顶点没有被绘制。你可以在这个photobucket album 中看到目标图像和我的图像注意我正在使用我编写的着色器,它几乎什么都不做,但我不能说这是否会导致问题......
【解决方案2】:

看起来 opengl 函数希望写入顶点的总数,而不仅仅是不同顶点的数量。所以:

glDrawElements(GL_TRIANGLES, mesh->triangle.size()*3, GL_UNSIGNED_INT, mesh->triangle.data());
glDrawElements(GL_QUADS, mesh->quad.size()*4, GL_UNSIGNED_INT, mesh->quad.data());

【讨论】:

    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多