Click to See Complete Forum and Search --> : QGLWidget or OpenGL glitch


arlenagha
05-04-2003, 03:54 PM
i am reading Programming with Qt:2nd edition. in it is this code which uses QGLWidget for OpenGL drawing. i have compiled and run this same exact code with no problem. i must say that back then i had KDE installed. now when i try to run the same code i get some opengl drawing artifacts when resizing the window. i don't think it should have anything to do with KDE, however, the code is the same as it was. Does anyone know why this happens?


#include <qapplication.h>
#include <stdlib.h>
#include <qgl.h>

class SierpinskiWidget : public QGLWidget
{
public:
virtual void paintGL();
virtual void initializeGL();
virtual void resizeGL( int w, int h );
};

void SierpinskiWidget::paintGL()
{
typedef GLfloat point2[2];
point2 vertices[3] = {{ 0.0,.0},{200.0,175.00},{400.0,0.0}}; // triangle
point2 p = { width()/2.0, height()/2.0 }; // initial point

glClear( GL_COLOR_BUFFER_BIT );

/* Compute and plot 5000 new points */
for( int k = 0; k < 5000; k++ ) {
int j = rand() % 3; // pick a random vertex

// Compute the point between the vertex and the old point
p[0] = ( p[0] + vertices[j][0] ) / 2.0;
p[1] = ( p[1] + vertices[j][1] ) / 2.0;

// Plot new point
glBegin( GL_POINTS );
glVertex2fv( p );
glEnd();
}

glFlush();
}


void SierpinskiWidget::initializeGL()
{
glClearColor( 1.0, 1.0, 1.0, 1.0 );
glColor3f( 1.0, 0.0, 0.0 );
}


void SierpinskiWidget::resizeGL( int w, int h )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, w, 0.0, h );
glMatrixMode( GL_MODELVIEW );

updateGL();
}

int main( int argc, char* argv[] )
{
QApplication::setColorSpec( QApplication::CustomColor );
QApplication a(argc,argv);

SierpinskiWidget* w = new SierpinskiWidget();
w->resize( 400, 350 );
a.setMainWidget( w );
w->show();
return a.exec();
}

bwkaz
05-04-2003, 04:42 PM
Hmm... well, just so you know you're not crazy, I see the same artifacts (running it under twm).

Maybe it's a math precision issue? I'm not quite sure other than that...

Though I did notice the scaling is a bit ... off, too. Maybe related?

arlenagha
05-04-2003, 06:43 PM
well i don't have a problem with the scaling since it's always been like that though i know that it doesn't scale right.

would you think that this might be a Opengl related to Qt related problem. since i remember it working just fine when i had KDE installed( though i did run it in GNOME 2). do you have KDE installed or are you just running it in Gnome?

bwkaz
05-04-2003, 10:50 PM
Like I said, I'm running it in twm, which is neither.

I do have kdelibs, kdebase, and kdegames installed (only because I want to play kmines and kpat once in a while, and the other huge packages are prerequisites for those two games), but they weren't running at the time. None of KDE was.

The version of KDE is 3.0, with Qt 3.1.0.