vector translation problem (easy for experts!)

Started by
3 comments, last by Solias 19 years, 5 months ago
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]; }
Advertisement
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...
I'm loathe to change it because it works everywhere else !
Is there something in the way I've done the translations ?

cheers
Give the second object a different scale or something, it's probably hidden in the first object.
If at first you don't succeed, redefine success.
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.

This topic is closed to new replies.

Advertisement