PDA

View Full Version : glulookat replacement

lynear
01-05-2002, 11:45 PM
hmm... okay i am writing a 3d engine(isn't everyone?) but i am trying to stay away from glu library not glut or glaux but glu.h... and so i put together some thoughts and came up with a glulookat replacement... but it doesn't seem to work in theory it should but maybe i am missing something small.

all the functions do what they sound like...

vec is declared as

typdef float vec[3];

used an array for various reasons as opposed to a struct.

the last parameter of functions is always the result... i replaced gltranslatef with just adding the values to the matrix.

Maybe someone could help me... maybe i am forgetting to set a certain modelview matrix or something...

void lookAt(float eyex, float eyey, float eyez,
float centerx,float centery,float centerz,
float upx, float upy, float upz)
{
vec F,f={0},UP, up={0},s={0},u={0};
matrix M;

F[0]=centerx-eyex;
F[1]=centery-eyey;
F[2]=centerz-eyez;

UP[0]=upx;
UP[1]=upy;
UP[2]=upz;

NormalizeVector(F,&f);
NormalizeVector(UP,&up);

MultiplyVecs(f,up,&s);
MultiplyVecs(s,f,&u);

Reverse(f,&f);

ZeroMemory(&M,MATRIX4SIZE);

M[0][0]= s[0]; M[0][1]= s[1]; M[0][2]= s[2]; M[0][3]= 0;
M[1][0]= u[0]; M[1][1]= u[1]; M[1][2]= u[2]; M[1][3]= 0;
M[2][0]= f[0]; M[2][1]= f[1]; M[2][2]= f[2]; M[2][3]= 0;
M[3][0]= -eyex; M[3][1]= -eyey; M[3][2]= -eyez; M[3][3]= 1;

glMultMatrixf(M);
//glTranslatef (-eyex, -eyey, -eyez);
}

thanks a ton...

Dan

mbespalov
01-06-2002, 03:26 AM
I don't know what is wrong in your code, but
there are two glu implementations I'm aware of.
MESA www.mesa3d.org (http://www.mesa3d.org) and SGI OpenGL sample implementation (don't remember url, somewhere on the www.sgi.com) (http://www.sgi.com))
here is SGI's code

void GLAPI
gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,
GLdouble upz)
{
int i;
float forward[3], side[3], up[3];
GLfloat m[4][4];

forward[0] = centerx - eyex;
forward[1] = centery - eyey;
forward[2] = centerz - eyez;

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

normalize(forward);

/* Side = forward x up */
cross(forward, up, side);
normalize(side);

/* Recompute up as: up = side x forward */
cross(side, forward, up);

__gluMakeIdentityf(&amp;m[0][0]);
m[0][0] = side[0];
m[1][0] = side[1];
m[2][0] = side[2];

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

m[0][2] = -forward[0];
m[1][2] = -forward[1];
m[2][2] = -forward[2];

glMultMatrixf(&amp;m[0][0]);
glTranslated(-eyex, -eyey, -eyez);
}

And remember all matrix functions multiply generated matrix with the current(texture/projection/color/modelview...) matrix, don't forget to load identity matrix when needed, if you want place your viewing origin in the 3d scene use

MatrixMode(MODELVIEW)