• Welcome to Dark Bit Factory & Gravity.
 

glulookat vs hand made matrix

Started by ninogenio, November 18, 2006

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

ninogenio

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.
Challenge Trophies Won:

Jim

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
Challenge Trophies Won:

ninogenio

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.
Challenge Trophies Won:

taj

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.
Challenge Trophies Won:

Stonemonkey

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.

taj

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.
Challenge Trophies Won:

Stonemonkey

#6
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?


Jim

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
Challenge Trophies Won:

taj

Quote from: Stonemonkey on November 19, 2006
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!
Challenge Trophies Won:

ninogenio

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.
Challenge Trophies Won:

ninogenio

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
Challenge Trophies Won:

Jim

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
Challenge Trophies Won:

ninogenio

 :) 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).
Challenge Trophies Won:

Stonemonkey

dim shared as double should be fine i think but it should be (0 to 3,0 to 3) for a 4*4 matrix.

Jim

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
Challenge Trophies Won:

ninogenio

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:
Challenge Trophies Won:

taj

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?
Challenge Trophies Won:

ninogenio

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)
Challenge Trophies Won:

Jim

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
Challenge Trophies Won:

ninogenio

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
Challenge Trophies Won: