计算机图形学实验报告
一、实验题目:
《基于 OpenGL 的三维建筑场景模拟》
二、实验内容
绘制建筑整体框架、楼梯、阳台、门
绘制建筑内部家具:沙发、床(枕头+被子)、床头柜、窗帘、衣柜、茶几、
地板、电视、灯等
动作及控制:关、拉窗帘,推拉衣柜门、开关电视、开关灯、开关门、
开关窗户,可全方位控制视野(上楼)
添加光照效果
添加雾化效果
半透明效果:窗户、灯、玻璃茶几、阳台玻璃栏
三、关键技术介绍
控制:
W、S、A、D 对应 视野前、后、左、右
R、T 衣柜左门推拉
Y、U 衣柜右面推拉
I 窗户开关
O 门开关
P 电视开关
F 雾化模式 G 雾化开关
J、K、L 灯光控制
N 拉开窗帘、M 关窗帘
四、关键技术实现的核心代码
void DrawBed()
{
glPushMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[14]);
glTranslatef(0,0, 2);
gluCylinder(quadric, 7.0f, 7.0f, 8.0f, 32, 10);//右枕头
glTranslatef(0, 0, -12);
gluCylinder(quadric, 7.0f, 7.0f, 8.0f, 32, 10);//左枕头
glPopMatrix();
glPushMatrix();
glPushMatrix();
glTranslatef(-20,1, 0);
DrawCube(24, 2, 20);
glPopMatrix();
//被子
//中部
glPushMatrix();
glTranslatef(-31.5,-0.5,-10);
gluCylinder(quadric, 2.5f, 2.5f, 20.0f, 32, 32);
glPopMatrix();
//床尾
glRotatef(90, -1.0f, 0.0f, 0.0f);
glRotatef(90,0.0f, -1.0f, 0.0f);
glTranslatef(-0.5, -10,8);
//右侧
gluCylinder(quadric, 2.5f, 2.5f, 24.0f, 32, 32);
glTranslatef(0, 20,0 );
//左侧
gluCylinder(quadric, 2.5f, 2.5f, 24.0f, 32, 32);
glColor4f(1.0f, 2.0f, 1.0f, 1.0f);
glPopMatrix();
glTranslatef(-18, -2, 0);
glBindTexture(GL_TEXTURE_2D, texture[13]);
DrawCube(29, 5, 23);
//两个球
glBindTexture(GL_TEXTURE_2D, texture[14]);
glPushMatrix();
glTranslatef(-13.5,1.5, 10);
gluSphere(quadric, 2.5f, 32, 32);
glPopMatrix();
//右上
//右下
glPushMatrix();
glTranslatef(-13.5,1.5, -10);
gluSphere(quadric, 2.5f, 32, 32);
glPopMatrix();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, texture[0]);
}
void DrawCurtain()//绘制窗帘
{
glBindTexture(GL_TEXTURE_2D, texture[14]);
glPushMatrix();
glTranslatef(0.0, 28.0, -80.0);
glColor4f(1.0f, 2.0f, 1.0f, blendAlpha);
glRotatef(90, -1.0f, 0.0f, 0.0f);
for (int i = 10; i >=0; i--)//用曲面构成
{
gluCylinder(quadric, curtain, curtain, 32.0f, 32, 32);
glTranslatef(0.0, -move[i],0.0);
}
glColor4f(1.0f, 2.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glPopMatrix();
}
void DrawSofa()//绘制沙发
{
glPushMatrix();
glTranslated(-5,-2,-4);
for (int i = 0; i <8; i++)
{
glBindTexture(GL_TEXTURE_2D, texture[7]);
if (i == 0 || i == 7)
{
glPushMatrix();
保存模型视图矩阵
glTranslatef(0.0f, 4.0f,-4.0f);
gluSphere(quadric, 1.5f, 32, 32);
// 移动坐标系
// 绘制球体
glPopMatrix();
恢复模型视图矩阵
}
if (i < 7)
绘制沙发背
{
}
}
glPushMatrix();
glTranslated(0, 4.5, -4);
gluCylinder(quadric, 1.0f, 1.0f, 8.0f, 32, 32);//绘制圆柱
glPopMatrix();
glPushMatrix();
DrawCube(2.0f, 8.0f, 8.0f);
glPopMatrix();
glTranslatef(0, 0, 8);
绘制沙发坐垫
glTranslated(-3, -6, -60);
glBindTexture(GL_TEXTURE_2D, texture[9]);
gluSphere(quadric, 4.0f, 32, 32);
//绘制球体
for (int i = 0; i < 7; i++)
{
glPushMatrix();
glTranslated(1, -1, 0);
gluCylinder(quadric, 3.5f,3.5f, 8.0f, 32, 32);
//
//
//
//
//绘制圆柱
glPopMatrix();
glTranslatef(0, 0, 8);
}
gluSphere(quadric,4.0f, 32, 32);
//绘制球体
glTranslated(5, 0, -48);
glPopMatrix();
}
五、效果截图
开一个小灯的效果