Home Ask Login Register

Developers Planet

Your answer is one click away!

Calco February 2016

OpenGL - Shader loads but can only display 2D triangles

I have completely run out of ideas. I am trying to get my shaders to work in OpenGL, and have taken my code back to the super basics in an attempt to do so.

When I run my code using glDrawArrays I get a blue triangle, which tells me my shaders are working. I can also set the colour within the frag shader to prove that it fixes a colour. As you can see I used the variable 'numOfVerts' which I have tested, any value below 3 and as expected nothing draws, however any value above 3 draws the same basic 2D triangle and not a cube. The code for thisis as follows:

int DrawGLScene(GLvoid)                     
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

    glUseProgram(myShader.handle());  // use the shader
    GLuint matLocation = glGetUniformLocation(myShader.handle(), "ProjectionMatrix");
    glUniformMatrix4fv(matLocation, 1, GL_FALSE, &ProjectionMatrix[0][0]);
    //Draw code
    glBindVertexArray(m_vaoID);     // select VAO
    glDrawArrays(GL_TRIANGLES, 0, numOfVerts); //draw some geometry 
    glBindBuffer(GL_ARRAY_BUFFER, 0);  //unbind the buffer

    glBindVertexArray(0);
    glUseProgram(0); //turn off the current shader
    return TRUE;                                        // Keep Going
}

The other draw method I have in an attempt to draw the full cube uses glDrawElements, however all this does is show me a blank white screen:

int DrawGLScene(GLvoid)                             
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

    glUseProgram(myShader.handle());  // use the shader
    GLuint matLocation = glGetUniformLocation(myShader.handle(), "ProjectionMatrix");
    glUniformMatrix4fv(matLocation, 1, GL_FALSE, &ProjectionMatrix[0][0]);

    //draw objects
    glBindVertexArray(m_vaoID);     // select VAO       
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glDrawElements(GL_TRIANGLES, 36,        

Answers


Calco February 2016

This was solved by following Reto Koradi's comment advice, which is to ensure that the same type is used when defining the indices, when loading them into the buffer and when drawing.
So the correct working code is as follows:

To create the list of indices declare them as GLuint:

GLuint indicesTest5[] = { 
0,1,2,2,3,0,     // back
5,6,7,7,4,5,     // front
1,2,7,7,6,1,     // left
0,3,4,4,5,0,     // right
0,1,6,6,5,0,     // top
2,3,4,4,7,2      // bottom
};    

Ensure GLuint is used when loading into the buffer:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), indicesTest5, GL_STATIC_DRAW);

Ensure GL_UNSIGNED_INT is used when drawing:

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);

Post Status

Asked in February 2016
Viewed 1,658 times
Voted 12
Answered 1 times

Search




Leave an answer


Quote of the day: live life