计算机图形学
实验二
实验目的
理解掌握OPENGL图像平移、旋转和缩放的原理,并编程实现;
实验要求给定三维图元(立方体,四面体和球),把图元拼接成象形动物;
要求图元必须按照层级组织;
评分标准
现场演示+黑盒测试+源代码提交。
总分10分,分为3部分:
·拼接象形动物,5分;
·图元的层级组织,5分;
实现思路
本实验实现的为一个简单的机器人。采用的基本方法就是第八章层级建模方法中提到的压栈出栈。第一个push操作把当前的模视变换矩阵的一个备份压入堆栈的顶部。由于在堆栈中保留了当前变换矩阵的一个备份,所以可以立即进行其他的几何变换从而改变当前变换矩阵。在开头使用了push操作而在结尾使用了pop操作,可以防止程序的其他部分受该函数状态变化的影响,从而起到代码隔离的作用。按同样的方法可以编写其余部件的绘制函数。
实现结果
经过层级建模画出来一个机器人,头部采用球体而其他部分采用立方体或四方体组成。整个机器人的架构分为头-->脖子-->躯干-->手臂-->大腿-->小腿-->脚。因为实验未要求进行动画,本实验采用了简单的动画,即让机器人在原地360°循环踏步转圈。效果如图所示:
源码展示
#include
#include
#include "GL/glut.h"
#include
#include
#include
#include
static int shoulder1 = 0;
static int shoulder2 = 0;
static int hand=0;
static int turn1=0;
static int tag=0;
static int turn=0 ;//转弯
static float forward=0;//前进
static float elbow = 0 ,z=0;
int w;
int h;
int font=(int)GLUT_BITMAP_8_BY_13;
char s[30];
int frame,timeOwn,timebase=0;
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
//是否停止转动
bool IsStop=false;
//光照 使用光源
//GLfloat specular[] = { 1.0f, 1.0f, 1.0f,
-1.0f};//反射光
//GLfloat specref[] = { 1.0f,
1.0f, 1.0f, 1.0f };//a
GLfloat ambientLight[] = { 0.5f, 0.5f, 0.5f,
1.0f};//环绕光
GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f };
GLboolean bEdgeFlag = TRUE;
void showText(void);
void resetPerspectiveProjection() ;
void setOrthographicProjection() ;
//.....................................................//
//.....................................................//
void display(void)
{
glClear
(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
if (IsStop==false)
{
turn = (turn - 5) % 360;
if (forward<2)
{
turn1=turn;
forward = forward
-0.04*sin((GLfloat)turn1/360*3.14*2);
z=z-0.05*cos((GLfloat)turn1/360*3.14*2);
if(tag==0){
shoulder1 = (shoulder1 + 1);
shoulder2 = (shoulder2 - 1);
if(shoulder1>=0){elbow=elbow-1.2;}
else{elbow=elbow+1.2;}
}
else
{
shoulder1 = (shoulder1 - 1);
shoulder2 = (shoulder2 + 1);
if(shoulder1>=0){elbow=elbow+1.25;}
else{elbow=elbow-1.2;}
}
if(shoulder1>30){
tag=1;
}
if(shoulder1
tag=0;
}
}else
{
turn1=turn;
forward = forward
+0.04*sin((GLfloat)turn1/360*3.14*2);
z=z+0.05*cos((GLfloat)turn1/360*3.14*2);
if(tag==0){
shoulder1 = (shoulder1 - 1);
shoulder2 = (shoulder2 + 1);
}
else
{
shoulder1 = (shoulder1 + 1);
shoulder2 = (shoulder2 - 1);
}
if(shoulder1>30)
{
tag=0;
}
if(shoulder1
{
tag=1;
}
}
}
//
glBegin(GL_QUADS);
glColor3ub(230,232,0);
glVertex3f(8.0f, -3.0f,
-4.0f);
glVertex3f(-8.0f, -3.0f,
-4.0f);
glColor3ub(255,153,204);
glVertex3f(-8.0f, -3.0f,
4.0f);
glVertex3f(8.0f, -3.0f,
4.0f);
glEnd();
//运动
glPushMatrix();
glTranslatef (forward,0.0,z); //前进
glRotatef ((GLfloat) turn, 0.0, 1.0, 0.0);
glTranslatef (0.375,0.0, 0.0);//提起右大腿
glRotatef ((GLfloat) shoulder2, 1.0, 0.0, 0.0);
glTranslatef (0.0, -0.5, 0.0);
glColor3f(0.8,1.0,0.2);
glPushMatrix();
glScalef (0.5, 1.0, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef (0.0, -0.5, 0.0);//提起右小腿
glRotatef ((GLfloat) elbow, 1.0, 0.0, 0.0);
glTranslatef (0.0, -0.5, 0.0);
glColor3f(0.5,0.1,0.8);
glPushMatrix();
glScalef (0.5, 1.0, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef (0.0, -0.5, -0.1);//右脚
glColor3f(0.5,0.2,1.0);
glPushMatrix();
glScalef (0.5, 0.1, 0.7);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix
();
glPushMatrix();
glTranslatef (forward,0.0,z);
glRotatef ((GLfloat) turn, 0.0, 1.0, 0.0);
glTranslatef (-0.375, 0.0, 0.0);//左大腿
glRotatef ((GLfloat) shoulder1, 1.0, 0.0, 0.0);
glTranslatef (0.0, -0.5, 0.0);
glColor3f(0.8,1.0,0.2);
glPushMatrix();
glScalef (0.5, 1.0, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef (0.0, -0.5, 0.0);//左小腿
glRotatef ((GLfloat) elbow, 1.0, 0.0, 0.0);
glTranslatef (0.0, -0.5, 0.0);
glColor3f(0.5,0.1,0.8);
glPushMatrix();
glScalef (0.5, 1.0, 0.5);//缩放四方体
glutSolidCube(1.0);//画四方体
glPopMatrix();
glTranslatef (0.0, -0.5, -0.1);//左脚
glColor3f(0.5,0.2,1.0);
glPushMatrix();
glScalef (0.5, 0.1, 0.7);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef (forward,0.0,z);
glRotatef ((GLfloat) turn, 0.0, 1.0, 0.0);
glTranslatef (0.0, 1.0, 0.0);//躯干
glColor3f(0.5,0.5,1.0);
glPushMatrix();
glScalef (1.4, 2.0, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef (0.0, 1.20, 0.0);//脖子
glColor3f(0.5,0.5,1.0);
glPushMatrix();
glScalef (0.5, 0.5, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef (forward,0.0,z);
glRotatef
((GLfloat) turn, 0.0, 1.0, 0.0);
glTranslatef (0.0, 2.75, 0.0);//头
glColor3f(0.0,0.0,1.0);
glPushMatrix();
glScalef (0.5, 0.5, 0.5);
glutSolidSphere(1.0,100,100);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef (forward,0.0,z);
glRotatef
((GLfloat) turn, 0.0, 1.0, 0.0);
// glTranslatef (0.0, 2.75, 0.0);//帽子
// glColor3f(0.0,1.0,1.0);
// glPushMatrix();
// glScalef (0.5, 0.5, 0.5);
// glutSolidTetrahedron;
// glPopMatrix();
//
// glPopMatrix();
//
// glPushMatrix();
// glTranslatef (forward,0.0,z);
// glRotatef
((GLfloat) turn, 0.0, 1.0, 0.0);
glTranslatef (0.85, 1.75, 0.0);//右臂
glRotatef ((GLfloat) shoulder1, 1.0, 0.0, 0.0);
glTranslatef (0.0, -0.5, 0.0);
glColor3f(1.0,0.0,1.0);
glPushMatrix();
glScalef (0.3, 1.3, 0.4);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef (forward,0.0,z);
glRotatef ((GLfloat) turn, 0.0, 1.0, 0.0);
//
// glTranslatef (0.0, 1.05, 0.0);//提起右小腿
// glRotatef ((GLfloat) elbow, 1.0, 0.0, 0.0);
// glTranslatef (0.0, -0.5, 0.0);
//
// glColor3f(0.5,0.1,0.8);
// glPushMatrix();
// glScalef (0.5, 1.0, 0.5);
// glutSolidCube(1.0);
// glPopMatrix();
// glTranslatef (-0.85, 1.75, 0.0);//提起右大腿
// glRotatef ((GLfloat) shoulder2, 1.0, 0.0, 0.0);
//
// glTranslatef (0.0, -0.5, 0.0);
// glColor3f(0.8,1.0,0.2);
// glPushMatrix();
// glScalef (0.3, 1.3, 0.4);
// glutSolidCube(1.0);
// glPopMatrix();
// glTranslatef (-1.225, 1.25, 0.0);//提起右小腿
// glRotatef ((GLfloat) elbow, 1.0, 0.0, 0.0);
// glTranslatef (0.0, -0.5, 0.0);
//
// glColor3f(0.5,0.1,0.8);
// glPushMatrix();
// glScalef (0.5, 1.0, 0.5);
// glutSolidCube(1.0);
// glPopMatrix();
glTranslatef (-0.85, 1.75, 0.0);//左臂
glRotatef ((GLfloat) shoulder2, 1.0, 0.0, 0.0);
glTranslatef (0.0, -0.5, 0.0);
glColor3f(1.0,0.0,1.0);
glPushMatrix();
glScalef (0.3, 1.3, 0.4);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
//something
void reshape (int w1, int h1)
{
w=w1;
h=h1;
glViewport (0, 0, (GLsizei)
w1, (GLsizei) h1);
glMatrixMode
(GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat)
w1/(GLfloat) h1, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0,
-8.0);
}
void TimerFunction(int value)
{
display();
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
//主函数
int main(int argc, char** argv)
{
glutInit(&argc,
argv);
glutInitDisplayMode
(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (800,
600);
glutInitWindowPosition (100,
100);
glutCreateWindow("机器人");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(33, TimerFunction, 1);
glutMainLoop();
return 0;
}