问题描述
该程序使用GLAux(OpenGL辅助库)库,该库是GLUT库的一部分,现在已过时.我们需要使用GLUT重写程序.
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);
}
推荐答案
该程序使用glaux库,它是GLUT库的一部分,现在已经过时了.我们需要使用GLUT重写程序.
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.
我不是OpenGL专家,但根据我的回忆,GLAUX从来都不是GLUT的一部分.后者旨在替代GLAUX.
窃听显示 [ ^ ]认为在此示例程序中,对GLAUX库的许多调用可以用等效的GLUT函数替换,但有两个例外.
第一个是 auxKeyFunc(),其中每个调用将一个事件关联到一个键. GLUT使用函数 glutKeyboardFunc()/ glutKeyboardUpFunc()来读取大多数键,并使用 glutSpecialFunc()</i>/< i> glutSpecialUpFunc()剩下的.在每个语句的内部,都有一个 switch 语句用于解码按键并相应地进行操作.
第二个是对 auxWireSphere()的调用. GLAUX版本仅需要半径; 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:
<pre>// =======================================================================<br />
// solar.c<br />
// <br />
// Program to demonstrate how to use a local<br />
// coordinate method to position parts of a<br />
// model in relation to other model parts.<br />
// <br />
// Pressing the "a" key toggles the animation<br />
// Pressing the up and down arrow keys will<br />
// increase/decrease the animation rate<br />
// <br />
// =======================================================================<br />
<br />
#define WIN32_LEAN_AND_MEAN<br />
#define WIN32_EXTRA_LEAN<br />
#include "glut.h"<br />
<br />
// -----------------------------------------------------------------------<br />
// define the keyboard values GLUT doesn''t<br />
// -----------------------------------------------------------------------<br />
#define ESCAPE_KEY 0x1B<br />
#define SPACE_KEY 0x20<br />
<br />
// -----------------------------------------------------------------------<br />
// global flag to determine animation state<br />
// -----------------------------------------------------------------------<br />
static GLenum spinMode = GL_TRUE;<br />
<br />
static int HourOfDay = 0, DayOfYear = 0;<br />
static int AnimateIncrement = 24; // in hours<br />
<br />
// -----------------------------------------------------------------------<br />
// number of slices & stacks to use for the wireframe models<br />
// -----------------------------------------------------------------------<br />
#define NUM_SLICES_STACKS 16<br />
<br />
void KeyboardHandler(unsigned char key, int x, int y)<br />
{<br />
switch (key)<br />
{<br />
// ---------------------------------------------------------------<br />
// exit the application if the user enters <Q> or <ESC><br />
// ---------------------------------------------------------------<br />
case ESCAPE_KEY:<br />
case ''Q'':<br />
case ''q'':<br />
exit(0);<br />
break;<br />
<br />
// ---------------------------------------------------------------<br />
// start and stop the animation<br />
// ---------------------------------------------------------------<br />
case SPACE_KEY:<br />
case ''A'':<br />
case ''a'':<br />
spinMode = !spinMode;<br />
break;<br />
<br />
// ---------------------------------------------------------------<br />
// speed up the animation<br />
// ---------------------------------------------------------------<br />
case GLUT_KEY_UP:<br />
case ''U'':<br />
case ''u'':<br />
if (AnimateIncrement == 0)<br />
AnimateIncrement = 1;<br />
else<br />
AnimateIncrement *= 2;<br />
break;<br />
<br />
// ---------------------------------------------------------------<br />
// slow down the animation<br />
// ---------------------------------------------------------------<br />
case GLUT_KEY_DOWN:<br />
case ''D'':<br />
case ''d'':<br />
AnimateIncrement /= 2;<br />
break;<br />
}<br />
<br />
glutPostRedisplay();<br />
}<br />
<br />
void SpecialKeyboardHandler(int key, int x, int y)<br />
{<br />
switch (key)<br />
{<br />
case GLUT_KEY_UP:<br />
KeyboardHandler(GLUT_KEY_UP, x, y);<br />
break;<br />
<br />
case GLUT_KEY_DOWN:<br />
KeyboardHandler(GLUT_KEY_DOWN, x, y);<br />
break;<br />
}<br />
}<br />
<br />
void Animate (void)<br />
{<br />
// clear the redering window<br />
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br />
<br />
if (spinMode) {<br />
// calc animation parameters<br />
HourOfDay += AnimateIncrement;<br />
DayOfYear += HourOfDay/24;<br />
<br />
HourOfDay = HourOfDay%24;<br />
DayOfYear = DayOfYear%365;<br />
}<br />
<br />
// clear current matrix (Modelview)<br />
glLoadIdentity ();<br />
<br />
// back off six units<br />
glTranslatef (0.0f, 0.0f, -5.0f);<br />
<br />
// rotate the plane of the elliptic<br />
// (rotate the model''s plane about the<br />
// x axis by five degrees)<br />
glRotatef (5.0f, 1.0f, 0.0f, 0.0f);<br />
<br />
// draw the sun<br />
glColor3f (1.0, 0.75, 0.0);<br />
glutWireSphere (1.0, NUM_SLICES_STACKS, NUM_SLICES_STACKS);<br />
<br />
// draw the Earth<br />
glRotatef ((GLfloat) (360.0*DayOfYear/365.0), 0.0, 1.0, 0.0);<br />
glTranslatef (4.0, 0.0, 0.0);<br />
glPushMatrix (); // save matrix state<br />
glRotatef ((GLfloat) (360.0*HourOfDay/24.0), 0.0, 1.0, 0.0);<br />
glColor3f (0.2, 0.2, 1.0);<br />
glutWireSphere (0.2, NUM_SLICES_STACKS, NUM_SLICES_STACKS);<br />
glPopMatrix (); // restore matrix state<br />
<br />
// draw the moon<br />
glRotatef ((GLfloat) (360.0*12.5*DayOfYear/365.0), 0.0, 1.0, 0.0);<br />
glTranslatef (0.5, 0.0, 0.0);<br />
glColor3f (0.3, 0.3, 0.3);<br />
glutWireSphere (0.05, NUM_SLICES_STACKS, NUM_SLICES_STACKS);<br />
<br />
// flush the pipeline, swap the buffers<br />
glFlush ();<br />
glutSwapBuffers ();<br />
}<br />
<br />
<br />
// -----------------------------------------------------------------------<br />
// initialize OpenGL<br />
// -----------------------------------------------------------------------<br />
void OpenGLInit (void)<br />
{<br />
glShadeModel (GL_FLAT);<br />
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);<br />
glClearDepth (1.0f);<br />
glDepthFunc (GL_LEQUAL);<br />
glEnable (GL_DEPTH_TEST);<br />
}<br />
<br />
<br />
// -----------------------------------------------------------------------<br />
// called when the window is resized<br />
// -----------------------------------------------------------------------<br />
void ResizeWindow (int w, int h)<br />
{<br />
h = (h == 0) ? 1 : h;<br />
w = (w == 0) ? 1 : w;<br />
<br />
glViewport (0, 0, (GLsizei) w, (GLsizei) h);<br />
glMatrixMode (GL_PROJECTION);<br />
glLoadIdentity ();<br />
gluPerspective (45.0, (GLfloat) w / (GLfloat) h, 1.0f, 20.0f);<br />
<br />
// -------------------------------------------------------------------<br />
// select the Modelview matrix<br />
// -------------------------------------------------------------------<br />
glMatrixMode (GL_MODELVIEW);<br />
}<br />
<br />
<br />
// -----------------------------------------------------------------------<br />
// main routine<br />
// set up OpenGL, hook up callbacks<br />
// and start the main loop<br />
// -----------------------------------------------------------------------<br />
int main (int argc, char** argv)<br />
{<br />
// -------------------------------------------------------------------<br />
// setup basic GLUT stuff<br />
// -------------------------------------------------------------------<br />
glutInit(&argc, argv);<br />
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);<br />
<br />
// -------------------------------------------------------------------<br />
// create the window<br />
// -------------------------------------------------------------------<br />
glutInitWindowSize(620, 160);<br />
glutInitWindowPosition(100, 150);<br />
glutCreateWindow("Solar System Example (GLUT)");<br />
<br />
// -------------------------------------------------------------------<br />
// Initialize OpenGL as we like it..<br />
// -------------------------------------------------------------------<br />
OpenGLInit ();<br />
<br />
// -------------------------------------------------------------------<br />
// set up callback functions<br />
// -------------------------------------------------------------------<br />
glutDisplayFunc(Animate);<br />
glutIdleFunc(Animate);<br />
glutReshapeFunc(ResizeWindow);<br />
glutKeyboardFunc(KeyboardHandler);<br />
glutSpecialFunc(SpecialKeyboardHandler);<br />
<br />
// -------------------------------------------------------------------<br />
// start handling events until the application is exited<br />
// -------------------------------------------------------------------<br />
glutMainLoop ();<br />
<br />
// -------------------------------------------------------------------<br />
// due to the exit() in the key events, this is never called.<br />
// -------------------------------------------------------------------<br />
return (0);<br />
}<br />
</pre>
这篇关于在不使用过时的GLAux库的情况下重写程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!