Skip to main content
GameDev.net gamedev.net
🔒 Locked

ViewVector and angles...

Started by skullfire Nov 24, 2003 at 11:45 PM 10 replies 950+ views
Original Post
skullfire
skullfire
Well, I want to figure out the 3 angles needed(for X,Y and Z)needed to rotate based on a viewvector. This is because i''m using viewvectors for objects in my game, and when the object renderer comes in, it has to rotate the model... This is what i have to far...not werking too good.
	glTranslatef(_vPosition._x,_vPosition._y,_vPosition._z);
	CVector v = _vView-_vPosition;
	Normalize(v);
	glRotatef(v._z,0,0,1);
	glRotatef(v._y,0,1,0);
	glRotatef(v._x,1,0,0);
/*
	float RotZ = atan(v._y/v._z);
	float RotY = atan(v._x/v._z);
	float RotX = atan(v._z/v._y);
	glRotatef(RotZ,0,0,1);
	glRotatef(RotY,0,1,0);
	glRotatef(RotX,1,0,0);
*/
z80
z80
How about using gluLookAt()? Given an eye position, a look-at point and an up vector it will compute a viewing matrix.

[EDIT]: Fixed typos

[edited by - z80 on November 25, 2003 4:35:47 AM]
skullfire
skullfire
quote:
Original post by z80
How about using gluLookAt()? Given an eye position, a look-at point and an up vector it will compute a viewing matrix.

[EDIT]: Fixed typos

[edited by - z80 on November 25, 2003 4:35:47 AM]

Well, this can''t be done cuz we''re not modifying the camera with this code. Lets say that code is for ALL the objects on the scene, like if I have a lot of cubes, all of the cubes will have their own view and position vectors, therefore, i must use glRotatef..unless some1 has a smarter idea... please help!
z80
z80
Yeah ok, you need to change the modelview matrix. I would compute a suiting matrix and apply it with glLoadMatrix() before rendering each of the objects.

What we need is a 4x4 matrix like this:


x_axis.x y_axis.x z_axis.x 0
x_axis.y y_axis.y z_axis.y 0
x_axis.z y_axis.z z_axis.z 0
pos.x pos.y pos.z 1


The pos vector is already in your _vPosition variable, so our problem is to calculate x_axis, y_axis and z_axis. The following is probably along the lines of how you''d do it:

z_axis = Vec3Normalize(Vec3Sub(_vView, _vPosition))
x_axis = Vec3Normalize(Vec3Cross(z_axis, up))
y_axis = Vec3Cross(x_axis, z_axis)

up is a vector defining "up" for your objects ([0, 1, 0] or whatever).
z80
z80
Duh.. Messed up when typing my password, so I cant make the above source look good.

Trying again ;-)


// Select modelview matrix

glMatrixMode(GL_MODELVIEW);

// Render objects

for (int i = 0; i < num_objects; i++)
{
// Setup transformation matrix

glLoadIdentity();
gluLookAt(
_vPosition.x, _vPosition.y, _vPosition.z,
_vView.x, _vView.y, _vView.z,
0, 1, 0
);

// Render object

myObject(i).Render();
}


[edited by - z80 on November 25, 2003 3:06:42 PM]
skullfire
skullfire
Hmm..interesting, thx... i''ll be testing this when I get home(stupid college, heh).Some questions though:
1) won''t the LoadIdentity + gluLookAt have a negative impact over the FPS count?
2) if I do it like this, then I''d have to move the Camera.Update()(Which is basically another gluLookAt) to the end of my code, right?
thx!
z80
z80
There are some multiplication and square root calculations inside glLookAt() so yes it will slow you down, but not too much unless you have lots and lots of objects. You will probably have to implement it and see if its a problem, then you will have to consider how to optimize if it turns out too slow.

Regarding the gluLookAt for your cam, I think you should put it before rendering the objects, or it wont take effect.
skullfire
skullfire
Well, just tried the gluLookAt function...not werking right...gonna try s''more and then, if it doesnt werk, ill do the cross thingie. thx neways
skullfire
skullfire
Well, it''s not werking man... neither of the 2 methods is giving me the desired results, and I''ve even tried the g_cam.Look() on both before and after the transformations.
I even added a glPushMatrix/glPopMatrix before each glLoadIdentity, but no...not visually correct.

the gluLookAt looks like it''s modifying the projection matrix, cuz there appears to be some kind of scissor testing or something...
z80
z80
Hmm.. Perhaps I will throw some code together to try it out -- might not get the time tho.
skullfire
skullfire

glPushMatrix();
glTranslatef(_vPosition._x,_vPosition._y,_vPosition._z);
CVector v = _vView - _vPosition;
if(v != 0) {
// Normalize(v);

float RotX = TODEG( atan2f(v._z,v._y) );
float RotY = TODEG( atan2f(v._x,v._z) );
float RotZ = TODEG( atan2f(v._y,v._x) );
glRotatef(RotX,1,0,0);
glRotatef(RotY,0,1,0);
glRotatef(RotZ,0,0,1);
}
//Draw stuff here

glPopMatrix();

Well, this is kinda working... can some1 tell me if this seems right?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.