Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: about matrix usage in opengl

  1. #1
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52

    about matrix usage in opengl

    Here is my test,
    ...

    // Called to draw scene
    void RenderScene(void)
    {
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Save the matrix state and do the rotations
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    float b[16] =
    {1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    0,0,-300,1};
    glMultMatrixf(b);


    // Move the light after we draw the sun!
    glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
    // Draw the Earth
    glColor3ub(0,0,255);
    float c[16] =
    {1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    105,0,0,1};
    glMultMatrixf(c);
    glutSolidSphere(15.0f, 30, 17);

    // Restore the matrix state
    glPopMatrix(); // Modelview matrix

    }
    ...

    and i change the code link this,
    ...

    // Called to draw scene
    void RenderScene(void)
    {
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Save the matrix state and do the rotations
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    float b[16] =
    {1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    0,0,-300,1};
    //glMultMatrixf(b);

    // Move the light after we draw the sun!
    glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
    // Draw the Earth
    glColor3ub(0,0,255);
    float c[16] =
    {1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    105,0,0,1};
    //glMultMatrixf(c);

    float product[16];
    m3dMatrixMultiply44(product,b,c);
    glMultMatrix(product);
    glutSolidSphere(15.0f, 30, 17);

    // Restore the matrix state
    glPopMatrix(); // Modelview matrix
    }
    ...


    first i construct two matrix,use glMulMatrix to multiply the first,and then use it again to multiply the sencod.
    And the second case is multiply the two matrices and then use glMultiMatrix to mutiply th product,
    Do the both of two way get the same result???

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171
    The order of matrix multiplies is very important. In your case you have performed the following in case 1)
    M' = (M * B) * C
    For case 2)
    P= B * C
    M' = M * P
    which is the same as case 1 - just written differently.
    The lighting will be different though, because the call to glLightfv(GL_LIGHT0,GL_POSITION,lightPos) will be affected by the current value of the ModelView matrix.

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    942
    If I read correctly it should do the same. Your first way can be mathematically expressed as

    M1 = ( A * B ) * C

    whereas the second calculation would be

    M2 = A * (B * C)

    Since matrix multiplication is associative M1 == M2. Be aware that matrix multiplication is generally NOT commutative, thus

    M1 * M2 != M2 * M1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •