links: |- index -|- home -| end

in page: preamble hello world Solar2 downloads


2011-11-25: Added Solar2 example, with full source, compile able in Windows and Linux/Unix systems, linked with freeglut.

2009-06-22: Updated ogl02 example. Now compiles in Ubuntu (linux) as well ;=)) Contains some 49 'examples'. Needs some PLIB libraries.

OpenGL: Lots of open source animation and games programs use it, and, as the web page states, it is 'The Industry's Foundation for High Performance Graphics'. OpenGL is a registered trademark of SGI. A lot of the sample code dates back to people like Mark J. Kilgard, circa 1997-1998, but is still very relevant today.

There are a number of 'wrappers' which extend the OpenGL API, and try to make it easier for programmers. These fall under the generic name GLUT - OpenGL Utility Toolkit. It implements a simple windowing application programming interface (API) for OpenGL. The 'latest' source from the OpenGL site is glut-3.7, circa Dec, 1998, but others have taken over this, and produced later versions, with some extensions as well, like freeglut, hosted on sourceforge...

And there are also a number of 'wrapper', that wrap the whole process, like OpenSceneGraph, PLIB, wxWidget, etc... but this page is only about OpenGL and GLUT!


Hello World

The basic interface goes like this simple 'Hello World' application :-

// ogl01.cxx
#include <stdlib.h>  // for exit()
#include <GL/glut.h> // for the standard implementation
static void out_string_at(void *font, int x, int y, char *string)
#ifdef __FREEGLUT_EXT_H__
   // this is ONLY if GLUT has this extended mode
   glRasterPos2f((GLfloat)x, (GLfloat)y);
   glutBitmapString( font, (const unsigned char *)string );
   int len, i;
   glRasterPos2f((GLfloat)x, (GLfloat)y); // set position
   len = (int) strlen(string);
   for (i = 0; i < len; i++) { // output character by character
      glutBitmapCharacter(font, string[i]);
// set some INITIAL values
static void init01(void) 
   glClearColor (1.0, 1.0, 1.0, 0.0);  // clear to color
   glShadeModel (GL_FLAT); // The default value is GL_SMOOTH
// called to DISPLAY what ever desired
static void display01(void)
   glClear(GL_COLOR_BUFFER_BIT); // clear screen, to glClearColor()
   glColor3f(1.0,0.0,0.0); // output the string, in red
   out_string_at( GLUT_BITMAP_TIMES_ROMAN_24, 14, 5, "Hello World" );
   glFlush (); // use glutSwapBuffers(); if GLUT_DOUBLE
// called initially, and when SIZE is changed
static void reshape01(int w, int h)
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); // set view to ALL
   glMatrixMode (GL_PROJECTION); // use PROJECTION matrix
   glLoadIdentity (); // and load it...
   // set to whole screen
   gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
// called on each KEYBOARD character
static void keyboard01(unsigned char key, int x, int y)
   if((key == 0x1b)||(key == 'q')||(key == 'Q'))
      exit(0); // exit on any of the above keyboard inputs
// OS entry - simple Hello World application
int main (int argc, char** argv)
   glutInit(&argc, argv); // initialize GLUT - must be first
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // set MODE
   glutInitWindowSize (150, 30); // set size (or default size used)
   glutCreateWindow ("Hello"); // create window, and set TITLE
   init01(); // some local init...
   glutDisplayFunc(display01);   // set DISPLAY function
   glutReshapeFunc(reshape01);   // set SIZE function
   glutKeyboardFunc(keyboard01); // set KEYBOARD function
   glutMainLoop(); // run application, never to return
   return 0; /* ANSI C requires main to return int. */
// eof - ogl01.cxx

The glutInit(&argc, argv); must be called to initialize GLUT, and it accepts a small number of arguments, and ignores all others... There are a number of parameters that can be used in the glutInitDisplayMode(...) call, establishing the primary 'mode' for the window... Then glutInitWindowSize(iWidth, iHeight); will set the initial windows size, and show the window, glutCreateWindow("Title"); will set the window title.

Next it is usual to do some local initialization, init01(), followed by the setting of the 'callback' functions, glutDisplayFunc(display), glutReshapeFunct(reshape), and glutKeyboardFunc(keyboard). There are of course several other 'callback' functions that can be set, like function keys, mouse movement, etc, not used in the above simple 'Hello World' application.

Finally glutMainLoop(), to run the application. There will normally be no return from this function.

The source below contains some 40 plus examples of OpenGL/GLUT coding... taken form various places...


Solar2 Example

2011-11-25: This is a simple sample program demonstrates how to use a local coordinate method to position parts of a model in relation to other model parts. It draws a simple solar system, with a sun, planet and moon, and outputs a simple text frame rate. It was based on sample code from the OpenGL programming guide by Woo, Neider, Davis, Addison-Wesley, author  Samuel R. Buss, and the high resolution timer and FPS text output on code by Song Ho Ahn, copyright (c) 2003.

Full source is available in the table below. The linux/unix and windows source are in fact exactly the SAME.



*** As always, take care downloading and running executables from the web! ;=(( ***

Usually sample code is given in separate application, but here I have put it all in ONE application, ogl02.exe. ogl02.exe is run giving the number of the application sample to run - there are over 40 samples/examples... : contains ogl02.exe, the main WIN32 runtime executable : contains the full source, including MSVC8 build files, and unix automake files.

Date Zip Size MD5
2009-06-22 91,283 5b8fdd85ef94a6d75e866949a530a0bf
2009-06-22 287,084 b6956955999f24e6515d2545004fbbdf
Original WIN32 only version
2009-06-19 286,457 d1be72752344c40327065d2a448c8290
2009-06-19 162,362 1bcb24934ef7d2d28a18af23ebc1f954

The updated source of 22nd has been tested in Ubuntu 64-bit linux, with the same functionality, once compiled... it has a '' shell, but this MUST be adjusted to suit your environment. Else the usual unix mantra - ./ && ./configure [--prefix=PREFIX] [--with-plib=<PATH>] && make && make install ...

2011-11-25: Added this specific example - Some downloads:
solar2-0.0.1.tar.gz: Linux/Unix full source Windows source (but is essentially the same as the above) Just a solar2.exe binary executable for Windows The ORIGINAL source, on which this solar2 was based.

Date Link Size MD5
2011/11/25 solar2-0.0.1.tar.gz 116,006 ebc4ce400bba687ac5424ca8984dad3b
2011/11/25 126,654 8a8bacd8a3952c8864dc42076a2c4f8e
2011/11/25 117,637 aecb869191f05977c83068b65f078e08
2002/07/24 22,557 4258fe229d64a25f1d5370154541a294

This is a good example of drawing objects relative to one another with an earth revolving around a sun, and a moon revolving around the earth. A left and right mouse click and move will change the orientation and depth resp. of the 'system', setting the 'camera' angle and depth.


checked by tidy  Valid HTML 4.01 Transitional