PDA

View Full Version : GluLookAt



guju
05-03-2002, 06:11 AM
Hi there!
I have try to create a PostProcessor for FE-Meshes. But now i have problems with gluLookAt. I created the following code:
void MyGLWidget::drawNodes()
{
for(int i=0;i<numVertices;i++)
{
glBegin (GL_POINTS);
glVertex3f(vertices[i*3],vertices[i*3+1],vertices[i*3+2]);
glEnd();
}
}
void MyGLWidget: http://www.opengl.org/discussion_boards/ubb/tongue.gifaintGL()
{

glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
gluLookAt(0.,0.,6*radius,center[0],center[1],center[2],0.,1.,0.);
glTranslatef (movex,movey ,0.0);
glRotatef (xRot, 0.0, 1.0, 0.0);
glRotatef (yRot, 1.0, 0.0, 0.0);
glScalef(scale, scale, scale);
drawNodes();
glFlush(); // Make sure everything is drawn before restarting timer
}


void MyGLWidget::setViewPoint()
{
angle=60;
center[0]=(xmax+xmin)/2;
center[1]=(ymax+ymin)/2;
center[2]=(zmax+zmin)/2;
radius=sqrt((xmax-center[0])*(xmax-center[0])+(ymax-center[1])*(ymax-center[1])+(zmax-center[2])*(zmax-center[2]));
distance = radius/tan(angle*3.14156/360);
resizeGL(MyGLWidget::width(),MyGLWidget::height()) ;
}

void MyGLWidget::initializeGL()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
scale = 1;
xRot=0;
yRot=0;
movex=0;
movey=0;
}

void MyGLWidget::resizeGL( int w, int h )
{
float aspect=float(w)/float(h);
float top,bottom,left,right;
left=center[0]-radius*2;
right=center[0]+radius*2;
bottom=center[1]-radius*2;
top=center[1]+radius*2;
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(left,right,bottom,top,0.01,(radius*2));
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}

void MyGLWidget::mouseReleaseEvent( QMouseEvent * event)
{
down = FALSE;
}

void MyGLWidget::mouseMoveEvent( QMouseEvent * event)
{
if (down && (event->state()==(LeftButton|ControlButton))) {
xRot += (event->x()-xRot_old);
yRot += (event->y()-yRot_old);
xRot_old=event->x();
yRot_old=event->y();
emit updateGL();
}else if (down && (event->state()==(MidButton|ControlButton))) {
scale =scale+((yRot_old - event->y()) / 100.0);
yRot_old=event->y();
if (scale<0.) scale = 0;
emit updateGL();
}else if (down && (event->state()==(RightButton|ControlButton))) {
movex+=(event->x()-xRot_old)/5;
movey+=(-event->y()+yRot_old)/5;
xRot_old=event->x();
yRot_old=event->y();
emit updateGL();
}else if (down && (event->state()==(LeftButton|ShiftButton))) {
emit updateGL();
}
}

But if i start the program, i can not see anything. What could be wrong, because i defined gluLookAt to the center of the object??

Bob
05-03-2002, 06:29 AM
Here I can see a possible problem.



gluLookAt(0.,0.,6*radius,center[0],center[1],center[2],0.,1.,0.);
.
.
.
glOrtho(left,right,bottom,top,0.01,(radius*2));

You are sure looking towards the center of the object, but look at the position from where you are looking.

To make it simple, say that center[] = {0,0,0}, i.e. the object is located at the origin, and you are not doing any kind of translation (movex and movey is zero). Now, you have placed the viewpoint 6*radius units away from the object. Then look at the glOrtho call, the far plane is only 2*radius units away. That means, everything that is more than 2*radius units away form the viewpoint are not rendered.

Try push the far clip plane furhter out, so that the object is located inside the view volume.

And another tip.



void MyGLWidget::drawNodes()
{
for(int i=0;i<numVertices;i++)
{
glBegin (GL_POINTS);
glVertex3f(vertices[i*3],vertices[i*3+1],vertices[i*3+2]);
glEnd();
}
}

This is bad. Put the glBegin/glEnd pair outside the for loop, like this.



void MyGLWidget::drawNodes()
{
glBegin (GL_POINTS);
for(int i=0;i<numVertices;i++)
{
glVertex3f(vertices[i*3],vertices[i*3+1],vertices[i*3+2]);
}
glEnd();
}

guju
05-03-2002, 07:01 AM
Hi Bob!
Thanks for your answer. I changed the 6*radius to 3*radius, but it did not work. I changed glOrtho to :
gluPerspective(angle, (GLfloat)w / (GLfloat)h, 0.01, 3*radius);
and i could see everything. So, i do not know what is the reason, why Ortho does not work.
regards
Juergen