计算机图形学设计人物派大星,以下是参考代码。
需要函数库的私信我。
运行环境:VC 6.0
引擎库:OpenGL
完整代码:
#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[] = {10.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[] = {0.99, 0.4, 0.4},
black[] ={0.0, 0.0, 0.0},
silver[] = {0.8, 0.8, 0.8};
GLfloat green[] = {0.16, 0.59, 0.35},
white[] = {1.0, 1.0, 1.0},
yellow[] ={1.0, 1.0, 0.0};
GLfloat ye[] ={0.6, 0.95, 0.2};
GLfloat mat_shininess[] = {50.0};
GLUquadric *LV, *RV, *H, *LP, *RP;
void myinit(void)
{
//set up overall light data, including specular=ambient=light colors
glClearColor(0.874, 0.52, 0.56, 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);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
//头
void personHead(void)
{
glPushMatrix();
glTranslatef(0.0,0.0,20.0);
glScalef(1.0,1.0,2.0);
glutSolidSphere(1.5,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,0.0,17.0);
glScalef(1,1,2);
glutSolidSphere(2,26,26);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,0.0,14.0);
glScalef(1,1,2);
glutSolidSphere(2.5,32,32);
glPopMatrix();
}
//身体
void personBody2(void)
{
glPushMatrix();
glTranslatef(0.0,0.0,8.0);
glScalef(1,1,1);
glutSolidSphere(5,30,30);
glPopMatrix();
}
void personBody3(void)
{
glPushMatrix();
glTranslatef(0.0,0.0,8.3);
glScalef(1,1,1);
glutSolidSphere(5.1,30,30);
glPopMatrix();
}
void personBody(void)
{
glPushMatrix();
glTranslatef(0.0,0.0,11.6);
glScalef(1.0,1.0,1);
glutSolidSphere(3.5,20,20);
glPopMatrix();
}
//左胳膊
void personArmLeft(void)
{
glPushMatrix();
glTranslatef(5.2,0.0,10.0);
glRotatef(130,0,1,0);
glScalef(1.0,1.0,3);
glutSolidSphere(1.2,20,20);
glPopMatrix();
}
//左腿
void personLegLeft(void)
{
glPushMatrix();
//glRotatef(170,0,1,0);
glTranslatef(-2,0.0,2.5);
glScalef(1.0,1.0,2.5);
glutSolidSphere(1.2,20,20);
glPopMatrix();
}
void personLegLeft2(void)
{
glPushMatrix();
//glRotatef(170,0,1,0);
glTranslatef(-2,0.0,4.2);
glScalef(1.0,1.0,2.5);
glutSolidSphere(1.4,20,20);
glPopMatrix();
}
//右胳膊
void personArmRight(void)
{
glPushMatrix();
glTranslatef(-5.2,0.0,10.0);
glRotatef(-130,0,1,0);
glScalef(1.0,1.0,3);
glutSolidSphere(1.2,20,20);
glPopMatrix();
}
//右腿
void personLegRight(void)
{
glPushMatrix();
//glRotatef(-170,0,1,0);
glTranslatef(2,0.0,2.5);
glScalef(1.0,1.0,2.5);
glutSolidSphere(1.2,20,20);
glPopMatrix();
}
void personLegRight2(void)
{
glPushMatrix();
//glRotatef(-170,0,1,0);
glTranslatef(2,0.0,4.2);
glScalef(1.0,1.0,2.5);
glutSolidSphere(1.4,20,20);
glPopMatrix();
}
//眼睛
void personEye(void)
{
glPushMatrix();
glTranslatef(1,1.8,17.5);
glScalef(1.0,1.0,1.5);
glutSolidSphere(1,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-1,1.8,17.5);
glScalef(1.0,1.0,1.5);
glutSolidSphere(1,30,30);
glPopMatrix();
}
void personEye2(void)
{
glPushMatrix();
glTranslatef(1,2.6,17);
glScalef(1.0,1.0,1.5);
glutSolidSphere(0.4,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-1,2.6,17);
glScalef(1.0,1.0,1.5);
glutSolidSphere(0.4,30,30);
glPopMatrix();
}
//嘴巴
void personMouse(void)
{
glPushMatrix();
glTranslatef(0,2,14.3);
glScalef(2.0,1.0,0.1);
glutSolidSphere(0.8,30,30);
glPopMatrix();
}
void display(void)
{
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,-10.0);
glVertex3f(20.0,-10.0,-10.0);
glVertex3f(20.0,10.0,-10.0);
glVertex3f(-20.0,10.0,-10.0);
glEnd();
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
glPushMatrix();
personEye();
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,black);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,black);
glPushMatrix();
personEye2();
personMouse();
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ye);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,ye);
glPushMatrix();
personBody2();
glPushMatrix();
glRotatef(angle2,0.0,0.0,1.0); //W:左腿旋转
glPushMatrix();
personLegLeft2();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angle4,0.0,0.0,1.0); //S:右腿旋转
glPushMatrix();
personLegRight2();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,red);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red);
glPushMatrix();
personHead();
personBody();
personBody3();
glPopMatrix();
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();
personLegLeft2();
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();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.6,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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)
{
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': //右胳膊旋转
angle3 += 10.0;
glutPostRedisplay();
break;
case 'W': //左腿旋转
angle2 += 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;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("光照派大星");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
myinit();
glutMainLoop();
return 0;
}
效果图: