Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [427]

By Root 1392 0
1.0, 0.6, -0.4 ); glVertex3f( 1.0, -0.6, -0.4 );

glVertex3f( -1.0, -0.6, -0.4 ); glVertex3f( -1.0, 0.6, -0.4 );

glEnd();

glBegin( GL_LINE_LOOP ); /* other end cap */

glVertex3f( 1.0, 0.6, 0.4 ); glVertex3f( 1.0, -0.6, 0.4 );

glVertex3f( -1.0, -0.6, 0.4 ); glVertex3f( -1.0, 0.6, 0.4 );

glEnd();

glFlush();

}

static void timeout( int value )

{

rot++; if( rot >= 360. ) rot = 0.;

glutPostRedisplay();

glutTimerFunc( 50, timeout, 0 );

}

int main( int argc, char** argv )

{

/* initialize glut */

glutInit(&argc, argv);

/* set display mode */

glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);

/* output window size */

glutInitWindowSize(500,500);

glutwin = glutCreateWindow("Running Linux 3D Demo");

glutDisplayFunc(disp);

/* define the color we use to clearscreen */

glClearColor(0.,0.,0.,0.);

/* timer for animation */

glutTimerFunc( 0, timeout, 0 );

/* enter the main loop */

glutMainLoop();

return 0;

}

Qt

As an example of how to do OpenGL programming with a more general-purpose GUI toolkit, we will redo the GLUT example from the previous section in C++ with the Qt toolkit. Qt is available from http://www.trolltech.com/ under the GPL license and is used by large free software projects such as KDE.

We start out by creating a subclass of QGLWidget, which is the central class in Qt's OpenGL support. QGLWidget works like any other QWidget, with the main difference being that you do the drawing with OpenGL instead of a QPainter. The callback function used for drawing with GLUT is now replaced with a reimplementation of the virtual method paintGL(), but otherwise it works the same way. GLUT took care of adjusting the viewport when the window was resized, but with Qt, we need to handle this manually. This is done by overriding the virtual method resizeGL(int w, int h). In our example we simply call glViewport() with the new size.

Animation is handled by a QTimer that we connect to a method timout() to have it called every 50 milliseconds. The updateGL() method serves the same purpose as glutPostRedisplay() in GLUT—to make the application redraw the window.

The actual OpenGL drawing commands have been omitted because they are exactly the same as in the previous example. Here is the full example:

#include

#include

#include

class RLDemoGLWidget : public QGLWidget {

Q_OBJECT

public:

RLDemoGLWidget(QWidget* parent,const char* name = 0);

public slots:

void timeout();

protected:

virtual void resizeGL(int w, int h);

virtual void paintGL();

private:

float rot;

};

RLDemoGLWidget::RLDemoGLWidget(QWidget* parent, const char* name)

: QGLWidget(parent,name), rot(0.)

{

QTimer* t = new QTimer( this );

t->start( 50 );

connect( t, SIGNAL( timeout() ),

this, SLOT( timeout() ) );

}

void RLDemoGLWidget::resizeGL(int w, int h)

{

/* adjust viewport to new size */

glViewport(0, 0, (GLint)w, (GLint)h);

}

void RLDemoGLWidget::paintGL()

{

/* exact same code as disp() in GLUT example */

...

}

void RLDemoGLWidget::timeout()

{

rot++; if( rot >= 360. ) rot = 0.;

updateGL();

}

int main( int argc, char** argv )

{

/* initialize Qt */

QApplication app(argc, argv);

/* create gl widget */

RLDemoGLWidget w(0);

app.setMainWidget(&w);

w.resize(500,500);

w.show();

return app.exec();

}

#include "main.moc"

Integrated Development Environments

Whereas software development on Unix (and hence Linux) systems is traditionally command-line-based, developers on other platforms are used to so-called integrated development environments (IDEs) that combine an editor, a compiler, a debugger, and possibly other development tools in the same application. Developers coming from these environments are often dumbfounded when confronted with the Linux command line and asked to type in the gcc command.[*]

In order to cater to these migrating developers, but also because Linux developers are increasingly demanding more comfort, IDEs have been developed for Linux as well. There are few of them out there, but only one of them, KDevelop, has seen widespread

Return Main Page Previous Page Next Page

®Online Book Reader