OpenGL and RenderMan, Basic Program Structure
[ contents ]
Basic Programming Structures

RenderMan

This is a boilerplate RenderMan program. One thing you will notice is that there aren't any windowing calls. The RenderMan Interface specifies output to rendering applications, so windowing is handled by X or by OpenGL. For clarity, I have eliminated anything relating to windowing. The main() function first executes init() which initializes the RenderMan Interface and sets graphic state options. Then main() loops through a frame counter calling display() for each frame that is to be output. This loop exits when all of the frames are output and then RiEnd() to close the RIB file.

OpenGL

This is a boilerplate OpenGL program. The main() function initializes the window's position, size, and display modes. Then it declares which functions will handle the display and reshape callbacks. display() is the main drawing routine, while the reshape function handles window movement and projection model options. Finally, glutMainLoop() is entered, and all windows are opened for rendering, event processing begins, and the display() callback is triggered. Once entered this loop never exits.

void main(void){
/* This is simply the loop generating function. main() first calls init() to initialize the RIB file output and graphics state options, then it enters into a loop that executes display() until the last frame of the animation */

int frame, frameend = 2120;

init();

for(frame = 1; frame <= frameend; frame++)
{
display(frame, frameend);
};

RiEnd();

exit(1);
}

void init(void) {

/* This is called once to establish graphic state options, such as image size and pixel aspect ratio, projection model, and lighting attributes. This is also were you begin RIB output */

extern RtToken file;
RtFloat fov = 60;

RiBegin(file);

RiFormat(640, 480, 1);
RiProjection("perspective", RI_FOV, (RtPointer)&fov, RI_NULL);

RiLightSource("distantlight", RI_NULL);
}

void display(int frame) {

/* RiFrameBegin() starts frame n and RiDisplay() places the name of the frame into the RIB file. RdLookAt() places the camera, and then geometry is called after RiWorldBegin(). At the end of display(), we exit the frame and return to the main loop to start over again */

sprintf(filename, "anim.%d.tif", (frame+1000));

RiFrameBegin(frame);
RiDisplay(filename, RI_FILE, RI_RGBA, RI_NULL);

RdLookAt(position, roll, direction);

RiWorldBegin();

...declare your geometry and transformations here.

RiWorldEnd();
RiFrameEnd();
}
int main(int argc, argv) {
/* This is where the glut window initialization, display mode initialization, and the main loop will be take place.*/

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWIndowSize(640, 480);
glutInitWindowPosition(0, 640);
glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}

void init(void) {

/* This is where you would enable state options such as lighting, depth buffering, shading model and polygon mode, to name a few.*/

glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

int frame = 1;

}

void display(void) {

/* The display function could be considered the current frame of your animation/program. At the end of the display function, you flush or swap the buffers and the frame is completed. This is where you would declare all geometry and do any tranformations*/

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(4.0, 1.0, 3.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);

...declare your geometry and transformations here.

glutSwapBuffers();
renderframe();

}

void reshape(void) {

/* the reshape function is where you declare your projection transformations */

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

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1.33, 1.0, 60.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void renderframe(void) {

/* renderframe() simply sends Unix a shell command that executes scrsave at the provided coordinates and to save the frame as image.#.rgb. */

char *movie[60];
extern int frame;

sprintf(movie, "usr/sbin/scrsave image.%d.rgb 8, 647, 8, 487", (frame+1000));
system(movie);

frame= frame+1;

}