#include<windows.h>
#include<gl/glut.h>
#include<gl/gl.h>
#include<gl/glu.h> //参数指定正方形的位置和大小
GLfloat x1=100.0f;
GLfloat y1=150.0f;
GLsizei rsize=; //正方形运动变化的步长
GLfloat xstep=1.0f;
GLfloat ystep=1.0f; //窗口的大小
GLfloat windowWidth;
GLfloat windowHeight; //画的物体
void DrawDUA();
void DrawTR(); //属性开关
void SunShine(void);//光照 //三维
GLfloat rtri;
GLfloat rquad; //开关
BOOL Draw3D=true;//三维动画演示开关
BOOL Draw2D=false;//二维动画演示开关
BOOL DrawAtoms=false;//原子动画演示开关 //三维属性开关
BOOL AS=true;//透视投影开关
BOOL OR=false;//正交平行投影
BOOL LIGHT=true;//光照开关 void RenderScene(void)
{
if(Draw3D)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(LIGHT)
SunShine(); glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f); DrawTR(); glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rquad,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f); DrawDUA(); rtri+=1.0f;
rquad-=0.5f;
} if(Draw2D)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rsize,y1+rsize);
} if(DrawAtoms)
{
//绕核旋转角度
static float fElect1=0.0f; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //重置模型视图矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(LIGHT)
SunShine(); //将图形沿Z轴负向移动
glTranslatef(0.0f,0.0f,-250.0f); //绘制红色原子核
glColor3f(1.0f,0.0f,0.0f);
glutWireSphere(10.0f,,); //绘制颜色变成绿色
glColor3f(0.0f,1.0f,0.0f); //绘制第一个电子
//保存当前的模型视图矩阵
glPushMatrix();
glRotatef(fElect1,0.0f,1.0f,0.0f);//绕y轴旋转一定角度
glTranslatef(90.0f,0.0f,0.0f);//平移一段距离
glutSolidSphere(6.0f,,);//画出电子 //恢复矩阵
glPopMatrix();
glColor3f(0.0f,0.0f,1.0f);//绘制颜色变成蓝色
//第二个电子
glPushMatrix();
glRotatef(45.0f,0.0f,0.0f,1.0f);
glRotatef(fElect1,0.0f,1.0f,0.0f);
glTranslatef(-70.0f,0.0f,0.0f);
glutSolidSphere(6.0f,,);
glPopMatrix(); glColor3f(1.0f,1.0f,0.0f);//绘制颜色变成黄色
//第三个电子
glPushMatrix();
glRotatef(-45.0f,0.0f,0.0f,1.0f);
glRotatef(fElect1,0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,60.0f);
glutSolidSphere(6.0f,,);
glPopMatrix();
fElect1+=10.0f;
if(fElect1>360.0f)
fElect1=10.0f;
}
glutSwapBuffers();
} void ChangeSize(GLsizei w,GLsizei h)
{
if(h==)
h==; //设置视区尺寸
glViewport(,,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); if(Draw3D||DrawAtoms)
{
//修剪空间(透视投影)
if(AS)
{
GLfloat fAspect;
fAspect =(float)w/(float)h;
gluPerspective(45.0,fAspect,1.0,500.0);
} //(正交平行投影)
if(OR)
{
if(w<=h)
glOrtho(-2.25,2.25,-2.25*h/w,2.25*h/w,-10.0,10.0);
else
glOrtho(-2.25*h/w,2.25*h/w,-2.25,2.25,-10.0,10.0);
} } if(Draw2D)
{
if(w<=h)
{
windowHeight=250.0f*h/w;
windowWidth=250.0f;
}
else
{
windowWidth=250.0f*w/h;
windowHeight=250.0f;
} glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f);
} glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} void TimerFunction(int value)
{
if(x1>windowWidth-rsize||x1<)
xstep=-xstep;
if(y1>windowHeight-rsize||y1<)
ystep=-ystep; x1+=xstep;
y1+=ystep; glutPostRedisplay();
glutTimerFunc(,TimerFunction,);
} void SetupRC(void)
{
if(Draw3D||DrawAtoms)
{
glEnable(GL_DEPTH_TEST);//启用深度测试
glFrontFace(GL_CCW);
}
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void TimerFunc(int value)
{
glutPostRedisplay();
if(Draw3D)
{
glutTimerFunc(,TimerFunc,);
}
if(DrawAtoms)
{
glutTimerFunc(,TimerFunc,);
} }
int main(int argc,char* argv[])
{
if(Draw2D)
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(,TimerFunction,);
}
if(Draw3D||DrawAtoms)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("原子示例");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
if(DrawAtoms)
glutTimerFunc(,TimerFunc,);
else
glutTimerFunc(,TimerFunc,);
} SetupRC();
glutMainLoop();
}
void DrawTR()
{
glBegin(GL_TRIANGLES);
//
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f); //
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f); //
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); //
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,1.0f); glEnd();
} void DrawDUA()
{
glBegin(GL_QUADS); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f); glColor3f(1.0f,0.5f,0.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f); glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f); glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glEnd();
} void SunShine(void)
{ GLfloat sun_light_position[]={0.0f,0.0f,0.0f,1.0f};
GLfloat sun_light_ambient[]={0.0f,0.0f,0.0f,1.0f};
GLfloat sun_light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
GLfloat sun_light_specular[]={1.0f,1.0f,1.0f,1.0f}; glLightfv(GL_LIGHT0,GL_POSITION,sun_light_position);
glLightfv(GL_LIGHT0,GL_AMBIENT,sun_light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sun_light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,sun_light_specular); glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}