I am trying to deform a cube, so I need to triangulate the faces to obtain more vertices. I have just tested the back face but got only 2 triangles, spent hours to figure out why but couldn’t figure it out…
here are the significant parts of the code
void
init( void )
{
int indx=0;
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
/*back*/
for (int yi=2;yi>=-2;yi--) {
for (int xi=-2;xi<=2;xi++) {
vertices_faces.push_back(vec3(xi/2.0,yi/2.0,-1.0));
if (xi<2&&yi>-2){
indices.push_back(indx);
indices.push_back(indx+1);
indices.push_back(indx+5);
}
if (xi>-2&&yi>-2){
indices.push_back(indx+5);
indices.push_back(indx+4);
indices.push_back(indx);
}
indx++;
}
}
programID = LoadShaders( "trianglesExp.vert", "trianglesExp.frag" );//"fshader.fshader");
glUseProgram(programID);
glCreateVertexArrays( 1, VAOs );
glBindVertexArray( VAOs[0] );
glCreateBuffers( 2, Buffers );
glBindBuffer( GL_ARRAY_BUFFER, Buffers[0] );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,Buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),&indices[0],GL_DYNAMIC_DRAW);
glBufferData( GL_ARRAY_BUFFER, sizeof(vec3)*vertices_faces.size(), NULL, GL_DYNAMIC_DRAW);
glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(vec3)*vertices_faces.size(), &vertices_faces[0][0]);
glEnable(GL_DEPTH_TEST);
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0 );
glEnableVertexAttribArray( 0 );
}
void
display( void )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
MVPID =glGetUniformLocation(programID, "MVP");
mat4 MV = translate(mat4(1.0f), vec3(0.0f, 0.0f, -8.0f));
mat4 p = ortho(-3.0f,3.0f,-3.0f,3.0f,4.2f,12.0f);
MVP = p * MV;
glUniformMatrix4fv(MVPID, 1, GL_FALSE, &MVP[0][0]);
glDrawElements(GL_TRIANGLES,indices.size(),GL_UNSIGNED_INT,nullptr);
glutSwapBuffers();
}