Advertisement

Object moving with camera.

Started by January 28, 2016 12:11 PM
1 comment, last by LetsDoThis 8 years, 11 months ago

I was planning to make a small room using the same vertices.


    2.0f ,0.0f, -2.0f,        2.0f,2.0f,
    -2.0f,0.0f,-2.0f,        0.0f,2.0f,
    -2.0f,0.0f, 2.0f,        0.0f,0.0f,
    2.0f, 0.0f, 2.0f,        2.0f,0.0f

GLuint indices[] = {
    0,1,3,
    1,2,3
};

the first 3 coordinates is for the vertices and the 2 other are for the texture

I manage to put a floor but when I tried putting a wall this where I got stuck with


    model = glm::mat4();
    
        glm::vec3 wallPos(0.0f, 0.0f, -2.0f);
        model = glm::translate(model, wallPos);
        model = glm::rotate(model, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f));
        projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
        glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
        glBindVertexArray(buffer.VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

What I am trying to do is use the same vertices on making the floor to also making the wall. Basically I am making a big cube but with parts that is kind of like detachable. I tried rotating it first and then translate still having the same issue. Whats confusing as well is that the floor doesnt move when I move my camera yet the wall just kept on following my camera. Since the vertices of the wall is exactly the same with the floor, I dont understand why is the floor not moving yet the wall is moving. Here is the code of floor


         view = camera.look();
        projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
        glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
        glBindVertexArray(buffer.VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

You can see they are pretty much the same. And I am using a basic shader. Really basic one.


gl_Position = projection * view * model * vec4(position, 1.0f);

What is going on here? Thanks

ooookkkkkaaaayyy

Very intrestingsmile.png

Got any idea?

ooookkkkkaaaayyy

Advertisement

Got it. I forgot to enable depth buffer. tongue.png

What is happening is that it may look like that the wall is moving with my camera but what actually is going on is that I am seeing the bottom part of the wall and that the floor is being overwritten by wall.

ooookkkkkaaaayyy

This topic is closed to new replies.

Advertisement