Question

I got a problem with drawing on a QGLWidget. I have several quads on the widget which I can move around when pressing some keys. As long as I just draw quads, everything works fine but now I want to add some lines using:

    glBegin(GL_LINE);
glColor3f(c[0], c[1], c[2]);
glVertex3f(v1.x, v1.y, v1.z);
glVertex3f(v2.x, v2.y, v2.z);
glEnd;

The drawing also works fine, but the clearing of the glwidget doesn't work anymore. Means that I see everything I ever drawed on it. Just to mention. I tried the same with GLUT using the same initializations and it worked, but since I have switched to Qt it doesn't work anymore. paintGL(), resizeGL() and initializeGL() are below.

void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 0.0f, 0.0f, -10.0f, -20.0f, 0.0f, 20.0f, -10.0f);

glTranslatef(0.0f, -30.0f, -40.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(s_deg, 0.0f, 0.0f, 1.0f);
glRotatef(s_deg2, cos(DEGRAD(s_deg)), sin(DEGRAD(s_deg)), 0.0f);


float colors[8][3] = {
    0.5, 0.0, 0.0,
    0.0, 0.5, 0.0,
    0.0, 0.0, 0.5,
    1.0, 0.5, 0.5,
    0.5, 1.0, 0.5,
    0.5, 0.5, 1.0,
    0.9, 0.9, 0.9,
    0.1, 0.1, 0.1,

}; //red, green, blue, red shiny, green shiny, blue shine, light grey, dark grey

for(int i=0;i<glBoxes.size();i++) {
    glBoxes.at(i).setColor(colors[i]);
    glBoxes.at(i).drawCuboid();
    glBoxes.at(i).drawCuboidGrid();
}

}

void GLWidget::initializeGL() {    
glDepthFunc(GL_LESS);
glClearColor(0.2, 0.2, 0.2, 0.2);
glClearDepth(1.0);

}

void GLWidget::resizeGL(int width, int height) {    
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0.0f, 0.0f, (float)width, (float)height);
glLoadIdentity();
gluPerspective(45.0f, (float)width/(float)height, 0.5f, 100.0f);
glMatrixMode(GL_MODELVIEW);

}

any Ideas?

Was it helpful?

Solution

The tokens accepted by glBegin are

  • GL_POINTS
  • GL_LINES
  • GL_TRIANGLES
  • GL_TRIANGLE_FAN
  • GL_TRIANGLE_STRIP
  • GL_QUADS
  • GL_QUAD_STRIP

and

  • GL_POLYGON

The token used by you, GL_LINE (not the missing trailing S) is not valid for glBegin.


The statement glEnd; will evaluate the address of the function glEnd and silently discard the result. Could it be, that you have a Pascal or Delphi background? In C like languages you have to add a matched pair of parentheses to make it a function call. Functions that don't take a parameter are called with an empty pair of parentheses. E.g. in your case glEnd();.


Not related to your problem. All of the code in resizeGL should go to the head of paintGL (use the widget's width() and height() getters). Also what you have in initializeGL belongs to paintGL.

The proper use of initializeGL is to do one-time initialization, like loading textures, and shaders, preparing FBOs and such.

resizeGL is meant to re-/initialize stuff that depends on the window's size and which is quite time consuming to change, like renderbuffers and/or textures used as attachment in FBOs used for window sized post-processing or similar. Setting the projection matrix does not belong there, and neither does the viewport. Those go into paintGL.

glDepthFunc, glClearColor and glClearDepth directly influence the drawing process and as such belong with the drawing code.

Also you should not use the immediate mode (glBegin … glEnd) at all. It's been outdated ever since OpenGL-1.1 was released over 15 years ago. Use Vertex Arrays, with the possible addition of Buffer Objects.

OTHER TIPS

glEnd; should be glEnd();. This may actually fix your problem.

GL_LINE isn't a valid token for glBegin. To draw lines, you need GL_LINES (that's subtle, and you're in good company - this is a common mistake).

GL_LINE is used to control how polygons are rendered, which is controlled by the glPolygonMode function.

It must be GL_LINES instead of GL_LINE. The symbols accepted by glBegin are plural (i.e. GL_QUADS, GL_LINE_STRIPS...).

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