0

I create a cube like normal using 8 vertex points that outline a cube and use indices to draw each individual triangle. However, when I create my camera matrix and rotate it using the lookat function with glm it rotates the entire screen positions not world positions.

glm::mat4 Projection = glm::mat4(1);
Projection = glm::perspective(glm::radians(60.0f), (float)window_width / (float)window_hight, 0.1f, 100.0f);


const float radius = 10.0f;
float camX = sin(glfwGetTime()) * radius;
float camZ = cos(glfwGetTime()) * radius;


glm::mat4 View = glm::mat4(1);
View = glm::lookAt(
    glm::vec3(camX, 0, camZ), 
    glm::vec3(0, 0, 0),
    glm::vec3(0, 1, 0)  
);

glm::mat4 Model = glm::mat4(1);

glm::mat4 mvp = Projection * View * Model;

Then in glsl:

uniform mat4 camera_mat4
void main()
{
    vec4 pos = vec4(vertexPosition_modelspace, 1.0) * camera_mat4;


    gl_Position.xyzw = pos;
}

Example: GLM rotating screen coordinates not cube

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
capslpop
  • 73
  • 9
  • 3
    If you use native OpenGL notation for your matrix math then try to change the `vec4 pos = vec4(vertexPosition_modelspace, 1.0) * camera_mat4;` into `vec4 pos = camera_mat4*vec4(vertexPosition_modelspace, 1.0);` Or change the order of your matrix operations or inverse your matrices ... all depends on your notations you use ... for more info see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) and look for difference between local and global transforms – Spektre Jul 12 '21 at 07:03
  • @Spektre Yep that did it! Thanks! – capslpop Jul 12 '21 at 14:58

0 Answers0