0
$\begingroup$

i am a beginner to opengl following an online guide, I am trying to make a simple translation to my triangle. The triangle did render correctly when i made a float uniform to translate it, but when i multiply a mat4 to translate it, i get a black screen. Opengl outputs no error, i can't tell where is the problem. Here is the code: https://pastebin.com/en2zpPTh

Here's my drawing loop:

//Loop until window close
while (!glfwWindowShouldClose(mainWindow))
{
    //Get + Handle user input events
    glfwPollEvents();

    if (direction)
    {
        triOffset += triIncrement;
    }
    else
    {
        triOffset -= triIncrement;
    }

    if (abs(triOffset >= triMaxOffset))
    {
        direction = !direction;
    }

    //Clear window
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    //Draw
    glUseProgram(shader);

    glm::mat4 model;
    /*
    [1,0,0,0]
    [0,1,0,0]
    [0,0,1,0]
    [0,0,0,1]
    */
    model = glm::translate(model, glm::vec3(triOffset, 0.0f, 0.0f));

    glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));


    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glBindVertexArray(0);
    glUseProgram(0);

    glfwSwapBuffers(mainWindow);

    return 0;
}

and here's my vertex shader:

// Vertex Shader
static const char* vShader = " \n"
"#version 330                  \n"
"layout (location = 0) in vec3 pos;\n"
"uniform mat4 model;                        \n"
"void main()\n"
"{\n"
"   gl_Position = model * vec4(pos, 1.0);\n"
"}\n";

And fragment shader:

//Fragment shader
static const char* fShader =  " \n"
"#version 330                  \n"
"out vec4 color;               \n"
"void main()\n"
"{\n"
"   color = vec4(1.0, 1.0, 0.5, 1.0);\n"
"}\n";
$\endgroup$
  • $\begingroup$ uniformModel = glGetUniformLocation(shader, "model"); << Do you call glUseProgram(..) before this call ? High up on my checklist would be to see if the uniform was bound correctly. $\endgroup$ – PaulHK Jun 26 '18 at 3:48
  • $\begingroup$ There is definitly a problem with the matrix, because it is not set to an identity matrix, even when i call glm::mat4 model = glm::mat4(); , so the translation isn't happening, and the triangle is not drawn. $\endgroup$ – Nathan Lenas Jun 26 '18 at 9:48
  • $\begingroup$ Yes i added the glUseProgram(shader); before ;uniformModel = glGetUniformLocation(shader, "model"); but i think now the problem is on the cpu side because when i breakpoint the render loop the matrix is always [0,0,0,0] instead of identity matrix $\endgroup$ – Nathan Lenas Jun 26 '18 at 9:57
1
$\begingroup$

We fixed it ! it was glm::mat4(); no initializing to a identity matrix, glm::mat4(1.0f); works fine, thank you for helping me !

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.