OpenGL大作业 三维小破房设计(附报告)
立即下载
资源介绍:
大一刚上完程序设计基础课的大作业,要求用到图形库,懵逼的我 临时参考网上的程序初步了解了一下。
https://blog.csdn.net/GUOXIAOHE0000/article/details/80893549
在上链接的代码中进行了修改,删去了一些东西。增加了房屋外部结构、内部家具、草地、树木,月亮和字母。另外增加了方向键等按键调整视角
包括源代码以及实验报告
仅用于学习及应付作业
希望可以帮助你
#include
#include
#include
#include
GLuint drawcube,drawsphere,drawteapot;
static float a=0.0;
static GLdouble eye=1.0;
static GLfloat locate=1.0;
static int lflag=GL_TRUE,rflag=GL_TRUE;
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,w/h,0.5,1000); //透视效果
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(80,100,160,0,0,0,0,10,0);//设置观察点
}
void init1() //初始化
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//画出正方体显示列表
drawcube=glGenLists(1);
glNewList(drawcube,GL_COMPILE);
glutSolidCube(1);
glEndList();
//画出茶壶显示列表
drawteapot=glGenLists(1);
glNewList(drawteapot,GL_COMPILE);
glutSolidTeapot(1);
glEndList();
//设置普通灯光照0位置及参数;
GLfloat position0[]={30,5,30,1};
GLfloat light0s[]={0.10,0.10,0.10,0.0};
GLfloat light0d[]={0.6,0.7,0.7};
GLfloat light0a[]={0.9,0.9,0.9};
glLightfv(GL_LIGHT0,GL_POSITION,position0);
glLightfv(GL_LIGHT0,GL_SPECULAR,light0s);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0d);
glLightfv(GL_LIGHT0,GL_AMBIENT,light0a);
//设置探照灯光照1位置及参数
GLfloat position1[]={-60,40,0,1};
GLfloat light1s[]={1.0,1.0,1.0,1.0};
GLfloat light1d[]={0.06,0.1,0.1};
GLfloat light1a[]={0.91,0.99,0.96};
GLfloat direction[]={0,-60,0,1};
glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,direction);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,30.0);
glLightfv(GL_LIGHT1,GL_POSITION,position1);
glLightfv(GL_LIGHT1,GL_SPECULAR,light1s);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light1d);
glLightfv(GL_LIGHT1,GL_AMBIENT,light1a);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
void SpecialKeys(int key, int x, int y) //按键功能
{
if(key==GLUT_KEY_LEFT)
{
a+=1.0;
glutPostRedisplay();
}
if(key==GLUT_KEY_RIGHT)
{
a-=1.0;
glutPostRedisplay();
}
if(key==GLUT_KEY_DOWN)
{
eye-=0.05;
glutPostRedisplay();
}
if(key==GLUT_KEY_UP)
{
eye+=0.05;
glutPostRedisplay();
}
if(key==GLUT_KEY_F1)
{
locate+=5.0;
glutPostRedisplay();
}
if(key==GLUT_KEY_F2)
{
locate-=5.0;
glutPostRedisplay();
}
}
void draw() //绘制
{
if(lflag)
glEnable(GL_LIGHT0);
if(rflag)
glEnable(GL_LIGHT1);
glClearColor(0.51,0.40,0.5,0.0); //背景色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(a,0.0,1.0,0.0);
glScaled(eye,eye,eye);
glTranslatef(0,locate,0);
glPushMatrix();
//绘制地面;
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(1.0,1.0,1.0,0.0);
glTranslatef(0,-20,0);
glScalef(147.5,1,147.5);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
//外地面
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,100.0/255,0.0,0.5);
glTranslatef(0,-22,0);
glScalef(300,2,300);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.5);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
//外下地面
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(210.0/255,105.0/255,30.0/255,0.5);
glTranslatef(0,-48,0);
glScalef(300,50,300);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.5);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
/*******以下为NPU字母,若不需要请注释*******/
//字
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(100.0/255,0.0,0.0,0.0);
glRotatef(30.0, 1, 0, 0);
glRotatef(30.0, 0.0f, 1.0f, 0.0f);
glTranslatef(-152,95,-200);
glScalef(10,120,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(100.0/255,0.0,0.0,0.0);
glTranslatef(-228,132,-5);
glScalef(10,110,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(100.0/255,0.0,0.0,0.0);
glTranslatef(-230,132,-70);
glScalef(10,110,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
//P字母
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,100.0/255,0.0,0.0);
glTranslatef(-230,132,-100);
glScalef(10,110,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,100.0/255,0.0,0.0);
glTranslatef(-230,157,-160);
glScalef(10,60,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,100.0/255,0.0,0.0);
glTranslatef(-230,130,-132.5);
glScalef(10,10,60);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,100.0/255,0.0,0.0);
glTranslatef(-230,182,-132.5);
glScalef(10,10,60);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
//u字母
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,0.0,100.0/255,0.0);
glTranslatef(-230,132,-190);
glScalef(10,110,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,0.0,100.0/255,0.0);
glTranslatef(-230,80,-222.5);
glScalef(10,10,70);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(0.0,0.0,100.0/255,0.0);
glTranslatef(-230,132,-255);
glScalef(10,110,10);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
/*******以上为NPU字母,若不需要请注释*******/
//墙1
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(1.0,1.0,1.0,0.0);
glTranslatef(75,0,0);
glScalef(2,150,150);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);
glutWireCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
//墙2
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glColor4f(1.0,1.0,1.0,0.0);
glTranslatef(-75,0,0);
glScalef(2,150,150);
glCallList(drawcube);
glColor4f(0.0,1.0,0.0,0.0);