Sharpie
Sharpie

Reputation: 135

OpenGL First Cube Render Not Working

I have setup all the vertices and indices for my cube, my GLFW and GLEW inits perfectly every run and my shaders compile and run just fine. Whenever I try multiply my cube's vertices by a matrix parsed into the shader, I see nothing every time I run the app. I have tried both perspective and orthographic projections and nothing appears to work. Here are fragments of my code. glCall is also just a definition for debugging OpenGL Calls.

while (!glfwWindowShouldClose(window)) 
{
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glm::mat4 model = glm::mat4();
    glm::mat4 view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, -3.0f));
    //glm::mat4 proj = glm::ortho(-8.0f, 8.0f, -6.0f, 6.0f);
    glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 1.0f, 100.0f);
    glUniformMatrix4fv(glGetUniformLocation(shader, "proj"), 1, GL_FALSE, &proj[0][0]);
    glUniformMatrix4fv(glGetUniformLocation(shader, "view"), 1, GL_FALSE, &view[0][0]);
    glUniformMatrix4fv(glGetUniformLocation(shader, "model"), 1, GL_FALSE, &model[0][0]);

    glCall(glUseProgram(shader));
    glCall(glBindVertexArray(cube.VAO));
    glCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube.IBO));
    glCall(glDrawElements(GL_TRIANGLES, cube.indexCount, GL_UNSIGNED_INT, 0));

    glfwSwapBuffers(window);
    glfwPollEvents();
}

Here is my shader code aswell:

#SHADER vertex
#version 430 

layout(location = 0) in vec3 position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    gl_Position = proj * view * model * vec4(position, 1.0f);
}

#SHADER fragment
#version 430 

layout(location = 0) out vec4 color;
uniform vec4 colour;

void main()
{
    color = vec4(vec3(1.0, 0.5, 0.31) * vec3(1.0, 1.0, 1.0), 1.0);
}

And these are the cube vetices and indices i utilize if they are needed:

GLfloat vertices[24] = {
    // front
    -1.0, -1.0,  1.0,
    1.0, -1.0,  1.0,
    1.0,  1.0,  1.0,
    -1.0,  1.0,  1.0,
    // back
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    1.0,  1.0, -1.0,
    -1.0,  1.0, -1.0
};
GLint indices[36] = {
    // front
    0, 1, 2,
    2, 3, 0,
    // top
    1, 5, 6,
    6, 2, 1,
    // back
    7, 6, 5,
    5, 4, 7,
    // bottom
    4, 0, 3,
    3, 7, 4,
    // left
    4, 5, 1,
    1, 0, 4,
    // right
    3, 2, 6,
    6, 7, 3
};

I always get nothing displaying on the screen except for if I only multiply the position in my shader by only the orthogonal projection matrix and nothing else. I have set the ViewPort and Enabled Depth Buffering, but nothing else. Is there anything else I need to enable or disable or modify, because I'm confused as to why nothing will render in the cull of my projection.

Upvotes: 1

Views: 88

Answers (1)

Rabbid76
Rabbid76

Reputation: 211230

glUniform* specify the value of a uniform variable for the current program object

This means glUseProgram has to be done before glUniformMatrix4fv:

GLint locPrj = glGetUniformLocation( shader, "proj" );
GLint locView = glGetUniformLocation( shader, "view" );
GLint locModel = glGetUniformLocation( shader, "model" );

.....

glUseProgram(shader);

glUniformMatrix4fv(locPrj, 1, GL_FALSE, &proj[0][0]);
glUniformMatrix4fv(locView, 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(locModel, 1, GL_FALSE, &model[0][0]);


To set up a identity matrix you should use glm::mat4(1.0f);

glm::mat4 model = glm::mat4(1.0f);


Further, I recommend to use glm::lookAt, for setting up the view matrix. Try something like:

 glm::mat4 view = glm::lookAt( 
     glm::vec3( 2.0f, -5.0f, 2.0f ),
     glm::vec3( 0.0f, 0.0f, 0.0f ),
     glm::vec3( 0.0f, 0.0f, 1.0f ) );

Upvotes: 2

Related Questions