我正在尝试绘制金字塔(工作正常)和 Y 轴线(不工作)。
这是相关代码:
class Example
{
bool init();
void render();
GLuint m_vboV;
GLuint m_vboC;
GLuint m_vboI;
vector<GLfloat> m_vertex;
vector<GLfloat> m_colour;
vector<GLuint> m_indice;
};
#define BUFFER_OFFSET(i) ((char *) NULL + (i))
bool Example::init()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
m_vertex.push_back( 0.0F); m_vertex.push_back( 0.5F); m_vertex.push_back( 0.0F); // PYRAMID
m_vertex.push_back(-0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back(-0.5F);
m_vertex.push_back(-0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back( 0.5F);
m_vertex.push_back( 0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back( 0.5F);
m_vertex.push_back( 0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back(-0.5F);
m_vertex.push_back( 0.0F); m_vertex.push_back(-1.0F); m_vertex.push_back( 0.0F); // LINE
m_vertex.push_back( 0.0F); m_vertex.push_back( 1.0F); m_vertex.push_back( 0.0F);
m_indice.push_back(0); // PYRAMID
m_indice.push_back(1);
m_indice.push_back(2);
m_indice.push_back(3);
m_indice.push_back(4);
m_indice.push_back(1);
m_indice.push_back(5); // LINE
m_indice.push_back(6);
m_colour.push_back(0.0F); m_colour.push_back(0.0F); m_colour.push_back(0.0F); // PYRAMID
m_colour.push_back(0.0F); m_colour.push_back(0.0F); m_colour.push_back(1.0F);
m_colour.push_back(0.0F); m_colour.push_back(1.0F); m_colour.push_back(0.0F);
m_colour.push_back(0.0F); m_colour.push_back(1.0F); m_colour.push_back(1.0F);
m_colour.push_back(1.0F); m_colour.push_back(0.0F); m_colour.push_back(0.0F);
m_colour.push_back(1.0F); m_colour.push_back(0.0F); m_colour.push_back(1.0F);
m_colour.push_back(1.0F); m_colour.push_back(1.0F); m_colour.push_back(0.0F); // LINE
m_colour.push_back(1.0F); m_colour.push_back(1.0F); m_colour.push_back(1.0F);
glGenBuffers(1, &m_vboC);
glGenBuffers(1, &m_vboI);
glGenBuffers(1, &m_vboV);
glBindBuffer(GL_ARRAY_BUFFER, m_vboV);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_vertex.size(), &m_vertex[0], GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vboC);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_colour.size(), &m_colour[0], GL_STATIC_DRAW);
glColorPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboI);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indice.size(), &m_indice[0], GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
return true;
}
void Example::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(2, 5, -4, 0, 0, 0, 0, 1, 0);
glDrawElements(GL_TRIANGLE_FAN, 8, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glTranslatef(1.5F, 0.0F, 0.0F);
glDrawElements(GL_LINES, 2, GL_UNSIGNED_INT, BUFFER_OFFSET(6));
}
对 glDrawElements 的第一次调用似乎工作正常,即使值不同。金字塔绘制正确。
然而,第二次调用不会绘制最后两个顶点,而是介于两者之间。如果我更改 BUFFER_OFFSET,它将通过下一个点绘制,但在显示驱动程序崩溃之前永远不会获得最后两个点。
最终的结果应该是一个金字塔,在它的一侧有一条垂直线。
我花了大概 2 个小时以上,尝试了许多不同的设置和值组合。