2

Im trying to make a 3d engine from scratch and normal perspectave projection works good but trying to implement the camera has been a pain.

Right now what I'm doing is using mous offset to change pitch and yaw:

float x_diff = (WIDTH / 2) - mousePos.x;
x_diff /= WIDTH;

float y_diff = (HEIGHT / 2) - mousePos.y;
y_diff /= HEIGHT;

yaw += x_diff;
pitch += y_diff;

Then using that and the position to create the view matrix:

ApplyView(vertex, cameraPosition, yaw, pitch);

Vector3 ApplyView(Vector3 vertex, Vector3 cameraPosition, float yaw, float pitch) {
    // adapted from https://www.3dgep.com/understanding-the-view-matrix/
    float cosPitch = cos(pitch);
    float sinPitch = sin(pitch);
    float cosYaw = cos(yaw);
    float sinYaw = sin(yaw);

    Vector3 xaxis(cosYaw, 0, -sinYaw);
    Vector3 yaxis(sinYaw * sinPitch, cosPitch, cosYaw * sinPitch);
    Vector3 zaxis(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw);


    float lookAt[4][4] = {{ xaxis.x,                            yaxis.x,                            zaxis.x,                           0},
                          { xaxis.y,                            yaxis.y,                            zaxis.y,                           0},
                          { xaxis.z,                            yaxis.z,                            zaxis.z,                           0},
                          {-DotProduct(xaxis, cameraPosition), -DotProduct(yaxis, cameraPosition), -DotProduct(zaxis, cameraPosition), 1}};

    float pos[4] = { vertex.x * lookAt[0][0] + vertex.y * lookAt[1][0] + vertex.z * lookAt[2][0] + 0 * lookAt[3][0],
                     vertex.x * lookAt[0][1] + vertex.y * lookAt[1][1] + vertex.z * lookAt[2][1] + 0 * lookAt[3][1],
                     vertex.x * lookAt[0][2] + vertex.y * lookAt[1][2] + vertex.z * lookAt[2][2] + 0 * lookAt[3][2],
                     0 };

    return Vector3(pos[0], pos[1], pos[2]);
}

Right now I can see the 3 cubes but when I move the mouse it rotates the cubes around the centre instead of rotationg the camera.

enter image description here

enter image description here

  • 1
    Your code *is* rotating the camera. The output is in total agreement with the result one would expect. It's just the black background making it a little less obvious. – Ronald Souza Sep 29 '22 at 13:15
  • @RonaldSouza why arent the cubes moving then? i would expect it to move in and out of frame as i rotate it? – Noah Burchell Sep 29 '22 at 13:19
  • 1
    It's just that you are changing the view matrix (the very purpose - and title - of the source you are studying). Extracted from there, towards the view matrix: " *In a computer program the camera doesn’t move at all and in actuality, the world is just moving in the opposite direction and orientation of how you would want the camera to move in reality.* " You need to move forward on the tutorial and check the camera transformation part. – Ronald Souza Sep 29 '22 at 14:05
  • @RonaldSouza i looked and maybe i missunderstood because i took the inverse of the current view matrix and it still produced the same result – Noah Burchell Sep 30 '22 at 09:48

0 Answers0