OpenGL进阶(二)自定义矩阵加载

来源:互联网 发布:jq signature.js 编辑:程序博客网 时间:2024/05/12 07:33

opengl里面的平移,旋转,缩放都是基于矩阵的运算,我们可以很方便地通过设定参数的方式调用一些接口函数来实现,同时我们也可以通过自定义的矩阵来实现上述的基本变换。

首先来看一个渲染程序。

[cpp] view plaincopy
  1. GLfloat rtri;                          
  2. GLfloat posX;  
  3. GLfloat posY;  
  4. GLfloat scale=0.5f;  
[cpp] view plaincopy
  1. void renderGL()  
  2. {  
  3.     // Clear the color and depth buffers.   
  4.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  5.     // We don't want to modify the projection matrix. */  
  6.     glMatrixMode( GL_MODELVIEW );  
  7.     glLoadIdentity( );  
  8.     glTranslatef(posX,0.0f,-6.0f);    
  9.     glRotatef(rtri,0.0f,1.0f,0.0f);                 
  10.     glScalef(scale,scale,scale);  
  11.     glBegin(GL_TRIANGLES);                    
  12.         glColor3f(1.0f,0.0f,0.0f);              
  13.         glVertex3f( 0.0f, 1.0f, 0.0f);            
  14.         glColor3f(0.0f,1.0f,0.0f);              
  15.         glVertex3f(-1.0f,-1.0f, 0.0f);            
  16.         glColor3f(0.0f,0.0f,1.0f);              
  17.         glVertex3f( 1.0f,-1.0f, 0.0f);            
  18.     glEnd();                          
  19.     if(posX<3.0f) posX+=0.001f;  
  20.     else posX=-3.0f;  
  21.     if(scale<2.0f) scale+=0.0005f;  
  22.     else scale=0.5f;  
  23.     rtri+=0.2f;                                                                                                                     
  24.     SDL_GL_SwapBuffers( );  
  25. }  

程序很简单,运行的结果就是一个不断旋转+缩放+平移的三角形。




所谓的变换就是于矩阵相乘,下面来看看不同的变换所对应的是什么样的矩阵。

平移变换:

P=[

1,0,0,0,

0,1,0,0,

0,0,1,0,

posX,posY,posZ,1

]

旋转变换(以Y轴为例,a为旋转角度)

Y=[

cos(a),0,-sin(a),0,

0,1,0,0,

sin(a),0,cos(a),0,

0,0,0,1

]

比例变换

S=[

qx,0,0,0,

0,qy,0,0,

0,0,qz,0,

0,0,0,1

]

对于矩阵运算不了解的可以参考《计算机图形》或者DirectX的龙书。

在OpenGL中关于使用自定义矩阵的函数有两个glLoadMatrixf(m)和glMultMatrixf(m).

前者是加载一个矩阵,后者则是将m与当前矩阵相乘。

下面是使用自定义矩阵的程序。

[cpp] view plaincopy
  1. void renderGL()  
  2. {  
  3.     GLfloat moveMatrix[]=  
  4.     {1.0f, 0.0f, 0.0f,0.0f,             
  5.     0.0f,1.0f, 0.0f, 0.0f,            
  6.     0.0f, 0.0f, 1.0f, 0.0f,           
  7.     posX, 0.0f, -6.0f, 1.0f};  
  8.   
  9.     GLfloat scaleMatrix[]=  
  10.     {  
  11.     scale, 0.0f, 0.0f,0.0f,             
  12.     0.0f,scale, 0.0f, 0.0f,            
  13.     0.0f, 0.0f, scale, 0.0f,           
  14.     0.0, 0.0f, 0.0f, 1.0f   
  15.     };  
  16.   
  17.     GLfloat rotateYMatrix[]=  
  18.     {  
  19.     cos(rtri),0.0f,-sin(rtri),0.0f,  
  20.     0.0f,1.0f,0.0f,0.0f,  
  21.     sin(rtri),0.0f,cos(rtri),0.0f,  
  22.     0.0f,0.0f,0.0f,1.0f  
  23.     };  
  24.   
  25.     // Clear the color and depth buffers.   
  26.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  27.     // We don't want to modify the projection matrix. */  
  28.     glMatrixMode( GL_MODELVIEW );  
  29.     glLoadIdentity();  
  30.     glMultMatrixf(moveMatrix);   
  31.     glMultMatrixf(scaleMatrix);   
  32.     glMultMatrixf(rotateYMatrix);   
  33.     glBegin(GL_TRIANGLES);                    
  34.         glColor3f(1.0f,0.0f,0.0f);              
  35.         glVertex3f( 0.0f, 1.0f, 0.0f);            
  36.         glColor3f(0.0f,1.0f,0.0f);              
  37.         glVertex3f(-1.0f,-1.0f, 0.0f);            
  38.         glColor3f(0.0f,0.0f,1.0f);              
  39.         glVertex3f( 1.0f,-1.0f, 0.0f);            
  40.     glEnd();                          
  41.     if(posX<3.0f) posX+=0.001f;  
  42.     else posX=-3.0f;  
  43.     if(scale<2.0f) scale+=0.0005f;  
  44.     else scale=0.5f;  
  45.     rtri+=0.002f;                                                                                                 
  46.     SDL_GL_SwapBuffers( );  
  47. }  

得到的效果和前面的程序完全相同。
原创粉丝点击