PDA

View Full Version : how to translate 2nd object based on 1st object



coda
12-11-2003, 05:04 PM
i drew a rect and wish to add another rect on top of it (all in 3D) but it doesn't seem to show in the expected way. this is my code and not sure where is wrong:

void CMainDialog::RenderScene()
{
UpdateData(TRUE);

// dimensions from dialog
length =(m_length/m_resolution);
breath =(m_breadth/m_resolution);
x = length/2;
y = breath/2;

SetupRC();//settings
glTranslatef(0.0f,-1.5f,-7.0f);
glRotatef(rquad,1.0f,1.0f,1.0f);

//drawing 1st rect
if(m_length!=0 && m_breadth!=0){
glColor3ub(242,134,60);
shape();}

//drawing 2nd rect on top of 1st
if(m_thick!=0){
glTranslatef(0.0f,y,0.0f);
y=((m_thick/m_resolution)/2);
glColor3ub(255,153,204);
shape();}
}

void CMainDialog::shape()
{

//normals for 6 surfaces of cube
GLfloat n[6][3]={{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}};

//vertex indices for 6 faces of cube
GLint faces[6][4]={{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };

//coordinates of vertices
v[0][0] = v[1][0] = v[2][0] = v[3][0] = x;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = -x;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -y;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = y;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

int i=0;

for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();}
}

the 1st rect showed OK but the 2nd rect whenever i changed the m_thick, i would expect the 2nd to still rest nicely on top of 1st rect but when the m_thick is too larger/smaller than m_breadth, the 2nd rect seem to "eat into" 1st rect or either floating above 1st rect, how do i do proper translate?

ZbuffeR
12-11-2003, 06:01 PM
Try to use y AFTER updating it maybe :

y=((m_thick/m_resolution)/2); glTranslatef(0.0f,y,0.0f);

And don't forget that matrix operations (glTranslate,glRotate and the like) are computed in 'reversed' order. A.B != B.A with matrices. For pure translation it is ok, but if you mix rotate/translate, watch your step.