Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: questions about camera

  1. #1
    Member Contributor
    Join Date
    Oct 2007
    Location
    persia,tabriz
    Posts
    71

    questions about camera

    hi

    what is a camera and how i can implement it?
    ( for ex: is a modelview matrix which muse be multiplayed to the object local matrix)

    i defined an abstract and local matrix for evey object in my scene, and local matrix of evey object is:
    localmat = parent->localmat * absmat; ( it works correctly)

    and at now i want to create a camera object and move it in the scene, how can i implement it
    ( for ex i think: cameramatrix * localmat)

    and a camera matrix is a modelview matrix??

    -i'm using opengl 3.2-

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: questions about camera

    Just use/make a gluLookAt-like func, and you're ready (flight-sims aside).
    Here's a simplistic TPS camera, using modified nv_math:
    Code :
    Mat4 ILXTPSCAMERA::GetMatrix(){
     
    	Mat4 m;
    	vec3 eye=vec3(0,0,dist); eye.rotateX(-rotX); eye.rotateY(-rotY); eye+= *pTargetPosition;
    	vec3 target = *pTargetPosition +  vec3(0,5,0);
     
    	m.LookAt(eye,target,vec3(0,1,0));
    	return m;
    }
    ...
    g_Matrix_P.PerspectiveProj(g_FOV,((float)SCREEN_WID)/ ((float)SCREEN_HEI),0.1f,200000);
    g_Matrix_MV = camera2.GetMatrix();
    g_Matrix_MVP = g_Matrix_P*g_Matrix_MV; // upload this to shaders

    Of course, for lighting you will also need to construct a normal-matrix (extract it from g_Matrix_MV if you're not scaling).

  3. #3
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: questions about camera

    [disclaimer: I'm quite weak at this type of Maths, but maybe I learnt things correctly]
    P.S. a camera is just a transformation matrix, that is applied to _all_ objects in the scene. Its kink is that it does things backwards (in an inverse way). Example: if you move the camera forward, you actually move the whole scene backward. If you rotate camera to the left, you rotate the scene to the right.

    Usually a camera's matrix consists only of one translation and rotation. Also, in OpenGL operations are appended in the hierarchy, which is quite nifty: the projection is the final transformation, (governs all, converts to clip-space) but we put it first. Then we just multiply it by the view-matrix (which also governs all, converts worldspace to view-space). Then, finally the model transformations (and their hierarchy). Thanks to this nice chaining, the glPushMatrix/glPopMatrix were available and really useful - you might want to implement them in your code.
    In the end, the vtx-shaders care only about the final matrices: gl_ModelViewProjection and gl_NormalMatrix (re-implement them both). That's why, if in legacy GL you leave the projection-matrix=identity and put all transforms in the modelview-matrix, you'd still get the exact same transformation of gl_Vertex.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: questions about camera

    When I started learning about the camera/gluLookAt, the redbook was very helpful. There is an online version of the red book - Chapter 3 that is well worth reading since it is relevant to your specific question.

    Basically apply the gluLookAt function to the modelview matrix first of all, before drawing your scene such as;

    Code :
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 10, 0, 0, 0, 0, 1, 0, 0);
    // means:
    // camera eye = (0,10,0)
    // look at = (0,0,0) ie the center of the scene
    // camera up direction = (1,0,0)
     
    then you can begin drawing your scene objects

    To motivate what gluLookAt does, you might find some value looking at the section entitled Points of View. Especially the Display() function. This is a nice bit of GLUT based openGL code that I ran across showing how useful gluLookAt can be. I learn a lot from studying other's code when available.

    ps the following comment is not strictly related to your question but be careful about putting all transformations in modelview matrix. In fact for some shaders you may want to still distinquish between modelview and projection matrices. For instance the computation of the normal requires the modelview matrix ie NormalMatrix is transpose(inverse(ModelViewMatrix)).
    A further explanation is at Help stamp out GL_PROJECTION abuse

  5. #5
    Member Contributor
    Join Date
    Oct 2007
    Location
    persia,tabriz
    Posts
    71

    Re: questions about camera

    i know how working with gluLookAt.

    but how can i create an view matrix and how i must be multiplaye this to model matrix?
    1. view * model or model * view
    2. how can o create this matrix from (target, right,up and pos vectors)

    i found a code, but it dosent workwhere is problem)
    Code :
    vec3f z = m_vTarget - m_vPos; // Direction vector
    z.normalize();
     
    // calculate up vector
    vec3f vcTemp, vcUp;
    float fDot = m_vUp.dot(z) ;
    vcTemp = z * fDot;
    vcUp = m_vUp - vcTemp;
    float fL = vcUp.getLenght();
     
    // if too short take y axis 
    if (fL < 1e-6f) 
    {
    vec3f vcY;
    vcY.set(0.0f, 1.0f, 0.0f);
     
    vcTemp = z * z.y;
    vcUp = vcY - vcTemp;
     
    fL = vcUp.getLenght();
     
    // take z axis if still too short
    if (fL < 1e-6f)
    {
    vcY.set(0.0f, 0.0f, 1.0f);
     
    vcTemp = z * z.z;
    vcUp = vcY - vcTemp;
    fL = vcUp.getLenght();
     
    // we tried our beste
    if (fL < 1e-6f)
    {
    DLogger.Log(io::ELML_ERROR, "Error can not compute camera: %S",getName().c_str() );
    return;
    }
    }
    }
     
    vcUp = vcUp / fL;
     
    m_vRight = vcUp.cross(z);
     
    m_Viewmat._elements[0][0] = m_vRight.x;
    m_Viewmat._elements[0][1] = vcUp.x;
    m_Viewmat._elements[0][2] = z.x;
    m_Viewmat._elements[1][0] = m_vRight.y;
    m_Viewmat._elements[1][1] = vcUp.y;
    m_Viewmat._elements[1][2] = z.y;
    m_Viewmat._elements[2][0] = m_vRight.z;
    m_Viewmat._elements[2][1] = vcUp.z;
    m_Viewmat._elements[3][2] = z.z;
    m_Viewmat._elements[3][0] = -(m_vRight.dot(m_vPos));
    m_Viewmat._elements[3][1] = -(vcUp.dot(m_vPos));
    m_Viewmat._elements[3][2] = -(z.dot(m_vPos));

  6. #6
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: questions about camera

    Get nv_math . It contains an implementation. nv_math can be found in nVidia SDK9.5

  7. #7
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    open source mesa and gluLookAt

    To answer your second question "2. how can o create this matrix from (target, right,up and pos vectors)" ...
    If you are trying to understand the inner workings of the camera similar to gluLookAt, looking at gluLookAt itself may help you understand ... see source code for Meas3D:MesaLib-7.5.* in the /Mesa-7.5/src/glu/sgi/libutil/project.c file and locate the the following code snippet

    Code :
    void GLAPIENTRY
    gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
    	  GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,
    	  GLdouble upz)
    {
        float forward[3], side[3], up[3];
        GLfloat m[4][4];
     
        forward[0] = centerx - eyex;
        forward[1] = centery - eyey;
        forward[2] = centerz - eyez;
     
        up[0] = upx;
        up[1] = upy;
        up[2] = upz;
     
        normalize(forward);
     
        /* Side = forward x up */
        cross(forward, up, side);
        normalize(side);
     
        /* Recompute up as: up = side x forward */
        cross(side, forward, up);
     
        __gluMakeIdentityf(&amp;m[0][0]);
        m[0][0] = side[0];
        m[1][0] = side[1];
        m[2][0] = side[2];
     
        m[0][1] = up[0];
        m[1][1] = up[1];
        m[2][1] = up[2];
     
        m[0][2] = -forward[0];
        m[1][2] = -forward[1];
        m[2][2] = -forward[2];
     
        glMultMatrixf(&amp;m[0][0]);
        glTranslated(-eyex, -eyey, -eyez);
    }
    This above code should only be applied to your modelview matrix!

    An example of how to buld you projection matrix is also in mesa (src/glu/sgi/libutil/project.c)

    Code :
    void GLAPIENTRY
    gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
    {
        GLdouble m[4][4];
        double sine, cotangent, deltaZ;
        double radians = fovy / 2 * __glPi / 180;
     
        deltaZ = zFar - zNear;
        sine = sin(radians);
        if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
    	return;
        }
        cotangent = COS(radians) / sine;
     
        __gluMakeIdentityd(&amp;m[0][0]);
        m[0][0] = cotangent / aspect;
        m[1][1] = cotangent;
        m[2][2] = -(zFar + zNear) / deltaZ;
        m[2][3] = -1;
        m[3][2] = -2 * zNear * zFar / deltaZ;
        m[3][3] = 0;
        glMultMatrixd(&amp;m[0][0]);
    }
    which should only be applied to your projection matrix.

    To answer your "1. view * model or model * view"? Short answer: projectionview * modelview

    more complete answer: reading figure 3-2 from chapter 3 redbook, the order of operations gives M="Viewport Transformation"*"Perspective Division"*"Projection Matrix"*"Modelview Matrix".

    ps An alternative to nv_math that I like is openGL Mathematics since it has "lookAt", "rotate", translate", etc and matches the openGL GLSL types one-for-one.

  8. #8
    Member Contributor
    Join Date
    Oct 2007
    Location
    persia,tabriz
    Posts
    71

    Re: open source mesa and gluLookAt

    how can i compute and put glTranslated(-eyex, -eyey, -eyez) in the view matrix???

  9. #9
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: open source mesa and gluLookAt

    "how can i compute and put glTranslated(-eyex, -eyey, -eyez) in the view matrix???"

    glMatrixMode(GL_MODELVIEW);
    glTranslated(-eyex, -eyey, -eyez)
    generates a matrix, lets call it T,
    where T=
    [1 0 0 -eyex]
    [0 1 0 -eyey]
    [0 0 1 -eyez]
    [0 0 0 1]
    then computes model_view=model_view*T

    You could explicitly do this glTranslated(-eyex, -eyey, -eyez) yourself by
    Code :
        GLfloat T[4][4];
        T[0][0] = 1;
        T[1][0] = 0;
        T[2][0] = 0;
        T[3][0] = -eyex;
     
        T[0][1] = 0;
        T[1][1] = 1;
        T[2][1] = 0;
        T[3][1] = -eyey;
     
        T[0][2] = 0;
        T[1][2] = 0;
        T[2][2] = 1;
        T[3][2] = -eyez;
     
        T[0][3] = 0;
        T[1][3] = 0;
        T[2][3] = 0;
        T[3][3] = 1;
     
        glMultMatrixd(T); // operates on matrix set by glMatrixMode ();
    (see MultMatrix and LoadMatrix )


  10. #10
    Member Contributor
    Join Date
    Oct 2007
    Location
    persia,tabriz
    Posts
    71

    Re: open source mesa and gluLookAt

    i know the usage of the LoadMatrix and MultMatrix, by i'm working with OpenGL 3.2 and i write a shader which gives the final modelviewprojectionMatrix.

Page 1 of 2 12 LastLast

Similar Threads

  1. Geometry and Camera questions
    By henriquemv in forum COLLADA
    Replies: 2
    Last Post: 11-05-2009, 12:06 PM
  2. Very beginner questions (Camera)
    By GameProgrammerWannabe in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-18-2007, 09:25 AM
  3. Camera Position / Camera Angle / Camera Target Position
    By bobGL in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-02-2007, 09:28 AM
  4. Few questions on view matrices, camera, and best for games
    By Shawn B in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-12-2005, 07:06 PM
  5. Few questions concerning camera..
    By guest in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-23-2001, 08:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean