#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
static GLfloat angle = 0.0;
static GLfloat angle1 = 0.0;
static GLfloat angle2 = 0.0;
static GLfloat angle3 = 0.0;
static GLfloat angle4 = 0.0;
GLfloat light_position[] = {0.0, 40.0, 6.0, 1.0};
GLfloat light_color[] = {1.0, 1.0, 1.0, 1.0};
GLfloat ambient_color[] = {0.2, 0.2, 0.2, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat red[] = {1.0, 0.0, 0.0, 1.0},black[] ={0.0, 0.0, 0.0, 1.0},
silver[] = {0.8, 0.8, 0.8, 1.0};
GLfloat green[] = {0.0, 1.0, 0.0, 1.0},white[] = {1.0, 1.0, 1.0, 1.0},
yellow[] ={1.0, 1.0, 0.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLUquadric *LV, *RV, *H, *LP, *RP;//left vertical, right vertical, horizontal, left pad, right pad
void myinit(void)
{
//set up overall light data, including specular=ambient=light colors
glClearColor(0.7, 0.7, 0.8, 0.0);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient_color);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_color);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_color);
/*attributes*/
glEnable(GL_LIGHTING);//so lighting models are used
glEnable(GL_LIGHT0); //we'll use LIGHTO
glEnable(GL_DEPTH_TEST);//allow z-buffer display
}
//头
void personHead(void)
{
glPushMatrix();
glTranslatef(0.0,0.0,20.0);
//glScalef(1.0,1.0,2.0);
glutSolidSphere(1.5,20,20);
glPopMatrix();
}
//身体
void personBody(void)
{
glPushMatrix();
glTranslatef(0.0,0.0,11.0);
glScalef(1.0,1.0,2.0);
glutSolidSphere(3.5,20,20);
glPopMatrix();
}
//左胳膊
void personArmLeft(void)
{
glPushMatrix();
glRotatef(150.,0.,1.,0.);
glTranslatef(-11.0,0.0,-10.0);
glScalef(1.0,1.0,3.5);
glutSolidSphere(1,20,20);
glPopMatrix();
}
//左腿
void personLegLeft(void)
{
glPushMatrix();
glRotatef(170.,0.,1.,0.);
glTranslatef(-4.5,0.0,-3.0);
glScalef(1.0,1.0,3.2);
glutSolidSphere(1,20,20);
glPopMatrix();
}
//右胳膊
void personArmRight(void)
{
glPushMatrix();
glRotatef(-150.,0.,1.,0.);
glTranslatef(11.0,0.0,-10.0);
glScalef(1.0,1.0,3.5);
glutSolidSphere(1,20,20);
glPopMatrix();
}
//右腿
void personLegRight(void)
{
glPushMatrix();
glRotatef(-170.,0.,1.,0.);
glTranslatef(4.5,0.0,-3.0);
glScalef(1.0,1.0,3.2);
glutSolidSphere(1,20,20);
glPopMatrix();
}
void display(void)
{
//GLint i;
//clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0.0,0.0,1.0);//rotate model
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
//ground
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,green);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green);
glBegin(GL_QUADS);
glNormal3f(0.0,1.0,0.0);
glVertex3f(-20.0,-10.0,0.0);
glVertex3f(20.0,-10.0,0.0);
glVertex3f(20.0,10.0,0.0);
glVertex3f(-20.0,10.0,0.0);
glEnd();
//rugby ball
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,yellow);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,yellow);
/*
for(i=0;i<10;i++)
{
glPushMatrix();
glTranslatef(-18.0 + 4.1 * (float)i,0.0,
(30.25-((5.5-(float)i)) * (5.5-(float)i))/3.0 +2.0);
glRotatef(-30.0 *(float)i,0.0,1.0,0.0);
glTranslatef(0.0,0.0,-2.0);
rugbyBall();
glPopMatrix();
}
*/
glPushMatrix();
personHead();
personBody();
glPushMatrix();
glRotatef(angle1,0.0,0.0,1.0); //A:左胳膊旋转
glPushMatrix();
personArmLeft();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angle2,0.0,0.0,1.0); //W:左腿旋转
glPushMatrix();
personLegLeft();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angle3,0.0,0.0,1.0); //D:右胳膊旋转
glPushMatrix();
personArmRight();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angle4,0.0,0.0,1.0); //S:右腿旋转
glPushMatrix();
personLegRight();
glPopMatrix();
glPopMatrix();
glPopMatrix();
//goal posts
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,silver);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,silver);
glPushMatrix();
glTranslatef(18.0,3.0,0.0);
LV = gluNewQuadric();
gluCylinder(LV,0.2,0.2,10.0,10,1);
glPopMatrix();
glPushMatrix();
glTranslatef(18.0,-3.0,0.0);
RV = gluNewQuadric();
gluCylinder(RV,0.2,0.2,10.,10,1);
glPopMatrix();
glPushMatrix();
glTranslatef(18.,3.,3.);
H = gluNewQuadric();
gluCylinder(H,0.2,0.2,6.,10,1);
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
glPushMatrix();
glTranslatef(18.,3.,0.);
LP = gluNewQuadric();
gluCylinder(LP,0.3,0.3,1.,10,1);
glPopMatrix();
glPushMatrix();
glTranslatef(18.,-3.,0.);
RP = gluNewQuadric();
gluCylinder(RP,0.3,0.3,1.,10,1);
glPopMatrix();
glPopMatrix();//undo last rotation you set
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,1.0,60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*eye point, center of view, up*/
gluLookAt(0.0,30.0,6.0, 0.0,0.0,6.0, 0.0,0.0,1.0);
}
void keyboard(unsigned char key,int x, int y)
{
/*if(key)
{
angle += 10.0;
glutPostRedisplay();
}*/
switch (key)
{
case 'R': //整体旋转
angle += 10.0;
glutPostRedisplay();
break;
case 'T': //整体平移
glTranslatef(1.0,0.0,0.0);
glutPostRedisplay();
break;
case 'A': //左胳膊旋转
angle1 += 10.0;
glutPostRedisplay();
break;
case 'D': //右胳膊旋转
angle2 += 10.0;
glutPostRedisplay();
break;
case 'W': //左腿旋转
angle3 += 10.0;
glutPostRedisplay();
break;
case 'S': //右腿旋转
angle4 += 10.0;
glutPostRedisplay();
break;
}
}
int main(int argc,char** argv)
{
cout<<"功能键介绍:"<<endl;
cout<<"'R': 整体旋转"<<endl;
cout<<"'T': 整体平移"<<endl;
cout<<"'A': 左胳膊旋转"<<endl;
cout<<"'D': 右胳膊旋转"<<endl;
cout<<"'W': 左腿旋转"<<endl;
cout<<"'S': 右腿旋转"<<endl;
/*Standard GLUT initialization*/
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,800);
glutInitWindowPosition(0,0);
glutCreateWindow("Flyint Rruby Ball");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
myinit();
glutMainLoop();
return 0;
}
|