Question

I'm trying to use the QGLWidget to use OpenGL in a Qt application.

I made a subclass of QGLWidget etc., and thought I'd test if it works.

However, the following code is not working as I expected it would:

void MyGLWidget::paintGL() {
  gluLookAt(0,0,-10,0.5,0.5,0,0,1,0);
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1,0,0);
  glBegin(GL_POLYGON);
  glVertex3f(0,0,0);
  glVertex3f(0,1,0);
  glVertex3f(1,1,0);
  glVertex3f(1,0,0);
  glEnd();
}

What I imagined this would do is look at the center of a red square. But instead, when I run it, it seems to extremely briefly look at the center of the square, and then the display just becomes black.

Is there something I'm doing wrong? I'm not doing any OpenGL anywhere else. The function above is the only OpenGL code in the subclass (for now there's nothing in ::resizeGL() and ::initializeGL()).

Was it helpful?

Solution 2

It seems that there were two problems with my code. First, I had to set up the view port in :

void MyGLWidget::resizeGL(int w, int h) {
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();
  gluPerspective(45.,((GLfloat)w)/((GLfloat)h),0.1f,1000.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

and secondly, I had to call glLoadIdentity() before calling gluLookAt() in paintGL().

OTHER TIPS

I think you need to setup the OpenGL viewport. There are some Qt+OpenGL examples in the SDK. Take a look!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top