OpenGL in Qt doesn't draw red square

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()).

Answers


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


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().


Need Your Help

Parsing Windows Form variables to the main method for use

c# winforms

I am working on a settings panel for my visualization tool right now that offers granular controls over things displayed on the graph, like choosing the color of text, title text input, etc.

CSS Semi-fixed Element?

javascript html css

I remember seeing an example of this recently, but for the life of me I can't find the site.