Announcement

Collapse
No announcement yet.

OpenGL -- glMultMatrix

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenGL -- glMultMatrix

    (Maybe this should be in the third party add-ons forum, not sure..?)

    I'm attempting to implement different rigid body dynamics code
    in my vehicle model DLL. This winds up producing a 4X4 matrix
    for the orientation of the vehicle. Previously, I was using
    yaw, pitch, and roll directly and using glRotate3f to let OpenGL
    produce the necessary rotation matrix for animation directly, but
    that results in gimble lock and causes things to break the laws of
    conservation of angular momentum in the simulation code... That's a
    bad thing.

    So... What I need to do is use the GLMultMatrixf function, but am
    having trouble getting it working properly. OpenGL manuals are mostly
    in C, and Sublevel 6's site does not cover GLMultMatrixf in PowerBasic.

    Here is the C definition from the manual:

    Code:
    void glMultMatrixf( const GLfloat *m )
    
    Parameters
      m  Specifies a pointer a to 4x4 matrix stored	in column-major	order as
         sixteen consecutive values.
    Here's what I don't understand. An example code in C shows this:

    Code:
      
      GLfloat mat1[]={2,3,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
      GLfloat mat2[]={3,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
      glMatrixMode(GL_MODELVIEW);
      glLoadMatrixf(mat1);
      glMultMatrixf(mat2);
    Here, shouldn't mat1 and mat2 be pointers as per the definition?

    Here's one of my unsuccessful attempts to do it in PowerBasic (the
    rotation matrix is set to simply be not rotated and centered at the
    origin of the world coordinate system):

    Code:
    DIM CarRotationMatrix(0 TO 15) AS SINGLE
    DIM PointerToCarRotationMatrix AS SINGLE PTR
    PointerToCarRotationMatrix = VARPTR(CarRotationMatrix())
    
       glMatrixMode %GL_MODELVIEW
       glLoadIdentity
       ''Set view transform:
       '''Camera location
      glRotatef -RollCam,0,0,1
      glRotatef -PitchCam,1,0,0
      glRotatef YawCam,0,1,0 'Yaw
      glTranslatef -XCam,-YCam,ZCam    
    
    CarRotationMatrix(0) = 1
    CarRotationMatrix(1) = 0
    CarRotationMatrix(2) = 0
    CarRotationMatrix(3) = 0
    CarRotationMatrix(4) = 0
    CarRotationMatrix(5) = 1
    CarRotationMatrix(6) = 0
    CarRotationMatrix(7) = 0
    CarRotationMatrix(8) = 0
    CarRotationMatrix(9) = 0
    CarRotationMatrix(10) =  1
    CarRotationMatrix(11) =  0
    CarRotationMatrix(12) =  0
    CarRotationMatrix(13) =  0
    CarRotationMatrix(14) =  0
    CarRotationMatrix(15) =  1
    
    glMultMatrixf CarRotationMatrix
    'glMultMatrixf @PointerToCarRotationMatrix
    'glMultMatrixf PointerToCarRotationMatrix
    I've tried to do this with all of the glMultMatrixf calls shown in turn.
    The first one shows something, but it's not correct, while the second
    and third show nothing at all.

    Am I translating the C code incorrectly here?

    Thanks!


    ------------------
    Todd Wasson
    -------------
    Performance Simulations
    Drag Racing and Top Speed
    Prediction Software http://PerformanceSimulations.Com
    PB Racing Sim: http://performancesimulations.com/scnshot4.htm


    [This message has been edited by Todd Wasson (edited October 15, 2002).]
    Todd Wasson
    http://PerformanceSimulations.Com
    PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

  • #2
    Nevermind, I found the solution:

    Code:
    glMultMatrix CarRotationMatrix(0)


    ------------------
    Todd Wasson
    -------------
    Performance Simulations
    Drag Racing and Top Speed
    Prediction Software
    http://PerformanceSimulations.Com
    Todd Wasson
    http://PerformanceSimulations.Com
    PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

    Comment


    • #3
      Hi Todd, how's it going?!

      I have been working with a fair amount of matrix manipulations,
      and gimble-busting in recent months, and while you seem to have
      a fix for this problem, I'd be happy to pass along any code
      snippets and exchange ideas with you if you'd like.

      Depending on just what you're trying to do, an IK engine might
      be an interesting venue. Regardless, I've discovered that
      quaternions (and on certain occations, spherical coordinants)
      are the only reliable ways to avoid gimble-lock.

      While the euler coordinate system can represent any 3-dimensional
      position and orientation, as you are painfully aware, the
      solution often appears counter-intuitive.

      Look forward to hearing from you!



      ------------------
      Scott Martindale
      [email protected]
      Scott Martindale
      [email protected]

      Comment


      • #4
        Hi Scott!

        The rigid body system doesn't have a problem with gimble lock
        even though it uses standard matrices instead of quats. However,
        for general 3-D modelling, this rigid body approach doesn't really
        work; it's only for physical simulations where you specify a force
        vector somewhere on the body and want the resulting orientation
        afterwards. For other purposes (general 3D stuff), quats would
        probably be best as you've said. I'd be happy to exchange ideas
        with you on some stuff if you'd like, although I know very little
        about quaternion math. A buddy of mine uses them in his simulator
        however, and is always very helpful. I could ask him questions if
        necessary:
        http://www.racer.nl

        What is an IK engine? Inverse kinematics? The system I'm using
        now is perfect for vehicle simulation and is working great as it
        is. However, when I get back into the graphics system, a linked
        list system for models and vertices will be used. That seems to
        be a very good way to store and render objects.

        I did get the Cobra model you created awhile back. Very impressive!
        Thanks!

        As usual, feel free to email me or we can talk here too, of course.

        Thanks,


        ------------------
        Todd Wasson
        -------------
        Performance Simulations
        Drag Racing and Top Speed
        Prediction Software http://PerformanceSimulations.Com

        PowerBasic Racing Simulator:http://performancesimulations.com/scnshot4.htm

        [This message has been edited by Todd Wasson (edited November 29, 2002).]
        Todd Wasson
        http://PerformanceSimulations.Com
        PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

        Comment


        • #5
          See The Matrix and Quaternions FAQ in our Downloads section:

          http://www.powerbasic.com/files/pub/...ixQuat_Faq.zip

          ------------------
          Tom Hanlin
          PowerBASIC Staff

          Comment

          Working...
          X