I am trying to run a simple example of OpenGL and I am facing the following issue. When I place the camera perfectly in front on my object, the object is not displayed at all. But when I move the camera (even by 0.00001) the object is displayed.
class GLWidget : public QGLWidget{
void initializeGL(){
glEnable(GL_DEPTH_TEST);
update_timer_ = new QTimer(this);
connect(update_timer_, SIGNAL(timeout()), this, SLOT(update()));
update_timer_->start(0.017);
}
/// @note camera decides renderer size
void resizeGL(int width, int height){
if (height==0) height=1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
}
void paintGL(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt(0,0, 10.0,0,0,0,0,0,1);
glBegin(GL_QUADS);
glColor3ub(255,0,0);
glVertex3d(1,1,1);
glVertex3d(1,1,-1);
glVertex3d(-1,1,-1);
glVertex3d(-1,1,1);
glColor3ub(0,255,0);
glVertex3d(1,-1,1);
glVertex3d(1,-1,-1);
glVertex3d(1,1,-1);
glVertex3d(1,1,1);
glColor3ub(0,0,255);
glVertex3d(-1,-1,1);
glVertex3d(-1,-1,-1);
glVertex3d(1,-1,-1);
glVertex3d(1,-1,1);
glColor3ub(255,255,0);
glVertex3d(-1,1,1);
glVertex3d(-1,1,-1);
glVertex3d(-1,-1,-1);
glVertex3d(-1,-1,1);
glColor3ub(0,255,255);
glVertex3d(1,1,-1);
glVertex3d(1,-1,-1);
glVertex3d(-1,-1,-1);
glVertex3d(-1,1,-1);
glColor3ub(255,0,255);
glVertex3d(1,-1,1);
glVertex3d(1,1,1);
glVertex3d(-1,1,1);
glVertex3d(-1,-1,1);
glEnd();
glFlush();
}
private:
QTimer* update_timer_;
};
int main(int argc, char *argv[]){
QApplication app(argc, argv);
GLWidget widget;
widget.resize(800,600);
widget.show();
return app.exec();
}
In this case I cannot see the object but if I use:
gluLookAt(0,0.00001, 10.0,0,0,0,0,0,1);
In this case, I can see the cube (And it looks perfectly in the middle of the screen).
Did I forgot to enable something in OpenGL or is there something wrong in the way I am using the gluLookAt
function?
Thanks in advance.
gluLookAt
takes 3 parameters:
The upvector must not be parallel to the direction in which you're looking (aka. 2nd - 1st argument) which is the case in your first call.
Your camera is at {0,0,10}
and you look at {0,0,0}
so you look in -z direction. Your upvector is {0,0,1}
which is the same direction as you're facing (*-1
).
Try gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);