Sign in to follow this  

Model paralell to surface

This topic is 3668 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

hi, I getting some problems trying to rendering my model paralell to surface. I made my model on milkshape looking at z axis (0,0,1) When i render the model i do this:
glPushMatrix();
glTranslatef(modelpos.x, modelpos.y, modelpos.z); //here i tranlate the model to its position
glRotatef(model.angleyrotation, 0, 1, 0); //rotate the model depending of selected direction

Until here everthing goes right. After doing this i try to move my model paralell to surface, thats what im do:
tvector3f updir           = vec_init_direccion(VEC_DIR_Y);  //updir
tvector3f axisforrotation = vec_producto_vectorial_p(surfacenomal, &updir); //cross product to get the axis for rotation
float angle               = vec_angulo_p(&updir, surfacenomal); //get the angle between the this two vectors
glRotatef(angle, axisforrotation.x, axisforrotation.y, axisforrotation.z); //apply it
glPopMatrix();

The problem is that my model always rotated axisforrotation acording to its original position (z axis) after it the model is rotated by Y axis acording to its direction, but i made first the rotation on Y axis and after the axisforrotation!!. Why it works on that way? Thanks in advance.

Share this post


Link to post
Share on other sites

Hey ,

are u sure b4 u do ur transformations that ur object on the origin point ?

please post more info about ur current matrix b4 u do the pushing,
this is all i can think about .

take care.

Share this post


Link to post
Share on other sites
Consider the follwing:

glScalef(...);
glTranslatef(...);

This will actually translate first and then apply the scale. This is because the matrices are multiplied from the right (ScalingMatrix * TranslationMatrix) * VertexPosition.

Maybe you have to reorder your transformations to get the desired result.

Share this post


Link to post
Share on other sites
Hi,

I tried to reorder the glrotatef calls but if i put the axisforrotation call first i get very strange results.

Before this, thats the way i set the camera


glLoadIdentity();
gluLookAt(t_cam.v_position.current.x,
t_cam.v_position.current.y,
t_cam.v_position.current.z, //current camera position
modelpos.x, modelpos.y, modelpos.z, //model position
0.0, 1.0, 0.0);



And after this i dont touch any openglmatrix until the models draw procedure.

Thanks for your help!!.

Share this post


Link to post
Share on other sites
First, thanks for your time and help.

At the begining i have no axis rotation for paralell surface, so i didt make this line:

glRotatef(angle, axisforrotation.x, axisforrotation.y, axisforrotation.z);

And it worked perfect, not parallel to surface, but it worked well, but i wanted my model to be able to creep, and i will only add this line when my model is in that position.

Here some pics (green line is zaxis and red line is de model surface). As you can see my modelsurface is always rotated on its original axis (z) and only seems to work in pic 1 when it goes in z direction.

description of your image

description of your image

description of your image

Thanks!!!

Share this post


Link to post
Share on other sites
hmmm i think i got it..

try to make ur vector be on one of main axises ex. i,

to do this u need :

1) compute the angle between ur normal on current surrface by.

normal (dot) i = cos(theta)

2) do rotation to allign it on i axis.

glRotatef(-theta,1,0,0);

3) do rotation based on ur computed angle.

4) return the vector to orignal place.

glRotatef(theta,1,0,0);

i am sure there is bug somewhere >.>;;

but try and see ^^;

take care.

[Edited by - ff8 on December 2, 2007 4:55:21 PM]

Share this post


Link to post
Share on other sites

nvm my last post has some errors >.>;;

i think u need to make XZ-plan perpendicular ur normal.

to do this :

1) compute angle between normal and x-axis .

angleX=cos^-1(normal (dot) i)

2) compute angle between normal and z-zxis.

angleZ=cos^-1(normal (dot) k)

then do this :

glTranslatef(...);
glRotatef(-angleX,1,0,0);
glRotatef(-angleZ,0,0,1);


i hope this works and plz ^^;;;

take care.

Share this post


Link to post
Share on other sites
hi again

i dont know if you told me something like this


float angleX = vec_angulo_p(&xaxis, surfacenomal);
float angleZ = vec_angulo_p(&zaxis, surfacenomal);

glRotatef(-angleX, 1,0,0);
glRotatef(-angleZ, 0,0,1);

glRotatef(angle, axisforrotation.x, axisforrotation.y, axisforrotation.z);

glRotatef(angleZ, 0,0,1);
glRotatef(angleX, 1,0,0);



with this i get the rotation on x model axis, before it was on z model axis.

This is quite frustrating, anyway i really apreciate your help!

i have a couple of ideas but i will try it tomorrow, now im very tired!.

Thanks

Share this post


Link to post
Share on other sites

try this alone w/o ur rotating vector :


glTranslatef(...);
glRotatef(-angleX,1,0,0);
glRotatef(-angleZ,0,0,1);


glRotatef(angle, axisforrotation.x, axisforrotation.y, axisforrotation.z); <- no need

after trying it tell me what u get ,

take care.

Share this post


Link to post
Share on other sites
Hi, thanks again for your time and good morning!!

Im tried to explain the problem on a paper because im quite lost.

I think that is how i did it first:

description of your image

I calculate the normal for the model matrix not the world matrix as it supose to be.

I need to turn the model matrix according to world normal:

description of your image

I tried to do what you told me last, and i getting strange results again. i Think in last code you align modelmatrix with model normal

description of your image

i Think that the big problem is getting normal model as is supose to be in world (normal world).

Are you agree with me?

Share this post


Link to post
Share on other sites
Solved...

I put the gltranslation based on model direction after the axisforrotation (i tought that i was the first thing i did...) and it works!!, im feeling stupid, what a waste of human time.

Thanks ff8.

Share this post


Link to post
Share on other sites
Here it is:


glPushMatrix();
glTranslatef(modelpos.x, modelpos.y, modelpos.z); //here i tranlate the model to its position


//adjust to surface
tvector3f updir = vec_init_direccion(VEC_DIR_Y); //updir
tvector3f axisforrotation = vec_producto_vectorial_p(surfacenomal, &updir); //cross product to get the axis for rotation
float angle = vec_angulo_p(&updir, surfacenomal); //get the angle between the this two vectors
glRotatef(angle, axisforrotation.x, axisforrotation.y, axisforrotation.z); //apply it



glRotatef(model.angleyrotation, 0, 1, 0); //rotate the model depending of selected direction
glPopMatrix();





Now it seems so easy...

Share this post


Link to post
Share on other sites

This topic is 3668 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this