#include<gl/glut.h>
#include<math.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
float r1,r2;
void display()
{
glPushMatrix();
glTranslatef(0.0,-0.15,0.0);
glRotatef(r1,0.0,1.0,0.0);//旋转风车
glTranslatef(0.0,0.2,0.0);
glPushMatrix();
glTranslatef(0.0,0.0,0.0);
glScalef(0.6,1.0,0.6);
glPushMatrix();/////////////////////屋顶
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,0.0,0.5);
glVertex3f(0.5,0.0,0.5);
glVertex3f(0.0,0.6,0.0);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(1.0,1.0,0.0);
glVertex3f(0.5,0.0,0.5);
glVertex3f(0.5,0.0,-0.5);
glVertex3f(0.0,0.6,0.0);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0,1.0,0.0);
glVertex3f(0.5,0.0,-0.5);
glVertex3f(-0.5,0.0,-0.5);
glVertex3f(0.0,0.6,0.0);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0,1.0,1.0);
glVertex3f(-0.5,0.0,-0.5);
glVertex3f(-0.5,0.0,0.5);
glVertex3f(0.0,0.6,0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPushMatrix();////////////////////屋檐下面部分
glTranslatef(0.0,-0.5,0.0);
glScalef(0.6,1.0,0.6);
glPushMatrix();
glColor3f(0.3,1.0,0.7);
glutSolidCube(1.0);
glColor3f(1.0,0.0,0.7);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,0.3,0.5);
glRotatef(r2,0.0,0.0,1.0);//旋转风车叶子
glPushMatrix();
for(int i=0;i<4;i++)////////////风车叶子
{
glPushMatrix();
glRotatef((90.0*i),0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0,1.0,0.0);
glVertex3f(0.0,0.0,0.0);
glColor3f(1.0,1.0,0.5);
glVertex3f(0.0,0.5,0.0);
glColor3f(1.0,0.5,0.8);
glVertex3f(0.25,0.6,0.0);
glEnd();
glPopMatrix();
}
glPopMatrix();
glPushMatrix();
glColor3f(0.6,0.8,0.8);
glBegin(GL_QUAD_STRIP);/////////////风车的转轴
for(int i1=0;i1<=390;i1+=30)
{
float p=(float)(i1*3.14/180);
glVertex3f((float)0.05*sin(p)/2,(float)0.05*cos(p)/2,-0.8f );
glVertex3f((float)0.05*sin(p)/2,(float)0.05*cos(p)/2,0.0f );
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0,0.8,0.2);
glVertex3f(0.0,0.0,0.0);
for(int j=0;j<=390;j+=30)
{
float p1=(float)(j*3.14/180);
float p2=(float)((j+1)*3.14/180);
glVertex3f((float)0.05*sin(p1)/2,(float)0.05*cos(p1)/2,0.0f );
glVertex3f((float)0.05*sin(p2)/2,(float)0.05*cos(p2)/2,0.0f );
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0,0.0,0.2);
glVertex3f(0.0,0.0,-0.8);
for(int k=0;k<=390;k+=30)
{
float p3=(float)(k*3.14/180);
float p4=(float)((k+1)*3.14/180);
glVertex3f((float)0.05*sin(p3)/2,(float)0.05*cos(p3)/2,-0.8f );
glVertex3f((float)0.05*sin(p4)/2,(float)0.05*cos(p4)/2,-0.8f );
}
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
void renderScene()//OpenGL图形处理
{
glClearColor(0.1f,0.1f,0.2f,0.0f); // 设置刷新背景色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 刷新背景
//glEnable(GL_DEPTH_TEST);//允许深度测试
glLoadIdentity(); // 重置当前的模型观察矩阵
display();
glFlush(); // 更新窗口
glutSwapBuffers ( ); // 切换缓冲区
r1+=0.1;if(r1>360) r1=0;
r2+=2;if(r2>360) r2=0;
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE|GLUT_DEPTH);//GLUT_SINGLE
glutInitWindowPosition(350,50);
glutInitWindowSize(750,600);
glutCreateWindow("风车");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
}
评论0