您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > OpenGL室内三维环境(北邮计算机图形学作业)
..计算机图形学第二次作业姓名:学号:学院:任课教师:..一.设计要求试设计一个室内三维环境,并利用OPENGL展示它的三维效果。要求:(1)包含基本的实体元素:球,多面体,椎体,柱体,曲面等;(2)有全局光照和纹理功能(3)程序具有交互功能二.交互说明键盘的4个按键:上键:视角上移下键:视角下移左键:视角左移右键:视角右移三.效果展示....四.程序代码#includestdlib.h#includestdio.h#includeglew.h#includeglut.h#includewindows.h#pragmacomment(lib,glew32.lib)#definecheckImageWidth64#definecheckImageHeight64staticGLubytecheckImage[checkImageHeight][checkImageWidth][4];staticGLuinttexName;staticGLfloatxrot=0;staticGLfloatyrot=0;//函数功能:绘制地面纹理图案voidmakeCheckImage(void){inti,j,c;for(i=0;icheckImageHeight;i++){for(j=0;jcheckImageWidth;j++){c=((((i&0x16)==0)^((j&0x16))==0))*255;checkImage[i][j][0]=(GLubyte)c;checkImage[i][j][1]=(GLubyte)c;checkImage[i][j][2]=(GLubyte)c;..checkImage[i][j][3]=(GLubyte)255;}}}//函数功能:初始化voidSetupRC(void){glEnable(GL_LIGHTING);//启用灯源glEnable(GL_LIGHT0);//启用0号灯glShadeModel(GL_SMOOTH);//设置光滑着色模式glEnable(GL_DEPTH_TEST);//启用深度测试glEnable(GL_NORMALIZE);//启用法向量glClearColor(1.0f,1.0f,1.0f,0.0f);//为色彩缓冲区指定用于清除的值//设置表面材料的属性GLfloatmat_ambient[]={0.6f,0.6f,0.6f,1.0f};GLfloatmat_diffuse[]={0.5f,0.5f,0.5f,1.0f};GLfloatmat_specular[]={1.0f,1.0f,1.0f,1.0f,};GLfloatmat_shininess[]={40.0f};glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);//指定环境泛光的强度glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);//漫反射的强度glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);//镜面反射的强度glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);//镜面反射光的会聚强度//纹理makeCheckImage();//绘制地面纹理图案glPixelStorei(GL_UNPACK_ALIGNMENT,1);//控制像素存储模式glGenTextures(1,&texName);//用来生成纹理的数量为1glBindTexture(GL_TEXTURE_2D,texName);//绑定纹理//纹理滤波,图象从纹理图象空间映射到帧缓冲图象空间glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,checkImageWidth,checkImageHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,checkImage);}//函数功能:绘制墙壁voidDrawWall(void){glPushMatrix();//矩阵堆栈压入..glTranslated(1.2,0.01,1.2);//将变换矩阵与当前矩阵相乘,使原点移动到(参数值坐标)glScaled(2.4,0.02,2.4);//将比例矩阵与当前矩阵相乘,将当前图形沿x,y,z轴分别放大为原来的(参数值)倍glutSolidCube(1.0);//size=1.0的实心立方体glPopMatrix();//矩阵堆栈弹出}//函数功能:绘制立方柱体voidDrawPillar(void){glPushMatrix();glTranslated(0,0.15,0);glScaled(0.02,0.3,0.02);glutSolidCube(1.0);glPopMatrix();}//函数功能:绘制桌子voidDrawTable(void){glPushMatrix();glTranslated(0.05,0.3,0.05);glScaled(0.6,0.02,0.6);glutSolidCube(1.0);//绘制桌面glPopMatrix();glPushMatrix();glTranslated(0.275,0,0.275);DrawPillar();//绘制桌腿glTranslated(0,0,-0.45);DrawPillar();//绘制桌腿glTranslated(-0.45,0,0.45);DrawPillar();//绘制桌腿glTranslated(0,0,-0.45);DrawPillar();//绘制桌腿glPopMatrix();}//函数功能:绘图voidRenderScene(void){GLfloatlight_position[]={2.0f,6.0f,3.0f,0.0f};glLightfv(GL_LIGHT0,GL_POSITION,light_position);//指定0号光源的位置..glMatrixMode(GL_PROJECTION);//对投影矩阵应用随后的矩阵操作glLoadIdentity();//将当前的用户坐标系的原点移到了屏幕中心GLfloath=1.0;//窗口的一半高度glOrtho(-h*64/48.0,h*64/48.0,-h,h,0.1,100.0);//将当前的可视空间设置为正投影空间:左,右,下,上,近,远。glMatrixMode(GL_MODELVIEW);//对模型视景矩阵堆栈应用随后的矩阵操作glLoadIdentity();gluLookAt(2,1.6,2,0,0.2,0,0,1,0);//设置观察坐标系//开始绘制glRotatef(xrot,1.0f,0.0f,0.0f);//旋转轴经过原点,方向为(1,0,0),旋转角度为xrot,方向满足右手定则glRotatef(yrot,0.0f,1.0f,0.0f);//旋转轴经过原点,方向为(0,1,0),旋转角度为yrotglClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glPushMatrix();glRotated(90.0,0.0,0.0,1.0);DrawWall();//绘制一面墙壁glPopMatrix();glPushMatrix();glRotated(-90.0,1.0,0.0,0.0);DrawWall();//绘制另一面墙壁glPopMatrix();glPushMatrix();glTranslated(0.4,0,0.4);DrawTable();//绘制桌子glPopMatrix();glPushMatrix();glTranslated(0.2,0.1,0.85);glScaled(0.8,0.8,1.0);glutSolidCube(0.2);//绘制箱子glPopMatrix();glPushMatrix();glTranslated(0.6,0.38,0.6);glRotated(-100,0,1,0);glutSolidTeapot(0.1);//绘制茶壶..glPopMatrix();glPushMatrix();glTranslated(0.3,0.33,0.5);glRotated(180,0,180,180);glutSolidTorus(0.02f,0.05,25,50);//绘制手镯【丝状花环】glPopMatrix();glPushMatrix();glTranslated(0.45,0.42,0.3);glutSolidSphere(0.1,15,50);//绘制球体glPopMatrix();glPushMatrix();glTranslated(1.0,0.35,0.3);glScaled(0.03,0.7,0.03);glutSolidCube(1.0);//绘制灯柱glPopMatrix();glPushMatrix();glTranslated(1.0,0.7,0.3);glRotated(190,180,200,270);glutSolidCone(0.15,0.25f,30,25);//绘制圆锥型灯罩glPopMatrix();//绘制纹理glEnable(GL_TEXTURE_2D);//开启纹理glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);//映射方式glBegin(GL_QUADS);//绘制地面//绘制四边形并给出当前顶点所对应的纹理坐标glTexCoord2f(0.0,0.0);glVertex3f(0.0,0.0,0.0);glTexCoord2f(0.0,1.0);glVertex3f(0.0,0.0,2.4);glTexCoord2f(1.0,1.0);glVertex3f(2.4,0.0,2.4);glTexCoord2f(1.0,0.0);glVertex3f(2.4,0.0,0.0);glEnd();glDisable(GL_TEXTURE_2D);//关闭纹理glFlush();//绘图结果显示到屏幕上}//函数功能:处理按键交互信息voidSpecialKeys(intkey,intx,inty){..switch(key){caseGLUT_KEY_UP://上键xrot+=1.0;break;caseGLUT_KEY_DOWN://下键xrot-=1.0;break;caseGLUT_KEY_LEFT://左键yrot+=1.0;break;caseGLUT_KEY_RIGHT://右键yrot-=1.0;break;default:break;}glutPostRedisplay();//标记当前窗口需要重新绘制glFlush();//绘图结果显示到屏幕上}//函数功能:改变窗口大小voidChangeSize(intw,inth){GLfloatlightPos[]={-50.f,50.0f,100.0f,1.0f};GLfloatnRange=1.9f;if(h==0)h=1;glViewport(0,0,w,h);//重新设置屏幕上的窗口大小glMatrixMode(GL_PROJECTION);//后继操作都在投影变换范围内glLoadIdentity();//设置
本文标题:OpenGL室内三维环境(北邮计算机图形学作业)
链接地址:https://www.777doc.com/doc-7349664 .html