glpushmatrix:我与OpenGL的故事之四:太阳-地球-月亮

 2021-07-12 8:06    77  

glpushmatrix:我与OpenGL的故事之四:太阳-地球-月亮动画

核心代码static void Animate(void)

{

// Clear the redering window

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (spinMode) {

// Update the animation state

HourOfDay += AnimateIncrement;

DayOfYear += AnimateIncrement/24.0;

HourOfDay = HourOfDay - ((int)(HourOfDay/24))*24;

DayOfYear = DayOfYear - ((int)(DayOfYear/365))*365;

}

// Clear the current matrix (Modelview)

glLoadIdentity();

// Back off eight units to be able to view from the origin.

glTranslatef ( 0.0, 0.0, -8.0 );

// Rotate the plane of the elliptic

// (rotate the model's plane about the x axis by fifteen degrees)

glRotatef( 15.0, 1.0, 0.0, 0.0 );

// Draw the sun -- as a yellow, wireframe sphere 太阳

glColor3f( 1.0, 1.0, 0.0 );

glutWireSphere( 1.0, 15, 15 );

// Draw the Earth 地球

// First position it around the sun

// Use DayOfYear to determine its position

glRotatef( 360.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );

glTranslatef( 4.0, 0.0, 0.0 );

glPushMatrix(); // Save matrix state

// Second, rotate the earth on its axis.

// Use HourOfDay to determine its rotation.

glRotatef( 360.0*HourOfDay/24.0, 0.0, 1.0, 0.0 );

// Third, draw the earth as a wireframe sphere.

glColor3f( 0.2, 0.2, 1.0 );

glutWireSphere( 0.4, 10, 10);

glPopMatrix(); // Restore matrix state

// Draw the moon. 月亮

// Use DayOfYear to control its rotation around the earth

glRotatef( 360.0*12.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );

glTranslatef( 0.7, 0.0, 0.0 );

glColor3f( 0.3, 0.7, 0.3 );

glutWireSphere( 0.1, 5, 5 );

// Flush the pipeline, and swap the buffers

glFlush();

glutSwapBuffers();

if ( singleStep ) {

spinMode = GL_FALSE;

}

glutPostRedisplay(); // Request a re-draw for animation purposes

}

// Initialize OpenGL's rendering modes

void OpenGLInit(void)

{

glShadeModel( GL_FLAT );

glClearColor( 0.0, 0.0, 0.0, 0.0 );

glClearDepth( 1.0 );

glEnable( GL_DEPTH_TEST );

}

// ResizeWindow is called when the window is resized

static void ResizeWindow(int w, int h)

{

float aspectRatio;

h = (h == 0) ? 1 : h;

w = (w == 0) ? 1 : w;

glViewport( 0, 0, w, h ); // View port uses whole window

aspectRatio = (float)w/(float)h;

// Set up the projection view matrix (not very well!)

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

gluPerspective( 60.0, aspectRatio, 1.0, 30.0 );

// Select the Modelview matrix

glMatrixMode( GL_MODELVIEW );

本文标签:地球月亮

原文链接:https://www.xgfox.com/jsyd/786.html

本文版权:如无特别标注,本站文章均为原创。