The conversion of a Red Book example

In this section:

The example in listing 1.2 of the Red Book, simple.c is reproduced below. Each line in the source is numbered to aid description.

 1 /* simple.c */
 3 #include <GL/gl.h>
 4 #include <GL/glaux.h>
 5 #include <stdlib.h>
 8 int main(int argc, char** argv)
 9 {
10   auxInitDisplayMode (AUX_SINGLE | AUX_RGB);
11   auxInitPosition (0, 0, 500, 500);
12   auxInitWindow ("Simple OpengGL Sample");
14   glClearColor (0.0, 0.0, 0.0, 0.0);
15   glClear(GL_COLOR_BUFFER_BIT);
16   glColor3f(1.0, 1.0, 1.0);
17   glMatrixMode (GL_PROJECTION);
18   glLoadIdentity ();
19   glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
20   glBegin(GL_POLYGON);
21   glVertex2f(-0.5, -0.5);
22   glVertex2f(-0.5, 0.5);
23   glVertex2f(0.5, 0.5);
24   glVertex2f(0.5, -0.5);
25   glEnd();
26   glFlush();
27   _sleep (10000);
28   return(0);
29 }

When converting to Fortran, lines 1 to 9 may be ignored. Lines 10 to 12 describe the window properties to GLAUX. These properties are:

AUX_SINGLE single buffered. This is ClearWin+ default and is not necessary in the ClearWin+ version.

AUX_RGB use RGB colours. ClearWin+ only supports RGB colours for OpenGL. Colour index modes are unsuitable for shading and anti-aliasing and so are not supported.

0, 0 initial window position. This is specified using %sp (Window Position) in ClearWin+.

500, 500 initial window size. This is specified using %og (OpenGL Graphics Region) in ClearWin+.

"Simple OpenGL Sample" window caption.

In addition, GLAUX windows may be closed using the Escape key. Specify this with %es (Escape Closes) using ClearWin+.

ClearWin+ uses %lw (Leave Window Open) to allow a window to stay open when the winio@ call returns. If %lw were not specified, the drawing commands following would have no effect.

Lines 10 to 12 are replaced with the following calls to winio@. The ampersands at the end of every line are used to combine several winio@ calls into one format window.

c----Allows the user to close the window with the <esc> key
c----Window caption
     i=winio@(%ca[Simple OpengGL Sample]&')
c----Sets initial window position to (0,0)
c----Makes the same style of window as GLAUX
c----Creates the OpenGL window and returns leaving it open
     i=winio@('%og[static]%lw',500, 500,ctrl)

This may all be combined into:

     i=winio@('%es%ca[Simple OpengGL Sample]&')
    &         0,0,500,500,ctrl)

The use of line 28 with the Red Book example is to keep the window open for 10 seconds before closing it. This is unnecessary since the ClearWin+ window will stay open anyway until the user closes it. Note that the integer variable, ctrl is required since we have used %lw but that the variable itself is unused.

The Fortran include files clearwin.ins and opengl.ins must be included at the top of the main program.

The remainder of the program is virtually unchanged. The full ClearWin+ version appears below.

     PROGRAM Simple
     INCLUDE <clearwin.ins>,nolist
     INCLUDE <opengl.ins>,nolist
     REAL t1,t2
     INTEGER i, ctrl
     i=winio@('%es%ca[Simple OpengGL Sample]&')
    &         0, 0, 500, 500, ctrl)
     CALL glClearColor (0.0, 0.0, 0.0, 0.0)
     CALL glColor3f(1.0, 1.0, 1.0)
     CALL glMatrixMode(GL_PROJECTION)
     CALL glLoadIdentity()
     CALL glOrtho(-1d0, 1d0, -1d0, 1d0, -1d0, 1d0)
     CALL glBegin(GL_POLYGON)
     CALL glVertex2f(-0.5, -0.5)
     CALL glVertex2f(-0.5, 0.5)
     CALL glVertex2f( 0.5, 0.5)
     CALL glVertex2f( 0.5, -0.5)
     CALL glEnd()
     CALL glFlush()

As can be seen, the OpenGL calls and their positions are unchanged. A couple of calls to winio@ have replaced the calls to GLAUX. Overall, the two programs are very similar. This will be reinforced but qualified when we look at other examples.


Argument types

Call-backs and initialisation

Animation and multiple windows



Copyright © 1999-2020 Silverfrost Limited