Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Simple error with glDrawElements(GL_TRIANGLES)

  1. #1
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Simple error with glDrawElements(GL_TRIANGLES)

    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:
    Code :
    	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:
    Code :
    	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?

  2. #2
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    Reverse the winding order of the second triangle (indexes 3/2/1) and it should work.

  3. #3
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    Nope. Still only the left triangle. :S

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    Change this line
    Code :
    mesh.index[0]=0;
    mesh.index[1]=1;
    to
    Code :
    mesh.index[0]=1;
    mesh.index[1]=0;
    and see if u get the right result.
    Reason:
    The first triangle is wound clockwise whereas the second one is counterclockwise. This will work for triangle strip but for triangles you need to maintain the same winding for all of the triangles.
    Regards,
    Mobeen

  5. #5
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    Still no change. Making just that alteration or doing it and mhagain's at the same time doesn't change the result. As well, mhagain's was basically the same idea, but just changing the winding of the second triangle instead of the first.

    As well, isn't CULL_FACE disabled by default? I haven't enabled it, so the winding order (and thus the definition of front or back faces) shouldn't matter that much, should it?

  6. #6
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    463

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    Is environment.element_num returning the right number of indices? (6 in the first case, 4 in the second case) It could be the case that you aren't uploading enough index data.

    GL_CULL_FACE initial value is disabled. It is a common cause of "missing" triangles, although not in your case.

  7. #7
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    My, don't I feel silly. Since I was just testing things, I hard-coded element_num to be exactly what I wanted it to be. Only I'd hard-coded it to the wrong value for some reason.

    As I said in the OP, this was a stupid, stupid mistake. Nevermind me. >.>

  8. #8
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    Or not. Well, those two triangles are working, but now I've got another problem which I'm fairly certain is related, but I can't point it out.

    The code is now
    Code :
    environment.vertex_num = 5;
    	environment.index_num = 9;
    	mesh.vertex = new NVertex[environment.vertex_num];
    	mesh.index = new unsigned int[environment.index_num];
    	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.5,0,1,0,0);
    	mesh.vertex[3].Set(1,0,0,0,0,1);
    	mesh.vertex[4].Set(0.5,-1,0,1,1,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;
     
    	mesh.index[6]=4;
    	mesh.index[7]=3;
    	mesh.index[8]=1;
     
    	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.index_num, mesh.index,GL_STATIC_DRAW);
    	//...
    	glDrawElements(GL_TRIANGLES, environment.index_num, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

    Notice how I do not even have a (0,0,0) coordinate. However, if I now run the program, both of the now-working triangles appear, but the third triangle seems to lose its last vertex, which is drawn as a (0,0,0) point. This time, vertex_num and index_num are right there so that any mistakes become quickly evident.

  9. #9
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    463

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    It could be an issue with how NVertex is declared, the compiler might be putting in extra bytes of padding after the position + color fields for alignment reasons, so you might need to override this somehow, to force NVertex to be tightly packed. Alternatively, if you have other float values that need to be included, you could pack them into the spaces.

  10. #10
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Re: Simple error with glDrawElements(GL_TRIANGLES)

    This is the NVertex class:
    Code :
    class NVertex
    {
    	float x, y, z;
    	float r,g,b;
    public:
    	NVertex();
    	NVertex(float x, float y, float z, float r=1, float g=1, float b=1);
    	void Set(float _x, float _y, float _z, float _r=1, float _g=1, float _b=1);
    	void SetColor(float _r, float _g, float _b);
    	void SetPos(float _x, float _y, float _z);
    };

    It's just floats, which are 4-byte chunks, and some functions with the same privacy setting, which I've been told do not increase the memory footprint of the class in any modern compiler (I'm using VC++2008Express, which I take to be a pretty modern compiler. Can't use 2010 because this project is eventually going to be merged with Qt, which isn't VC++2010-portable yet).

Page 1 of 2 12 LastLast

Similar Threads

  1. error at gldrawelements, for drawing cube
    By saikumar in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-16-2015, 06:51 AM
  2. Replies: 7
    Last Post: 03-01-2013, 09:57 AM
  3. glDrawElements error!!!!
    By Shao tian in forum OpenGL: Basic Coding
    Replies: 17
    Last Post: 02-22-2011, 03:45 AM
  4. Replies: 0
    Last Post: 05-21-2008, 02:16 PM
  5. OpenGL Error With glDrawElements
    By 3DG in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 03-18-2001, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean