Skip to main content
GameDev.net gamedev.net
🔒 Locked

Camera Class problem

Started by maich Jun 16, 2010 at 1:15 PM 1 replies 950+ views
Original Post
maich
maich
Hi guys,

I have a camera class (see below) implementation and it works well when the viewer is looking straight at the scene. When I pitch and am looking at the scene from up top and try to yaw, my objects are flattened! I am new to graphics and cant figure out why. Any help is appreciated. Here is the code.

class Camera{    public:        Camera ();        Camera (Vector3D eye, Vector3D lookat, Vector3D up);                /* Update the graphics device view matrix */        void set_viewMatrix ();        void set_viewport (int x0, int y0, int width, int height);        void set_viewShape (float fov_angle, float aspect_ratio, float znear, float zfar);                /* Modify camera */        void set_camera ();        void set_up ();        void roll_Camera (float theta);        void pitch_Camera (float theta);        void yaw_Camera (float theta);        /* Translate camera */        void slide_forward (float delta);        void slide_backward (float delta);        void slide_left (float delta);        void slide_right (float delta);        void slide_up (float delta);        void slide_down (float delta);        void slide_Camera (float du, float dv, float dn);        MATRIX_4X4 * setcamera ();        /* get the view and projection vectors */        Point3D * get_viewportParams () { return &viewport_params; }        MATRIX_4X4 * get_viewMtx () { return &viewMtx }// return &viewMtx }        MATRIX_4X4 * get_projectionMtx () { return &perspectiveMtx }            //private:        /* The three orientation vectors */        Vector3D cam_u, cam_v, cam_n, cam_up;                /* The origin and lookat points */        Vector3D cam_eye;        Vector3D cam_lookat;        Vector3D cam_translate;        /* Accumulation values */        float roll_theta, pitch_theta, yaw_theta, zoom_theta;        float u_delta, v_delta, n_delta;        /* The view and perspective matrices */        MATRIX_4X4 viewMtx;        MATRIX_4X4 perspectiveMtx;        /* The window to viewport parameters */        Point3D viewport_params;};

/************************************* The implementation ****************************/
Camera::Camera (Vector3D eye, Vector3D lookat, Vector3D up){    cam_eye = eye;    cam_lookat = lookat;    cam_up = up; //(up.x, up.y, up.z);    /* Set the u, v and n states to well known values */    //set_default_uvn_state ();    /* Set all other values to zero */    zoom_theta = roll_theta = pitch_theta = yaw_theta = 0.0;    u_delta = v_delta = n_delta = 0.0;        mtx44_setidentity (&viewMtx);    mtx44_setidentity (&perspectiveMtx);        /* Update the view matrix */    set_camera();}void Camera::set_camera (){    //set_default_uvn_state ();    /* Step 1. Calculate and normalize the view direction vector */    cam_n = cam_eye - cam_lookat;    cam_n.normalize ();        /* Step 2. Calculate the real up vector (note - the given up vector may not be orthogonal to cam_n) */    cam_v = cam_up - ((cam_n * cam_up) * cam_n);    cam_v.normalize ();        /* Step 3. Calculate u (the side vector). To maintain right handedness, we calculate U = V * N */    /* also note that U is a unit vector by construction */    cam_u = cross_product (cam_v, cam_n);        /* Step 4. Calculate the translation component */    Vector3D trans_vector(-cam_eye.x, -cam_eye.y, -cam_eye.z);    cam_translate.set (trans_vector * cam_u,                       trans_vector * cam_v,                       trans_vector * cam_n);    /* Update the view matrix */    set_viewMatrix ();}void Camera::set_viewMatrix (){    viewMtx.m[0][0] = cam_u.x; viewMtx.m[0][1] = cam_v.x; viewMtx.m[0][2] = cam_n.x; viewMtx.m[0][3] = 0.0;    viewMtx.m[1][0] = cam_u.y; viewMtx.m[1][1] = cam_v.y; viewMtx.m[1][2] = cam_n.y; viewMtx.m[0][3] = 0.0;    viewMtx.m[2][0] = cam_u.z; viewMtx.m[2][1] = cam_v.z; viewMtx.m[2][2] = cam_n.z; viewMtx.m[0][3] = 0.0;    /* The translation row */    viewMtx.m[3][0] = cam_translate.x; viewMtx.m[3][1] = cam_translate.y; viewMtx.m[3][2] = cam_translate.z;    viewMtx.m[3][3] = 1.0;}void Camera::roll_Camera (float theta){    MATRIX_4X4 rmat;    mtx44_setidentity (&rmat);    /* rotation about the z axis */    //mtx44_rotationaxis (&rmat, cam_n, DEGREETO_RADS(theta));    mtx44_rotateZ (&rmat, DEGREETO_RADS(theta));    /* rotate both u and v */    mtxv14_multiply (&cam_u, &cam_u, &rmat);    /* then v */    mtxv14_multiply (&cam_v, &cam_v, &rmat);    /* then update the view matrix */    set_viewMatrix ();}void Camera::pitch_Camera (float theta){    MATRIX_4X4 pmat;    mtx44_setidentity (&pmat);    /* rotation about the z axis */    //mtx44_rotationaxis (&pmat, cam_u, DEGREETO_RADS(theta));    mtx44_rotateX (&pmat, DEGREETO_RADS(theta));    /* rotate both n and v */    mtxv14_multiply (&cam_n, &cam_n, &pmat);    /* then v */    mtxv14_multiply (&cam_v, &cam_v, &pmat);        /* then update the view matrix */    set_viewMatrix ();}void Camera::yaw_Camera (float theta){        MATRIX_4X4 ymat;    mtx44_setidentity (&ymat);    /* rotation about the z axis */    //mtx44_rotationaxis (&ymat, cam_v, DEGREETO_RADS(theta));    mtx44_rotateY (&ymat, DEGREETO_RADS(theta));    /* rotate both u and n */    mtxv14_multiply (&cam_u, &cam_u, &ymat);    /* then v */    mtxv14_multiply (&cam_n, &cam_n, &ymat);    /* then update the view matrix */    set_viewMatrix ();}void Camera::slide_forward (float du){    cam_translate.z += du;    set_viewMatrix ();}void Camera::slide_backward (float du){    cam_translate.z -= du;    set_viewMatrix ();}

Some methods are not shown as they relate to projection. Not sure they are needed. Again, thanks for looking and any suggestions are welcome!!!

maich-maich

[Edited by - maich on June 16, 2010 2:08:32 PM]
haegarr
haegarr
It would be much easier for us to read and understand your code (e.g. due to indentation and syntax coloring) if you follow the guidelines as explained in the forum's FAQs. So please wrap your code with [source] and [/source] tags, perhaps the declaration and the implementation in 2 separate blocks. Thanks.
maich
maich
Quote:
Original post by haegarr
It would be much easier for us to read and understand your code (e.g. due to indentation and syntax coloring) if you follow the guidelines as explained in the forum's FAQs. So please wrap your code with tags, perhaps the declaration and the implementation in 2 separate blocks. Thanks.


Done..

Okay to be specific, when I yaw the camera, no problems. If e.g i yaw, pitch and roll the camera, the objects look sheared?? The objects in the scene get deformed and I'm certain its the view matrix that causes that (perhaps its not orthogonal anymore) but I dont know why. Has anybody implemented a camera that can help out? Thanks in advance.

[Edited by - maich on June 17, 2010 5:10:19 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.