I’m having what seems to be a bit of an odd error which certainly is my own fault. I’ve just started using OpenGL 3.3, so I’m still feeling my way around.
Here’s a code snippet to give an idea of the error:
mesh.vertex = new NVertex[4];
mesh.index = new unsigned int[6];
mesh.vertex[0].Set(-1,0,0,0,0,1);
mesh.vertex[1].Set(0,1,0,0,1,0);
mesh.vertex[2].Set(0,0,0,1,0,0);
mesh.vertex[3].Set(1,0,0,0,0,1);
mesh.index[0]=0;
mesh.index[1]=1;
mesh.index[2]=2;
mesh.index[3]=1;
mesh.index[4]=2;
mesh.index[5]=3;
glGenBuffers(1, &environment.VBO);
glBindBuffer(GL_ARRAY_BUFFER, environment.VBO);
glBufferData(GL_ARRAY_BUFFER, mesh.size(), &mesh.vertex[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0); //VERTEX COORDINATES
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1); //VERTEX COLORS
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(12));
glGenBuffers(1,&environment.VIO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,environment.VIO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*environment.element_num, mesh.index,GL_STATIC_DRAW);
//...
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
If I run this code, only the first triangle appears. However, if I change it for GL_TRIANGLE_STRIP (and make the necessary adjustments), it works and both triangles appear:
mesh.vertex = new NVertex[4];
mesh.index = new unsigned int[4];
mesh.vertex[0].Set(-1,0,0,0,0,1);
mesh.vertex[1].Set(0,1,0,0,1,0);
mesh.vertex[2].Set(0,0,0,1,0,0);
mesh.vertex[3].Set(1,0,0,0,0,1);
mesh.index[0]=0;
mesh.index[1]=1;
mesh.index[2]=2;
mesh.index[3]=3;
glGenBuffers(1, &environment.VBO);
glBindBuffer(GL_ARRAY_BUFFER, environment.VBO);
glBufferData(GL_ARRAY_BUFFER, mesh.size(), &mesh.vertex[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0); //VERTEX COORDINATES
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1); //VERTEX COLORS
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(12));
glGenBuffers(1,&environment.VIO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,environment.VIO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*environment.element_num, mesh.index,GL_STATIC_DRAW);
//...
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
I’m sure this is a stupid, stupid mistake, but I can’t figure out what it is. Any help?