基于opengl的3d旋转魔方的实现--终期报告加源码内容摘要:

C 语言知识,也对编程及 3D 设计产生了一定的兴趣,分析问题和解决问题的能力也得到了不小的提高。 而且,我也学到了很多人生的道理。 在做一件看似很难的事时,我14 们要有一个总体的框架,不要有畏难心理。 静下心来,沉着的分析问题,问题总会迎刃而解的。 因此,成功的关键在于你是否拥有这样的心理。 参考文献 [1]孙鑫 VC++深入详解 第三版 北京 电子工业出版社 2020年 126 [2]杨柏林,陈根浪,徐静 OpenGL 编程精粹 第三版 北京 机械工 业出版社 2020年 1121 [3]周纯杰,刘正林,何顶新,周凯波 标准 C 语言程序设计及应用 第二版 武汉 华中科技大学出版社 2020年 1263 [4]Dave Shreiner, The Khronons OpenGLARB Working Group OpenGL编程指南 第七版 北京 机械工业出版社 2020年 21328 15 附上源码 第一个文件 头文件 ifndef _MO_FANG_H_ define _MO_FANG_H_ include // windows 的头文件 include include gl/ // 包含 OpenGL 核心库 include gl/ // 包含 OpenGL 实用库 include gl/ // 包含 OpenGL 辅助库 include // 包含数学函数库 define PI2 define CYCLE_COUNT 90 define MAX_CHAR 128 //用于在窗口显示文字而定义的常数 define FRONT 0 define BACK 1 define LEFT 2 define RIGHT 3 define TOP 4 define BOTTOM 5 typedef struct { GLfloat vx,vy,vz。 16 }CubeVertex。 typedef struct { GLfloat p[3]。 }stPoint。 typedef struct { stPoint CubePoint[8]。 }stCube。 void reset_model()。 void Rotate_ZM()。 void Rotate_ZZ()。 void Rotate_ZP()。 void Rotate_XM()。 void Rotate_XZ()。 void Rotate_XP()。 void Rotate_YM()。 void Rotate_YZ()。 void Rotate_YP()。 void Rotate_Z(int ii)。 17 void Rotate_Y(int ii)。 void Rotate_X(int ii)。 void enable_X_roatate(int direction)。 void enable_Y_roatate(int direction)。 void enable_Z_roatate(int direction)。 void selectFont(int size, int charset, const char* face)。 void drawCNString(const char* str)。 void drawString(const char* str)。 extern stCube Cube[27]。 extern int rotAngle。 extern int rotCount。 extern int rotX,rotY,rotZ。 extern GLfloat Vx,Vy。 endif 第二个文件 /* * 这个 cpp 文件包含三阶魔方变换的常规函数 * 作者为张世清 学 号 U202020185 * 华中科技大学电子系集成 1001 班 * 当然成果还离不开组员徐兮、雷韦拉以及何兆华的支持 */ //加入自己编写的魔方头文件,其中包含一些变量定义与函数声明 include //对立方体各顶点编号 18 static stPoint CubePoint[8]= { { , , }, //0 { , , }, //1 {, , }, //2 {, , }, //3 {, , }, //4 {, , }, //5 {, , }, //6 {, , }, //7 }。 extern int cs。 stCube Cube[27]。 //定义一个 3 阶魔方 stCube Static_Cube[27]。 //定义一个静态的 3 阶魔方 //旋转控制 int rotAngle = 1。 //旋转角度 int rotCount。 //一个旋转周期内旋转次数 CYCLE_COUNT = 90/rotAngle。 int rotX,rotY,rotZ。 //指定那个轴旋转 int rotDirect=1。 //正反向旋转 //对当前魔方各个立方体进行编号 BYTE ZP[9] = {0,1,2,3,4,5,6,7,8}。 //z 轴方向正向一层 BYTE ZZ[9] = {9,10,11,12,13,14,15,16,17}。 //z 轴方向中间一层 BYTE ZM[9] = {18,19,20,21,22,23,24,25,26}。 //z 轴方向负向一层 BYTE YM[9] = {0,1,2,11,10,9,18,19,20}。 //y 轴方向负向一层 BYTE YZ[9] = {3,4,5,14,13,12,21,22,23}。 //y 轴方向中 间一层 19 BYTE YP[9] = {6,7,8,17,16,15,24,25,26}。 //y 轴方向正向一层 BYTE XM[9] = {2,3,8,17,12,11,20,21,26}。 //x 轴方向正向一层 BYTE XZ[9] = {1,4,7,16,13,10,19,22,25}。 //x 轴方向中间一层 BYTE XP[9] = {0,5,6,15,14,9,18,23,24}。 //x 轴方向负向一层 //对静态魔方各个立方体进行编号 const BYTE SZP[9] = {0,1,2,3,4,5,6,7,8}。 //z 轴方向正向一层 const BYTE SZZ[9] = {9,10,11,12,13,14,15,16,17}。 //z 轴方向中间一层 const BYTE SZM[9] = {18,19,20,21,22,23,24,25,26}。 //z 轴方向负向一层 const BYTE SYM[9] = {0,1,2,11,10,9,18,19,20}。 //y 轴方向负向一层 const BYTE SYZ[9] = {3,4,5,14,13,12,21,22,23}。 //y 轴方向中间一层 const BYTE SYP[9] = {6,7,8,17,16,15,24,25,26}。 //y 轴方向正向一层 const BYTE SXM[9] = {2,3,8,17,12,11,20,21,26}。 //x 轴方向正向一层 const BYTE SXZ[9] = {1,4,7,16,13,10,19,22,25}。 //x 轴方向中间一层 const BYTE SXP[9] = {0,5,6,15,14,9,18,23,24}。 //x 轴方向负向一层 void Rotate(stCube *pCube,float angle,float x0,float y0,float z0)。 //声明旋转函数 //判断两个立方体是否重合,即判断两个立方体所包含的所有顶点坐标是否相同 int is_equal(stCube *pc1,stCube *pc2){ float x1,x2,y1,y2,z1,z2。 int isFind = 0。 for( int i=0。 i8。 i++) { x1 = pc1CubePoint[i].p[0]。 y1 = pc1CubePoint[i].p[1]。 z1 = pc1CubePoint[i].p[2]。 isFind = 0。 for(int j=0。 j8。 j++) 20 { x2 = pc2CubePoint[j].p[0]。 y2 = pc2CubePoint[j].p[1]。 z2 = pc2CubePoint[j].p[2]。 if( fabs(x1 x2)1e1 amp。 amp。 fabs(y1y2)1e1 amp。 amp。 fabs(z1z2)1e1) { pc1CubePoint[i].p[0] = pc2CubePoint[j].p[0]。 //消除累计误差 pc1CubePoint[i].p[1] = pc2CubePoint[j].p[1]。 pc1CubePoint[i].p[2] = pc2CubePoint[j].p[2]。 isFind = 1。 break。 } } if( isFind == 0) return 0。 } return 1。 } //旋转之后,查找当前旋转面里面包含新的角点 void Update_Cube_index() { int i,j,k=0。 //根据逐一判断立方体是否重合来更新每一层所包含的立方体的索引,总共三个方向有九层 k =0。 for( i=0。 i9。 i++) { for( j=0。 j27。 j++) if( is_equal( amp。 Cube[j], amp。 Static_Cube[ SZM[i] ] ) ) { ZM[k++] = j。 } 21 } k=0。 for(i=0。 i9。 i++) { for(j=0。 j27。 j++) if(is_equal( amp。 Cube[j], amp。 Static_Cube[ SZZ[i] ] )) { ZZ[k++]=j。 } } k = 0。 for( i=0。 i9。 i++) { for( j=0。 j27。 j++) if( is_equal( amp。 Cube[j] , amp。 Static_Cube[ SZP[i] ] ) ) { ZP[k++] = j。 } } k =0。 for( i=0。 i9。 i++) { for( j=0。 j27。 j++) if( is_equal( amp。 Cube[j], amp。 Static_Cube[ SXM[i] ] ) ) { XM[k++] = j。 } } k =0。 for( i=0。 i9。 i++) 22 { for( j=0。 j27。 j++) if( is_equal( amp。 Cube[j], amp。 Static_Cube[ SXZ[i] ] ) ) { XZ[k++] = j。 } } k = 0。 for( i=0。 i9。 i++) { for(j=0。 j27。 j++) if( is_equal( amp。 Cube[j], amp。 Static_Cube[ SXP[i] ] ) ) { XP[k++] = j。 }。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。