report4郭小明 三维图形程序设计 电子科技大学

更新时间:2024-05-05 05:15:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

课程名称:三维图形程序设计学 院:计算机科学与工程专 业:计算机科学与技术指导教师:曹跃学生姓名:郭小明学 号:实验成绩:日 期:实验报告 2011060100010

2013年 5 月 3

电 子 科 技 大 学

实 验 报 告

实验四 OpenGL纹理映射编程实验

实验室名称:A2—412 实验时间:2013.5.3

一.实验目的

1、理解OpenGL纹理映射的相关原理;

2、掌握与纹理映射相关的OpenGL API函数及其用法; 3、熟悉纹理映射设置的基本步骤。

二.实验内容

(一)编程任务1——绘制一个具有纹理和光照的房间

为在第三次实验中生成的房间(如上图)添加数字图像生成的纹理。 要求:

1、使用纹理对象;

2、墙壁、地板和天花板的数字图像生成的纹理要不相同; 3、屋内至少有一个点光源;

4、可用右键菜单交互式的开启或关闭场景光照和纹理;

5、可用右键菜单设置纹理环境(GL_REPLACE和GL_MODULATE)。分析这两种方式中哪种适宜于有光照的环境下。 6、

三.实验要求

1. 实验前要做好充分准备;

2. 实验中独立完成源程序编辑、编译、链接过程,运行并记录程序运行结

果;

3. 对程序结果进行分析。 四.实验报告

1. OpenGL纹理映射的基本原理和思想; 2. OpenGL纹理映射的基本步骤; 3. 编程任务1

a) 程序说明。说明程序的功能、结构。

b) 编制的应用程序上机调试的情况、上机调试步骤、调试所遇到的问题

是如何解决的,并对编程过程中的问题进行分析,对执行结果进行分析。

c) 写出源程序清单(详细注释)和执行结果。 d) 实验结果的分析。

五、实验器材(设备、元器件):

Microsoft Windows XP Professional 版本2002 Service Pack 3 VC++ 6.0

六、 墙壁原图片

源程序:

#include #include #include #include #define SIZE 512

GLint HITS; int flag_xyz = 1; int flag_light = 1; int mousex,mousey; float movex,movey,movez;

typedef struct {

GLubyte * image_data; GLsizei width; GLsizei height;

} Data; Data* data[4]; GLuint textures[4];

float PI = 3.1415926; void display();

void drawSphere(GLfloat,GLfloat,GLfloat,int);

float wide=0.8,height=0.8,Dept=0.8;

void init() { }

int wide_screen=300,height_screen=300;

float eyex = 0,eyey = 0,eyez = 1.7;

void reshape(int w, int h) {

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); wide_screen = w; height_screen = h; glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluPerspective(60,1,1,30);//3.设置视景体,glFrustum gluLookAt(eyex,eyey,eyez,0,0,0,0,1,0); //glPixelStoref(GL_UNPACK_ALIGNMENT,1); glGenTextures(4,textures); for(int i=0;i<4;i++){ }

//printf(\

glBindTexture(GL_TEXTURE_2D,textures[i]); glTexImage2D(

GL_TEXTURE_2D, 0, GL_RGB, data[i]->width, data[i]->height , 0,

GL_RGB, GL_UNSIGNED_BYTE, data[i]->image_data);

glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_TEXTURE_2D);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

}

float light_postion[4]={wide-0.2,height-0.2,-Dept,1}; float light_ambient [4]={1.0,1.0,1.0,0.8}; float light_diffuse [4]={1.0,1.0,1.0,0.8}; float light_specular [4]={1.0,1.0,1.0,0.8}; //float light_ambient [4]={0.6,0.6,0.6,0.5}; //float light_diffuse [4]={0.4,0.4,0.4,0.5}; //float light_specular [4]={0.8,0.8,0.8,0.5};

float proxy[3] = {wide-0.2,height-0.2,-Dept};

void createLightAndProxy(GLenum mode) { }

void create_wall() {

//设置墙壁材质

GLfloat Material_ambient[4]={0.2,0.2,0.2,1.0}; if(mode == GL_SELECT) { }

glLoadName(2);

glTranslatef(-2*movex/wide_screen,-2*movey/height_screen,-2*movez/wide_screen); if(mode == GL_SELECT) { }

if(flag_light == 1)

drawSphere(proxy[0],proxy[1]-wide/12,proxy[2],1); glLoadName(1); //设置光源相关

glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1); glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0); glLightfv(GL_LIGHT0,GL_POSITION,light_postion); glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);

glTranslatef(2*movex/wide_screen,2*movey/height_screen,2*movez/wide_screen);

//近处的墙

glColor3f(0.6,0.5,0.0); glNormal3f(0,0,-1); glTexCoord2f(0,0);

glVertex3f(-wide,-height,Dept); glTexCoord2f(16,0);

glVertex3f(wide,-height,Dept); glTexCoord2f(16,16); glVertex3f(wide,height,Dept); glTexCoord2f(0,16);

glVertex3f(-wide,height,Dept);

//右边的墙

glColor3f(0.5,0.5,0.8); glNormal3f(-1,0,0); glTexCoord2f(0,0);

glVertex3f(wide,-height,-Dept); glTexCoord2f(16,0);

glVertex3f(wide,-height,Dept); glTexCoord2f(16,16); glVertex3f(wide,height,Dept); glTexCoord2f(0,16);

glVertex3f(wide,height,-Dept);

//左边的墙

glColor3f(0.5,0.5,0.8); glNormal3f(1,0,0); glTexCoord2f(0,0);

glVertex3f(-wide,-height,Dept); glTexCoord2f(16,0);

glVertex3f(-wide,-height,-Dept); glTexCoord2f(16,16);

glBindTexture(GL_TEXTURE_2D,textures[0]); glBegin(GL_QUADS);

glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,Material_ambient); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,Material_diffuse); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,Material_specular); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,Material_shiness); GLfloat Material_diffuse[4]={0.8,0.8,0.6,1.0}; GLfloat Material_specular[4]={0.2,0.2,0.5,1.0}; GLfloat Material_shiness = 64.0;

}

void create_floor() { }

void create_quad(int i,int j) {

if((i+j)%2 == 1 ) else

glColor3f(0.0,0.0,0.0); glVertex3f(-wide/16,0,Dept/16); glVertex3f(wide/16,0,Dept/16); glBegin(GL_QUADS);

glColor3f(0.0,0.5,0.5);

glBindTexture(GL_TEXTURE_2D,textures[3]); glBegin(GL_QUADS); glColor4f(0.4,0.3,0.3,0.6); glNormal3f(0,1,0); glTexCoord2f(0.0,0.0); glVertex3f(-wide,-height,Dept); glTexCoord2f(16.0,0); glVertex3f(wide,-height,Dept); glTexCoord2f(16.0,16.0); glVertex3f(wide,-height,-Dept); glTexCoord2f(0.0,16.0); glVertex3f(-wide,-height,-Dept); glEnd();

//远处的墙

glColor3f(0.5,0.5,0.8); glNormal3f(0,0,1); glTexCoord2f(0,0);

glVertex3f(-wide,-height,-Dept); glTexCoord2f(16,0);

glVertex3f(wide,-height,-Dept); glTexCoord2f(16,16); glVertex3f(wide,height,-Dept); glTexCoord2f(0,16);

glVertex3f(-wide,height,-Dept);

glVertex3f(-wide,height,-Dept); glTexCoord2f(0,16);

glVertex3f(-wide,height,Dept);

glEnd();

}

void create_ceil() {

/************************************************************************/ /*采用纹理贴图的方式,就可以放弃循环画小图的方式了,下面的代码就不再使用*/ /************************************************************************/ /* int j =1; int i=1;

glTranslatef(0,height,0);

//glBindTexture(GL_TEXTURE_2D,textures[1]); glNormal3f(0,1,0); for(;j<=16;j++) {

glTranslatef(-wide+j*wide/8-wide/16,0,0); for(i=1;i<=16;i++)

glBindTexture(GL_TEXTURE_2D,textures[1]); glBegin(GL_QUADS);

glColor4f(0.4,0.3,0.3,0.6); glNormal3f(0,-1,0); glTexCoord2f(0.0,0.0); glVertex3f(-wide,height,-Dept); glTexCoord2f(16.0,0); glVertex3f(wide,height,-Dept); glTexCoord2f(16.0,16.0); glVertex3f(wide,height,Dept); glTexCoord2f(0.0,16.0); glVertex3f(-wide,height,Dept);

glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,Material_ambient); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,Material_diffuse); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,Material_specular); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,Material_shiness); GLfloat Material_ambient[4]={0.8,0.8,0.8,1.0}; GLfloat Material_diffuse[4]={0.8,0.8,0.8,1.0}; GLfloat Material_specular[4]={0.4,0.3,0.5,1.0}; GLfloat Material_shiness = 1.0;

glVertex3f(wide/16,0,-Dept/16); glVertex3f(-wide/16,0,-Dept/16);

glEnd();

glEnd();

}

void drawSphere(GLfloat xx=0,GLfloat yy=0,GLfloat zz=0,int flag=0) {

x[0] = radius * sin(angle_z) * cos(angle_xy); y[0] = radius * sin(angle_z) * sin(angle_xy); z[0] = radius * cos(angle_z);

for(j=0; j

angle_xy = j * step_xy;

float angle_z = 0.0; float angle_xy = 0.0; int i=0, j=0; if(flag == 0) else

glColor3f(1.0,1.0,1.0); glBegin(GL_QUADS); for(i=0; i

angle_z = i * step_z; glColor3f(0.5,0.25,0); GLfloat radius; if(flag == 0) else

radius=wide/16; GLfloat M=20; GLfloat N=20; float step_z = PI/M; float step_xy = 2*PI/N; float x[4],y[4],z[4];

radius=2*wide/8; }

glTranslatef(0,-height,0); */

{ }

glTranslatef(wide-j*wide/8+wide/16,0,0);

glTranslatef(0,0,-Dept+i*Dept/8-Dept/16); create_quad(i,j);

glTranslatef(0,0,Dept-i*Dept/8+Dept/16);

}

void yuanzhui() {

glTranslatef(-wide/2,-height,-Dept/2); glBegin(GL_TRIANGLE_FAN); glColor3f(0,0.25,0.5); glNormal3f(1,1,1); glVertex3f(0,height/1.2,0); for(int i=0;i<=32;i++) { } glEnd();

glBegin(GL_TRIANGLE_FAN);

glColor3f(0,0.25,0.5); glNormal3f(0,1,0); glVertex3f(0,0,0); for(int i=0;i<=32;i++) { }

glVertex3f(0.2*cos(i*PI/16), 0.01 , 0.2*sin(i*PI/16)); glVertex3f(0.2*cos(i*PI/16),0,0.2*sin(i*PI/16)); } glEnd();

}

for(int k=0; k<4; k++) { }

glNormal3f(x[k],y[k],z[k]);

glVertex3f(xx+x[k], yy+y[k],zz+z[k]);

x[3] = radius * sin(angle_z) * cos(angle_xy + step_xy); y[3] = radius * sin(angle_z) * sin(angle_xy + step_xy); z[3] = radius * cos(angle_z);

x[2] = radius*sin(angle_z + step_z)*cos(angle_xy + step_xy); y[2] = radius*sin(angle_z + step_z)*sin(angle_xy + step_xy); z[2] = radius*cos(angle_z + step_z);

x[1] = radius * sin(angle_z + step_z) * cos(angle_xy); y[1] = radius * sin(angle_z + step_z) * sin(angle_xy); z[1] = radius * cos(angle_z + step_z);

glEnd();

glTranslatef(wide/2,height,Dept/2);

}

void createFurnishings() { }

void draw(GLenum mode) { }

void display() { }

int flag_move = 0 ;

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60,1,1,30);

gluLookAt(eyex,eyey,eyez,0,0,0,0,1,0); draw(GL_RENDER); glFlush(); glutSwapBuffers();

createLightAndProxy(mode); create_wall(); create_floor(); create_ceil(); createFurnishings();

glMatrixMode(GL_MODELVIEW); if(flag_light == 1) { } else { }

glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);

glTranslatef(0,-height+2*wide/8,-Dept+2*Dept/8); drawSphere();

glTranslatef(0,height-2*wide/8,Dept-2*Dept/8); yuanzhui();

void process(GLint hits,GLuint buffer[]) { }

GLuint select_Buffer[SIZE];

void mouse(int key,int state,int x,int y) {

gluPickMatrix((GLdouble)x,(GLdouble)(viewport[3]-y),5,5,viewport); gluPerspective(60,1,1,30);

glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();

glInitNames(); glPushName(0);

glSelectBuffer(SIZE,select_Buffer); glRenderMode(GL_SELECT); GLint hits; GLint viewport[4];

if(key == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

mousex = x; mousey = y;

glGetIntegerv(GL_VIEWPORT,viewport); ptr = (GLint *) buffer; for (i = 0; i < hits; i++) { }

names = *ptr; ptr+=3;

for (j = 0; j < names; j++) { /* for each name */ }

if(*ptr==1) { } ptr++;

flag_move = 1;

/* for each hit */

unsigned int i, j; GLint names, *ptr;

}

void motion(int x,int y) { }

void keyboard(unsigned char key,int x,int y) {

if(flag_move == 0) else { }

switch(flag_xyz) { case 1: case 2: }

glutPostRedisplay(); mousex = x; mousey = y;

movex = x-mousex+movex; movey = -y+mousey+movey; break;

movez = x-mousex+movez; break; return; }

if(key == GLUT_LEFT_BUTTON && state == GLUT_UP) { }

flag_move = 0;

hits = glRenderMode(GL_RENDER); process(hits,select_Buffer); HITS = hits; glutPostRedisplay();

glMatrixMode(GL_PROJECTION); glPopMatrix(); glFlush();

draw(GL_SELECT);

gluLookAt(eyex,eyey,eyez,0,0,0,0,1,0);

case 3:

}

void MenuFunc(int data) {

switch(data) { case 1: switch(key) { case 'x':

eyex -=0.1; break; eyey -=0.1; break; eyez -=0.1; break; eyex +=0.1; break; eyey +=0.1; break; eyez +=0.1; break;

case 'y':

case 'z':

case 'X':

case 'Y':

case 'Z':

case 'h': case 'H':

flag_xyz = 1; break;

case 's': case 'S':

flag_xyz = 2; break;

case 'l': case 'L': }

glutPostRedisplay();

flag_xyz = 3; break; return; break;

default:

}

static GLint ImageWidth; static GLint ImageHeight; static GLint PixelLength; static GLubyte* PixelData;

Data* imageInit(char * path_name){

fseek(pFile, 54, SEEK_SET); fread(PixelData, PixelLength, 1, pFile); // 读取像素数据

PixelData = (GLubyte*)malloc(PixelLength); if( PixelData == 0 )

exit(0); // 计算像素数据长度 PixelLength = ImageWidth * 3; while( PixelLength % 4 != 0 )

++PixelLength; PixelLength *= ImageHeight; // 读取图象的大小信息 fseek(pFile, 0x0012, SEEK_SET);

fread(&ImageWidth, sizeof(ImageWidth), 1, pFile); fread(&ImageHeight, sizeof(ImageHeight), 1, pFile); // 打开文件

FILE* pFile = fopen(path_name, \); if( pFile == 0 ) { }

printf(\文件未找到\); exit(0); }

glutPostRedisplay();

flag_light = 0; break; flag_light = 1; break; break;

case 2:

default:

} int Menu;

int main(int argc,char ** argv) { }

glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInit(&argc,argv);

glutInitWindowSize(wide_screen,height_screen); glutCreateWindow(argv[0]); init();

Menu = glutCreateMenu(MenuFunc); glutAddMenuEntry(\关闭光源!\,1); glutAddMenuEntry(\打开光源!\,2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutMainLoop(); return 0;

char* tupian[4]={\,\,\,\}; for(int i=0;i<4;i++){ }

data[i] = imageInit(tupian[i]); // 关闭文件 fclose(pFile);

Data* rv = (Data *)malloc(sizeof(Data)); rv->image_data = PixelData; rv->width = ImageWidth; rv->height = ImageHeight; return rv;

七.实验结果:

开灯之后的情况

关灯之后的情况

改变纹理和视角之后的情况

八.总结、改进建议及心得体会:

1、理解了OpenGL纹理映射的相关原理;

2、掌握了与纹理映射相关的OpenGL API函数及其用法; 3、熟悉了纹理映射设置的基本步骤。

本文来源:https://www.bwwdw.com/article/2kfg.html

Top