PDA

View Full Version : QGLWidget won't display anything except the clear color



alexbcd
5th October 2010, 17:21
Hello,

I have started to develop a 3D viewer on a computer, and I would like to test it on another one. Both of them are under ubuntu lucid lynx. Unfortunately, on the second computer, the QGLWidget is blank. I have made up a minimal application that doesn't work neither on that second computer :


#ifndef GLVIEWER_H
#define GLVIEWER_H
#include <QtOpenGL/qgl.h>

class GLViewer : public QGLWidget {

public:
GLViewer(QWidget * parent = 0);
~GLViewer();

protected:
int viewport[4];

void initializeGL();
void resizeGL (int width, int height);
void paintGL();


};

#endif // GLVIEWER_H

and


#include "GLViewer.h"

GLViewer::GLViewer(QWidget * parent)
: QGLWidget(parent)
{

}

GLViewer::~GLViewer() {

}

void GLViewer::initializeGL() {

}

void GLViewer::resizeGL(int width, int height) {
viewport[0] = 0;
viewport[1] = 0;
viewport[2] = width;
viewport[3] = height;
glViewport(0, 0, width, height);
}

void GLViewer::paintGL() {
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0,0,.5);
glVertex3f(0,1,.5);
glVertex3f(1,1,.5);
glEnd();
}

What it does is to draw a blank area in clearColor, but nothing else. It seems however that the Qt precompiled demo are working well with opengl. Where can the problem come from ?

manmohan
6th October 2010, 06:01
you have to organise your code ...you have to go along in this way. this is just an example you can place your own opengl drawing code in paintGL() function

void GLWidget::initializeGL()
{
qglClearColor(Qt::gray);
glShadeModel(GL_SMOOTH);
}
void GLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(width) / height;
glFrustum(-x, +x, -1.0, +1.0, 4.0, 20.0);
glMatrixMode(GL_MODELVIEW); }

void GLWidget::paintGL()
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -15.0);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
pobj = gluNewQuadric();
gluQuadricDrawStyle(pobj, GLU_LINE);
gluQuadricNormals(pobj, GLU_SMOOTH);
qglColor(Qt::red);
gluCylinder(pobj, 1, 1, 3, 30, 30); }