作业题目:

图形变换:实现一个图形绕任意直线旋转的程序。

要求:把一个三维图形绕任意一条直线旋转,需要有初始图形,和旋转后的图形,最好也可以实时控制旋转。

最少要做出绕z轴旋转。

原理:http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation.html

或参见《计算机图形学》

   1:  #include "stdafx.h"
   2:  #include<gl/glut.h>
   3:  #include<windows.h>
   4:  #include<gl/GLU.h>
   5:  #include<gl/GL.h>
   6:  #include <math.h>
   7:  GLfloat rtri=45,posX=1,posY=1.0,posZ=1,scale=0.5;
   8:   
   9:  void keyboard(unsigned char key,int x,int y){
  10:      switch (key) {
  11:      case 'l':
  12:          rtri=rtri+5;
  13:          if(rtri>=360)
  14:              rtri=0.0;
  15:          glutPostRedisplay();
  16:          break;
  17:      case 'r':
  18:          rtri=rtri-5;
  19:          if(rtri<=0)
  20:              rtri=360;
  21:          glutPostRedisplay();
  22:          break;
  23:      case 's':
  24:          scale=scale+0.1;
  25:          if(scale>=1.0)
  26:              scale=0.0;
  27:          glutPostRedisplay();
  28:          break;
  29:      case 'S':
  30:          scale=scale-0.1;
  31:          if(scale<=0.0)
  32:              scale=1.0;
  33:          glutPostRedisplay();
  34:          break;
  35:      case 'x':
  36:          posX = posX+0.1;
  37:          if(posX>=1.0)
  38:              posX=0;
  39:          glutPostRedisplay();
  40:          break;
  41:      case 'X':
  42:          posX = posX-0.1;
  43:          if(posX<=0.0)
  44:              posY=1.0;
  45:          glutPostRedisplay();
  46:          break;
  47:      case 'y':
  48:          posY = posY+0.1;
  49:          if(posY>=1.0)
  50:              posY=0;
  51:          glutPostRedisplay();
  52:          break;
  53:      case 'Y':
  54:          posY = posY-0.1;
  55:          if(posY<=0.0)
  56:              posY=1;
  57:          glutPostRedisplay();
  58:          break;
  59:      case 'z':
  60:          posZ = posZ+0.1;
  61:          if(posZ>=1.0)
  62:              posZ=0;
  63:          glutPostRedisplay();
  64:          break;
  65:      case 'Z':
  66:          posZ = posZ-0.1;
  67:          if(posZ<=0.0)
  68:              posZ=1.0;
  69:          glutPostRedisplay();
  70:          break;
  71:      case 27:
  72:          exit(0);
  73:          break;
  74:      }
  75:  }
  76:  void rotate_Matrix(float r,float x,float y,float z){
  77:      float l=sqrt(x*x+z*z);
  78:      float v=sqrt(y*y+z*z);
  79:      float sin_r1=y/v;
  80:      float cos_r1=z/v;
  81:      float sin_r2=-x/l;
  82:      float cos_r2=z/l;
  83:      GLfloat revTranslateMatrix[]=
  84:      {
  85:          1.0f,0.0f, 0.0f,posX,           
  86:          0.0f,1.0f, 0.0f,posY,          
  87:          0.0f,0.0f, 1.0f,posZ,        
  88:          0.0f,0.0f, 0.0f,1.0f
  89:      };
  90:      GLfloat translateMatrix[]=
  91:      {
  92:          1.0f,0.0f, 0.0f,-posX,           
  93:          0.0f,1.0f, 0.0f,-posY,          
  94:          0.0f,0.0f, 1.0f,-posZ,        
  95:          0.0f,0.0f, 0.0f,1.0f
  96:      };
  97:   
  98:      GLfloat scaleMatrix[]=
  99:      {
 100:          scale, 0.0f, 0.0f,0.0f,           
 101:          0.0f,scale, 0.0f, 0.0f,          
 102:          0.0f, 0.0f, scale, 0.0f,         
 103:          0.0, 0.0f, 0.0f, 1.0f 
 104:      };
 105:      GLfloat rotateXMatrix[]=
 106:      {
 107:          1.0f,0.0f ,0.0f  ,0.0f,
 108:          0.0f,cos_r1,sin_r1,0.0f,
 109:          0.0f,-sin_r1,cos_r1,0.0f,
 110:          0.0f,0.0f ,0.0f ,1.0f
 111:      };
 112:      GLfloat revRotateXMatrix[]=
 113:      {
 114:          1.0f,0.0f ,0.0f  ,0.0f,
 115:          0.0f,cos_r1,-sin_r1,0.0f,
 116:          0.0f,sin_r1,cos_r1,0.0f,
 117:          0.0f,0.0f ,0.0f ,1.0f
 118:      };
 119:      GLfloat rotateYMatrix[]=
 120:      {
 121:          cos_r2 ,0.0f,-sin_r2,0.0f,
 122:          0.0f   ,1.0f,0.0f  ,0.0f,
 123:          sin_r2,0.0f,cos_r2,0.0f,
 124:          0.0f   ,0.0f,0.0f  ,1.0f
 125:      };
 126:      GLfloat revRotateYMatrix[]=
 127:      {
 128:          cos_r2 ,0.0f,sin_r2,0.0f,
 129:          0.0f   ,1.0f,0.0f  ,0.0f,
 130:          -sin_r2,0.0f,cos_r2,0.0f,
 131:          0.0f   ,0.0f,0.0f  ,1.0f
 132:      };
 133:      GLfloat rotateZMatrix[]=
 134:      {
 135:          cos(r),-sin(r),0.0f,0.0f,
 136:          sin(r),cos(r) ,0.0f,0.0f,
 137:          0.0f  ,0.0f   ,1.0f,0.0f,
 138:          0.0f  ,0.0f   ,0.0f,1.0f
 139:      };
 140:      //glMultMatrixf(translateMatrix); 
 141:      glMultMatrixf(rotateXMatrix);
 142:      glMultMatrixf(rotateYMatrix);
 143:      glMultMatrixf(rotateZMatrix);
 144:      glMultMatrixf(revRotateYMatrix);
 145:      glMultMatrixf(revRotateXMatrix);
 146:      //glMultMatrixf(revTranslateMatrix);
 147:  }
 148:  void drawModel(){
 149:      glLineWidth(1);
 150:      glColor3f(1,1,1);  
 151:      //glutWireSphere(1,20,16);
 152:      glutWireTeapot(0.5);
 153:      glLineWidth(4);
 154:      
 155:  }
 156:  void drawAxis(float size){
 157:      //draw axis
 158:      glLineWidth(3);
 159:      glBegin(GL_LINES);
 160:      glColor3f(1.0,0,0);
 161:      glVertex3f(0,0,0);
 162:      glVertex3f(size,0,0);
 163:      glColor3f(0,1,0);
 164:      glVertex3f(0,0,0);
 165:      glVertex3f(0,size,0);
 166:      glColor3f(0,0,1);
 167:      glVertex3f(0,0,0);
 168:      glVertex3f(0,0,size);
 169:      glEnd();
 170:      glLineWidth(1);
 171:   
 172:      //draw arrows
 173:      glPointSize(5);
 174:      glBegin(GL_POINTS);
 175:      glColor3f(1,0,0);
 176:      glVertex3f(size,0,0);
 177:      glColor3f(0,1,0);
 178:      glVertex3f(0,size,0);
 179:      glColor3f(0,0,1);
 180:      glVertex3f(0,0,size);
 181:      glEnd();
 182:      glPointSize(1);
 183:  }
 184:   
 185:  void renderGL()
 186:  {
 187:      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 188:      glMatrixMode( GL_MODELVIEW );
 189:      glLoadIdentity();
 190:      glBegin(GL_LINES);                  
 191:      glColor3f(0.5,0.5,0.5);            
 192:      glVertex3f(0,0,0);                     
 193:      glVertex3f(posX,posY,posZ);
 194:      printf("x:%f y:%f z:%f\n",posX,posY,posZ);
 195:      glEnd(); 
 196:      drawAxis(2.5);
 197:      rotate_Matrix(rtri,posX,posY,posZ);
 198:      drawModel();
 199:      glutSwapBuffers();
 200:  }
 201:   
 202:  int initGL(int argc,char **argv){
 203:      glutInit(&argc,argv);
 204:      glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
 205:      glutInitWindowSize(400,400);
 206:      int handle=glutCreateWindow("rotate my model");
 207:      glutDisplayFunc(renderGL);
 208:      glutKeyboardFunc(keyboard);
 209:      return handle;
 210:  }
 211:   
 212:  int _tmain(int argc, _TCHAR* argv[])
 213:  {
 214:      initGL(argc,(char**)argv);
 215:      glutMainLoop();
 216:      return 0;
 217:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

计算机图形学 - 图形变换(opengl版)的更多相关文章

  1. 我的新书《计算机图形学基础(OpenGL版)》

    我的新书<计算机图形学基础(OpenGL版)>今年6月份在清华大学出版社出版了!新书与原在机械工业出版社出的<计算机图形学>相比,主要有以下不同: 1.加重OpenGL的内容, ...

  2. 《计算机图形学基础(OpenGL版)》使用院校(更新)

    从清华大学出版社责任编辑处获悉,很多高等院校选用了我们这本教材,读者反应不错! 另外,编辑提供了一份详细的使用院校名单如下: 河南科技学院 中原工学院 河北工程大学 防空兵学院 伊犁师院电信学院 吉林 ...

  3. 《计算机图形学基础(OpenGL版)》勘误表

    第1版第1次印刷: 所在页码 所在行 原内容 更正为 41 16 k=Δx/Δy k=Δy/Δx 46 6 s-t=2Δy/Δx(xi+1)+2b+2yi-1 s-t=2Δy/Δx(xi+1)+2b- ...

  4. 新书《计算机图形学基础(OpenGL版)》PPT已发布

    为方便有些老师提前备课,1-10章所有章节已发布到本博客中. 欢迎大家下载使用,也欢迎大家给我们的新书反馈与意见,谢谢!

  5. 计算机图形学(第2版 于万波 于硕 编著)第45页的Bresenham算法有错误

    计算机图形学(第2版 于万波 于硕 编著)第45页的Bresenham算法有错误: 书上本来要写的是以x为阶越步长的方法,但是他写的是用一部分y为阶越步长的方法(其实也写的不对),最后以x为阶越步长的 ...

  6. [计算机图形学]视图变换:MVP变换、视口变换

    目录 一.MVP变换 1. 模型变换 1.1 缩放矩阵 1.2 旋转矩阵 1.3 平移矩阵 2. 视角变换 3. 投影变换 二.Viewport变换 一.MVP变换 MVP变换是模型变换(M).视角变 ...

  7. 计算机图形学 opengl版本 第三版------胡事民 第三章更多的绘图工具

    opengl  计算机图形学 第三版   第二部分   第三章更多的绘图工具 3.1   概述 第2章中  我们绘图使用的是屏幕窗口的基础坐标系    以像素为单位 屏幕坐标从左下角x从0延伸到scr ...

  8. 计算机图形学 opengl版本 第三版------胡事民 第四章 图形学中的向量工具

    计算机图形学 opengl版本 第三版------胡事民 第四章  图形学中的向量工具 一   基础 1:向量分析和变换   两个工具  可以设计出各种几何对象 点和向量基于坐标系定义 拇指指向z轴正 ...

  9. 64 计算机图形学入门(1)——OpenGL环境配置与图形流水线(图像管线)

    0 引言 最近想学一下计算机图形学方面的知识,原因如下.目前本人接触了数字图像处理(opencv)以及点云处理(PCL)方面的知识,对从图像和点云中提取特征信息,并将特征转化为底层/中层语义信息有了一 ...

随机推荐

  1. js的bind方法

    转载:http://www.jb51.net/article/94451.htm http://www.cnblogs.com/TiestoRay/p/3360378.html https://seg ...

  2. shell正则表达式(zhuan)

    匹配中文字符的正则表达式:[u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串的长度(一个 ...

  3. Visual Studio 2013启用AnkSVN

    1. 官网下载AnkSVN  https://ankhsvn.open.collab.net/ 2. 安装 3. 启用 When you enable AnkhSVN as a VS.NET sour ...

  4. CSS巩固

    1. 浮动元素与非浮动元素在一行,浮动元素不占宽度.所以应将非浮动元素改为浮动,或让非浮动元素的宽度为当前行的宽度. 元素浮动之后,周围的元素会重新排列. 2. 布局找模板,或参考其他网站! 自己进行 ...

  5. oracle判断字段是否存在语句

    declare v_cnt number; begin select count(*) into v_cnt from dba_tab_columns where table_name='T_IDC_ ...

  6. 用一条sql语句显示数据百分比并加百分号

    来源于:http://neil-han.iteye.com/blog/1948124 求数值所占比重 关键点:(round(t1.cnt/t2.totalCount*100,2))||'%'

  7. Oracle之物化视图

    来源于:http://www.cnblogs.com/Ronger/archive/2012/03/28/2420962.html 近期根据项目业务需要对oracle的物化视图有所接触,在网上搜寻关于 ...

  8. re模块(正则表达式)

    re 模块:正则表达式import re 内置模块: 1> re.match(pattern,string) pattern:就是正则表达式 string:被操作的对象 match,search ...

  9. 函数也是对象,本片介绍函数的属性、方法、Function()狗仔函数。

    1.arguments.length表示实参的个数. 2.arguments.callee.length表示形参个数. function test(a,b,c,d,e,f){ alert(argume ...

  10. .net的Hello World之旅

    class Program    {        //这是主函数,是程序的入口        static void Main(string[] args)        {            ...