Jump to content
  • Advertisement
Sign in to follow this  
Specchum

OpenGL Doing without gluLookAt?

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

We are using OpenGL ES for a mobile project. The problem is that there is no gluLookAt function since GLUT is not available in our case. Now, I'm trying to develop a FPS camera and is the routine have calculated a forward, right and up vector to maintain my camera orientation. So far, so good. However, how do I build my camera matrix with these three vectors without gluLookAt? I read something about using the inverse camera transforms or something like that, but frankly, it all went right over my head! Anyone care to tell me how to go about this?

Share this post


Link to post
Share on other sites
Advertisement
Don't know about OpenGL ES, but I'll try to help.

One idea would be to calculate your own modelview matrix and place it on stack instead of calling any view routine. Apply necessary transformation to your matrix.

But as you already have vectors calculated, you can use glTranslate and glRotate instead of gluLookAt.

Tell me what exactly vectors you have (what each of them represent) and I'll try to help you with the transformations.

Share this post


Link to post
Share on other sites
gluLookAt is not a part of GLUT, but GLU. Don't know if you have GLU though, but missing GLUT does not mean missing gluLookAt.

Anyway, if you still want to implement your own, you can download the source for Mesa, which includes, if I'm not mistaken, two complete implementations of the GLU library. Have a look there how they do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Specchum
... The problem is that there is no gluLookAt function since GLUT is not available in our case. ...


gluLookAt belongs to GLU not GLUT.

You must understand geometry transformations and matrix manipulation before doing you LookAt version.

Share this post


Link to post
Share on other sites
if you want glulookat without glu look into opengl sample implementation from SGI, but if you want here is my pascal equivalent of their code, sorry about weird syntax highlightning but gamedev forums have no pascal highlighter it seems:

[source=pascal]
// this is glulookat equivalent translated from ogl-sample.
// matrix should be single but i use single precision

procedure normalize(var v: array of single);
var
r: single;
begin
r:= sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);

if (r = 0) then exit;

v[0]:= v[0] / r;
v[1]:= v[1] / r;
v[2]:= v[2] / r;
end;

procedure crossp(var v1, v2, result: array of single);
begin
result[0]:= v1[1]*v2[2] - v1[2]*v2[1];
result[1]:= v1[2]*v2[0] - v1[0]*v2[2];
result[2]:= v1[0]*v2[1] - v1[1]*v2[0];
end;

procedure LookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz: single);
var
i: integer;
forw, side, up: array[0..2] of single;
m: TMatrix4f;
begin
forw[0]:= centerx - eyex;
forw[1]:= centery - eyey;
forw[2]:= centerz - eyez;

up[0]:= upx;
up[1]:= upy;
up[2]:= upz;

normalize(forw);

(* Side:= forw x up *)
crossp(forw, up, side);
normalize(side);

(* Recompute up as: up:= side x forw *)
crossp(side, forw, up);

m:= IdentityHmgMatrix;
m[0][0]:= side[0];
m[0][1]:= side[1];
m[0][2]:= side[2];

m[1][0]:= up[0];
m[1][1]:= up[1];
m[1][2]:= up[2];

m[2][0]:= -forw[0];
m[2][1]:= -forw[1];
m[2][2]:= -forw[2];

glMultMatrixf(@m);
glTranslated(-eyex, -eyey, -eyez);
end;

Share this post


Link to post
Share on other sites
Quote:
Original post by Specchum
Now, I'm trying to develop a FPS camera and is the routine have calculated a forward, right and up vector to maintain my camera orientation.


If you already got that you wouldn't want to have gluLookAt throw away half your results and rebuild the matrix, anyway.

Your camera matrix is already there: right/up/forward/position.

Now you want to transform everything the other way, ie. you want to load the inverse of this matrix as modelview before rendering your scene. That part was basically posted, but to make it easier and more OpenGL matrix like you need to know that your typical transformation matrix (assuming normalized right/up/fwd vectors) is very user friendly. Transpose the 3x3 rotational part and transform the old position by the new rotation.

So, with Transform being your camera matrix in the usual ogl way (0-4:right, 5-8:up, etc. actually, I like having extra pointers called right,up,fwd into the array for clarities sake).


float viewmatrix[16]={
Transform[0], Transform[4], -Transform[8], 0,
Transform[1], Transform[5], -Transform[9], 0,
Transform[2], Transform[6], -Transform[10], 0,
-(Transform[0]*Transform[12] + Transform[1]*Transform[13] + Transform[2]*Transform[14]),
-(Transform[4]*Transform[12] + Transform[5]*Transform[13] + Transform[6]*Transform[14]),
Transform[8]*Transform[12] + Transform[9]*Transform[13] + Transform[10]*Transform[14], 1};

or:

float viewmatrix[16]={
right.x, up.x, -fwd.x, 0,
right.y, up.y, -fwd.y, 0,
right.z, up.z, -fwd.z, 0,
-(right.x*pos.x + right.y*pos.y + right.z*pos.z),
-(up.x*pos.x + up.y*pos.y + up.z*pos.z,
fwd.x*pos.x + fwd.y*pos.y + fwd.z*pos.z, 1};


Note that z/fwd is inverted and the new position coords are simply -(pos dot old_axis).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Apologies for the delay in getting back but things at work needed my attention. :)

Right. Thanks for all the lovely information. It looks like that's exactly what I was after and hopefully I should get it to work at this end. Provided I can sort out my fixed point mathematics first! No end of trouble with it. Bah!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!