1.旋转的球
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f( 1, 0, 0 );
glPushMatrix();
glTranslated(0,0,-3);
glRotated(60,30,-15,0);//倾斜
glRotated(step,0,1,1);//旋转轴
glutWireSphere(1,16,16);
glPopMatrix();
glutSwapBuffers();
}
void resize(int width, int height)
{
float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
step -= 5.0f;
if(key == GLUT_KEY_DOWN)
step += 5.0f;
step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(1,1,1,1);
glutMainLoop();
return 0;
}
2.旋转的茶壶
#include
#include
static int iDirection = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
switch (iDirection)
{
case 0:
gluLookAt(0.0,0.0,5.0, 0,0,0, 0.0,1.0,0.0);
break;
case 1:
gluLookAt(0.0,0.0,5.0, 0,0,0, 1.0,0.0,0.0);
break;
case 2:
gluLookAt(5.0,0.0,0.0, 0,0,0, 0.0,1.0,0.0);
break;
case 3:
gluLookAt(5.0,0.0,0.0, 0,0,0, 0.0,-1.0,0.0);
break;
default:
gluLookAt(0.0,0.0,5.0, 0,0,0, 0.0,1.0,0.0);
break;
}
glColor3f( 0, 1, 0 );
glutWireTeapot(1);
glutSwapBuffers();
}
void resize(int width, int height)
{
float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
gluPerspective(45.0, ar, 1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void NormalKeys(unsigned char key, int x, int y)
{
switch (key)
{
case '1':
iDirection = 0;
break;
case '2':
iDirection = 1;
break;
case '3':
iDirection = 2;
break;
case '4':
iDirection = 3;
break;
default:
break;
}
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutKeyboardFunc(NormalKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(0,0,0,1);
glutMainLoop();
return 0;
}
3.日地系统
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f( 1, 0, 0 );//中心球
glTranslated(0,0,-80);
glutSolidSphere(5,32,32);
glColor3f(0,0,1);
glPushMatrix();
glRotated(10,0,0,1);
glRotated(step,0,1,0);
glTranslated(0,0,20);
glutSolidSphere(3,32,32);
glPopMatrix();
glutSwapBuffers();
}
void resize(int width, int height)
{
float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
//gluPerspective(45.0, ar, 2.0, 100.0);
//glOrtho (-30.0f, 30.0f, -30.0f/ar, 30.0f/ar,2.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void NormalKeys(unsigned char key, int x, int y)
{
switch(key)
{
case '1':
step += 5.0f;
break;
case '2':
step -= 5.0f;
break;
case 'z':
glEnable(GL_DEPTH_TEST);
break;
case 'x':
glDisable(GL_DEPTH_TEST);
break;
default:
break;
}
step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutKeyboardFunc(NormalKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
例2
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f( 1, 1, 0 );
glTranslated(0,0,-80);
glutSolidSphere(5,32,32);
glColor3f(0,0,1);
glPushMatrix();
glRotated(10,0,0,1);
glRotated(step,0,1,0);
glTranslated(0,0,20);
glutSolidSphere(3,32,32);
glPopMatrix();
glutSwapBuffers();
}
void resize(int width, int height)
{
float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
//gluPerspective(45.0, ar, 2.0, 100.0);
//glOrtho (-30.0f, 30.0f, -30.0f/ar, 30.0f/ar,2.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void animation(void)
{
step += 1.0f;//翻转速度
step = (GLfloat)((int)step % 360);//翻转角度
glutPostRedisplay();
}
/*
void NormalKeys(unsigned char key, int x, int y)
{
switch(key)
{
case '1':
step += 5.0f;
break;
case '2':
step -= 5.0f;
break;
case 'z':
glEnable(GL_DEPTH_TEST);
break;
case 'x':
glDisable(GL_DEPTH_TEST);
break;
default:
break;
}
step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}*/
int main( int argc, char ** argv )
{/*
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
//glutKeyboardFunc(NormalKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
*/
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 1000, 1000 );//画布大小
glutCreateWindow( "OpenGL Examples!" );
glutDisplayFunc( display );
glutIdleFunc(animation);
glutReshapeFunc(resize);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);//平滑着色
//glShadeModel(GL_SMOOTH);
glutMainLoop();
return 0;
}