3d object orientation with standard Opengl commands
category: general [glöplog]
Hi sceners!
I need some help in order to finish some intro i'm coding
say I have a vector with points (x0,y0,z0),(x1,y1,z1) , i'd like to orientate a object according my vector
i try the following , it works in only one axis (eg : xy or xz or yz ) but not with the three at the same time :(
i know there is many other ways to orientate the object more properly (eg : using a matrix and do rotation by myself and give opengl directly good vertex values ... ) but i d like to use directly a opengl command
I need some help in order to finish some intro i'm coding
say I have a vector with points (x0,y0,z0),(x1,y1,z1) , i'd like to orientate a object according my vector
i try the following , it works in only one axis (eg : xy or xz or yz ) but not with the three at the same time :(
Code:
dx=x1-x0;
dy=y1-y0;
dz=z1-z0;
roll = (atan2(y,x)/2*PI*360);
pitch = (atan2(z,y)/2*PI*360);
yaw = (atan2(z,x)/2*PI*360);
glRotatef(roll,0,0,1);
glRotatef(pitch,1,0,0);
glRotatef(yaw,0,1,0);
//draw my object
i know there is many other ways to orientate the object more properly (eg : using a matrix and do rotation by myself and give opengl directly good vertex values ... ) but i d like to use directly a opengl command
my advice is to _never_ use angles (and trigonometric functions) unless strictly necessary.. and use vectors instead. You can (and should) basically do everything with vector operations. For example in this case, what you need is to construct a basis (a orthonormal matrix) where one axis is your desired orientation vector (x1y1z1-x0y0z0), and the other two are orthogonal to it (via cross products). When you have these two vectors, pack them into the matrix and you are done. Not, it is easy, sounds complex, and can take some time to program if it's your first time (as I think it is). I can post some code tonight (I have to go now), if nobody did it before. Note, 3D APIs are crap regarding matrix notation (both of them, even OpenGL), don't block if nothing works, try to transpose the matrix and see.
You'll find example code to do such a thing here.
http://dbfinteractive.com/index.php?topic=2050.0
Its in basic, not C, but converting should be trivial.
http://dbfinteractive.com/index.php?topic=2050.0
Its in basic, not C, but converting should be trivial.
When I such such a thing, I mean, the way iq is suggesting.
thx a lot auld (very interesting url ) and iq !! i'm checking it
you can also use gluLookAt..
>Navis,
Here is glulookat implementation
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html
(to compare with : http://dbfinteractive.com/index.php?topic=2050.0 )
I try to use it in order to align my object according the two point vertex
I put 0,1,0 as up vector but i know its wrong
Here is glulookat implementation
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html
(to compare with : http://dbfinteractive.com/index.php?topic=2050.0 )
I try to use it in order to align my object according the two point vertex
Code:
glTranslate(x,y,z);
glulookat(x,y,z,oldx,oldy,oldz,0,1,0);
glTranslate(x,y,z);
//draw object
I put 0,1,0 as up vector but i know its wrong