Author Topic: glulookat vs hand made matrix  (Read 20434 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 #20 on: November 25, 2006 »
hey guys just wondering if you could have a look and see where ive gone wrong here it rotates the piramid fine but not the box so im taking it theres a but in my code when im trying to rotate on all three axises ive looked a few times but cant see what ive done wrong.

big thanks in advance for any help.

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"

dim shared as double rtri , rquad

screen 18, 16, , 2

glViewport 0, 0, 640, 480                     
glMatrixMode GL_PROJECTION                   
glLoadIdentity                       
gluPerspective 45.0, 640.0/480.0, 0.1, 100.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   

dim shared as double pirmatrix(0 to 3,0 to 3)
dim shared as double boxmatrix(0 to 3,0 to 3)

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.141 / 180.0 )
        Pcos( x ) = cos( x * 3.141 / 180.0 )
next

declare sub draw_piramid( byval matrix as double ptr )
declare sub draw_box( byval matrix as double ptr )
declare sub loadentityidentity( matrix as double ptr )
declare sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )

loadentityidentity( @pirmatrix(0,0) )
loadentityidentity( @boxmatrix(0,0) )

positionentity( @pirmatrix(0,0) , -4.0 , 0 , -16.0 )
positionentity( @boxmatrix(0,0) , 1.0 , 0 , -5.0 )

do

    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
    glLoadIdentity

            rotateentity( @pirmatrix(0,0) , 0.0 , rtri , 0.0 )
            rotateentity( @boxmatrix(0,0) , rquad , rquad , rquad )

            draw_piramid( @pirmatrix(0,0) )
    draw_box( @boxmatrix(0,0) )
           
            rquad = rquad -0.15
            rtri  = rtri + 0.2
 
flip
loop while inkey$ = ""



sub loadentityidentity( matrix as double ptr )
    matrix[0] = 1
    matrix[5] = 1
    matrix[10] = 1
    matrix[15] = 1
end sub



sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
    matrix[12] = xpos
    matrix[13] = ypos
    matrix[14] = zpos
end sub



sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
    matrix[12] += xpos
    matrix[13] += ypos
    matrix[14] += zpos
end sub



sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
   
    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
            matrix[5] = Pcos( xpos )
            matrix[6] = -Psin( xpos )
            matrix[10] = Psin( xpos )
            matrix[11] = Pcos( xpos )
    endif

    'rotate on yaxis
    if ypos then
            matrix[0] = Pcos( ypos )
            matrix[2] = Psin( ypos )
            matrix[8] = -Psin( ypos )
            matrix[10] = Pcos( ypos )
    endif

    'rotate on zaxis
    if zpos then
            matrix[0] = Pcos( zpos )
            matrix[1] = -Psin( zpos )
            matrix[4] = Psin( zpos )
            matrix[5] = Pcos( zpos )
    endif

end sub



sub draw_piramid( byval matrix as double ptr )

            glpushmatrix
            glmultmatrixd( matrix )

    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( byval matrix as double ptr )

            glpushmatrix
            glmultmatrixd( matrix )             
   
    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 ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #21 on: November 25, 2006 »
phew after a lot of head sctratching( and reading various tuts heres what ive come up with ) does this look ok to you guys and if theres better ways of doing this stuff please share.


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"

screen 18, 16, , 2

glViewport 0, 0, 640, 480                     
glMatrixMode GL_PROJECTION                   
glLoadIdentity                       
gluPerspective 45.0, 640.0/480.0, 0.1, 100.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   

dim shared as double pirmatrix(0 to 3,0 to 3)
dim shared as double boxmatrix(0 to 3,0 to 3)

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

declare sub draw_piramid( byval matrix as double ptr )
declare sub draw_box( byval matrix as double ptr )
declare sub loadentityidentity( matrix as double ptr )
declare sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as double ptr , byval source as double ptr )
declare sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )

loadentityidentity( @pirmatrix(0,0) )
loadentityidentity( @boxmatrix(0,0) )

positionentity( @pirmatrix(0,0) , -4.0 , 0 , -16.0 )
positionentity( @boxmatrix(0,0) , 1.0 , 0 , -5.0 )

do

    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
    glLoadIdentity

    rotateentity( @pirmatrix(0,0) , 0.0 , 1 , 0.0 )
    rotateentity( @boxmatrix(0,0) , 1 , 0 , 0 )
    rotateentity( @boxmatrix(0,0) , 0 , 1 , 0 )
    rotateentity( @boxmatrix(0,0) , 0 , 0 , 1 )
   
    draw_piramid( @pirmatrix(0,0) )
    draw_box( @boxmatrix(0,0) )
           
flip
loop while inkey$ = ""



sub loadentityidentity( matrix as double ptr )
   
    dim as integer x
   
    for x = 0 to 15
         
          if x = 0 or x = 5 or x = 10 or x = 15 then
               matrix[x] = 1
          else
               matrix[x] = 0
          endif
         
    next
   
end sub



sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
    matrix[12] = xpos
    matrix[13] = ypos
    matrix[14] = zpos
end sub



sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
    matrix[12] += xpos
    matrix[13] += ypos
    matrix[14] += zpos
end sub



sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
   
    dim as double Tmatrix1(0 to 3,0 to 3) , Tmatrix2(0 to 3,0 to 3)
   
    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( @Tmatrix1(0,0) )
            Tmatrix1(1,1) = Pcos( xpos )
            Tmatrix1(2,1) = -Psin( xpos )
            Tmatrix1(1,2) = Psin( xpos )
            Tmatrix1(2,2) = Pcos( xpos )
            mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
            copymatrix4x4( matrix , @Tmatrix2(0,0) )
    endif

    'rotate on yaxis
    if ypos then
            loadentityidentity( @Tmatrix1(0,0) )
            Tmatrix1(0,0) = Pcos( ypos )
            Tmatrix1(2,0) = Psin( ypos )
            Tmatrix1(0,2) = -Psin( ypos )
            Tmatrix1(2,2) = Pcos( ypos )
            mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
            copymatrix4x4( matrix , @Tmatrix2(0,0) )
    endif

    'rotate on zaxis
    if zpos then
            loadentityidentity( @Tmatrix1(0,0) )
            Tmatrix1(0,0) = Pcos( zpos )
            Tmatrix1(1,0) = -Psin( zpos )
            Tmatrix1(0,1) = Psin( zpos )
            Tmatrix1(1,1) = Pcos( zpos )
            mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
            copymatrix4x4( matrix , @Tmatrix2(0,0) )
    endif

end sub



sub copymatrix4x4( destination as double ptr , byval source as double ptr )
   
    dim x as integer
   
    for x=0 to 15
        destination[x] = source[x]
    next
   
end sub



sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
   
    dim as integer a,x,y
    dim as double accum

    for y = 0 to 3
       
          for x = 0 to 3
             
                accum = 0
               
                for a = 0 to 3
                      accum += matrix1[y*4+a] * matrix2[a*4+x]
                next
               
                resultmatrix[y*4+x] = accum
               
          next
         
    next
   
end sub



sub draw_piramid( byval matrix as double ptr )

    glpushmatrix
    glmultmatrixd( matrix )

    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( byval matrix as double ptr )

    glpushmatrix
    glmultmatrixd( matrix )             
   
    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

<edited my loadidentity func as i forgot to flush all the other elements with 0>
« Last Edit: November 25, 2006 by ninogenio »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookas vs hand made matrix
« Reply #22 on: November 25, 2006 »
Apart from the fact you can build the rotation matrix all in one go, that's about as simple as it gets for a quick demo.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #23 on: November 25, 2006 »
yeah i was meaning to ask how would i go about building it in one as i tried and it almost worked but not quite  also if i wanted to spin my objects in a negative dir how would i go about that as i obviously cant push negative values through.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookas vs hand made matrix
« Reply #24 on: November 26, 2006 »
Right now you're clamping the tilt/turn/roll to between 0 and 360.  Don't do that.  If the value is -ve, keep adding 360 until it's positive.  If it's over 360, keep subtracting 360 until it's less than 360.  Alternatively, you can use 'mod'.  In C that's
Code: [Select]
a = fmod(a,360.0);
if (a<0) a=a+360.0;
not sure how that pans out in freebasic though.
Here's how I build my matrix in one go in C
Code: [Select]
    double cr   = cos(tilt);
    double sr   = sin(tilt);
    double ct   = cos(turn);
    double st   = sin(turn);
    double cp   = cos(roll);
    double sp   = sin(roll);
double stsr = st * sr;
double stcr = st * cr;

m->m00 = cp * ct;
m->m01 = sp * ct;
m->m02 = -st;

m->m10 = cp * stsr - sp * cr;
m->m11 = sp * stsr + cp * cr;
m->m12 = ct * sr;

m->m20 = stcr * cp + sp * sr;
m->m21 = stcr * sp - cp * sr;
m->m22 = ct * cr;
That builds a 3x3 matrix which applies tilt, then turn, then roll, in that order.  Sometimes that's not what you want - for flying spaceship controls you often want roll, then tilt, then turn.  Anyway, the above is just your 3 matrices, one multiplied by the other - which order you multiply them in gives you the different possibilities.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #25 on: November 26, 2006 »
ahh cheers for that jim!

very nice to know, now that its camera time will i just have a camera matrix thats the same as my object one then at after each render use glloadmatrixd(cameramatrix) then when drawing my objects use glmulmatrixd(objmatrix) and let gl multiply all my object matrixes with the camera one?

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookas vs hand made matrix
« Reply #26 on: November 26, 2006 »
Kind of.  There's a bit more work to do on the camera matrix.
Consider your your camerta has a M=3x3 matrix and an T=x,y,z translation, then the matrix for an object would be
Code: [Select]
m0 m1 m2 0
m3 m4 m5 0
m6 m7 m8 0
 x  y  z  1
For the camera, you need to transpose the matrix part...
Code: [Select]
m0 m3 m6 0
m1 m4 m7 0
m2 m5 m8 0
 x  y  z  1
...and rotate the translation by that new matrix.
Code: [Select]
(x y z 1) * new matrix = (Tx Ty Tz 1)
then you have
Code: [Select]
m0 m3 m6 0
m1 m4 m7 0
m2 m5 m8 0
 Tx  Ty  Tz  1
I usually break that up a bit, but that's the fundamental plan.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #27 on: November 26, 2006 »
so i load the transposed camera matrix and multiply the objects with that is that right?

sorry for all the questions this is the first time ive really delved into matrix maths.

but cheers for all the help.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookas vs hand made matrix
« Reply #28 on: November 26, 2006 »
Yes.  That's the idea.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #29 on: November 27, 2006 »
cool i got it going it was easyer than i thought obviously theres more to it than whats here but i got the basics going.

ill have to add a transpose func i didnt need one here as my mulmatrix fun transposes the matrix
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"

screen 18, 16, , 2

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 0.1, 100.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

dim shared as double pirmatrix(0 to 3,0 to 3)
dim shared as double boxmatrix(0 to 3,0 to 3)
dim shared as double cam(0 to 3,0 to 3)

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

declare sub draw_piramid( byval matrix as double ptr )
declare sub draw_box( byval matrix as double ptr )
declare sub loadentityidentity( matrix as double ptr )
declare sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as double ptr , byval source as double ptr )
declare sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )

loadentityidentity( @pirmatrix(0,0) )
loadentityidentity( @boxmatrix(0,0) )
loadentityidentity( @cam(0,0) )

positionentity( @pirmatrix(0,0) , -4.0 , 0 , -16.0 )
positionentity( @boxmatrix(0,0) , 1.0 , 0 , -5.0 )

do

glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
glLoadIdentity
rotateentity( @cam(0,0) , 0,0,1)
glloadmatrixd( @cam(0,0) )

rotateentity( @pirmatrix(0,0) , 0.0 , 1 , 0.0 )
rotateentity( @boxmatrix(0,0) , 1 , 0 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 1 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 0 , 1 )

draw_piramid( @pirmatrix(0,0) )
draw_box( @boxmatrix(0,0) )

flip
loop while inkey$ = ""



sub loadentityidentity( matrix as double ptr )

dim as integer x

for x = 0 to 15

if x = 0 or x = 5 or x = 10 or x = 15 then
matrix[x] = 1
else
matrix[x] = 0
endif

next

end sub



sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] = xpos
matrix[13] = ypos
matrix[14] = zpos
end sub



sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] += xpos
matrix[13] += ypos
matrix[14] += zpos
end sub



sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )

dim as double Tmatrix1(0 to 3,0 to 3) , Tmatrix2(0 to 3,0 to 3)

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( @Tmatrix1(0,0) )
Tmatrix1(1,1) = Pcos( xpos )
Tmatrix1(2,1) = -Psin( xpos )
Tmatrix1(1,2) = Psin( xpos )
Tmatrix1(2,2) = Pcos( xpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on yaxis
if ypos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( ypos )
Tmatrix1(2,0) = Psin( ypos )
Tmatrix1(0,2) = -Psin( ypos )
Tmatrix1(2,2) = Pcos( ypos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on zaxis
if zpos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( zpos )
Tmatrix1(1,0) = -Psin( zpos )
Tmatrix1(0,1) = Psin( zpos )
Tmatrix1(1,1) = Pcos( zpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

end sub



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

dim x as integer

for x=0 to 15
destination[x] = source[x]
next

end sub



sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )

dim as integer a,x,y
dim as double accum

for y = 0 to 3

for x = 0 to 3

accum = 0

for a = 0 to 3
accum += matrix1[y*4+a] * matrix2[a*4+x]
next

resultmatrix[y*4+x] = accum

next

next

end sub



sub draw_piramid( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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: November 27, 2006 by ninogenio »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookas vs hand made matrix
« Reply #30 on: November 27, 2006 »
I can't see any of the new stuff in there - is this the right version?

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #31 on: November 27, 2006 »
oops i forgot about transposing :) anywho im trying this func to transpose but it doesnt seem to work the camera jitters back and forth so im guessing ive done it drastically wrong.

Code: [Select]
sub transposematrix( matrix as double ptr )
   
    dim as double tmp_matrix( 0 to 3 , 0 to 3 )
    dim as integer x,y
   
    copymatrix4x4( @tmp_matrix(0,0) , matrix )
 
    for y = 0 to 2
          for x = 0 to 2
                tmp_matrix(x,y) = matrix[y*4+x]
          next
    next
   
    tmp_matrix(0,3) *= matrix[12]
    tmp_matrix(1,3) *= matrix[13]
    tmp_matrix(2,3) *= matrix[14]
    copymatrix4x4( matrix , @tmp_matrix(0,0) )
   
end sub
« Last Edit: November 27, 2006 by ninogenio »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: glulookas vs hand made matrix
« Reply #32 on: November 27, 2006 »
Code: [Select]
(x y z 1) * new matrix = (Tx Ty Tz 1)The last bit should be a matrix multiply, not just multiplying the new vector by the old one. 

Are you re-constructing the camera matrix each frame?

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #33 on: November 28, 2006 »
heres where its at now.

now that ive added the multiply the objects shoot away into the distance.

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"

screen 18, 16, , 2

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 0.1, 100.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

dim shared as double pirmatrix(0 to 3,0 to 3)
dim shared as double boxmatrix(0 to 3,0 to 3)
dim shared as double cam(0 to 3,0 to 3)

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

declare sub draw_piramid( byval matrix as double ptr )
declare sub draw_box( byval matrix as double ptr )
declare sub loadentityidentity( matrix as double ptr )
declare sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as double ptr , byval source as double ptr )
declare sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
declare sub transposematrix( matrix as double ptr )

loadentityidentity( @pirmatrix(0,0) )
loadentityidentity( @boxmatrix(0,0) )
loadentityidentity( @cam(0,0) )

positionentity( @pirmatrix(0,0) , -4.0 , 0 , -16.0 )
positionentity( @boxmatrix(0,0) , 1.0 , 0 , -5.0 )

do

glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
glLoadIdentity


rotateentity( @cam(0,0) , 0,0,1)
transposematrix( @cam(0,0) )
glloadmatrixd( @cam(0,0) )

rotateentity( @pirmatrix(0,0) , 0.0 , 1 , 0.0 )
rotateentity( @boxmatrix(0,0) , 1 , 0 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 1 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 0 , 1 )

draw_piramid( @pirmatrix(0,0) )
draw_box( @boxmatrix(0,0) )

flip
loop while inkey$ = ""



sub loadentityidentity( matrix as double ptr )

dim as integer x

for x = 0 to 15

if x = 0 or x = 5 or x = 10 or x = 15 then
matrix[x] = 1
else
matrix[x] = 0
endif

next

end sub



sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] = xpos
matrix[13] = ypos
matrix[14] = zpos
end sub



sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] += xpos
matrix[13] += ypos
matrix[14] += zpos
end sub



sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )

dim as double Tmatrix1(0 to 3,0 to 3) , Tmatrix2(0 to 3,0 to 3)

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( @Tmatrix1(0,0) )
Tmatrix1(1,1) = Pcos( xpos )
Tmatrix1(2,1) = -Psin( xpos )
Tmatrix1(1,2) = Psin( xpos )
Tmatrix1(2,2) = Pcos( xpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on yaxis
if ypos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( ypos )
Tmatrix1(2,0) = Psin( ypos )
Tmatrix1(0,2) = -Psin( ypos )
Tmatrix1(2,2) = Pcos( ypos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on zaxis
if zpos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( zpos )
Tmatrix1(1,0) = -Psin( zpos )
Tmatrix1(0,1) = Psin( zpos )
Tmatrix1(1,1) = Pcos( zpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

end sub



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

dim x as integer

for x=0 to 15
destination[x] = source[x]
next

end sub



sub transposematrix( matrix as double ptr )
   
    dim as double tmp_matrix( 0 to 3 , 0 to 3 ) , tmp_matrix2( 0 to 3 , 0 to 3 )
    dim as integer x,y
   
    copymatrix4x4( @tmp_matrix(0,0) , matrix )

    for y = 0 to 3
          for x = 0 to 3
                tmp_matrix(x,y) = matrix[y*4+x]
          next
    next
   
    mulmatrix4x4(  matrix , @tmp_matrix(0,0) , @tmp_matrix2(0,0) )
    copymatrix4x4( matrix , @tmp_matrix2(0,0) )
   
end sub



sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )

dim as integer a,x,y
dim as double accum

for y = 0 to 3

for x = 0 to 3

accum = 0

for a = 0 to 3
accum += matrix1[y*4+a] * matrix2[a*4+x]
next

resultmatrix[y*4+x] = accum

next

next

end sub



sub draw_piramid( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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: glulookas vs hand made matrix
« Reply #34 on: November 28, 2006 »
You haven't really done what I said.
To build the camera matrix *each frame*
1) build the matrix as you would for an object
2) flip the top 3x3 of the matrix (transpose it - effectively this is an inverse matrix.  Think about what happens when you look at the real world through a camera, when you rotate the camera to the left, the world in the viewfinder rotates right).
3) take the bottom row of the 4x4 matrix you built in step 1, this is the translation,  (x y z 1), and multiply it by the 3x3 part of your new matrix that you build in step 2. ie,
Tx = x * m0 + y * m1 + z * m2
Ty = x * m4 + y * m5 + z * m6
Tz = x * m8 + y * m9 + z * m10
Put that in the bottom row of the new matrix.

You have 2 problems right now.
1) You're not rebuilding the camera matrix each frame - you're building it once at the start.  Every time you transpose it you go back and forward between inverse and normal rotation - that's where you were yesterday.
2) You're not rotating the translation part of the matrix correctly.
When you get that translation wrong, it'll just zoom off.

Slow down and concentrate on what you're doing rather than just hacking it round until it works :)

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #35 on: November 28, 2006 »
yeah sorry jim i do tend to get carried away  ;D

heres the transposed version not sure if im multiplying the translation part quite right but it looks ok on screen.

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"

screen 18, 16, , 2

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 0.1, 100.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

dim shared as double pirmatrix(0 to 3,0 to 3)
dim shared as double boxmatrix(0 to 3,0 to 3)
dim shared as double cam(0 to 3,0 to 3)

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

declare sub draw_piramid( byval matrix as double ptr )
declare sub draw_box( byval matrix as double ptr )
declare sub loadentityidentity( matrix as double ptr )
declare sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as double ptr , byval source as double ptr )
declare sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
declare sub transposematrix( matrix as double ptr )
declare sub multranslmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )


loadentityidentity( @pirmatrix(0,0) )
loadentityidentity( @boxmatrix(0,0) )

positionentity( @pirmatrix(0,0) , -4.0 , 0 , -16.0 )
positionentity( @boxmatrix(0,0) , 1.0 , 0 , -5.0 )

dim shared cam_z as double = 0

do

glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
glLoadIdentity

loadentityidentity( @cam(0,0) )
rotateentity( @cam(0,0) , 0,0,cam_z)
transposematrix( @cam(0,0) )
glloadmatrixd( @cam(0,0) )

rotateentity( @pirmatrix(0,0) , 0.0 , 1 , 0.0 )
rotateentity( @boxmatrix(0,0) , 1 , 0 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 1 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 0 , 1 )

draw_piramid( @pirmatrix(0,0) )
draw_box( @boxmatrix(0,0) )

flip

cam_z += 1
if cam_z>360 then cam_z -= 360

loop while inkey$ = ""



sub loadentityidentity( matrix as double ptr )

dim as integer x

for x = 0 to 15

if x = 0 or x = 5 or x = 10 or x = 15 then
matrix[x] = 1
else
matrix[x] = 0
endif

next

end sub



sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] = xpos
matrix[13] = ypos
matrix[14] = zpos
end sub



sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] += xpos
matrix[13] += ypos
matrix[14] += zpos
end sub



sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )

dim as double Tmatrix1(0 to 3,0 to 3) , Tmatrix2(0 to 3,0 to 3)

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( @Tmatrix1(0,0) )
Tmatrix1(1,1) = Pcos( xpos )
Tmatrix1(2,1) = -Psin( xpos )
Tmatrix1(1,2) = Psin( xpos )
Tmatrix1(2,2) = Pcos( xpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on yaxis
if ypos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( ypos )
Tmatrix1(2,0) = Psin( ypos )
Tmatrix1(0,2) = -Psin( ypos )
Tmatrix1(2,2) = Pcos( ypos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on zaxis
if zpos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( zpos )
Tmatrix1(1,0) = -Psin( zpos )
Tmatrix1(0,1) = Psin( zpos )
Tmatrix1(1,1) = Pcos( zpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

end sub



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

dim x as integer

for x=0 to 15
destination[x] = source[x]
next

end sub



sub transposematrix( matrix as double ptr )
   
    dim as double tmp_matrix( 0 to 3 , 0 to 3 ) , tmp_matrix2( 0 to 3 , 0 to 3 )
    dim as integer x,y
   
    copymatrix4x4( @tmp_matrix(0,0) , matrix )

    for y = 0 to 2
          for x = 0 to 2
                tmp_matrix(x,y) = matrix[y*4+x]
          next
    next
   
    multranslmatrix4x4(  matrix , @tmp_matrix(0,0) , @tmp_matrix2(0,0) )
    copymatrix4x4( matrix , @tmp_matrix2(0,0) )
   
end sub



sub multranslmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
   
    dim as double Tx , Ty , Tz
   
    Tx = matrix1[12] * matrix2[0] + matrix1[13] * matrix2[1] + matrix1[14] * matrix2[2]
    Ty = matrix1[12] * matrix2[4] + matrix1[13] * matrix2[5] + matrix1[14] * matrix2[6]
    Tz = matrix1[12] * matrix2[8] + matrix1[13] * matrix2[9] + matrix1[14] * matrix2[10]

    copymatrix4x4( resultmatrix , matrix2 )
   
    resultmatrix[12] = Tx
    resultmatrix[13] = Ty
    resultmatrix[14] = Tz

end sub



sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )

dim as integer a,x,y
dim as double accum

for y = 0 to 3

for x = 0 to 3

accum = 0

for a = 0 to 3
accum += matrix1[y*4+a] * matrix2[a*4+x]
next

resultmatrix[y*4+x] = accum

next

next

end sub



sub draw_piramid( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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: glulookas vs hand made matrix
« Reply #36 on: November 29, 2006 »
Sounds promising then :D

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #37 on: November 29, 2006 »
 :) i found a bit of a problem with the above code last night when i pan the camera out and spin the camera on the y axis the camera does not spin on its origin i mean it sort of sweeps round the objects it looks a bit weird so im wondering have i messed up the translations
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: glulookas vs hand made matrix
« Reply #38 on: December 01, 2006 »
never mind i had the translation round the wrong way this works really well cheers jim :buddies:

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"

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

dim shared as double pirmatrix(0 to 3,0 to 3)
dim shared as double boxmatrix(0 to 3,0 to 3)
dim shared as double cam(0 to 3,0 to 3)

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

declare sub draw_piramid( byval matrix as double ptr )
declare sub draw_box( byval matrix as double ptr )
declare sub loadentityidentity( matrix as double ptr )
declare sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
declare sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )
declare sub copymatrix4x4( destination as double ptr , byval source as double ptr )
declare sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
declare sub transposematrix( matrix as double ptr )
declare sub multranslmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
declare sub Grid( byval overall_size as integer , byval spacings as integer )


loadentityidentity( @pirmatrix(0,0) )
loadentityidentity( @boxmatrix(0,0) )

positionentity( @pirmatrix(0,0) , 1.0 , 1 , -16.0 )
positionentity( @boxmatrix(0,0) , 0 , 0 , 0 )

dim shared cam_z as double = 0

do

glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
glLoadIdentity

loadentityidentity( @cam(0,0) )
moveentity( @cam(0,0) , 0 , -5 , -25 )
rotateentity( @cam(0,0) , cam_z , 0 , 0 )
transposematrix( @cam(0,0) )
glloadmatrixd( @cam(0,0) )

rotateentity( @pirmatrix(0,0) , 0.0 , 1 , 0.0 )
rotateentity( @boxmatrix(0,0) , 1 , 0 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 1 , 0 )
rotateentity( @boxmatrix(0,0) , 0 , 0 , 1 )

draw_piramid( @pirmatrix(0,0) )
draw_box( @boxmatrix(0,0) )
Grid( 500 , 10 )

flip

cam_z += 1
if cam_z>360 then cam_z -= 360

loop while inkey$ = ""



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( matrix as double ptr )

dim as integer x

for x = 0 to 15

if x = 0 or x = 5 or x = 10 or x = 15 then
matrix[x] = 1
else
matrix[x] = 0
endif

next

end sub



sub positionentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
matrix[12] = xpos
matrix[13] = ypos
matrix[14] = zpos
end sub



sub moveentity( matrix as double ptr , byval xpos as double , byval ypos as double , byval zpos as double )
       
      dim as integer j,k
      dim as double Tmatrix1(0 to 3, 0 to 3) ,  Tmatrix2(0 to 3, 0 to 3)

      loadentityidentity( @Tmatrix1(0,0) )
      Tmatrix1(3,0) = xpos
      Tmatrix1(3,1) = ypos
      Tmatrix1(3,2) = zpos

      mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )

      copymatrix4x4( matrix , @Tmatrix2(0,0) )

end sub



sub rotateentity( matrix as double ptr , xpos as double , ypos as double , zpos as double )

dim as double Tmatrix1(0 to 3,0 to 3) , Tmatrix2(0 to 3,0 to 3)

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( @Tmatrix1(0,0) )
Tmatrix1(1,1) = Pcos( xpos )
Tmatrix1(2,1) = -Psin( xpos )
Tmatrix1(1,2) = Psin( xpos )
Tmatrix1(2,2) = Pcos( xpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on yaxis
if ypos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( ypos )
Tmatrix1(2,0) = Psin( ypos )
Tmatrix1(0,2) = -Psin( ypos )
Tmatrix1(2,2) = Pcos( ypos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

'rotate on zaxis
if zpos then
loadentityidentity( @Tmatrix1(0,0) )
Tmatrix1(0,0) = Pcos( zpos )
Tmatrix1(1,0) = -Psin( zpos )
Tmatrix1(0,1) = Psin( zpos )
Tmatrix1(1,1) = Pcos( zpos )
mulmatrix4x4( @Tmatrix1(0,0) , matrix , @Tmatrix2(0,0) )
copymatrix4x4( matrix , @Tmatrix2(0,0) )
endif

end sub



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

dim x as integer

for x=0 to 15
destination[x] = source[x]
next

end sub



sub transposematrix( matrix as double ptr )
   
    dim as double tmp_matrix( 0 to 3 , 0 to 3 ) , tmp_matrix2( 0 to 3 , 0 to 3 )
    dim as integer x,y
   
    copymatrix4x4( @tmp_matrix(0,0) , matrix )

    for y = 0 to 2
          for x = 0 to 2
                tmp_matrix(x,y) = matrix[y*4+x]
          next
    next
   
    multranslmatrix4x4(  matrix , @tmp_matrix(0,0) , @tmp_matrix2(0,0) )
    copymatrix4x4( matrix , @tmp_matrix2(0,0) )
   
end sub



sub multranslmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )
   
    dim as double Tx , Ty , Tz

    Tx = matrix2[0] * matrix1[12] + matrix1[1] * matrix2[13] + matrix1[2] * matrix2[14]
    Ty = matrix1[4] * matrix2[12] + matrix1[5] * matrix2[13] + matrix1[6] * matrix2[14]
    Tz = matrix1[8] * matrix2[12] + matrix1[9] * matrix2[13] + matrix1[10] * matrix2[14]

    copymatrix4x4( resultmatrix , matrix2 )
   
    resultmatrix[12] = Tx
    resultmatrix[13] = Ty
    resultmatrix[14] = Tz


end sub



sub mulmatrix4x4( byval matrix1 as double ptr , byval matrix2 as double ptr , resultmatrix as double ptr )

dim as integer a,x,y
dim as double accum

for y = 0 to 3

for x = 0 to 3

accum = 0

for a = 0 to 3
accum += matrix1[y*4+a] * matrix2[a*4+x]
next

resultmatrix[y*4+x] = accum

next

next

end sub



sub draw_piramid( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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( byval matrix as double ptr )

glpushmatrix
glmultmatrixd( matrix )

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: glulookas vs hand made matrix
« Reply #39 on: December 01, 2006 »
Code: [Select]
    Tx = matrix2[0] * matrix1[12] + matrix1[1] * matrix2[13] + matrix1[2] * matrix2[14]
    Ty = matrix1[4] * matrix2[12] + matrix1[5] * matrix2[13] + matrix1[6] * matrix2[14]
    Tz = matrix1[8] * matrix2[12] + matrix1[9] * matrix2[13] + matrix1[10] * matrix2[14]

This still looks suspicious, I think Tx is wrong.  I think the fist mul, matrix2 should be matrix1, and matrix1 should be matrix2...

Jim
Challenge Trophies Won: