PDA

View Full Version : 3D Object translation and resize



Vincent22
04-26-2012, 01:44 PM
Hi, in my scene I have two 2D squares and a 3D model. The model is centered inside one of the square using "glTranslate" with empiric values.

The problem is that when I resize the window the model is centered inside the square no more. I've tried using aspect ratio inside "glTranslate" but it's not work as I want.
Someone can help me?

Here is the code:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20.0, 20.0, -20.0, 20.0, 1.0, -30.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glColor3f(0.1,0.1,0.4);
glVertex2d(-17.0,17.0);
glVertex2d(0.0,17.0);
glVertex2d(0.0,0.0);
glVertex2d(-17.0,0.0);

glVertex2d(1.0,17.0);
glVertex2d(18.0,17.0);
glVertex2d(18.0,0.0);
glVertex2d(1.0,0.0);
glEnd();





glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)wwidth / (GLfloat)wheight, 1.0, 100.0);
gluLookAt(20.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
//gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 5.0f);
// glTranslatef(0.0f, 0.0f, 2.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glScalef(0.5, 0.5, 0.5);
glRotatef(180.0, 0.0, 1.0, 0.0);
glRotatef(-20.0, 0.0, 0.0, 1.0);
glTranslatef(0.0f, 7.0f+(wwidth/wheight), -16.0+(wheight/wwidth));
cycle1->drawCycle();

glPopMatrix();