William
William

Reputation: 29

Perspective matrix not showing my object

Before adding a perspective matrix (orthographic or perspective) my object would print out completely fine, but after adding them i can no longer view my object. I have debugged the program enough to know that the object's normal drawing procedures are being used; however i do not know whether it is being deleted/not loaded, or if it is simply out of view. I've included where i declared the matrices and my animation loop. Any help would be much appreciated!

//enables matrix uniform
GLuint uTransform = glGetUniformLocation(mProgram, "u_transform");

//enables depth testing
glEnable(GL_DEPTH_TEST);

//creates aspect ratio variable
float aspect = 500.0f / 500.0f;
//enables projection matrix
glm::mat4 pmat = glm::perspective(70.0f, aspect, 0.01f, 1000.0f);
//enables view matrix
glm::vec3 eye(0, -5, 2);
glm::vec3 center(0, 0, 0);
glm::vec3 up(0, 0, 1);
glm::mat4 vmat = glm::lookAt(eye, center, up);

while (!glfwWindowShouldClose(mWindow)) //animation loop
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    tmat = pmat * vmat * tmat;
    glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(tmat)); //sets data type of matrix
    for(int i = 0; i <= numberofshapes; i++)
    {
        glBindVertexArray(shapes[i].vao);
        glDrawArrays(shapes[i].drawtype, 0, shapes[i].numOfvertices);
    }
    if(w==true){
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.001f, 0.0f)) * tmat;
    }if(a==true){
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(-0.001f, 0.0f, 0.0f)) * tmat;
    }if(s==true){
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.001f, 0.0f)) * tmat;
    }if(d==true){
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.001f, 0.0f, 0.0f)) * tmat;
    }if(r==true){
        tmat = glm::rotate(glm::mat4(1.0f), glm::radians(0.1f), glm::vec3(0.0f, 1.0f, 0.0f)) * tmat;
    }
    glfwSwapBuffers(mWindow);
    counter+=1;
    glfwPollEvents();
}

Upvotes: 1

Views: 404

Answers (1)

Rabbid76
Rabbid76

Reputation: 211176

Following the documentation of glm::perspective, the filed of view angle has to be in radians not in degrees (since glm version 0.9.4).

Convert the angle from degrees to radians:

glm::perspective(glm::radians(70.0f), aspect, 0.01f, 1000.0f);

With the code

tmat = pmat * vmat * tmat;
glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(tmat));

you are continually concatenating the view and projection matrix to the final model view projection matrix in every frame.

Change it to:

glm::mat4 mvp = pmat * vmat * tmat;
glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(mvp));

Upvotes: 2

Related Questions