在openGL中渲染金字塔

时间:2015-10-13 02:47:30

标签: opengl rendering

我试图在openGL中渲染金字塔作为基本练习,但是顶点的位置没有按预期渲染。我希望金字塔以世界坐标系为中心,我正在使用的点就是这样。

glm::vec3 pos0(-width/2, -height/2, width/2); //front left vertex
glm::vec3 pos1(width/2, -height/2, width/2); //front right vertex
glm::vec3 pos2(-width/2, -height/2, -width/2);//back left vertex
glm::vec3 pos3(width/2, -height/2, -width/2); //back right vertex
glm::vec3 pos4(0.0f, height/2, 0.0f); //top vertex

我按照这个顺序用GL_DRAW_TRIANGLES绘制金字塔,并将CCW显示为正面。

indices.push_back(5); /////////// front face
indices.push_back(2);
indices.push_back(1);
indices.push_back(5); /////////// left face
indices.push_back(3);
indices.push_back(1); 
indices.push_back(5); /////////// back face
indices.push_back(3);
indices.push_back(4);
indices.push_back(5); /////////// right face
indices.push_back(4);
indices.push_back(2); 
indices.push_back(1); /////////// left bottom
indices.push_back(3);
indices.push_back(2); 
indices.push_back(2); /////////// right bottom
indices.push_back(3);
indices.push_back(4);

我希望前三个点是三角形的正面,但出于某种原因,似乎并非如此。颜色不是预期的颜色(每个顶点都有自己的颜色),并且高度几乎不会出现在金字塔的顶部。有没有人发现我的代码有问题?

1 个答案:

答案 0 :(得分:2)

OpenGL索引从0开始。对于5个顶点,索引范围必须为0到4。