0

As learned from this post, I've drawn a cube using the glDrawElementsfunction instead of drawing one made up of triangles specified in a massive list of vertices using the glDrawArrays(GL_TRIANGLES, 0, 6) function.

I successfully textured two triangles using the glDrawArrays function to create a textured square, but when I try the same methodology on my new glDrawElements method, I can't seem to get it to work...

This is how I draw the cube at the moment (which includes my attempt at texturing):

void SkyBox::constructGeometry(Shader* myShader)
{
    verts[0] = -dimX;   verts[ 1] = -dimY;  verts[ 2] = -dimZ; //0
    verts[3] = -dimX;   verts[ 4] =  dimY;  verts[ 5] = -dimZ; //1
    verts[6] =  dimX;   verts[ 7] =  dimY;  verts[ 8] = -dimZ; //2
    verts[9] =  dimX;   verts[10] = -dimY;  verts[11] = -dimZ; //3

    verts[12] = -dimX;   verts[13] = -dimY;  verts[14] = dimZ; //4
    verts[15] = -dimX;   verts[16] =  dimY;  verts[17] = dimZ; //5
    verts[18] =  dimX;   verts[19] =  dimY;  verts[20] = dimZ; //6
    verts[21] =  dimX;   verts[22] = -dimY;  verts[23] = dimZ; //7

    cols[0] = 0.0;   cols[ 1] = 0.0;  cols[ 2] = 0.0;
    cols[3] = 0.0;   cols[ 4] = 1.0;  cols[ 5] = 0.0;
    cols[6] = 0.0;   cols[ 7] = 0.0;  cols[ 8] = 1.0;
    cols[9] = 1.0;   cols[10] = 1.0;  cols[11] = 1.0;

    cols[12] = 1.0;   cols[13] = 0.0;  cols[14] = 0.0;
    cols[15] = 0.0;   cols[16] = 1.0;  cols[17] = 0.0;
    cols[18] = 0.0;   cols[19] = 0.0;  cols[20] = 1.0;
    cols[21] = 1.0;   cols[22] = 1.0;  cols[23] = 0.0;

    tris[0] =0; tris[1] =1; tris[2] =2;
    tris[3] =0; tris[4] =2; tris[5] =3;
    tris[6] =4; tris[7] =6; tris[8] =5;
    tris[9] =4; tris[10]=7; tris[11]=6;
    tris[12]=1; tris[13]=5; tris[14]=6;
    tris[15]=1; tris[16]=6; tris[17]=2;
    tris[18]=0; tris[19]=7; tris[20]=4;
    tris[21]=0; tris[22]=3; tris[23]=7;
    tris[24]=0; tris[25]=5; tris[26]=1;
    tris[27]=0; tris[28]=4; tris[29]=5;
    tris[30]=3; tris[31]=2; tris[32]=7;
    tris[33]=2; tris[34]=6; tris[35]=7;

    tex[0]  = 0.0;  tex[1] = 0.0;  tex[2] = 0.0;
    tex[3]  = 0.0;  tex[4] = 0.0;  tex[5] = 0.0;
    tex[6]  = 1.0;  tex[7] = 1.0;  tex[8] = 1.0;   
    tex[9]  = 1.0; tex[10] = 1.0; tex[11] = 1.0;
    tex[12] = 0.0; tex[13] = 1.0; tex[14] = 1.0;
    tex[15] = 0.0; tex[16] = 1.0; tex[17] = 0.0;
    tex[18] = 0.0; tex[19] = 1.0; tex[20] = 1.0;   
    tex[21] = 0.0; tex[22] = 0.0; tex[23] = 1.0;
    tex[24] = 0.0; tex[25] = 1.0; tex[26] = 0.0;
    tex[27] = 0.0; tex[28] = 1.0; tex[29] = 1.0;
    tex[30] = 0.0; tex[31] = 0.0; tex[32] = 1.0;   
    tex[33] = 0.0; tex[34] = 1.0; tex[35] = 1.0;

    glGenVertexArrays(2, &m_vaoID[0]);
    glBindVertexArray(m_vaoID[0]);
    glGenBuffers(3, m_vboID);

    GLint vertexLocation= glGetAttribLocation(myShader->handle(), "in_Position");
    GLint colorLocation= glGetAttribLocation(myShader->handle(), "in_Color");
    GLint texCoordLocation = glGetAttribLocation(myShader->handle(), "in_TexCoord");
    glUniform1i(glGetUniformLocation(myShader->handle(), "DiffuseMap"), 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
    glBufferData(GL_ARRAY_BUFFER, numOfVerts2 *3*sizeof(GLfloat), verts, GL_STATIC_DRAW);
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(vertexLocation);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
    glBufferData(GL_ARRAY_BUFFER, numOfVerts2 *3*sizeof(GLfloat), cols, GL_STATIC_DRAW);
    glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(colorLocation);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[2]);
    glBufferData(GL_ARRAY_BUFFER, numOfTexs *sizeof(GLfloat), tex, GL_STATIC_DRAW);
    glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0,0);
    glEnableVertexAttribArray(texCoordLocation);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numOfTris2 * 3 * sizeof(unsigned int), tris, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glEnableVertexAttribArray(0);

    glBindVertexArray(0);
}

And how I render it:

void SkyBox::render(Shader* myShader)           //shader expected
{
    glUseProgram(myShader->handle());       //shader
    glBindTexture(GL_TEXTURE_2D, texName);      //blending needed to use alpha channel
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    //draw objects
    glBindVertexArray(m_vaoID[0]);              // select VAO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);     //element arrays to draw indices from an array
    glDrawElements(GL_TRIANGLES, numOfTris2 *3, GL_UNSIGNED_INT, 0);

    glDisable(GL_BLEND);
    glUseProgram(0);
    glBindVertexArray(0);

}

I've been staring at it all evening so I maybe missing something really silly but I just can't see where I'm going wrong...

Community
  • 1
  • 1
Reanimation
  • 3,151
  • 10
  • 50
  • 88
  • Why do you have as many texture coordinates as you have indices? Texture coordinates are on a per-vertex basis, so you should have 16 of them. – Trillian Nov 13 '13 at 03:37
  • VAOs store element array bindings by the way. I would suggest setting the element array when you create your VAO and then never changing it (in other words, do not bind element array 0 after setup). Just bind the VAO you need and call `glDrawElements (...)` – Andon M. Coleman Nov 13 '13 at 03:39
  • Im not sure I understand why there should only be 16 when there is is 12 triangles to texture and each triangle has 3 vertices... Also using this method isn't very useful as I've only just realised I'd have to use the same texture for all sides of the cube... – Reanimation Nov 13 '13 at 13:49

0 Answers0