Sign in to follow this  
ade-the-heat

vector translation problem (easy for experts!)

Recommended Posts

ok, the code below correctly draws the sphere where I want it after a few translations have been done previously: ======================== glTranslatef(4, 0.0, 6); glRotatef(90.0, 0.0, 1.0, 0.0); glScalef(5.0, 5.0, 5.0); //below sphere works fine ! gluSphere(rocket, 1.0f/2.0f, 7, 7); ======================== I need that precise position of the sphere and so directly after drawing the sphere I do the following to get the vector CVector vec1(0.0f, 0.0f, 0.0f); CVector tailfin; //vector with new position TransformVector4WithModel(vec1, tailfin); Then I draw the sphere like this and it should be exactly where it was above: glPushMatrix(); glLoadIdentity(); glTranslatef(tailfin.x, tailfin.y, tailfin.z); gluSphere(rocket, 1.0f/2.0f, 7, 7); glPopMatrix(); except it's not there and I can't find it anywhere !! This has got to be dead easy and so you're going to have to talk to me like a 3 year old. ASIDE TransformVector4WithModel function is like this: ============================================= void TransformVector4WithModel(CVector &vec, CVector &outvec) { //used to get new position float modelViewMatrix[16]; glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix); outvec.x = modelViewMatrix[0]*vec.x + modelViewMatrix[4]*vec.y + modelViewMatrix[8]*vec.z + modelViewMatrix[12]; outvec.y = modelViewMatrix[1]*vec.x + modelViewMatrix[5]*vec.y + modelViewMatrix[9]*vec.z + modelViewMatrix[13]; outvec.z = modelViewMatrix[2]*vec.x + modelViewMatrix[6]*vec.y + modelViewMatrix[10]*vec.z + modelViewMatrix[14]; }

Share this post


Link to post
Share on other sites
The problem must be in the TransformVector function. Try to do it by using rows instead of columns. Like:

outvec.x = modelViewMatrix[0]*vec.x + modelViewMatrix[1]*vec.y
+ modelViewMatrix[2]*vec.z + modelViewMatrix[3];

and so on...

Share this post


Link to post
Share on other sites
Ok so your code looks like this?

glTranslatef(4, 0.0, 6);
glRotatef(90.0, 0.0, 1.0, 0.0);
glScalef(5.0, 5.0, 5.0);
//below sphere works fine !
gluSphere(rocket, 1.0f/2.0f, 7, 7);

CVector vec1(0.0f, 0.0f, 0.0f);
CVector tailfin; //vector with new position
TransformVector4WithModel(vec1, tailfin);

glPushMatrix();
glLoadIdentity();
glTranslatef(tailfin.x, tailfin.y, tailfin.z);
gluSphere(rocket, 1.0f/2.0f, 7, 7);
glPopMatrix();

Let's go step by step:

glTranslatef(4, 0.0, 6);
glRotatef(90.0, 0.0, 1.0, 0.0);
glScalef(5.0, 5.0, 5.0);

So, in reverse order we scale, rotate, and translate.

gluSphere(rocket, 1.0f/2.0f, 7, 7);

now we draw a sphere centered at 4,0,6 with some rotation and scale.

CVector vec1(0.0f, 0.0f, 0.0f);
CVector tailfin; //vector with new position
TransformVector4WithModel(vec1, tailfin);

Assuming your matrix math works:
we take (0,0,0) and scale -> (0,0,0)
we take (0,0,0) and rotate -> (0,0,0)
we take (0,0,0) and translate -> (4,0,6)

glPushMatrix();
glLoadIdentity();

ok matrix cleared

glTranslatef(tailfin.x, tailfin.y, tailfin.z);

tailfin = (4,0,6), so now we have a translation by that vector

gluSphere(rocket, 1.0f/2.0f, 7, 7);

ok, we draw a sphere centered at (4,0,6) without rotation or scale. We already drew a sphere at that same location, but the first sphere was 5x the size of this one. So this sphere is inside the first one.

Share this post


Link to post
Share on other sites

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