Page 1 of 1

Need help on OpenGL projection stuff

Posted: Mon Mar 27, 2006 2:40 pm
by Diedel
Given:

- viewer position vp (vector)
- viewer orientation vo (matrix)
- model position mp (vector)
- model orientation mo (matrix)

How do I have to manipulate the modelview matrix so that the model is properly rendered?

I had thought

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glMultMatrixf (vo);
glTranslatef (vp);
glMultMatrixf (mo);
glTranslatef (mp);

would do the trick; but neither this nor any permutation does. I have read tutorials and the redbook about this, but to no avail. :(

I had also tried

glMultMatrixf (mo); //rotate the model first
glTranslatef (vo - mo); //move to offset distance from viewer pos; tried vo + mo too
glMultMatrixf (mo); //rotate the entire scene depending on viewer orientation

Posted: Mon Mar 27, 2006 9:18 pm
by SuperSheep
Here's what I did to setup the view of a model in OOF Editor...

Code: Select all

 Call glMatrixMode(mmModelView)
 Call glLoadIdentity
 Call glTranslatef(0!, 0!, -1!)
 ' Move to starting position, rotation to draw model
 Call glTranslatef(rXaxisPan, rYaxisPan, rZoom)
 Call glRotatef(rYaxis, 1!, 0!, 0!)
 Call glRotatef(rXaxis, 0!, 1!, 0!)
This didn't use matrix's however in setting up the viewpoint so I'm not sure how applicable you'll find this, but at least you can see that...

1. Selected the modelview matrix
2. Loaded the identity matrix into it
3. Translated out from that position -1 in the Z axis
4. Translated out the X,Y and zoom level
5. Rotated about Y axis, then X axis

From what I understand, OpenGL uses matrix's behind the scenes when Translate and Rotate functions are used. Perhaps you can attempt doing discreet matrix multiplications in this order and at least have a starting point.

Wish I could be more help on the matrix stuff but it's been many years since I have worked with them.

Posted: Mon Mar 27, 2006 9:22 pm
by SuperSheep
Almost forgot, great site that you probably already know about, for tutorials on OpenGL...

http://nehe.gamedev.net/

and...

http://www.flipcode.com/documents/matrfaq.html

Posted: Tue Mar 28, 2006 3:24 am
by Diedel
Thx. I figured a few things, but still cannot render an object in the proper location.

The Nehe tuts plain suck, imho. Lots and lots of text (blah blah blah), lots of code around what you really want to know, and sucky explanations. The OpenGL Redbook is far better.

One problem I had to figure was that the matrixes in D2 had a different ordering from the OpenGL ones (apart from OpenGL being column major matrixes).

I am still having issues with lateral distortions when banking, and control direction reversing when being upside down, all related to matrix handling by D2. Ack.

Actually the first problem I wanted to solve was to render the Pyro in-game from external view, i.e. I am having Pyro position and orientation, and viewer position and orientation (which happens to be the Pyro pos/orient from a few frames before).

So what I need to do is to rotate the Pyro according to its orientation, move it out (glTranslate) from the view point (delta of Pyro and viewer) pos, and rotate the entire thing according to viewer orientation. But I always end up with the Pyro being far out.

In case you have too much time, I could tell you where to look in the D2X-XL code for that, and how to enable it (simple command line switch and a few code lines in main/oof.c::OOF_RenderObject() :mrgreen: ).

Posted: Tue Mar 28, 2006 1:06 pm
by Paul
It probably depends how your orientation matrices are defined...

In Event Horizon, I use position as <x, y, z>, and then orientation in terms of angles around the axes. Here's the code I use:

glLoadIdentity();
glRotatef( -sys->pitch, 1, 0, 0 );
glRotatef( -sys->roll, 0, 0, 1 );
glRotatef( -sys->angle, 0, 1, 0 ); // sys angle
glTranslatef( x - sys->x, y - sys->y, sys->z - z ); // position self
glRotatef( angle, 0, 1, 0 ); // rotate to own angle
glRotatef( roll, 0, 0, 1 );
glRotatef( pitch, 1, 0, 0 );

where \"sys\" is the player. But my axes are also defined kind of funny (left-handed), as you can see I have x-sys->x, y-sys->y, but then sys->z-z.

Posted: Wed Mar 29, 2006 7:20 am
by Diedel
Paul, I want to utilize the matrices already present in D2, or I would need to rewrite a real lot of stuff.

Currently I am simply trying to render the mine (not any objects) using the driver transformations:

float fZoom = ((float) viewInfo.zoom) / 65536.0f;
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
glScalef (1.0f, 1.0f, -fZoom); //scale the coordinates
glMultMatrixf (viewInfo.glViewf); //rotate view
glTranslatef (viewInfo.glPosf [0], viewInfo.glPosf [1], -viewInfo.glPosf [2]); //offset view point

//viewInfo.glViewf is the orientation matrix
//viewInfo.glPosf is the position vector
//fZoom holds the zoom factor scaled from D2's coordinate system (everything * 65536) to an OpenGL conformant one

I am pretty close, but strangely enough my call to glTranslate offsets the view position a little too much. Weird.

Edit:

I have it: I have to put all position offsets negative, not just z:

glTranslatef (-viewInfo.glPosf [0], -viewInfo.glPosf [1], -viewInfo.glPosf [2]);