//////////////////////////////////////////////////////////////
//自由落体
///////////////////////////////////////////////////////////////////////////////
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#include "GL/glut.h"
#include"iostream.h"
static int up = 0, down = 1;
static double height=4.5;
void init()
{
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_DEPTH_TEST );//打开深度缓存
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(0.0,0.0,0.0, 0.0,0.0,-100.0, 0.0,1.0,0.0);
glPushMatrix();
glTranslatef( 0.0, 0.0, -13.0 );
glTranslatef(0.0,(GLdouble)height,0.0);
glutSolidSphere( 0.4, 40, 40 );
// if(flag==0)
if(down == 1)
{
height-=0.5;
}
if(up ==1)
{
height+=0.5;
}
// glTranslated( 0.0, height, 0.0 );
if(height <= -4.5)
{
up = 1;
height = -4.5;
down = 0;
}
if(height>=4.5)
{
up = 0;
down = 1;
height = 4.5;
}
glPopMatrix();
glutSwapBuffers();
}
void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 40.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0 );
}
void TimerFunc(int value) {
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, 1); //seconds, func, para
}
/*void mouse( int button, int state, int x, int y )
{
switch ( button )
{
case GLUT_LEFT_BUTTON:
if ( state == GLUT_DOWN )
{
spin = ( spin + 45 ) % 360;
glutPostRedisplay();
}
break;
default:
break;
}
}*/
void main( )
{
glutInitDisplayMode( GLUT_RGB |GLUT_DOUBLE| GLUT_DEPTH );// GLUT_SINGLE
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 500, 500 );
glutCreateWindow("实例");
init();
glutDisplayFunc( display );
// glutMouseFunc( mouse );
glutReshapeFunc( reshape );
glutTimerFunc(500, TimerFunc, 1);
glutMainLoop();
return;
}