0

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?

user1294021
  • 312
  • 3
  • 10
  • Just a hint (not relevant to your problem (probably)): You should never hardcode memory structure offsets. Instead let the compiler figure it out. There's the `offsetof` macro of ANSI C for this: https://en.wikipedia.org/wiki/Offsetof – use that for the data parameter of a VBO. – datenwolf May 15 '12 at 01:20

1 Answers1

0

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.

Tim
  • 33,823
  • 10
  • 86
  • 115
  • I have no shaders whatsoever, so I suppose that is the reason it won't work. Also I have some test objects that simply use a diffuse colour, for some reason even without specifying glTexCoord's it made them all yellowish. I will look up using those, thank you. – user1294021 May 15 '12 at 01:56
  • I now have glVertexPointer(3, GL_FLOAT, sizeof(GL_FLOAT) * 3, &pExtra->GetVB()[0]); glNormalPointer(GL_FLOAT, sizeof(GL_FLOAT) * 3, &pExtra->GetNB()[0]); glTexCoordPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &pExtra->GetTB()[0]); glDrawElements(GL_TRIANGLES, pExtra->GetIB().size(), GL_UNSIGNED_SHORT, &pExtra->GetIB()[0]);

    I changed the Vertex Buffers from GLuint generated buffers to simple stl vectors that I pumped the vertices, normals, tex coords into. I then directly point gl to these vector's as they are contiguous. But now I don't even see my model, what am I doing wrong?

    – user1294021 May 15 '12 at 02:36
  • @user1294021 You also will need to replace glEnableVertexAttribArray with various calls to glEnableClientState to enable GL_VERTEX_ARRAY, GL_TEXCOORD_ARRAY, and GL_NORMAL_ARRAY – Tim May 15 '12 at 05:56