电子科大实验4-三维图形程序设计_第1页
电子科大实验4-三维图形程序设计_第2页
电子科大实验4-三维图形程序设计_第3页
电子科大实验4-三维图形程序设计_第4页
电子科大实验4-三维图形程序设计_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验报告课程名称:三维图形设计学院:计算机科学与工程专业:计算机科学与技术指导教师:周川学生姓名:学号:实验成绩:日期:2014年5月26日电子科技大学计算机学院实验中心电 子 科 技 大 学实 验 报 告一、实验四:OpenGL纹理映射编程实验二、实验室名称:主楼A2-412 实验时间:2014年5月26日 三、实验目的:1、理解OpenGL纹理映射的相关原理;2、掌握与纹理映射相关的OpenGL API函数及其用法;3、熟悉纹理映射设置的基本步骤。四、实验原理:1. openGL图形编程技术原理2. 纹理映射、纹理参数设置、纹理坐标的自动生成、纹理对象、数字纹理、图片纹理、光照、变换、菜单

2、。3.BMP知识:【基本组成】BMP文件是windows采用的图形文件格式,以像素为基本单位。一般由4部分组成,位图文件头,位图信息头,彩色表(24位不用彩色表),图像数据。【解码相关】1) windows.h中有BITMAPFILEHEADER和BITMAPINFOHEADER表示位图文件头和位图信息头。2) 一般BMP的24位图像是以BGR存储的,处理时需要注意。3) 一般BMP图像是从下到上,从左到右进行描述;PHOTOSHOP中有翻转行序(flip row over),采用了解码方便的从上到下从左到右描述,但处理时必须注意图像数据是相反的,高度是负值。4) 如果一个图像水平线字节数不是

3、4的倍数,这行就用空字节补齐,通常是ASCII 0。4. gluBuild2DMipmaps对BMP图片要求很低,任意大小都行,但是不太稳定。glTexImage2D支持64,128,256分辨率BMP,其他大小的可能会出现问题,稳定。五、实验内容:(一)编程任务1绘制一个具有纹理和光照的房间为在第三次实验中生成的房间(如上图)添加数字图像生成的纹理。要求:1、 使用纹理对象;2、 墙壁、地板和天花板的数字图像生成的纹理要不相同;3、 屋内至少有一个点光源;4、 可用右键菜单交互式的开启或关闭场景光照和纹理;5、 可用右键菜单设置纹理环境(GL_REPLACE和GL_MODULATE)。分析这

4、两种方式中哪种适宜于有光照的环境下。(二)编程任务2绘制一个具有背景纹理的钟表为在第二次次实验中生成的钟表上添加数字图像生成的纹理或者图片纹理。六、实验器材(设备、元器件):电脑型号戴尔 System Inspiron N4110 笔记本电脑操作系统 Windows 8 专业版 64位 ( DirectX 11 )软件环境 Microsoft Visual Studio Express 2012 for Windows Desktop OpenGL Glut3.7七、实验步骤:1.利用第三次实验的光照房间,去除多余功能。2.用纹理对象和数字纹理进行贴图。 3.利用第二次实验的钟表,去除多余功能

5、。4.用纹理对象和如下BMP图片纹理进行贴图(PSCS6生成,大小480480px,24位位图)。BMP贴图图片(G:/clockbg.bmp)BMP格式参数(无翻转行序)八、实验数据及结果分析:(一)纹理房间1. 代码:#include#include#define W 500/窗口宽#define H 500/窗口高/材质属性结构定义typedef struct matattrGLfloat ambient4;GLfloat diffuse4;GLfloat specular4;GLfloat shininess;matattr;/各种材质数据设置matattr ball=0.2,0.3,

6、0.3,1.0,0.3,0.3,0.85,1.0,0.5,0.5,0.81,1.0,50.0;matattr cone=0.3,0.0,0.0,1.0,0.6,0.0,0.0,1.0,0.8,0.6,0.6,1.0,50.0;matattr wall=0.2,0.5,0.5,1.0,0.2,0.5,0.5,1.0,0.9,0.9,0.9,1.0,100.0;matattr ceiling=0.5,0.5,0.7,1.0,0.5,0.5,0.9,1.0,0.9,0.9,0.9,1.0,100.0;matattr ground=0.9,0.0,0.4,1.0,0.9,0.0,0.4,1.0,0.9

7、,0.9,0.9,1.0,100.0;matattr light=1.0,1.0,0.1,1.0,1.0,1.0,0.1,1.0,0.8,0.8,0.2,1.0,50.0;/0光照(点光源)属性GLfloat position0=-2.0,3.0,8.0,1.0;GLfloat ambient0=0.1,0.1,0.1,1.0;GLfloat diffuse0=1.0,1.0,0.8,1.0;GLfloat specular0=1.0,1.0,1.0,1.0;/法向量GLfloat normals3=1.0,0.0,0.0,0.0,1.0,0.0,0.0,-1.0,0.0,0.0,-1.0,0

8、.0,0.0,0.0,-1.0,0.0,0.0,1.0;/全局光照参数设置(不使用默认的全局光)GLfloat global_ambient=0.5,0.5,0.5,1.0;/图片贴图数组GLubyte image443;GLubyte image2443;GLubyte image3443;GLuint TextureIDList 3;GLUquadric *p;/=/=/赋予材质函数void evmat(matattr *mat)glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat-ambient);glMaterialfv(GL_FRONT_AND_

9、BACK,GL_DIFFUSE,mat-diffuse);glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat-specular);glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,mat-shininess);/创建房间void CreateWall()glBindTexture(GL_TEXTURE_2D,TextureIDList1);/调已经加载进内存的纹理对象evmat(&wall);glNormal3fv(normals0);glBegin(GL_POLYGON);/前面glTexCoord2f(0.0,0

10、.0);glVertex3f(-5.0,-5.0,0.0);glTexCoord2f(0.0,15.0);glVertex3f(-5.0,5.0,0.0);glTexCoord2f(15.0,15.0);glVertex3f(-5.0,5.0,10.0);glTexCoord2f(15.0,0.0);glVertex3f(-5.0,-5.0,10.0);glEnd();glNormal3fv(normals1);glBegin(GL_POLYGON);/左边glTexCoord2f(0.0,0.0);glVertex3f(5.0,-5.0,0.0);glTexCoord2f(0.0,15.0)

11、;glVertex3f(5.0,-5.0,10.0);glTexCoord2f(15.0,15.0);glVertex3f(-5.0,-5.0,10.0);glTexCoord2f(15.0,0.0);glVertex3f(-5.0,-5.0,0.0);glEnd();glNormal3fv(normals2);glBegin(GL_POLYGON);/右边glTexCoord2f(0.0,0.0);glVertex3f(5.0,5.0,0.0);glTexCoord2f(0.0,15.0);glVertex3f(-5.0,5.0,0.0);glTexCoord2f(15.0,15.0);gl

12、Vertex3f(-5.0,5.0,10.0);glTexCoord2f(15.0,0.0);glVertex3f(5.0,5.0,10.0);glEnd();glNormal3fv(normals3);glBegin(GL_POLYGON);/后面glTexCoord2f(0.0,0.0);glVertex3f(5.0,-5.0,0.0);glTexCoord2f(0.0,15.0);glVertex3f(5.0,5.0,0.0);glTexCoord2f(15.0,15.0);glVertex3f(5.0,5.0,10.0);glTexCoord2f(15.0,0.0);glVertex3

13、f(5.0,-5.0,10.0);glEnd();void CreateCeiling()glBindTexture(GL_TEXTURE_2D,TextureIDList2);/调已经加载进内存的纹理对象evmat(&ceiling); glNormal3fv(normals4);glBegin(GL_POLYGON);glTexCoord2f(0.0,0.0);glVertex3f(-5.0,-5.0,10.0);glTexCoord2f(0.0,10.0);glVertex3f(5.0,-5.0,10.0);glTexCoord2f(10.0,10.0);glVertex3f(5.0,5

14、.0,10.0);glTexCoord2f(10.0,0.0);glVertex3f(-5.0,5.0,10.0);glEnd();void CreateGround()glBindTexture(GL_TEXTURE_2D,TextureIDList0);/调已经加载进内存的纹理对象evmat(&ground); glNormal3fv(normals5);glBegin(GL_POLYGON);glTexCoord2f(0.0,0.0);glVertex3f(-5.0,-5.0,0.0);glTexCoord2f(0.0,15.0);glVertex3f(5.0,-5.0,0.0);glT

15、exCoord2f(15.0,15.0);glVertex3f(5.0,5.0,0.0);glTexCoord2f(15.0,0.0);glVertex3f(-5.0,5.0,0.0);glEnd();void CreateFurnishings()evmat(&cone);glPushMatrix();glTranslatef(-4.0,-2.0,0.0);glutSolidCone(1.0,2.0,80,50);evmat(&ball);glTranslatef(1.0,2.0,1.0);gluSphere(p,1.0,80,50);glPopMatrix();void CreateLig

16、ht()glLightfv(GL_LIGHT0,GL_POSITION,position0);glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0);glLightfv(GL_LIGHT0,GL_SPECULAR,specular0);glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);void CreateWorld()CreateLight();CreateWall();CreateCeiling();CreateGround();CreateFurnishings();/主display函数void display()glClear

17、(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(80.0,1.0,1.0,100.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(4.5,0.0,5.0, 0.0,0.0,5.0, 0.0,0.0,1.0);CreateWorld();glutSwapBuffers();/菜单void mainmenu(int id)switch(id)case 1:glEnable(GL_

18、LIGHT0);break;case 2:glDisable(GL_LIGHT0);break;case 3:glEnable(GL_TEXTURE_2D);break;case 4:glDisable(GL_TEXTURE_2D);break;case 5:glTexEnvf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_MODULATE);break;case 6:glTexEnvf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_REPLACE);break;glutPostRedisplay();/初始化函数void init()i

19、nt i,j,t;glEnable(GL_DEPTH_TEST);/选0号,1号光照glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient);p=gluNewQuadric();gluQuadricDrawStyle(p, GLU_FILL);/计算贴图数组for (i=0;i4;i+)for (j=0;j4;j+)t=0;if (i2 &j=2 &j=2)t=255;imageij0=(GLubyte)t;imageij1=(GLubyte)t;imageij

20、2=(GLubyte)t;for (i=0;i4;i+)for (j=0;j4;j+)t=(j(4-i)?100:200;image2ij0=(GLubyte)t;image2ij1=(GLubyte)t-20;image2ij2=(GLubyte)t+20;for (i=0;i4;i+)for (j=0;j4;j+)t=60;if (i=j & i!=0 & j!=3)t=180;image3ij0=(GLubyte)t;image3ij1=(GLubyte)t-10;image3ij2=(GLubyte)t+10;/贴图参数设置glEnable(GL_TEXTURE_2D);glTexEn

21、vf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_MODULATE);glGenTextures(3,TextureIDList);/分配纹理号/-0号给地板-glBindTexture(GL_TEXTURE_2D,TextureIDList0);glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,4,4,0,GL_RGB,GL_UNSIGNED_BYTE,image);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);glTexParameterf(GL_TEXTURE_2D

22、,GL_TEXTURE_WRAP_T,GL_REPEAT);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);/-1号给墙壁-glBindTexture(GL_TEXTURE_2D,TextureIDList1);glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,4,4,0,GL_RGB,GL_UNSIGNED_BYTE,image2);glTexParameterf(

23、GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);/-2号给天花板-glBindTexture(GL_TEXTURE_2D,TextureIDList2);glTexImage2D(GL_T

24、EXTURE_2D,0,GL_RGB,4,4,0,GL_RGB,GL_UNSIGNED_BYTE,image3);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEARES

25、T);/创建菜单glutCreateMenu(mainmenu);glutAddMenuEntry(开启点光源,1);glutAddMenuEntry(关闭点光源,2);glutAddMenuEntry(使用纹理,3);glutAddMenuEntry(去除纹理,4);glutAddMenuEntry(纹理环境:modulate调制,5);glutAddMenuEntry(纹理环境:replace替换,6);glutAttachMenu(GLUT_RIGHT_BUTTON);void main(int argc,char *argv)glutInit(&argc,argv);glutInitD

26、isplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);glutInitWindowSize(500,500);glutInitWindowPosition(100,100);glutCreateWindow( );glutDisplayFunc(display);init();glutMainLoop(); 2. 结果:初始效果图 关闭点光源去除纹理调制模式替换模式菜单展示3. 分析:完成了要求;调制模式更适合与光照,因为会和表面的材质参数进行结合。(二)钟表纹理1. 代码:#include#include#include#includetypedef un

27、signed char byte;GLUquadric *p;struct tm* localTime; int hour, minute, second;byte *image;GLuint ID;int LoadBitmap(const char *file)GLuint ID;int width,height,i;byte t;FILE *fp;BITMAPFILEHEADER FileHeader;BITMAPINFOHEADER InfoHeader;fp=fopen(file,rb);if (!fp)/文件能否打开printf(文件打开失败!);fclose(fp);return

28、-1;fread(&FileHeader,sizeof(BITMAPFILEHEADER),1,fp);if (FileHeader.bfType!=0x4D42)/BMP文件类型校验printf(不是BMP标准图像!);fclose(fp);return -1;fread(&InfoHeader,sizeof(BITMAPINFOHEADER),1,fp);width=InfoHeader.biWidth;height=InfoHeader.biHeight;if (InfoHeader.biSizeImage=0)/确保图像数据大小 InfoHeader.biSizeImage=width

29、*height*3;fseek(fp,FileHeader.bfOffBits,SEEK_SET);/移动指针到图像image=(byte *)malloc(sizeof(byte)*InfoHeader.biSizeImage);/为图像分配内存if (!image)free(image);printf(没有足够的内存!);fclose(fp);return -1;fread(image,1,InfoHeader.biSizeImage,fp);for (i=0;itm_hour;minute=localTime-tm_min;second=localTime-tm_sec;void pro

30、cessHourhand()glPushMatrix();glRotatef(hour*30.0+minute*0.5,0.0,0.0,-1.0);createHourhand();glPopMatrix();void processMinutehand()glPushMatrix();glRotatef(minute*6.0+second*0.1,0.0,0.0,-1.0);createMinutehand();glPopMatrix();void processSecondhand()glPushMatrix();glRotatef(second*6.0,0.0,0.0,-1.0);createSecondhand();glPopMatrix();void display(void) glClear(GL_COLOR_BUFFER_BIT);createWatch();processHourhand();processMinuteh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论