zoukankan      html  css  js  c++  java
  • 4.4.2 OpenGL几何变换编程实例

    程序运行结果如下图:

    clip_image002

      1 #include <GL/glut.h>
      2 
      3 #include <stdlib.h>
      4 
      5 #include <math.h>
      6 
      7 /* 初始化显示窗口大小 */
      8 
      9 GLsizei winWidth=600,winHeight=600;
     10 
     11 /* 设置世界坐标系的显示范围 */
     12 
     13 GLfloat xwcMin=-300.0,xwcMax=300.0;
     14 
     15 GLfloat ywcMin=-300.0,ywcMax=300.0;
     16 
     17 void init (void)
     18 
     19 {
     20 
     21 /* 设置显示窗口的背景颜色为白色 */
     22 
     23 glClearColor(1.0,1.0,1.0,0.0);
     24 
     25 }
     26 
     27 class wcPt3D
     28 
     29 {
     30 
     31 public:
     32 
     33 GLfloat x, y, z;
     34 
     35 };
     36 
     37 /* 三维旋转变换,参数:旋转轴(由点p1和p2定义)和旋转角度(thetaDegrees)*/
     38 
     39 void rotate3D (wcPt3D p1, wcPt3D p2, GLfloat thetaDegrees)
     40 
     41 {
     42 
     43 /* 设置旋转轴的矢量 */
     44 
     45 float vx = (p2.x - p1.x);
     46 
     47 float vy = (p2.y - p1.y);
     48 
     49 float vz = (p2.z - p1.z);
     50 
     51 /*通过平移-旋转-平移复合变换序列完成任意轴的旋转(注意OpenGL中的反序表示)*/
     52 
     53 glTranslatef (p1.x, p1.y, p1.z); //③移动p1到原始位置
     54 
     55 /*②关于通过坐标原点的坐标轴旋转*/
     56 
     57 glRotatef (thetaDegrees, vx, vy, vz);
     58 
     59 glTranslatef (-p1.x, -p1.y, -p1.z); //①移动p1到原点位置
     60 
     61 }
     62 
     63 /* 三维比例放缩变换,参数:比例系数sx、sy、sz和固定点fixedPt */
     64 
     65 void scale3D (GLfloat sx, GLfloat sy, GLfloat sz, wcPt3D fixedPt)
     66 
     67 {
     68 
     69 /*通过平移-放缩-平移复合变换序列完成任意点为中心点的比例缩放*/
     70 
     71 /* ③反平移到原始位置*/
     72 
     73 glTranslatef (fixedPt.x, fixedPt.y, fixedPt.z);
     74 
     75 glScalef (sx, sy, sz); // ②基于原点的比例放缩变换
     76 
     77 /* ① 移动固定点到坐标原点*/
     78 
     79 glTranslatef (-fixedPt.x, -fixedPt.y, -fixedPt.z);
     80 
     81 }
     82 
     83 void displayFcn (void)
     84 
     85 {
     86 
     87 /* 设置变换中心点位置 */
     88 
     89 wcPt3D centroidPt,R_p1, R_p2;
     90 
     91 centroidPt.x=50;
     92 
     93 centroidPt.y=100;
     94 
     95 centroidPt.z=0;
     96 
     97 R_p1=centroidPt;
     98 
     99 R_p2.x=50;
    100 
    101 R_p2.y=100;
    102 
    103 R_p2.z=1;
    104 
    105 /* 设置几何变换参数*/
    106 
    107 wcPt3D p1,p2,fixedPt;
    108 
    109 p1= R_p1;
    110 
    111 p2= R_p2;
    112 
    113 fixedPt=centroidPt;
    114 
    115 GLfloat tx=0.0,ty=100.0,tz=0;
    116 
    117 GLfloat sx=0.5,sy=0.5,sz=1;
    118 
    119 GLdouble thetaDegrees = 90;
    120 
    121 glClear(GL_COLOR_BUFFER_BIT); // 清空显示窗口
    122 
    123 glMatrixMode (GL_MODELVIEW);
    124 
    125 glLoadIdentity(); //清空变换矩阵为单位矩阵,恢复原始坐标系环境
    126 
    127 /* 显示变换前几何对象 */
    128 
    129 glColor3f(0.0,0.0,1.0); // 设置前景色为蓝色
    130 
    131 glRecti(50,100,200,150); //显示蓝色矩形(变换前)
    132 
    133 /* 执行几何变换(注意以反序形式写出)*/
    134 
    135 glTranslatef (tx, ty, tz); // ③平移变换
    136 
    137 scale3D (sx, sy, sz, fixedPt); // ②比例放缩变换
    138 
    139 rotate3D (p1, p2, thetaDegrees); // ①旋转变换
    140 
    141 /* 显示变换后几何对象 */
    142 
    143 glColor3f(1.0,0.0,0.0); //重新设置前景色为红色
    144 
    145 glRecti(50,100,200,150); //显示红色矩形(变换后)
    146 
    147 glFlush();
    148 
    149 }
    150 
    151 void winReshapeFcn(GLint newWidth,GLint newHeight)
    152 
    153 {
    154 
    155 glMatrixMode(GL_PROJECTION);
    156 
    157 glLoadIdentity();
    158 
    159 gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax);
    160 
    161 glClear(GL_COLOR_BUFFER_BIT);
    162 
    163 }
    164 
    165 void main(int argc, char ** argv)
    166 
    167 {
    168 
    169 glutInit(&argc,argv);
    170 
    171 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    172 
    173 glutInitWindowPosition(50,50);
    174 
    175 glutInitWindowSize(winWidth,winHeight);
    176 
    177 glutCreateWindow("三维几何变换实例-OpenGL版复合变换");
    178 
    179 init();
    180 
    181 glutDisplayFunc(displayFcn);
    182 
    183 glutReshapeFunc(winReshapeFcn);
    184 
    185 glutMainLoop();
    186 
    187 }

    附上本实验的VC++工程代码(VC++2008)

  • 相关阅读:
    用java在mysql中随机插入9000 000条数据
    java连接mysql的一个小例子
    JDK环境变量配置
    JVM工作原理
    线程和进程的区别
    java实现链表
    内连接、外连接、左连接、右连接
    udp协议
    要看的东西
    eclipse快捷键
  • 原文地址:https://www.cnblogs.com/opengl/p/3790402.html
Copyright © 2011-2022 走看看