I’m having trouble rendering a texture onto my 3d model using a VAO, seems like problem with the UV’s. I can see the texture but its all stretched and wacky.
I have the UV coords, UV indices and the Vertices, Vert indices which I have parsed in from an .obj file.
I start by creating the buffers for only the verts, UV coords and the Vert indices.
//Push the entire vert array onto the card
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, positionSize , &pNewModel->mLocalMesh.vertices[0], GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0);
GLsizeiptr const uvSize =
pNewModel->mLocalMesh.textureCoords.size() *sizeof(glm::vec2);
//Push the entire vert array onto the card
glGenBuffers( 1, &UVbo );
glBindBuffer( GL_ARRAY_BUFFER, UVbo );
glBufferData( GL_ARRAY_BUFFER, uvSize , &pNewModel->mLocalMesh.textureCoords[0], GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0);
GLsizeiptr const elementSize = pNewModel->mLocalMesh.vertIndices.size() * sizeof( GLushort );
///TODO: create UV index buffer
///TODO: create vertexNormal index Buffer
glGenBuffers( 1, &ElementBufferName );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ElementBufferName );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, elementSize, &pNewModel->mLocalMesh.vertIndices[0], GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
//Setup the element buffer
///Vert Buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(shaderPOSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
//shader property
glEnableVertexAttribArray(shaderPOSITION);
///glActiveTexture(GL_TEXTURE0); Don't think I need this
glBindTexture(GL_TEXTURE_2D, pNewModel->mMaterial.texture1ID);
///UV buffer
glBindBuffer( GL_ARRAY_BUFFER, UVbo );
glVertexAttribPointer(pNewModel->mMaterial.shaderTEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(pNewModel->mMaterial.shaderTEXCOORD);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBufferName);
glBindVertexArray(0);
Then when I render:
glBindVertexArray( currModel.mMeshBuffers.vaoIDs.at(x) );
glDrawElements( GL_TRIANGLES, currModel.mLocalMesh.vertIndices.size() , GL_UNSIGNED_SHORT, 0 );
glBindVertexArray(0);
Feels like I’m missing the part where I need to tell openGL about the UV indices. But how do I do it using a VAO? There seems to be only binding for the vert indices in the VAO.
I’m only using non-deprecated functions.