Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

Usage

Basic

Using lescegra in your application involves the following steps:
  1. initialize the display mode
  2. set up the scene graph
  3. run the update-display loop
    1. update the scene graph
    2. display the scene

Note:
When using callback driven toolkits like GLUT the update-display loop is replaced by a timeout handler repeatedly updating the scene graph and triggering the redisplay of the scene.

Initialize the Display Mode

Before you can start using lescegra, you have to set up the OpenGL display mode. This mostly means creating a window for your application and enabling some OpenGL features you want to use.

Example:

 void init_display(void) {
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
     glutCreateWindow("lescegra sample");

     glClearColor(0.0, 0.0, 0.0, 0.0);

     glEnable(GL_DEPTH_TEST);
     glEnable(GL_LIGHTING);
     glEnable(GL_TEXTURE_2D);
     glEnable(GL_CULL_FACE);

     glCullFace(GL_BACK);
 }

Setting up the Scene

The first thing to do, after initializing the display mode, is to create your scene representation. This mostly involves creating a suitable camera and all the objects that will be visible.

Example:

 static LsgCamera* camera;
 static LsgNode* scene;

 void init_scene(void) {
     LsgPerspectiveCam* pcam;
     LsgCoords* coords;

     pcam = LsgPerspectiveCam_create();
     vertex_assign(pcam->location, 3.0, 4.0, 2.0);
     vertex_assign(pcam->lookat,   0.0, 0.0, 0.0);
     vertex_assign(pcam->up,       0.0, 1.0, 0.0);
     pcam->fovy =   45.0;
     pcam->aspect =  1.0;
     pcam->dmin =    0.1;
     pcam->dmax =   10.0;

     coords = LsgCoords_create(1.0);

     camera = (LsgCamera*)pcam;
     scene  = (LsgNode*)coords;
 }

Display the Scene

To display your scene you have to do the following steps:
  1. clear the screen
  2. initialize the modelview and projection matrix
  3. create the correct view frustum
  4. render the scene with a camera
  5. flush the OpenGL pipeline
  6. swap the display buffers

Note:
Steps 2 and 3 can mostly be moved into the program initialization phase as the display process should restore the transformation matrizes and the view frustum.
Example:
 void display(void) {
     LsgFrustum viewfrustum;
  
     LsgFrustum_init(&viewfrustum, matrix_identity, matrix_identity);
  
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();

     camera->display(camera, &viewfrustum, scene);

     glFlush();
     glutSwapBuffers();
 }

Animation

To get a time based animation, you have to repeatedly update your scene representation:
  1. update the scene graph (using an increasing time)
  2. mark all nodes as updated
  3. trigger redisplay

Note:
You should call the clean method after calling update to allow external changes, for example from keyboard handler functions, to propagate up the scene graph.
Example:
 void animate(int ignore) {
     float now;

     now = (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0;

     scene->update(scene, now);
     scene->clean(scene);

     glutPostRedisplay();
 }

(c) 2003, by Enno Cramer
generated on 13 Jul 2003
lescegra - doxygen