# 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.

## 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 on other sites

Hey ,

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

this is all i can think about .

take care.

##### 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 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.

##### Share on other sites
u sure u do rotation off the axis?

try to remove last rotation and see what u get?

##### 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.

Thanks!!!

##### 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 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 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 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 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:

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:

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

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 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 on other sites

good thing to know ^^',

can u put the final piece of code? i am really intresting in seeing it =0

take care.

##### 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 surfacetvector3f updir           = vec_init_direccion(VEC_DIR_Y);  //updirtvector3f axisforrotation = vec_producto_vectorial_p(surfacenomal, &updir); //cross product to get the axis for rotationfloat angle               = vec_angulo_p(&updir, surfacenomal); //get the angle between the this two vectorsglRotatef(angle, axisforrotation.x, axisforrotation.y, axisforrotation.z); //apply itglRotatef(model.angleyrotation, 0, 1, 0); //rotate the model depending of selected directionglPopMatrix();

Now it seems so easy...

##### 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.