您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > 《地形模型的三维可视化》程序设计
1《地形模型的三维可视化》程序设计一、题目请用OpenGL图形库,编制程序实现地形模型的三维可视化,并进行地形模型的纹理设置,并可以通过键盘进行交互操作(移动和旋转)。(注:给定的地形模型数据是已经建好的不规则三角网。)二、提交资料1.程序的详细步骤和所有相关数据;步骤:①录入三角形数据②获得地形的范围③绘制三角网④设置纹理坐标⑤计算参考点的位置⑥移动相关数据文件:数据文件DHS.dat三角网文件DHS.tri图片文件TERRAIN.BMP2.程序清单(包括程序说明);#includestdafx.h#includewindows.h#includeGL/gl.h#includeGL/glu.h#includeGL/glaux.h#includefstream.h#includemath.hvoidCALLBACKelbowAdd(void);voidCALLBACKelbowSubtract(void);voidCALLBACKshoulderAdd(void);voidCALLBACKshoulderSubtract(void);//点结构定义typedefstruct{longid;doublex;doubley;doublez;2}POINTXYZ;//三角形结构定义typedefstruct{longid;//三角形号longp[3];//三角形三个顶点的序号longxl[3];//拓扑关系}TRIANGLE;//顶点变量POINTXYZ*pPt;//三角形变量TRIANGLE*pTri;//顶点总数intiTotalNum;//三角形总数intiTotalTriNum;//外围结构typedefstructvrtagBOX{doubleminx;doubleminy;doubleminz;doublemaxx;doublemaxy;doublemaxz;}vrBOX;//外围结构vrBOX_box;unsignedintm_nID;//存储纹理的索引号intm_nWidth;//纹理图片的宽度intm_nHeight;//纹理图片的高度float(*_ptexture)[2];//存储三角网的纹理数据//视点参考点的偏移量doublelookx,looky,lookz;//移动的速度float_speed;//垂直方向视点和所要看实体的间的距离float_height;3//视点的位置double_eyeposition[3];//参考点的位置double_referencepoint[3];//鼠标位移与象素之比的分母,即象素数int_Pixels;//设置纹理坐标voidSetTextureCoord();//读取纹理数据boolLoadTriangleBMP(LPSTRszFileNameD);//向前移动voidMove(boolis_forward);//获得鼠标在x,y方向的相对位移量boolRotate(intdx,intdy);//计算视点与参考点的位置void_CalcuEyePositionAndReferencePoint(inttype,floatamount);voidmyinit(void);//读取点坐标、三角形数据voidReadData();voidRender();//获得外围voidSetBOX();voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voidCALLBACKdisplay(void);//读取纹理数据boolLoadTriangleBMP(char*szFileName){AUX_RGBImageRec*pBitmap=NULL;if(!szFileName)returnfalse;//装载数据并存储pBitmap=auxDIBImageLoad(szFileName);if(pBitmap==NULL)returnfalse;glGenTextures(1,&m_nID);//绑定纹理到纹理数组,并进行初始化。glBindTexture(GL_TEXTURE_2D,m_nID);//控制纹理,映射到片元(fragment)时怎样对待纹理。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_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);4//定义二维纹理glTexImage2D(GL_TEXTURE_2D,0,3,pBitmap-sizeX,pBitmap-sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,pBitmap-data);m_nWidth=pBitmap-sizeX;m_nHeight=pBitmap-sizeY;if(pBitmap)deletepBitmap;returntrue;}intmain(intargc,char*argv[]){/*设置双缓存和RGBA颜色模式*/auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA);auxInitPosition(0,0,1600,800);auxInitWindow(作业);myinit();auxReshapeFunc(myReshape);auxKeyFunc(AUX_LEFT,shoulderSubtract);auxKeyFunc(AUX_RIGHT,shoulderAdd);auxKeyFunc(AUX_UP,elbowAdd);auxKeyFunc(AUX_DOWN,elbowSubtract);auxMainLoop(display);return0;}voidCALLBACKelbowAdd(void){Move(true);}voidCALLBACKelbowSubtract(void){Move(false);}voidCALLBACKshoulderAdd(void){Rotate(50,0);}voidCALLBACKshoulderSubtract(void){Rotate(-50,0);}5//读取点坐标、三角形数据voidReadData(){ifstreamifile(f:\\qq\\DHS.dat);ifileiTotalNum;pPt=newPOINTXYZ[iTotalNum];//循环读入定顶点坐标数据;for(inti=0;iiTotalNum;i++){ifilepPt[i].idpPt[i].xpPt[i].ypPt[i].z;}ifile.close();ifstreamifile2(f:\\qq\\DHS.tri);ifile2iTotalTriNum;pTri=newTRIANGLE[iTotalTriNum];//循环读入定顶点坐标数据;for(i=0;iiTotalTriNum;i++){ifile2pTri[i].idpTri[i].p[0]pTri[i].p[1]pTri[i].p[2];}ifile2.close();SetBOX();}//功能:获得地形的范围voidSetBOX(){_box.minx=9900000;_box.miny=9900000;_box.minz=9900000;_box.maxx=-9900000;_box.maxy=-9900000;_box.maxz=-9900000;//对所有的顶点循环for(inti=0;iiTotalNum;i++){if(_box.minxpPt[i].x)_box.minx=pPt[i].x;if(_box.minypPt[i].y)_box.miny=pPt[i].y;if(_box.minzpPt[i].z)_box.minz=pPt[i].z;if(_box.maxxpPt[i].x)_box.maxx=pPt[i].x;if(_box.maxypPt[i].y)_box.maxy=pPt[i].y;6if(_box.maxzpPt[i].z)_box.maxz=pPt[i].z;}}voidmyinit(void){//读取点坐标、三角形数据ReadData();char*_bmpfilename;//纹理图片的文件名_bmpfilename=f:\\qq\\TERRAIN.BMP;//terrain.bmp;//suban_1.bmp;//读取纹理数据LoadTriangleBMP(_bmpfilename);//计算纹理坐标SetTextureCoord();//设置视点的一些初始参数lookx=5.0f;looky=0.0f;lookz=0.0f;_Pixels=3000;_height=2.50;_speed=5.0f;_eyeposition[0]=_box.minx-300;_eyeposition[1]=_box.miny-300;_eyeposition[2]=_box.maxz+50;_referencepoint[0]=_box.maxx;_referencepoint[1]=_box.maxy;_referencepoint[2]=_box.minz;}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,1.0*(GLfloat)w/(GLfloat)h,1.0,90000.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}voidCALLBACKdisplay(void)7{glClearColor(1.0,1.0,1.0,0.0);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glPushMatrix();//投影变换gluLookAt(_eyeposition[0],_eyeposition[1],_eyeposition[2],_referencepoint[0],_referencepoint[1],_referencepoint[2],0,0,1);glPushMatrix();glTranslated(_box.maxx+300,_box.maxy+100,_box.maxz+300);glColor3f(1.0,0.0,0.0);auxSolidSphere(50.0);glPopMatrix();GLfloatmat_ambient[]={1.0,1.0,1.0,1.0};GLfloatmat_diffuse[]={1.0,1.0,1.0,1.0};GLfloatmat_specular[]={1.0,1.0,1.0,1.0};GLfloatmat_shininess[]={50.0};GLfloatlight0_diffuse[]={1.0,1.0,1.0,1.0};//定义光源的位置GLfloatlight0_position[]={_box.maxx,_box.maxy,_box.maxz-800,1.0};GLfloatlight1_ambient[]={1.0,0.0,0.0,1.0};GLfloatlight1_diffuse[]={1.0,0.0,0.0,1.0};GLfloatlight1_specular[]={1.0,0.0,0.0,1.0};GLfloa
本文标题:《地形模型的三维可视化》程序设计
链接地址:https://www.777doc.com/doc-5109490 .html