Textured glDrawElement

501 views Asked by At

I am having trouble getting my 3D model textured, I am just diving into 3D models as I have been using basic primitives for testing purposes and have been working on more engine specific code. I am however having trouble with the glDrawElements function, I know my texture is loaded properly as I am able to draw it to a quad (note the glBegin in comments). However, when using glDrawElements I do not get the texture drawn. It does show the colour of part of the texture (yellow), and when I remove the texturing code it returns the 3D model to the last colour that has been set, so it is doing something. I am thinking maybe it is something to do with my glVertexAttributePointer , I get what these functions do sort of.

glMatrixMode(GL_MODELVIEW);
    glMultMatrixf(m_Props.WorldMatrix.data);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPTN), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPTN), (const GLvoid*)12);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPTN), (const GLvoid*)20);

    glBindBuffer(GL_ARRAY_BUFFER, pExtra->GetVB());
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pExtra->GetIB());

    glScalef(0.05, 0.05, 0.05);

    glEnable(GL_TEXTURE_2D);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, pTexExtra->GetTexture());

     glDrawElements(GL_TRIANGLES, pExtra->GetIndexCount(), GL_UNSIGNED_INT, 0); 
     /*glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(0.0f, 0.0f, 0.0f);
        glTexCoord2f(0, 1); glVertex3f(10.0f, 0.0f, 0.0f);
        glTexCoord2f(1, 1); glVertex3f(10.0f, 10.0f, 0.0f);
        glTexCoord2f(1, 0); glVertex3f(0.0f, 10.0f, 0.0f);
     glEnd();*/

    glDisable(GL_TEXTURE_2D);


    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

My VertexPTN class is simply a container for a Position, Texture Coordinate and Normal. So I am assuming as the positioning of my vectors in the class goes; Vector3f, Vector2f, Vector3f, I would set the last parameter of the glVertexAttributePointer to 0, 12, 20.

Is there any missunderstanding of anything that I am missing? I'm really not pro with openGL but I do understand a decent amount of the setup and basic rendering. What is causing my texture to not render to my mesh, an unset GL state?

1

There are 1 answers

3
Tim On BEST ANSWER

Some thoughts:

Are you using shaders? glVertexAttribPointer only works with shaders

  • If Yes: Are you certain that your inputs are 0, 1, and 2? If you don't set these before shader linking with glBindAttribLocation, they aren't guaranteed to be any particular values.
  • If No: You should be using glVertexPointer, glTexCoordPointer, etc instead of AttribPointer

Also if you're using shaders, then enabling/disable GL_TEXTURE_2D is redundant, it doesn't do anything.