Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C OpenGL graphics
The program uses GLAux (OpenGL Auxiliary Library) library, which was a part of the GLUT library and is now obsolete. We need to rewrite the program using GLUT.
 
/**************************************************
   solar.c
 

   Program to demonstrate how to use a local
   coordinate method to position parts of a 
   model in relation to other model parts.
   
   Pressing the "a" key toggles the animation
   Pressing the up and down arrow keys will
   increase/decrease the animation rate
 

**************************************************/
 

#include "glos.h" // MS specifc stuff

 
#include  // system OpenGL includes
#include 
#include 
 

static GLenum spinMode = GL_TRUE;
 

void OpenGLInit(void);
 

static void CALLBACK Animate(void );
static void CALLBACK Key_a(void );
static void CALLBACK Key_up(void );
static void CALLBACK Key_down(void );
static void CALLBACK ResizeWindow(GLsizei w, GLsizei h);
 

static int HourOfDay = 0, DayOfYear = 0;
static int AnimateIncrement = 24; // in hours

 
static void CALLBACK Key_a(void)
{
    spinMode = !spinMode;
}
 

static void CALLBACK Key_up(void)
{
    AnimateIncrement *= 2;
if ( 0 == AnimateIncrement )
AnimateIncrement = 1;
}
 

static void CALLBACK Key_down(void)
{
    AnimateIncrement /= 2;
}
 

static void CALLBACK Animate(void)
{
// clear the redering window
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 

    if (spinMode)
{
// calc animation parameters
        HourOfDay += AnimateIncrement;
        DayOfYear += HourOfDay/24;
 

        HourOfDay = HourOfDay%24;
        DayOfYear = DayOfYear%365;
}
// clear current matrix (Modelview)
    glLoadIdentity();
// back off six units 
    glTranslatef ( 0.0f, 0.0f, -5.0f );
// rotate the plane of the elliptic
// (rotate the model's plane about the
// x axis by five degrees)
glRotatef( 5.0f, 1.0f, 0.0f, 0.0f );
 

    // draw the sun
    glColor3f( 1.0, 1.0, 1.0 );
    auxWireSphere( 1.0 );
 

    // draw the Earth
    glRotatef( (GLfloat)(360.0*DayOfYear/365.0),
0.0, 1.0, 0.0 );
    glTranslatef( 4.0, 0.0, 0.0 );
    glPushMatrix(); // save matrix state
glRotatef( (GLfloat)(360.0*HourOfDay/24.0) 
 , 0.0, 1.0, 0.0 );
    glColor3f( 0.2, 0.2, 1.0 );
    auxWireSphere( 0.2 );
    glPopMatrix(); // restore matrix state

 
   glRotatef( (GLfloat)(360.0*12.5*DayOfYear/365.0),
0.0, 1.0, 0.0 );
    glTranslatef( 0.5, 0.0, 0.0 );
    glColor3f( 0.3, 0.3, 0.3 );
    auxWireSphere( 0.05 );
 

// flush the pipeline, swap the buffers
    glFlush();
    auxSwapBuffers();
 

}
 

// initialize OpenGL
void OpenGLInit(void)
{
    glShadeModel( GL_FLAT );
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClearDepth( 1.0f );
glDepthFunc( GL_LEQUAL );
    glEnable( GL_DEPTH_TEST );
}
 

// called when the window is resized
static void CALLBACK ResizeWindow(GLsizei w, GLsizei h)
{
    h = (h == 0) ? 1 : h;
w = (w == 0) ? 1 : w;
glViewport( 0, 0, w, h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 45.0, (GLfloat)w/(GLfloat)h, 1.0f, 20.0f );
// select the Modelview matrix
    glMatrixMode( GL_MODELVIEW );
}
 

// main routine
// set up OpenGL, hook up callbacks
// and start the main loop
int main( int argc, char** argv )
{
// we're going to animate it, so double buffer 
    auxInitDisplayMode(AUX_DOUBLE | AUX_RGB );
    auxInitPosition( 0, 0, 620, 160 );
    auxInitWindow( "Solar System Example" );
 

// Initialize OpenGL as we like it..
    OpenGLInit();
 

// set up callback functions
    auxKeyFunc( AUX_UP, Key_up ); // faster
    auxKeyFunc( AUX_DOWN, Key_down ); // slower
auxKeyFunc( AUX_a, Key_a ); //animate
    auxReshapeFunc( ResizeWindow );
 

// call this when idle
    auxIdleFunc( Animate );
// call this in main loop
auxMainLoop( Animate );
 

    return(0);
}
Posted 31-Jan-10 19:01pm
Edited 27-May-10 13:56pm
Moak11.4K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

stanley John wrote:
The program uses glaux library, which was a part of the GLUT library and is now obsolete. we need to rewrite the program using GLUT.

 
I'm not an OpenGL expert but from what I recall, GLAUX was never a part of GLUT; the latter was intended as a replacement for GLAUX.
 
Some sleuthing reveals[^] that many of the calls to the GLAUX library in this example program can be replaced with equivalent GLUT functions with two exceptions.
 
The first is auxKeyFunc() where each call wires an event to a single key. GLUT uses the function glutKeyboardFunc()/glutKeyboardUpFunc() to read most keys and glutSpecialFunc()</i>/<i>glutSpecialUpFunc() for the rest. Inside of each, a switch statement is used to decode the keystroke and proceed acoordingly.
 
The second is the call to auxWireSphere(). The GLAUX version requires only the radius; GLUT requires radius, number of slices (longitude), & number of stacks (latitude).
 
With that in mind, here's my solution:
 
// =======================================================================
// solar.c
// 
// Program to demonstrate how to use a local
// coordinate method to position parts of a
// model in relation to other model parts.
// 
// Pressing the "a" key toggles the animation
// Pressing the up and down arrow keys will
// increase/decrease the animation rate
// 
// =======================================================================

#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include "glut.h"
 
// -----------------------------------------------------------------------
// define the keyboard values GLUT doesn't
// -----------------------------------------------------------------------
#define ESCAPE_KEY	0x1B
#define SPACE_KEY   0x20
 
// -----------------------------------------------------------------------
// global flag to determine animation state
// -----------------------------------------------------------------------
static GLenum spinMode = GL_TRUE;
 
static int HourOfDay = 0, DayOfYear = 0;
static int AnimateIncrement = 24; // in hours

// -----------------------------------------------------------------------
// number of slices & stacks to use for the wireframe models
// -----------------------------------------------------------------------
#define NUM_SLICES_STACKS	16
 
void KeyboardHandler(unsigned char key, int x, int y)
{
	switch (key)
	{
		// ---------------------------------------------------------------
		// exit the application if the user enters <Q> or <ESC>
		// ---------------------------------------------------------------
		case ESCAPE_KEY:
		case 'Q':
		case 'q':
			exit(0);
			break;
 
		// ---------------------------------------------------------------
		// start and stop the animation
		// ---------------------------------------------------------------
		case SPACE_KEY:
		case 'A':
		case 'a':
			spinMode = !spinMode;
			break;
 
		// ---------------------------------------------------------------
		// speed up the animation
		// ---------------------------------------------------------------
		case GLUT_KEY_UP:
		case 'U':
		case 'u':
			if (AnimateIncrement == 0)
				AnimateIncrement = 1;
			else
				AnimateIncrement *= 2;
			break;
 
		// ---------------------------------------------------------------
		// slow down the animation
		// ---------------------------------------------------------------
		case GLUT_KEY_DOWN:
		case 'D':
		case 'd':
			AnimateIncrement /= 2;
			break;
	}
 
	glutPostRedisplay();
}
 
void SpecialKeyboardHandler(int key, int x, int y)
{
	switch (key)
	{
		case GLUT_KEY_UP:
			KeyboardHandler(GLUT_KEY_UP, x, y);
			break;
 
		case GLUT_KEY_DOWN:
			KeyboardHandler(GLUT_KEY_DOWN, x, y);
			break;
	}
}
 
void Animate (void)
{
	// clear the redering window
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
	if (spinMode) {
		// calc animation parameters
		HourOfDay += AnimateIncrement;
		DayOfYear += HourOfDay/24;
 
		HourOfDay = HourOfDay%24;
		DayOfYear = DayOfYear%365;
	}
 
	// clear current matrix (Modelview)
	glLoadIdentity ();
 
	// back off six units
	glTranslatef (0.0f, 0.0f, -5.0f);
 
	// rotate the plane of the elliptic
	// (rotate the model's plane about the
	// x axis by five degrees)
	glRotatef (5.0f, 1.0f, 0.0f, 0.0f);
 
	// draw the sun
	glColor3f (1.0, 0.75, 0.0);
	glutWireSphere (1.0, NUM_SLICES_STACKS, NUM_SLICES_STACKS);
 
	// draw the Earth
	glRotatef ((GLfloat) (360.0*DayOfYear/365.0), 0.0, 1.0, 0.0);
	glTranslatef (4.0, 0.0, 0.0);
	glPushMatrix (); // save matrix state
	glRotatef ((GLfloat) (360.0*HourOfDay/24.0), 0.0, 1.0, 0.0);
	glColor3f (0.2, 0.2, 1.0);
	glutWireSphere (0.2, NUM_SLICES_STACKS, NUM_SLICES_STACKS);
	glPopMatrix (); // restore matrix state

	// draw the moon
	glRotatef ((GLfloat) (360.0*12.5*DayOfYear/365.0), 0.0, 1.0, 0.0);
	glTranslatef (0.5, 0.0, 0.0);
	glColor3f (0.3, 0.3, 0.3);
	glutWireSphere (0.05, NUM_SLICES_STACKS, NUM_SLICES_STACKS);
 
	// flush the pipeline, swap the buffers
	glFlush ();
	glutSwapBuffers ();
}
 

// -----------------------------------------------------------------------
// initialize OpenGL
// -----------------------------------------------------------------------
void OpenGLInit (void)
{
	glShadeModel (GL_FLAT);
	glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth (1.0f);
	glDepthFunc (GL_LEQUAL);
	glEnable (GL_DEPTH_TEST);
}
 

// -----------------------------------------------------------------------
// called when the window is resized
// -----------------------------------------------------------------------
void ResizeWindow (int w, int h)
{
	h = (h == 0) ? 1 : h;
	w = (w == 0) ? 1 : w;
 
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluPerspective (45.0, (GLfloat) w / (GLfloat) h, 1.0f, 20.0f);
 
	// -------------------------------------------------------------------
	// select the Modelview matrix
	// -------------------------------------------------------------------
	glMatrixMode (GL_MODELVIEW);
}
 

// -----------------------------------------------------------------------
// main routine
// set up OpenGL, hook up callbacks
// and start the main loop
// -----------------------------------------------------------------------
int main (int argc, char** argv)
{
	// -------------------------------------------------------------------
	// setup basic GLUT stuff
	// -------------------------------------------------------------------
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 
	// -------------------------------------------------------------------
	// create the window
	// -------------------------------------------------------------------
	glutInitWindowSize(620, 160);
	glutInitWindowPosition(100, 150);
	glutCreateWindow("Solar System Example (GLUT)");
 
	// -------------------------------------------------------------------
	// Initialize OpenGL as we like it..
	// -------------------------------------------------------------------
	OpenGLInit ();
 
	// -------------------------------------------------------------------
	// set up callback functions
	// -------------------------------------------------------------------
	glutDisplayFunc(Animate);
	glutIdleFunc(Animate);
	glutReshapeFunc(ResizeWindow);
	glutKeyboardFunc(KeyboardHandler);
	glutSpecialFunc(SpecialKeyboardHandler);
 
	// -------------------------------------------------------------------
	// start handling events until the application is exited
	// -------------------------------------------------------------------
	glutMainLoop ();
 
	// -------------------------------------------------------------------
	// due to the exit() in the key events, this is never called.
	// -------------------------------------------------------------------
	return (0);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 27 May 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100