Author Topic: glulookat vs hand made matrix  (Read 20412 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #40 on: December 01, 2006 »
oops sorry for the typo.

i cleaned it all up a bit so not hopefully i can remove the fudgy bits that i didnt like.

also is there a way through the matrixes that i can move the move the camera up if say i rotate on the x axis and move forward or move left and right by rotating on the y axis and moving forward or will i have to do that sort of thing myself.
 
Code: [Select]
''
'' This Code Was Created By Jeff Molofee 2000
'' A HUGE Thanks To Fredric Echols For Cleaning Up
'' And Optimizing The Base Code, Making It More Flexible!
'' If You've Found This Code Useful, Please Let Me Know.
'' Visit My Site At nehe.gamedev.net


option explicit

#include once "GL/gl.bi"
#include once "GL/glu.bi"
#include once "fbgfx.bi"

screen 18, 16, , 2

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 0.1, 1000.0
glMatrixMode GL_MODELVIEW
glLoadIdentity


glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 0.5
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

type mat
     
     m0 as double
     m1 as double
     m2 as double
     m3 as double
     m4 as double
     m5 as double
     m6 as double
     m7 as double
     m8 as double
     m9 as double
     m10 as double
     m11 as double
     m12 as double
     m13 as double
     m14 as double
     m15 as double
     
end type

type entity_pos
     
     x_pos as double
     y_pos as double
     z_pos as double
     
end type

type entity_rot
   
    x_rot as double
    y_rot as double
    z_rot as double
   
end type

type entity
     
     position as entity_pos ptr
     rotation as entity_rot ptr
     matrix as mat ptr
     
end type

declare sub draw_piramid( object as entity ptr )
declare sub draw_box( object as entity ptr )
declare sub loadentityidentity( object as entity ptr )
declare sub positionentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentityW( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( object as entity ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as entity ptr , byval source as entity ptr )
declare sub mulmatrix4x4( byval matrix1 as entity ptr , byval matrix2 as entity ptr , resultmatrix as entity ptr )
declare sub transposematrix( matrix as entity ptr )
declare sub multranslmatrix4x4( byval m1 as entity ptr , byval m2 as entity ptr , destination as entity ptr )
declare sub Grid( byval overall_size as integer , byval spacings as integer )
declare sub renderworld( camera as entity ptr )
declare sub clearworld()
declare sub MoveRotateCam( camera as entity ptr )
declare function new_entity() as entity ptr
declare sub convertMarray( dest as double ptr , byval source as entity ptr )
declare sub delete_entity( object as entity ptr )

dim shared as double Psin(0 to 360) , Pcos(0 to 360)

dim x as integer

for x = 1 to 360
      Psin( x ) = sin( x * 3.1415 / 180.0 )
      Pcos( x ) = cos( x * 3.1415 / 180.0 )
next

dim shared as entity ptr object0 , object1 , camera

object0 = new_entity()
object1 = new_entity()

camera = new_entity()

loadentityidentity( object0 )
loadentityidentity( object1 )

positionentity( object0 , 1.0 , 1 , -16.0 )
positionentity( object1 , 0 , 1 , 0 )
positionentity( camera , 0 , -5 , -25 )



do
   
    draw_piramid( object0 )
    draw_box( object1 )

    Grid( 500 , 10 )

    renderworld( camera )
    clearworld()
   
    MoveRotateCam( camera )
   
loop while not inkey$ = chr(27)



delete_entity( object0 )
delete_entity( object1 )
delete_entity( camera )



function new_entity() as entity ptr

         dim tmp_entity as entity ptr

         tmp_entity = callocate( len(entity) )
         tmp_entity->position = callocate(len(entity_pos))
         tmp_entity->rotation = callocate(len(entity_rot))
         tmp_entity->matrix = callocate(len(mat))
         
         return(tmp_entity)

end function

sub MoveRotateCam( camera as entity ptr )

    if multikey(SC_LEFT) then camera->rotation->y_rot -= 1
    if multikey(SC_RIGHT) then camera->rotation->y_rot += 1
    if multikey(SC_UP) then camera->rotation->x_rot += 1
    if multikey(SC_DOWN) then camera->rotation->x_rot -= 1

    if multikey(SC_W) then camera->position->z_pos += 0.1
    if multikey(SC_S) then camera->position->z_pos -= 0.1
    if multikey(SC_A) then camera->position->x_pos += 0.1
    if multikey(SC_D) then camera->position->x_pos -= 0.1
    if multikey(SC_I) then camera->position->y_pos += 0.1
    if multikey(SC_K) then camera->position->y_pos -= 0.1

end sub



sub clearworld()

    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

end sub



sub delete_entity( object as entity ptr )

    deallocate(object->rotation)
    deallocate(object->position)
    deallocate(object->matrix)
    deallocate(object)

end sub



sub renderworld( camera as entity ptr )

    dim as double cam(0 to 3,0 to 3)

    glLoadIdentity
   
    loadentityidentity( camera )

    moveentity( camera , camera->position->x_pos , camera->position->y_pos , camera->position->z_pos )

    rotateentity( camera , 0 , camera->rotation->y_rot , 0 )
    rotateentity( camera , camera->rotation->x_rot , 0 , 0 )
    rotateentity( camera , 0 , 0 , camera->rotation->z_rot )
   
    transposematrix( camera )
   
    convertMarray( @cam(0,0) , camera )
    glloadmatrixd( @cam(0,0) )

    flip

end sub



sub convertMarray( dest as double ptr , byval source as entity ptr )

     dest[0] = source->matrix->m0
     dest[1] = source->matrix->m1
     dest[2] = source->matrix->m2
     dest[3] = source->matrix->m3
     dest[4] = source->matrix->m4
     dest[5] = source->matrix->m5
     dest[6] = source->matrix->m6
     dest[7] = source->matrix->m7
     dest[8] = source->matrix->m8
     dest[9] = source->matrix->m9
     dest[10] = source->matrix->m10
     dest[11] = source->matrix->m11
     dest[12] = source->matrix->m12
     dest[13] = source->matrix->m13
     dest[14] = source->matrix->m14
     dest[15] = source->matrix->m15

end sub



sub Grid( byval overall_size as integer , byval spacings as integer )
   
    dim i as double

    for i = -overall_size to overall_size step spacings
         
          glBegin(GL_LINES)
                  glColor3ub(150, 190, 150)
                  glVertex3f(-overall_size, 0, i)
                  glVertex3f(overall_size, 0, i)
                  glVertex3f(i, 0,-overall_size)
                  glVertex3f(i, 0, overall_size)
          glEnd()
       
     next
     
end sub



sub loadentityidentity( object as entity ptr )

     object->matrix->m0 = 1
     object->matrix->m1 = 0
     object->matrix->m2 = 0
     object->matrix->m3 = 0
     object->matrix->m4 = 0
     object->matrix->m5 = 1
     object->matrix->m6 = 0
     object->matrix->m7 = 0
     object->matrix->m8 = 0
     object->matrix->m9 = 0
     object->matrix->m10 = 1
     object->matrix->m11 = 0
     object->matrix->m12 = 0
     object->matrix->m13 = 0
     object->matrix->m14 = 0
     object->matrix->m15 = 1

end sub



sub positionentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
   
    object->position->x_pos = xpos
    object->position->y_pos = ypos
    object->position->z_pos = zpos
   
end sub



sub moveentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
       
      dim as entity ptr Tmp1 , Tmp2

      Tmp1 = new_entity()
      Tmp2 = new_entity()

      loadentityidentity( Tmp1 )
      Tmp1->matrix->m12 = xpos
      Tmp1->matrix->m13 = ypos
      Tmp1->matrix->m14 = zpos

      mulmatrix4x4( Tmp1 , object , Tmp2 )

      copymatrix4x4( object , Tmp2 )

      delete_entity(Tmp1)
      delete_entity(Tmp2)

end sub



sub moveentityW( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
       
      object->matrix->m12 += xpos
      object->matrix->m13 += ypos
      object->matrix->m14 += zpos

end sub



sub rotateentity( object as entity ptr , xpos as double , ypos as double , zpos as double )

    dim as entity ptr Tmp1 , Tmp2

    Tmp1 = new_entity()
    Tmp2 = new_entity()

    if xpos > 360 then xpos = 0
    if xpos < 0 then xpos = 360
    if ypos > 360 then ypos = 0
    if ypos < 0 then ypos = 360
    if zpos > 360 then zpos = 0
    if zpos < 0 then zpos = 360
   
    'rotate on xaxis
    if xpos then

        loadentityidentity( Tmp1 )
        Tmp1->matrix->m5 = Pcos( xpos )
        Tmp1->matrix->m6 = -Psin( xpos )
        Tmp1->matrix->m9 = Psin( xpos )
        Tmp1->matrix->m10 = Pcos( xpos )
        mulmatrix4x4( Tmp1 , object , Tmp2 )
        copymatrix4x4( object , Tmp2 )

    endif
   
    'rotate on yaxis
    if ypos then

        loadentityidentity( Tmp1 )
        Tmp1->matrix->m0 = Pcos( ypos )
        Tmp1->matrix->m2 = Psin( ypos )
        Tmp1->matrix->m8 = -Psin( ypos )
        Tmp1->matrix->m10 = Pcos( ypos )
        mulmatrix4x4( Tmp1 , object , Tmp2 )
        copymatrix4x4( object , Tmp2 )

    endif
   
    'rotate on zaxis
    if zpos then

        loadentityidentity( Tmp1 )
        Tmp1->matrix->m0 = Pcos( zpos )
        Tmp1->matrix->m1 = -Psin( zpos )
        Tmp1->matrix->m4 = Psin( zpos )
        Tmp1->matrix->m5 = Pcos( zpos )
        mulmatrix4x4( Tmp1 , object , Tmp2 )
        copymatrix4x4( object , Tmp2 )
 
    endif

    delete_entity(Tmp1)
    delete_entity(Tmp2)

end sub



sub copymatrix4x4( destination as entity ptr , byval source as entity ptr )

    destination->matrix->m0 = source->matrix->m0
    destination->matrix->m1 = source->matrix->m1
    destination->matrix->m2 = source->matrix->m2
    destination->matrix->m3 = source->matrix->m3
    destination->matrix->m4 = source->matrix->m4
    destination->matrix->m5 = source->matrix->m5
    destination->matrix->m6 = source->matrix->m6
    destination->matrix->m7 = source->matrix->m7
    destination->matrix->m8 = source->matrix->m8
    destination->matrix->m9 = source->matrix->m9
    destination->matrix->m10 = source->matrix->m10
    destination->matrix->m11 = source->matrix->m11
    destination->matrix->m12 = source->matrix->m12
    destination->matrix->m13 = source->matrix->m13
    destination->matrix->m14 = source->matrix->m14
    destination->matrix->m15 = source->matrix->m15

end sub



sub transposematrix( object as entity ptr )
   
    dim as entity ptr Tmp1 , Tmp2

    Tmp1 = new_entity()
    Tmp2 = new_entity()

    copymatrix4x4( Tmp1 , object )

    Tmp1->matrix->m0 = object->matrix->m0
    Tmp1->matrix->m1 = object->matrix->m4
    Tmp1->matrix->m2 = object->matrix->m8

    Tmp1->matrix->m4 = object->matrix->m1
    Tmp1->matrix->m5 = object->matrix->m5
    Tmp1->matrix->m6 = object->matrix->m9

    Tmp1->matrix->m8 = object->matrix->m2
    Tmp1->matrix->m9 = object->matrix->m6
    Tmp1->matrix->m10 = object->matrix->m10

    multranslmatrix4x4(  object , Tmp1 , Tmp2 )
    copymatrix4x4( object , Tmp2 )

    delete_entity(Tmp1)
    delete_entity(Tmp2)

end sub



sub multranslmatrix4x4( byval m1 as entity ptr , byval m2 as entity ptr , destination as entity ptr )
   
    dim as double Tx , Ty , Tz

    Tx = m1->matrix->m0 * m2->matrix->m12 + m1->matrix->m1 * m2->matrix->m13 + m1->matrix->m2 * m2->matrix->m14
    Ty = m1->matrix->m4 * m2->matrix->m12 + m1->matrix->m5 * m2->matrix->m13 + m1->matrix->m6 * m2->matrix->m14
    Tz = m1->matrix->m8 * m2->matrix->m12 + m1->matrix->m9 * m2->matrix->m13 + m1->matrix->m10 * m2->matrix->m14

    copymatrix4x4( destination , m2 )
   
    destination->matrix->m12 = Tx
    destination->matrix->m13 = Ty
    destination->matrix->m14 = Tz

end sub



sub mulmatrix4x4( byval matrix1 as entity ptr , byval matrix2 as entity ptr , resultmatrix as entity ptr )
 
    resultmatrix->matrix->m0 = matrix1->matrix->m0 * matrix2->matrix->m0 + matrix1->matrix->m1 * matrix2->matrix->m4 + matrix1->matrix->m2 * matrix2->matrix->m8 + matrix1->matrix->m3 * matrix2->matrix->m12
    resultmatrix->matrix->m1 = matrix1->matrix->m0 * matrix2->matrix->m1 + matrix1->matrix->m1 * matrix2->matrix->m5 + matrix1->matrix->m2 * matrix2->matrix->m9 + matrix1->matrix->m3 * matrix2->matrix->m13
    resultmatrix->matrix->m2 = matrix1->matrix->m0 * matrix2->matrix->m2 + matrix1->matrix->m1 * matrix2->matrix->m6 + matrix1->matrix->m2 * matrix2->matrix->m10 + matrix1->matrix->m3 * matrix2->matrix->m14
    resultmatrix->matrix->m3 = matrix1->matrix->m0 * matrix2->matrix->m3 + matrix1->matrix->m1 * matrix2->matrix->m7 + matrix1->matrix->m2 * matrix2->matrix->m11 + matrix1->matrix->m3 * matrix2->matrix->m15

    resultmatrix->matrix->m4 = matrix1->matrix->m4 * matrix2->matrix->m0 + matrix1->matrix->m5 * matrix2->matrix->m4 + matrix1->matrix->m6 * matrix2->matrix->m8 + matrix1->matrix->m7 * matrix2->matrix->m12
    resultmatrix->matrix->m5 = matrix1->matrix->m4 * matrix2->matrix->m1 + matrix1->matrix->m5 * matrix2->matrix->m5 + matrix1->matrix->m6 * matrix2->matrix->m9 + matrix1->matrix->m7 * matrix2->matrix->m13
    resultmatrix->matrix->m6 = matrix1->matrix->m4 * matrix2->matrix->m2 + matrix1->matrix->m5 * matrix2->matrix->m6 + matrix1->matrix->m6 * matrix2->matrix->m10 + matrix1->matrix->m7 * matrix2->matrix->m14
    resultmatrix->matrix->m7 = matrix1->matrix->m4 * matrix2->matrix->m3 + matrix1->matrix->m5 * matrix2->matrix->m7 + matrix1->matrix->m6 * matrix2->matrix->m11 + matrix1->matrix->m7 * matrix2->matrix->m15

    resultmatrix->matrix->m8 = matrix1->matrix->m8 * matrix2->matrix->m0 + matrix1->matrix->m9 * matrix2->matrix->m4 + matrix1->matrix->m10 * matrix2->matrix->m8 + matrix1->matrix->m11 * matrix2->matrix->m12
    resultmatrix->matrix->m9 = matrix1->matrix->m8 * matrix2->matrix->m1 + matrix1->matrix->m9 * matrix2->matrix->m5 + matrix1->matrix->m10 * matrix2->matrix->m9 + matrix1->matrix->m11 * matrix2->matrix->m13
    resultmatrix->matrix->m10 = matrix1->matrix->m8 * matrix2->matrix->m2 + matrix1->matrix->m9 * matrix2->matrix->m6 + matrix1->matrix->m10 * matrix2->matrix->m10 + matrix1->matrix->m11 * matrix2->matrix->m14
    resultmatrix->matrix->m11 = matrix1->matrix->m8 * matrix2->matrix->m3 + matrix1->matrix->m9 * matrix2->matrix->m7 + matrix1->matrix->m10 * matrix2->matrix->m11 + matrix1->matrix->m11 * matrix2->matrix->m15

    resultmatrix->matrix->m12 = matrix1->matrix->m12 * matrix2->matrix->m0 + matrix1->matrix->m13 * matrix2->matrix->m4 + matrix1->matrix->m14 * matrix2->matrix->m8 + matrix1->matrix->m15 * matrix2->matrix->m12
    resultmatrix->matrix->m13 = matrix1->matrix->m12 * matrix2->matrix->m1 + matrix1->matrix->m13 * matrix2->matrix->m5 + matrix1->matrix->m14 * matrix2->matrix->m9 + matrix1->matrix->m15 * matrix2->matrix->m13
    resultmatrix->matrix->m14 = matrix1->matrix->m12 * matrix2->matrix->m2 + matrix1->matrix->m13 * matrix2->matrix->m6 + matrix1->matrix->m14 * matrix2->matrix->m10 + matrix1->matrix->m15 * matrix2->matrix->m14
    resultmatrix->matrix->m15 = matrix1->matrix->m12 * matrix2->matrix->m3 + matrix1->matrix->m13 * matrix2->matrix->m7 + matrix1->matrix->m14 * matrix2->matrix->m11 + matrix1->matrix->m15 * matrix2->matrix->m15

end sub



sub draw_piramid( object as entity ptr )

    dim as double obj(0 to 3,0 to 3)

    loadentityidentity( object )

    moveentity( object , object->position->x_pos , object->position->y_pos , object->position->z_pos )
    rotateentity( object , 0 , object->rotation->y_rot , 0 )
    rotateentity( object , object->rotation->x_rot , 0 , 0 )
    rotateentity( object , 0 , 0 , object->rotation->z_rot )

    convertMarray( @obj(0,0) , object )

    glpushmatrix
    glmultmatrixd( @obj(0,0) )

    glBegin GL_TRIANGLES

            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f -1.0, -1.0, 1.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f 1.0, -1.0, -1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f 1.0, -1.0, -1.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f -1.0, -1.0, -1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f -1.0, -1.0, -1.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f -1.0, -1.0, 1.0

    glEnd
    glpopmatrix
   
end sub



sub draw_box( object as entity ptr )

    dim as double obj(0 to 3,0 to 3)
     
    loadentityidentity( object )

    moveentity( object , object->position->x_pos , object->position->y_pos , object->position->z_pos )
    rotateentity( object , 0 , object->rotation->y_rot , 0 )
    rotateentity( object , object->rotation->x_rot , 0 , 0 )
    rotateentity( object , 0 , 0 , object->rotation->z_rot )

    convertMarray( @obj(0,0) , object )

    glpushmatrix
    glmultmatrixd( @obj(0,0) )

    glBegin GL_QUADS

            glColor3f 0.0, 1.0, 0.0
            glVertex3f 1.0, 1.0, -1.0
            glVertex3f -1.0, 1.0, -1.0
            glVertex3f -1.0, 1.0, 1.0
            glVertex3f 1.0, 1.0, 1.0
           
            glColor3f 1.0, 0.5, 0.0
            glVertex3f 1.0, -1.0, 1.0
            glVertex3f -1.0, -1.0, 1.0
            glVertex3f -1.0, -1.0, -1.0
            glVertex3f 1.0, -1.0, -1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 1.0, 1.0, 1.0
            glVertex3f -1.0, 1.0, 1.0
            glVertex3f -1.0, -1.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
           
            glColor3f 1.0, 1.0, 0.0
            glVertex3f 1.0, -1.0, -1.0
            glVertex3f -1.0, -1.0, -1.0
            glVertex3f -1.0, 1.0, -1.0
            glVertex3f 1.0, 1.0, -1.0
           
            glColor3f 0.0, 0.0, 1.0
            glVertex3f -1.0, 1.0, 1.0
            glVertex3f -1.0, 1.0, -1.0
            glVertex3f -1.0, -1.0, -1.0
            glVertex3f -1.0, -1.0, 1.0
           
            glColor3f 1.0, 0.0, 1.0
            glVertex3f 1.0, 1.0, -1.0
            glVertex3f 1.0, 1.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
            glVertex3f 1.0, -1.0, -1.0

    glEnd
    glpopmatrix

end sub
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookat vs hand made matrix
« Reply #41 on: December 01, 2006 »
Quote
also is there a way through the matrixes that i can move the move the camera up if say i rotate on the x axis and move forward or move left and right by rotating on the y axis and moving forward or will i have to do that sort of thing myself.
You can do any and every possible position and orientation with the camera matrix, but I don't know what you mean exactly.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #42 on: December 02, 2006 »
well what im thinking of is say when a plane takes off it pitches and the plane moves up dependant on on how much pitch there is it rises and falls accordingly.

similarily when the plane rolls it moves left and right depending on the amount of roll.

this is what i want my camera to do so basically i want to modify the translation matrix depending on how much tilt turn roll values are being applyed.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookat vs hand made matrix
« Reply #43 on: December 02, 2006 »
I think when you say pitch you mean tilt.  Well, one way of making it all work is to have the object move forward along its matrix z direction.
That could be
Tx = Tx + matrix[12] * speed
Ty = Ty + matrix[13] * speed
Tz = Tz + matrix[14] * speed

Jim

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #44 on: December 02, 2006 »
that doesnt quite work jim.

its close though.

the way i want it is what ever angle the camera is at the camera will move in that direction when i add on to the zposition.

do you have any good links on 3d programing matrix maths jim its just id like to be able to create lots of diffrent camera with diffrent attributes but i dont know enough about this stuff atm.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookat vs hand made matrix
« Reply #45 on: December 02, 2006 »
Sorry, should have been
Code: [Select]
Tx = Tx + matrix[8] * speed
Ty = Ty + matrix[9] * speed
Tz = Tz + matrix[10] * speed
Basically the z axis (3rd row) of the matrix is the direction it's pointing in, so add that to the translation to move it forwards.
(1st row is across, 2nd row is up/down).

This is a pretty good faq http://www.faqs.org/faqs/graphics/algorithms-faq/

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #46 on: December 03, 2006 »
thanks for the faq jim its great.

btw that works fine the only problem i have with it though is the movecamera has to be after the rotation commands for it to work and that doesnt allow my camera to spin on its center any more ie the further away for 0,0,0 the camera is the more it sweeps round the scene.

 
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookat vs hand made matrix
« Reply #47 on: December 03, 2006 »
I apologise that I really haven't had a chance to scrutinise your code as closely as I would like.  OpenGL is out of action at home while ATI get their video drivers for Vista together.

You have to remember that unlike ordinary multiplication where 3 x 5 is the same as 5 x 3 (callled 'commutative') with matrix multiplication M x N is normally not the same as N x M.  This applies to translations too.  So the order you do object rotation and translation, and camera rotation and translation is very important.  I'm guessing that you've not quite got it in the right order just now.  For the movecamera thing to work, it HAS to be done after the rotation is built.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #48 on: December 04, 2006 »
sorry to hear of your vista troubles mate,

im not shure where ive gone wrong ive just spent the past hour moving my translations and rotations about and this is as close as i can get im shure its a problem with the translations but i just cant see where from.

anyway heres where ive got.

Code: [Select]
''
'' This Code Was Created By Jeff Molofee 2000
'' A HUGE Thanks To Fredric Echols For Cleaning Up
'' And Optimizing The Base Code, Making It More Flexible!
'' If You've Found This Code Useful, Please Let Me Know.
'' Visit My Site At nehe.gamedev.net


option explicit

#include once "GL/gl.bi"
#include once "GL/glu.bi"
#include once "fbgfx.bi"

screen 18, 16, , 2

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 0.1, 1000.0
glMatrixMode GL_MODELVIEW
glLoadIdentity


glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 0.5
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

type mat
     
     m0 as double
     m1 as double
     m2 as double
     m3 as double
     m4 as double
     m5 as double
     m6 as double
     m7 as double
     m8 as double
     m9 as double
     m10 as double
     m11 as double
     m12 as double
     m13 as double
     m14 as double
     m15 as double
     
end type

type entity_pos
     
     x_pos as double
     y_pos as double
     z_pos as double
     
end type

type entity_rot
   
    x_rot as double
    y_rot as double
    z_rot as double
   
end type

type entity
     
     position as entity_pos ptr
     rotation as entity_rot ptr
     matrix as mat ptr
     
end type

declare sub draw_piramid( object as entity ptr )
declare sub draw_box( object as entity ptr )
declare sub loadentityidentity( object as entity ptr )
declare sub positionentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentityW( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( object as entity ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as entity ptr , byval source as entity ptr )
declare sub mulmatrix4x4( byval matrix1 as entity ptr , byval matrix2 as entity ptr , resultmatrix as entity ptr )
declare sub transposematrix( matrix as entity ptr )
declare sub multranslmatrix4x4( byval m1 as entity ptr , byval m2 as entity ptr , destination as entity ptr )
declare sub Grid( byval overall_size as integer , byval spacings as integer )
declare sub renderworld( camera as entity ptr )
declare sub clearworld()
declare sub MoveRotateCam( camera as entity ptr )
declare function new_entity() as entity ptr
declare sub convertMarray( dest as double ptr , byval source as entity ptr )
declare sub delete_entity( object as entity ptr )

dim shared as double Psin(0 to 360) , Pcos(0 to 360)

dim x as integer

for x = 1 to 360
      Psin( x ) = sin( x * 3.1415 / 180.0 )
      Pcos( x ) = cos( x * 3.1415 / 180.0 )
next

dim shared as entity ptr object0 , object1 , camera

object0 = new_entity()
object1 = new_entity()

camera = new_entity()

loadentityidentity( object0 )
loadentityidentity( object1 )

positionentity( object0 , 1.0 , 1 , -16.0 )
positionentity( object1 , 0 , 1 , 0 )
positionentity( camera , 0 , -5 , -25 )



do
   
    draw_piramid( object0 )
    draw_box( object1 )

    Grid( 500 , 10 )

    renderworld( camera )
    clearworld()
   
    MoveRotateCam( camera )
   
loop while not inkey$ = chr(27)



delete_entity( object0 )
delete_entity( object1 )
delete_entity( camera )



function new_entity() as entity ptr

         dim tmp_entity as entity ptr

         tmp_entity = callocate( len(entity) )
         tmp_entity->position = callocate(len(entity_pos))
         tmp_entity->rotation = callocate(len(entity_rot))
         tmp_entity->matrix = callocate(len(mat))
         
         return(tmp_entity)

end function

sub MoveRotateCam( camera as entity ptr )

    if multikey(SC_LEFT) then camera->rotation->y_rot -= 1
    if multikey(SC_RIGHT) then camera->rotation->y_rot += 1
    if multikey(SC_UP) then camera->rotation->x_rot += 1
    if multikey(SC_DOWN) then camera->rotation->x_rot -= 1

    if multikey(SC_W) then camera->position->z_pos += 0.2
    if multikey(SC_S) then camera->position->z_pos -= 0.2
    if multikey(SC_A) then camera->position->x_pos += 0.2
    if multikey(SC_D) then camera->position->x_pos -= 0.2
    if multikey(SC_I) then camera->position->y_pos += 0.2
    if multikey(SC_K) then camera->position->y_pos -= 0.2

end sub



sub clearworld()

    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

end sub



sub delete_entity( object as entity ptr )

    deallocate(object->rotation)
    deallocate(object->position)
    deallocate(object->matrix)
    deallocate(object)

end sub



sub renderworld( camera as entity ptr )

    dim as double cam(0 to 3,0 to 3)
   
    loadentityidentity( camera )

    rotateentity( camera , 0 , camera->rotation->y_rot , 0 )
    rotateentity( camera , camera->rotation->x_rot , 0 , 0 )
    rotateentity( camera , 0 , 0 , camera->rotation->z_rot )
   
    moveentity( camera , camera->position->x_pos , camera->position->y_pos , camera->position->z_pos )

    transposematrix( camera )

    convertMarray( @cam(0,0) , camera )
    glloadmatrixd( @cam(0,0) )

    flip

end sub



sub convertMarray( dest as double ptr , byval source as entity ptr )

     dest[0] = source->matrix->m0
     dest[1] = source->matrix->m1
     dest[2] = source->matrix->m2
     dest[3] = source->matrix->m3
     dest[4] = source->matrix->m4
     dest[5] = source->matrix->m5
     dest[6] = source->matrix->m6
     dest[7] = source->matrix->m7
     dest[8] = source->matrix->m8
     dest[9] = source->matrix->m9
     dest[10] = source->matrix->m10
     dest[11] = source->matrix->m11
     dest[12] = source->matrix->m12
     dest[13] = source->matrix->m13
     dest[14] = source->matrix->m14
     dest[15] = source->matrix->m15

end sub



sub Grid( byval overall_size as integer , byval spacings as integer )
   
    dim i as double

    for i = -overall_size to overall_size step spacings
         
          glBegin(GL_LINES)
                  glColor3ub(150, 190, 150)
                  glVertex3f(-overall_size, 0, i)
                  glVertex3f(overall_size, 0, i)
                  glVertex3f(i, 0,-overall_size)
                  glVertex3f(i, 0, overall_size)
          glEnd()
       
     next
     
end sub



sub loadentityidentity( object as entity ptr )

     object->matrix->m0 = 1
     object->matrix->m1 = 0
     object->matrix->m2 = 0
     object->matrix->m3 = 0
     object->matrix->m4 = 0
     object->matrix->m5 = 1
     object->matrix->m6 = 0
     object->matrix->m7 = 0
     object->matrix->m8 = 0
     object->matrix->m9 = 0
     object->matrix->m10 = 1
     object->matrix->m11 = 0
     object->matrix->m12 = 0
     object->matrix->m13 = 0
     object->matrix->m14 = 0
     object->matrix->m15 = 1

end sub



sub positionentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
   
    object->position->x_pos = xpos
    object->position->y_pos = ypos
    object->position->z_pos = zpos
   
end sub



sub moveentity( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
       
      dim as entity ptr Tmp1 , Tmp2

      Tmp1 = new_entity()
      Tmp2 = new_entity()

      loadentityidentity( Tmp1 )

      Tmp1->matrix->m12 = xpos
      Tmp1->matrix->m13 = ypos
      Tmp1->matrix->m14 = zpos

      mulmatrix4x4( Tmp1 , object , Tmp2 )

      copymatrix4x4( object , Tmp2 )

      delete_entity(Tmp1)
      delete_entity(Tmp2)

end sub



sub moveentityW( object as entity ptr , byval xpos as double , byval ypos as double , byval zpos as double )
       
      object->matrix->m12 += xpos
      object->matrix->m13 += ypos
      object->matrix->m14 += zpos

end sub



sub rotateentity( object as entity ptr , xpos as double , ypos as double , zpos as double )

    dim as entity ptr Tmp1 , Tmp2

    Tmp1 = new_entity()
    Tmp2 = new_entity()

    if xpos > 360 then xpos = 0
    if xpos < 0 then xpos = 360
    if ypos > 360 then ypos = 0
    if ypos < 0 then ypos = 360
    if zpos > 360 then zpos = 0
    if zpos < 0 then zpos = 360
   
    'rotate on xaxis
    if xpos then

        loadentityidentity( Tmp1 )
        Tmp1->matrix->m5 = Pcos( xpos )
        Tmp1->matrix->m6 = -Psin( xpos )
        Tmp1->matrix->m9 = Psin( xpos )
        Tmp1->matrix->m10 = Pcos( xpos )
        mulmatrix4x4( Tmp1 , object , Tmp2 )
        copymatrix4x4( object , Tmp2 )

    endif
   
    'rotate on yaxis
    if ypos then

        loadentityidentity( Tmp1 )
        Tmp1->matrix->m0 = Pcos( ypos )
        Tmp1->matrix->m2 = Psin( ypos )
        Tmp1->matrix->m8 = -Psin( ypos )
        Tmp1->matrix->m10 = Pcos( ypos )
        mulmatrix4x4( Tmp1 , object , Tmp2 )
        copymatrix4x4( object , Tmp2 )

    endif
   
    'rotate on zaxis
    if zpos then

        loadentityidentity( Tmp1 )
        Tmp1->matrix->m0 = Pcos( zpos )
        Tmp1->matrix->m1 = -Psin( zpos )
        Tmp1->matrix->m4 = Psin( zpos )
        Tmp1->matrix->m5 = Pcos( zpos )
        mulmatrix4x4( Tmp1 , object , Tmp2 )
        copymatrix4x4( object , Tmp2 )
 
    endif

    delete_entity(Tmp1)
    delete_entity(Tmp2)

end sub



sub copymatrix4x4( destination as entity ptr , byval source as entity ptr )

    destination->matrix->m0 = source->matrix->m0
    destination->matrix->m1 = source->matrix->m1
    destination->matrix->m2 = source->matrix->m2
    destination->matrix->m3 = source->matrix->m3
    destination->matrix->m4 = source->matrix->m4
    destination->matrix->m5 = source->matrix->m5
    destination->matrix->m6 = source->matrix->m6
    destination->matrix->m7 = source->matrix->m7
    destination->matrix->m8 = source->matrix->m8
    destination->matrix->m9 = source->matrix->m9
    destination->matrix->m10 = source->matrix->m10
    destination->matrix->m11 = source->matrix->m11
    destination->matrix->m12 = source->matrix->m12
    destination->matrix->m13 = source->matrix->m13
    destination->matrix->m14 = source->matrix->m14
    destination->matrix->m15 = source->matrix->m15

end sub



sub transposematrix( object as entity ptr )
   
    dim as entity ptr Tmp1 , Tmp2

    Tmp1 = new_entity()
    Tmp2 = new_entity()

    copymatrix4x4( Tmp1 , object )

    Tmp1->matrix->m0 = object->matrix->m0
    Tmp1->matrix->m1 = object->matrix->m4
    Tmp1->matrix->m2 = object->matrix->m8

    Tmp1->matrix->m4 = object->matrix->m1
    Tmp1->matrix->m5 = object->matrix->m5
    Tmp1->matrix->m6 = object->matrix->m9

    Tmp1->matrix->m8 = object->matrix->m2
    Tmp1->matrix->m9 = object->matrix->m6
    Tmp1->matrix->m10 = object->matrix->m10

    multranslmatrix4x4(  object , Tmp1 , Tmp2 )
    copymatrix4x4( object , Tmp2 )

    delete_entity(Tmp1)
    delete_entity(Tmp2)

end sub



sub multranslmatrix4x4( byval m1 as entity ptr , byval m2 as entity ptr , destination as entity ptr )
   
    dim as double Tx , Ty , Tz

    Tx = m1->matrix->m0 * m2->matrix->m12 + m1->matrix->m1 * m2->matrix->m13 + m1->matrix->m2 * m2->matrix->m14
    Ty = m1->matrix->m4 * m2->matrix->m12 + m1->matrix->m5 * m2->matrix->m13 + m1->matrix->m6 * m2->matrix->m14
    Tz = m1->matrix->m8 * m2->matrix->m12 + m1->matrix->m9 * m2->matrix->m13 + m1->matrix->m10 * m2->matrix->m14

    copymatrix4x4( destination , m2 )
   
    Tx += m2->matrix->m8
    Ty += m2->matrix->m9
    Tz += m2->matrix->m10


    destination->matrix->m12 = Tx
    destination->matrix->m13 = Ty
    destination->matrix->m14 = Tz

end sub



sub mulmatrix4x4( byval matrix1 as entity ptr , byval matrix2 as entity ptr , resultmatrix as entity ptr )
 
    resultmatrix->matrix->m0 = matrix1->matrix->m0 * matrix2->matrix->m0 + matrix1->matrix->m1 * matrix2->matrix->m4 + matrix1->matrix->m2 * matrix2->matrix->m8 + matrix1->matrix->m3 * matrix2->matrix->m12
    resultmatrix->matrix->m1 = matrix1->matrix->m0 * matrix2->matrix->m1 + matrix1->matrix->m1 * matrix2->matrix->m5 + matrix1->matrix->m2 * matrix2->matrix->m9 + matrix1->matrix->m3 * matrix2->matrix->m13
    resultmatrix->matrix->m2 = matrix1->matrix->m0 * matrix2->matrix->m2 + matrix1->matrix->m1 * matrix2->matrix->m6 + matrix1->matrix->m2 * matrix2->matrix->m10 + matrix1->matrix->m3 * matrix2->matrix->m14
    resultmatrix->matrix->m3 = matrix1->matrix->m0 * matrix2->matrix->m3 + matrix1->matrix->m1 * matrix2->matrix->m7 + matrix1->matrix->m2 * matrix2->matrix->m11 + matrix1->matrix->m3 * matrix2->matrix->m15

    resultmatrix->matrix->m4 = matrix1->matrix->m4 * matrix2->matrix->m0 + matrix1->matrix->m5 * matrix2->matrix->m4 + matrix1->matrix->m6 * matrix2->matrix->m8 + matrix1->matrix->m7 * matrix2->matrix->m12
    resultmatrix->matrix->m5 = matrix1->matrix->m4 * matrix2->matrix->m1 + matrix1->matrix->m5 * matrix2->matrix->m5 + matrix1->matrix->m6 * matrix2->matrix->m9 + matrix1->matrix->m7 * matrix2->matrix->m13
    resultmatrix->matrix->m6 = matrix1->matrix->m4 * matrix2->matrix->m2 + matrix1->matrix->m5 * matrix2->matrix->m6 + matrix1->matrix->m6 * matrix2->matrix->m10 + matrix1->matrix->m7 * matrix2->matrix->m14
    resultmatrix->matrix->m7 = matrix1->matrix->m4 * matrix2->matrix->m3 + matrix1->matrix->m5 * matrix2->matrix->m7 + matrix1->matrix->m6 * matrix2->matrix->m11 + matrix1->matrix->m7 * matrix2->matrix->m15

    resultmatrix->matrix->m8 = matrix1->matrix->m8 * matrix2->matrix->m0 + matrix1->matrix->m9 * matrix2->matrix->m4 + matrix1->matrix->m10 * matrix2->matrix->m8 + matrix1->matrix->m11 * matrix2->matrix->m12
    resultmatrix->matrix->m9 = matrix1->matrix->m8 * matrix2->matrix->m1 + matrix1->matrix->m9 * matrix2->matrix->m5 + matrix1->matrix->m10 * matrix2->matrix->m9 + matrix1->matrix->m11 * matrix2->matrix->m13
    resultmatrix->matrix->m10 = matrix1->matrix->m8 * matrix2->matrix->m2 + matrix1->matrix->m9 * matrix2->matrix->m6 + matrix1->matrix->m10 * matrix2->matrix->m10 + matrix1->matrix->m11 * matrix2->matrix->m14
    resultmatrix->matrix->m11 = matrix1->matrix->m8 * matrix2->matrix->m3 + matrix1->matrix->m9 * matrix2->matrix->m7 + matrix1->matrix->m10 * matrix2->matrix->m11 + matrix1->matrix->m11 * matrix2->matrix->m15

    resultmatrix->matrix->m12 = matrix1->matrix->m12 * matrix2->matrix->m0 + matrix1->matrix->m13 * matrix2->matrix->m4 + matrix1->matrix->m14 * matrix2->matrix->m8 + matrix1->matrix->m15 * matrix2->matrix->m12
    resultmatrix->matrix->m13 = matrix1->matrix->m12 * matrix2->matrix->m1 + matrix1->matrix->m13 * matrix2->matrix->m5 + matrix1->matrix->m14 * matrix2->matrix->m9 + matrix1->matrix->m15 * matrix2->matrix->m13
    resultmatrix->matrix->m14 = matrix1->matrix->m12 * matrix2->matrix->m2 + matrix1->matrix->m13 * matrix2->matrix->m6 + matrix1->matrix->m14 * matrix2->matrix->m10 + matrix1->matrix->m15 * matrix2->matrix->m14
    resultmatrix->matrix->m15 = matrix1->matrix->m12 * matrix2->matrix->m3 + matrix1->matrix->m13 * matrix2->matrix->m7 + matrix1->matrix->m14 * matrix2->matrix->m11 + matrix1->matrix->m15 * matrix2->matrix->m15

end sub



sub draw_piramid( object as entity ptr )

    dim as double obj(0 to 3,0 to 3)

    loadentityidentity( object )

    moveentity( object , object->position->x_pos , object->position->y_pos , object->position->z_pos )

    rotateentity( object , 0 , object->rotation->y_rot , 0 )
    rotateentity( object , object->rotation->x_rot , 0 , 0 )
    rotateentity( object , 0 , 0 , object->rotation->z_rot )

    convertMarray( @obj(0,0) , object )

    glpushmatrix
    glmultmatrixd( @obj(0,0) )

    glBegin GL_TRIANGLES

            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f -1.0, -1.0, 1.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f 1.0, -1.0, -1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f 1.0, -1.0, -1.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f -1.0, -1.0, -1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 0.0, 1.0, 0.0
            glColor3f 0.0, 0.0, 1.0
            glVertex3f -1.0, -1.0, -1.0
            glColor3f 0.0, 1.0, 0.0
            glVertex3f -1.0, -1.0, 1.0

    glEnd
    glpopmatrix
   
end sub



sub draw_box( object as entity ptr )

    dim as double obj(0 to 3,0 to 3)
     
    loadentityidentity( object )

    moveentity( object , object->position->x_pos , object->position->y_pos , object->position->z_pos )

    rotateentity( object , 0 , object->rotation->y_rot , 0 )
    rotateentity( object , object->rotation->x_rot , 0 , 0 )
    rotateentity( object , 0 , 0 , object->rotation->z_rot )

    convertMarray( @obj(0,0) , object )

    glpushmatrix
    glmultmatrixd( @obj(0,0) )

    glBegin GL_QUADS

            glColor3f 0.0, 1.0, 0.0
            glVertex3f 1.0, 1.0, -1.0
            glVertex3f -1.0, 1.0, -1.0
            glVertex3f -1.0, 1.0, 1.0
            glVertex3f 1.0, 1.0, 1.0
           
            glColor3f 1.0, 0.5, 0.0
            glVertex3f 1.0, -1.0, 1.0
            glVertex3f -1.0, -1.0, 1.0
            glVertex3f -1.0, -1.0, -1.0
            glVertex3f 1.0, -1.0, -1.0
           
            glColor3f 1.0, 0.0, 0.0
            glVertex3f 1.0, 1.0, 1.0
            glVertex3f -1.0, 1.0, 1.0
            glVertex3f -1.0, -1.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
           
            glColor3f 1.0, 1.0, 0.0
            glVertex3f 1.0, -1.0, -1.0
            glVertex3f -1.0, -1.0, -1.0
            glVertex3f -1.0, 1.0, -1.0
            glVertex3f 1.0, 1.0, -1.0
           
            glColor3f 0.0, 0.0, 1.0
            glVertex3f -1.0, 1.0, 1.0
            glVertex3f -1.0, 1.0, -1.0
            glVertex3f -1.0, -1.0, -1.0
            glVertex3f -1.0, -1.0, 1.0
           
            glColor3f 1.0, 0.0, 1.0
            glVertex3f 1.0, 1.0, -1.0
            glVertex3f 1.0, 1.0, 1.0
            glVertex3f 1.0, -1.0, 1.0
            glVertex3f 1.0, -1.0, -1.0

    glEnd
    glpopmatrix

end sub

« Last Edit: December 04, 2006 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #49 on: December 07, 2006 »
ive been messing around quite a bit again with this and i just cant get it to work right if i multiply the translation matrix by the rotation the camera spins on its center so its definitly when i multiply my rotation matrix by my translation one that things go a little pair shaped.

ive moved the code about quite a lot with no joy.

would i be better mabey multiplying the translation by the rotation and moving the camera by the z pos my self.

and how would i go about it.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: glulookat vs hand made matrix
« Reply #50 on: December 07, 2006 »
I have no idea if this'll be any help at all ninogenio, I use 3*3 matrices and have the entity coordinates stored separately but this is what I use to set it up.

Code: [Select]
sub rotate_vertices(byval mesh as entity pointer,byval camera as entity pointer)

  Â  dim cam_xt as single=mesh->x-camera->x
  Â  dim cam_yt as single=mesh->y-camera->y
  Â  dim cam_zt as single=mesh->z-camera->z
  Â  
  Â  mesh->ident(0)=camera->ux0*mesh->ux0+camera->ux1*mesh->ux1+camera->ux2*mesh->ux2
  Â  mesh->ident(1)=camera->uy0*mesh->ux0+camera->uy1*mesh->ux1+camera->uy2*mesh->ux2
  Â  mesh->ident(2)=-(camera->uz0*mesh->ux0+camera->uz1*mesh->ux1+camera->uz2*mesh->ux2)
  Â  mesh->ident(3)=0.0

  Â  mesh->ident(4)=camera->ux0*mesh->uy0+camera->ux1*mesh->uy1+camera->ux2*mesh->uy2
  Â  mesh->ident(5)=camera->uy0*mesh->uy0+camera->uy1*mesh->uy1+camera->uy2*mesh->uy2
  Â  mesh->ident(6)=-(camera->uz0*mesh->uy0+camera->uz1*mesh->uy1+camera->uz2*mesh->uy2)
  Â  mesh->ident(7)=0.0

  Â  mesh->ident(8)=camera->ux0*mesh->uz0  + camera->ux1*mesh->uz1  +  camera->ux2*mesh->uz2
  Â  mesh->ident(9)=camera->uy0*mesh->uz0  + camera->uy1*mesh->uz1  +  camera->uy2*mesh->uz2
  Â  mesh->ident(10)=-(camera->uz0*mesh->uz0  +  camera->uz1*mesh->uz1  +  camera->uz2*mesh->uz2)
  Â  mesh->ident(11)=0.0

  Â  mesh->ident(12)=camera->ux0*cam_xt  +  camera->ux1*cam_yt  +  camera->ux2*cam_zt
  Â  mesh->ident(13)=camera->uy0*cam_xt  +  camera->uy1*cam_yt  +  camera->uy2*cam_zt
  Â  mesh->ident(14)=-(camera->uz0*cam_xt  +  camera->uz1*cam_yt  +  camera->uz2*cam_zt)
  Â  mesh->ident(15)=1.0

  Â  glloadmatrixf(@mesh->ident(0))

end sub

I'm doing a few passes for each frame and this only needs to be done once for each mesh, any subsequent pass only needs:

glloadmatrixf(@mesh->ident(0))
« Last Edit: December 07, 2006 by Stonemonkey »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #51 on: December 07, 2006 »
cool cheers stonemonkey im currently still trying without any luck but ill keep going.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #52 on: December 07, 2006 »
jim i just spotted this out of your link you put up would it be possible to use this to rotate my camera around its new xyz coords instead of its origin and still be able to make it move in whatever direction its pointed in.

Code: [Select]
/* The following routine converts an angle and a unit axis vector
        * to a matrix, returning the corresponding unit quaternion at no
        * extra cost. It is written in such a way as to allow both fixed
        * point and floating point versions to be created by appropriate
        * definitions of FPOINT, ANGLE, VECTOR, QUAT, MATRIX, MUL, HALF,
        * TWICE, COS, SIN, ONE, and ZERO.
        * The following is an example of floating point definitions.
        #define FPOINT double
        #define ANGLE FPOINT
        #define VECTOR QUAT
        typedef struct {FPOINT x,y,z,w;} QUAT;
        enum Indices {X,Y,Z,W};
        typedef FPOINT MATRIX[4][4];
        #define MUL(a,b) ((a)*(b))
        #define HALF(a) ((a)*0.5)
        #define TWICE(a) ((a)*2.0)
        #define COS cos
        #define SIN sin
        #define ONE 1.0
        #define ZERO 0.0
    */
    QUAT MatrixFromAxisAngle(VECTOR axis, ANGLE theta, MATRIX m)
    {
        QUAT q;
        ANGLE halfTheta = HALF(theta);
        FPOINT cosHalfTheta = COS(halfTheta);
        FPOINT sinHalfTheta = SIN(halfTheta);
        FPOINT xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz;
        q.x = MUL(axis.x,sinHalfTheta);
        q.y = MUL(axis.y,sinHalfTheta);
        q.z = MUL(axis.z,sinHalfTheta);
        q.w = cosHalfTheta;
        xs = TWICE(q.x);  ys = TWICE(q.y);  zs = TWICE(q.z);
        wx = MUL(q.w,xs); wy = MUL(q.w,ys); wz = MUL(q.w,zs);
        xx = MUL(q.x,xs); xy = MUL(q.x,ys); xz = MUL(q.x,zs);
        yy = MUL(q.y,ys); yz = MUL(q.y,zs); zz = MUL(q.z,zs);
        m[X][X] = ONE - (yy + zz); m[X][Y] = xy - wz; m[X][Z] = xz + wy;
        m[Y][X] = xy + wz; m[Y][Y] = ONE - (xx + zz); m[Y][Z] = yz - wx;
        m[Z][X] = xz - wy; m[Z][Y] = yz + wx; m[Z][Z] = ONE - (xx + yy);
        /* Fill in remainder of 4x4 homogeneous transform matrix. */
        m[W][X] = m[W][Y] = m[W][Z] = m[X][W] = m[Y][W] = m[Z][W] = ZERO;
        m[W][W] = ONE;
        return (q);
    }
    /* The routine just given, MatrixFromAxisAngle, performs rotation about
        * an axis passing through the origin, so only a unit vector was needed
        * in addition to the angle. To rotate about an axis not containing the
        * origin, a point on the axis is also needed, as in the following. For
        * mathematical purity, the type POINT is used, but may be defined as:
        #define POINT VECTOR
    */
    QUAT MatrixFromAnyAxisAngle(POINT o, VECTOR axis, ANGLE theta, MATRIX m)
    {
        QUAT q;
        q = MatrixFromAxisAngle(axis,theta,m);
        m[X][W] = o.x-(MUL(m[X][X],o.x)+MUL(m[X][Y],o.y)+MUL(m[X][Z],o.z));
        m[Y][W] = o.y-(MUL(m[Y][X],o.x)+MUL(m[Y][Y],o.y)+MUL(m[Y][Z],o.z));
        m[Z][W] = o.x-(MUL(m[Z][X],o.x)+MUL(m[Z][Y],o.y)+MUL(m[Z][Z],o.z));
        return (q);
    }
    /* An axis can also be specified by a pair of points, with the direction
        * along the line assumed from the ordering of the points. Although both
        * the previous routines assumed the axis vector was unit length without
        * checking, this routine must cope with a more delicate possibility. If
        * the two points are identical, or even nearly so, the axis is unknown.
        * For now the auxiliary routine which makes a unit vector ignores that.
        * It needs definitions like the following for floating point.
        #define SQRT sqrt
        #define RECIPROCAL(a) (1.0/(a))
    */
    VECTOR Normalize(VECTOR v)
    {
        VECTOR u;
        FPOINT norm = MUL(v.x,v.x)+MUL(v.y,v.y)+MUL(v.z,v.z);
        /* Better to test for (near-)zero norm before taking reciprocal. */
        FPOINT scl = RECIPROCAL(SQRT(norm));
        u.x = MUL(v.x,scl); u.y = MUL(v.y,scl); u.z = MUL(v.z,scl);
        return (u);
    }
    QUAT MatrixFromPointsAngle(POINT o, POINT p, ANGLE theta, MATRIX m)
    {
        QUAT q;
        VECTOR diff, axis;
        diff.x = p.x-o.x; diff.y = p.y-o.y; diff.z = p.z-o.z;
        axis = Normalize(diff);
        return (MatrixFromAnyAxisAngle(o,axis,theta,m));
    }
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookat vs hand made matrix
« Reply #53 on: December 08, 2006 »
You already have everything you need.  It's just a case of getting the rotations and translations in the right order.  It's just getting the two matrices right for the object and the camera.  One day you might need quaternions, but while you're stuck with the basic viewing parameters it's not going to help by adding them into the mix.
Again, I'm sorry I can't be more help - without being able to run anything OpenGL here I'm stuck.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookat vs hand made matrix
« Reply #54 on: December 08, 2006 »
np mate i thought it might be worth a go.
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: glulookat vs hand made matrix
« Reply #55 on: December 11, 2006 »
Hey guys. I haven't been around for a while. Depending on how much you're doing, it wont make much difference if you use GluLookAt() or if you write your own LookAt function.

Zap and I made an ogl Sokoban game a while back, and it uses some really strange methods, but it still runs smooth for most people. You can get it here, if you want to look at it.

http://rel.betterwebber.com/junk.php?id=82

Later... :)
The Dr. is INsane!!!