Found at: http://publish.ez.no/article/articleprint/49/ |
Building 3D worlds with OpenGL |
All the major game developers do it, why can't you? Using OpenGL is not simple, but it's not impossible either. (Some experience in C++ and/or Qt may be required to follow this article.)
|
|
| A 3D coordinate system. Z points away from you. |
|
| Rotating landscape with building |
void GameWidget::initializeGL()
{
// lighting settings
static GLfloat pos[4] = { 5.0, 5.0, 5.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, pos );
glEnable( GL_CULL_FACE );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_DEPTH_TEST );
static GLfloat ared[4] = { 0.8, 0.1, 0.0, 1.0 };
Landscape = glGenLists(1); // start building a display list
glNewList( Landscape, GL_COMPILE );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ared );
buildLandscape();
glEndList(); // done with display list
glEnable( GL_NORMALIZE );
}
|
void GameWidget::resizeGL( int width, int height )
{
GLfloat w = (float) width / (float) height;
GLfloat h = 1.0;
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -w, w, -h, h, 5.0, 60.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0.0, 0.0, -40.0 );
}
|
void GameWidget::paintGL()
{
if ( Animate )
Angle += 2.0;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glTranslatef( 0.0, -5.0, 0.0 );
glRotatef( Angle, 0.0, 1.0, 0.0 );
glCallList( Landscape ); // call display list
glPopMatrix();
}
|
tar -zxf qgl-demo.tar.gz cd qgl-demo tmake -o Makefile qgl-demo.pro make ./qgl-demo |