Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: ninogenio on November 18, 2006
-
just wondering wether creating a camera matrix like we would do for a 2d engine is better than the glulookat function and also is it a good idea to use object matrixes for rotating and positioning an object rather than opengl hardwired translatef and glrotatef functions.
-
If you can build the matrices yourself, then you don't need gluLookAt.
If you can use OpenGL to make the object matrices for other stuff, then it might be quicker. You really don't want to be doing any geometry on the vertices or objects yourself at all.
Jim
-
whati was thinking off is keeping my object rotations in a 4x4 matrix and then letting opengl do the transformaions with glMultMatrixf() and before letting gl do the transforms mutiplying all the object matrixes with the camera one does that sound like it might work? and how much slower might it be.
-
It'll work of course.
glLoadIdentity();
glLoadMatrix();
In whichever matrix mode you are in (eg PROJECTION or MODELVIEW) is what you need. Speed wise its hard to say. Depends on how much CPU you are using already I suppose. In terms of drawing performance, I doubt there is much in it. OGL drawing calls are very efficient.
Lastly the *huge* advantage of doing it this way is if you ever need the inverse of the current transform for some reason, its a hell of a lot easier if you do it the way you suggest than gl transform ways. Also can be used for collision detection (eg if you know all the transfoms done on an object, its easier to detect 3d collisions then relying on ogl blindly). On the other hand, coding wise, its a hell of lot harder to get it all right and efficient.
-
No idea speedwise but atm I use 3*3 matrices, multiply with the camera matrix and load them. Works ok so far but probably not the best way to go about it.
-
As far as I know thats dangerous stonemonkey. OGL can really screw up as pipeline stages expect seperate view transform and model transforms so if you are applying the matrices to your camera matrix and loading that as a single matrix (on either stack) some things could screw up. I cant remember why though :-(...
I'm almost certain the best way to use your own matrices is:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gllLoadMatrix(mycameraMatrix);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gllLoadMatrix(mymodelMatrix);
This is definitely safe.
-
Well, as far as it know's i'm just sending it a model matrix and leaving the cameramatrix alone and as i'm doing my own lighting atm there's no problem there either. (as far as i know)
btw, shouldn't that be GL_MODELVIEW?
-
You don't need glLoadIdentity before glLoadMatrix. I always combine the camera/object matrices by hand and set that in modelview. That leaves the projection matrix constant. You're always going to be changing one or other of those matrices for each entity.
Jim
-
Well, as far as it know's i'm just sending it a model matrix and leaving the cameramatrix alone and as i'm doing my own lighting atm there's no problem there either. (as far as i know)
btw, shouldn't that be GL_MODELVIEW?
Yes mistake, and Jims right, the Identity stuff isnt neede befoe a loadmatrix call ... heh , and me a size coder!
-
cool! cheers guys to think i was on the right track :)
im going to bash an example up just to make shure it looks ok ill post my findings.
-
right i started tinkering with my own matrices i created an object matrice and am sending it into the modelveiw with the glloadmatrixd command in freebasic it looks like this.
glloadmatrixd(@matrix(0,0))
my matrix the way it is now is just for object position.
the program compiles fine but the object isnt visable its like theres no translations done.so what im thinking is mabey freebasic gl doesnt like a 2d array for the matrix and expects a 16 element 1d array either that or mabey it expects the matrix to start from 1,1?
i was just wondering if anyone has experimented and could shed some light.
i got my matrix stuff from here.
http://nehe.gamedev.net/data/articles/article.asp?article=02
-
Three possibilities - in OpenGL z is -ve into the screen, when you would normally have z -ve behind the screen, so try negging the z of your translation. Second, your matrix might be round the wrong way. Try flipping the values in the array about the top-left to bottom-right axis.
eg.
0 1 2
3 4 5
6 7 8
would become
0 3 6
1 4 7
2 5 8
Third, you might not have put 1's on the diagonal of the matrix. If you have
0 0 0 0
0 0 0 0
0 0 0 0
x y z 0
you need to make that
1 0 0 0
0 1 0 0
0 0 1 0
x y z 1
Try any combination of those :D
Jim
-
:) cheers jim.
the way i have matrix is like so.
0 1 2 3
5 6 7 8
10 11 12
13 14 15
1 0 0 0
0 1 0 0
0 0 1 0
x y z 1
and my z is - ill have a wee play though.
will gl be fine with my matrix being dim shared as double matrix(0 to 4,0 to 4).
-
dim shared as double should be fine i think but it should be (0 to 3,0 to 3) for a 4*4 matrix.
-
Definitely, you need in an array which is 4x4 doubles, or just 16 doubles.
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Jim
-
right i never thought but the 2d matrix array gets pushed through as a pointer which makes it 1d which inturn would have been nocking every thing out by 1 phew i need a lie down :) ill get it going now cheers guys :cheers:
-
One more possiblity , how are you setting your PROJECTION matrix? If you arent, your object may be outside the default view volume of opengl which is -1..1 in all directions. In other words, your object has to fit iinto the unit cube to be visible (ie all the co-ordinates after you apply your model matrix must be -1.0..1.0). Maybe its too big?
-
hmm i just do this on the projection matrix my object is inside the 1 -1 range but i wouldnt like to think i would be restricted to this range so how would i go about setting the projection matrix to allow vert cords bigger than 1 -1
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective(45,800/600,1,100)
-
That looks fine to me. You then just call
glViewport(0,0,screenwidth,screenheight);and that makes the -1:1 projected coordinates map on to the whole screen.
Jim
-
cheers jim i got it going this afternoon this is just a nehe example modifyed to work with hand made matrixes when i get some time ill put object rotations and scaling in the matrixes and add a camera also ill have to clean the code up a bit
''
'' 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 single 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)
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 )
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
draw_piramid( @pirmatrix(0,0) )
draw_box( @boxmatrix(0,0) )
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 draw_piramid( byval matrix as double ptr )
rtri = rtri + 0.2
glpushmatrix
glmultmatrixd( matrix )
glRotatef rtri, 0.0, 1.0, 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( byval matrix as double ptr )
rquad = rquad -0.15
glpushmatrix
glmultmatrixd( matrix )
glRotatef rquad,1.0, 1.0, 1.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
-
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.
''
'' 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
-
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.
''
'' 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>
-
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
-
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.
-
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
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
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
-
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?
-
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
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...
m0 m3 m6 0
m1 m4 m7 0
m2 m5 m8 0
x y z 1
...and rotate the translation by that new matrix.
(x y z 1) * new matrix = (Tx Ty Tz 1)
then you have
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
-
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.
-
Yes. That's the idea.
Jim
-
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
''
'' 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
-
I can't see any of the new stuff in there - is this the right version?
Jim
-
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.
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
-
(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
-
heres where its at now.
now that ive added the multiply the objects shoot away into the distance.
''
'' 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
-
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
-
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.
''
'' 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
-
Sounds promising then :D
Jim
-
:) 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
-
never mind i had the translation round the wrong way this works really well cheers jim :buddies:
''
'' 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
-
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
-
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.
''
'' 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
-
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
-
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.
-
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
-
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.
-
Sorry, should have been
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
-
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.
-
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
-
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.
''
'' 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
-
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.
-
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.
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))
-
cool cheers stonemonkey im currently still trying without any luck but ill keep going.
-
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.
/* 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));
}
-
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
-
np mate i thought it might be worth a go.
-
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... :)