【GPU编程】《The Cg Tutorial》学习之坐标变换(Transformation) .

一、坐标系统
顶点坐标变换是所有的顶点程序所必须实现的任务之一。因为光栅化需要被变换的坐标来装配图元和产生片断。
图一显示了用于处理顶点坐标的传统变换。图示解释了坐标从某种坐标空间的一种变换到下一个变换之间的转变。
【GPU编程】《The Cg Tutorial》学习之坐标变换(Transformation) ._第1张图片
图一 坐标系统和用于顶点处理的变换
对象空间
应用程序制定顶点位置是在被称为对象空间(object space)或者模型空间(model space)中。一个对象的对象空间经常和另外一个对象的对象空间无关。

世界空间
世界坐标空间的目的是为了提供不同对象之间的相对关系。通过模型变换可以从对象空间变换为世界空间。所有的变换都可以用一个4*4的矩阵来表示。通过把这些矩阵相乘,可以把这些变换连在一起。这将会很有用。

视图空间
典型的视图变换包含了这样的一个平移:把世界坐标空间中的眼睛的位置移到视图空间的原点,然后适当旋转眼睛。
大多数的光照和渲染计算需要比如坐标、表面法线等属性。一般,这些计算在视图空间或者对象空间中进行会更加有效率。在一个场景中,世界坐标空间对于应用程序建立各不同的对象之间的空间关系很有用。但是对于计算光照和其他渲染则效率不高。因此,我们把代表模型和视图变换的两个矩阵合成一个矩阵:模型视图矩阵:用模型矩阵乘以视图矩阵即可。

剪切空间
Cg顶点程序输出的顶点坐标是剪切空间中的坐标,而且必须输出一个剪切空间的坐标。POSITION语意就代表一个顶点程序的特定的输出是剪切空间坐标。
把视图空间的坐标变换为剪切空间的坐标的变换成为投影变换
OpenGL和DirectX对于剪切空间有着不同的规则定义。在OpenGL中,可见的物体必须在一个坐标轴对其的立方体中:剪切空间坐标的x、y和z分量必须小于或者等于w分量,即:-w<=x<=w,-w<=y<=w,-w<=z<=w。
图二显示了投影矩阵如何把视图空间中的机器人变换到剪切空间中。

图二 投影矩阵的作用
剪切空间的坐标是齐次坐标形式<x,y,z,w>。
但是我们需要计算2D坐标和相应的深度值。

规范化设备坐标
通过透视除法获得规范化设备坐标。这样所有可见的几何数据都在一个立方体里面:坐标在<-1,-1,-1>和<1,1,1>之间(在OpenGL中)。

视口变换
变换为窗口坐标。

二、原理应用
Cg顶点程序通常接受的是对象空间中的顶点坐标。
图三显示了把对象坐标变换为剪切坐标的两种方式:

图三 变换到剪切空间中

三、程序实例(OpenGL + Cg + C++):
程序绘制了一个不断旋转的网格状球体。
顶点Cg程序vertex.cg:
[cpp] view plain copy print ?
  1. struct output_v{  
  2.     float4 oPosition : POSITION;  
  3.     float3 color : COLOR;  
  4. };  
  5.   
  6. output_v main_v(float4 position : POSITION,  
  7.                 float3 color : COLOR,  
  8.                 uniform float4x4 modelViewProj)  
  9.                 {  
  10.                     output_v OUT;  
  11.                     // Get OpenGL state matrices   
  12.                     modelViewProj = glstate.matrix.mvp;  
  13.                      // Transform vertex   
  14.                     OUT.oPosition = mul(modelViewProj,position);  
  15.                     OUT.color = color;  
  16.                       
  17.                     return OUT;  
  18.                 }  
cpp文件main.cpp:
[cpp] view plain copy print ?
  1. #pragma comment(lib,"cg.lib")   
  2. #pragma comment(lib,"cgGL.lib")   
  3.   
  4. #include <windows.h>   
  5. #include <GL/glut.h>   
  6. #include <CG/cg.h>   
  7. #include <CG/cgGL.h>   
  8. #include <iostream>   
  9.   
  10. using namespace std;  
  11.   
  12. //some global variables   
  13. static CGcontext context;  
  14. static CGprofile myCgVertexProfile;  
  15. static CGprogram myCgVertexProgram;  
  16. static const char *myProgramName = "CgSample8_VertexTransform",  
  17. *myVertexProgramFileName = "vertex.cg",  
  18. *myVertexProgramName = "main_v";  
  19. bool CgUsed = true;  
  20. static float angle = 0.0;  
  21.   
  22. static void checkForCgError(const char *situation)  
  23. {  
  24.     CGerror error;  
  25.     const char *string = cgGetLastErrorString(&error);  
  26.     if (error != CG_NO_ERROR)  
  27.     {  
  28.         cout << myProgramName << " "  
  29.             <<situation << " "  
  30.             << string << endl;  
  31.         if( CG_COMPILER_ERROR == error)  
  32.             cout << cgGetLastListing(context) << endl;  
  33.         exit(1);  
  34.     }  
  35. }  
  36.   
  37. void init()  
  38. {  
  39.     glClearColor(1.0,1.0,1.0,1.0);  
  40.     glShadeModel(GL_SMOOTH);  
  41.   
  42.     //init Cg    
  43.     context = cgCreateContext();  
  44.   
  45.     myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);  
  46.     checkForCgError("selecting vertex profile");  
  47.   
  48.     myCgVertexProgram = cgCreateProgramFromFile(  
  49.         context,  
  50.         CG_SOURCE,  
  51.         myVertexProgramFileName,  
  52.         myCgVertexProfile,  
  53.         myVertexProgramName,  
  54.         NULL);  
  55.     checkForCgError("Creating vertex Cg program from file");  
  56.   
  57.     cgGLLoadProgram(myCgVertexProgram);  
  58.     checkForCgError("loading vertex program");  
  59. }  
  60.   
  61. void keyboard(unsigned char key,int x,int y)  
  62. {  
  63.     switch(key)  
  64.     {  
  65.     case 'c':  
  66.         CgUsed = !CgUsed;  
  67.         glutPostRedisplay();  
  68.         break;  
  69.     case 27:  
  70.         cgDestroyProgram(myCgVertexProgram);  
  71.         cgDestroyContext(context);  
  72.         exit(0);  
  73.         break;  
  74.     }  
  75. }  
  76.   
  77. void reshape(int w,int h)  
  78. {  
  79.     glViewport(0,0,(GLsizei)w,(GLsizei)h);  
  80.     glMatrixMode(GL_PROJECTION);  
  81.     glLoadIdentity();  
  82.     gluPerspective(80.0f,(GLfloat)w / (GLfloat)h,1,100);  
  83.     glMatrixMode(GL_MODELVIEW);  
  84.     glLoadIdentity();  
  85.     gluLookAt(0,0,20,  
  86.         0,0,0,  
  87.         0,1,0);  
  88. }  
  89.   
  90. void idle()  
  91. {  
  92.     angle += 1.0;  
  93.     if(angle > 360)  
  94.         angle -= 360;  
  95.   
  96.     glutPostRedisplay();  
  97. }  
  98.   
  99. void display()  
  100. {  
  101.     angle += 1.0;  
  102.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  103.   
  104.     if (CgUsed)//enable Cg    
  105.     {  
  106.         cout << "Cg is been used !" << endl;  
  107.           
  108.         cgGLEnableProfile(myCgVertexProfile);  
  109.         checkForCgError("enabling vertex profile");  
  110.   
  111.         cgGLBindProgram(myCgVertexProgram);  
  112.         checkForCgError("binding vertex program");  
  113.     }  
  114.     else  
  115.         cout << "Cg is not used !" << endl;  
  116.   
  117.     glColor3f(1.0,0.0,0.0);  
  118.     glLoadIdentity();  
  119.     glTranslatef(0.0,0.0,-5.0);  
  120.     glRotatef(angle,1.0,0.0,0.0);  
  121.     glutWireSphere(1.0,30,30);  
  122.   
  123.     /*glColor3f(0.0,1.0,0.0); 
  124.     glLoadIdentity(); 
  125.     glTranslatef(-5,0.0,0.0); 
  126.     glutWireCone(1.5,3.5,20,20);*/  
  127.   
  128.     cgGLDisableProfile(myCgVertexProfile);  
  129.     checkForCgError("disabling vertex profile");  
  130.   
  131.     glutSwapBuffers();  
  132. }  
  133.   
  134. int main(int argc,char **argv)  
  135. {  
  136.     glutInit(&argc,argv);  
  137.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
  138.     glutInitWindowPosition(0,0);  
  139.     glutInitWindowSize(600,600);  
  140.     glutCreateWindow("Cg Test 4");  
  141.     init();  
  142.     glutDisplayFunc(display);  
  143.     glutReshapeFunc(reshape);  
  144.     glutKeyboardFunc(keyboard);  
  145.     glutIdleFunc(idle);  
  146.     glutMainLoop();  
  147.   
  148.     return 0;  
  149. }  
运行结果:
【GPU编程】《The Cg Tutorial》学习之坐标变换(Transformation) ._第2张图片

你可能感兴趣的:(【GPU编程】《The Cg Tutorial》学习之坐标变换(Transformation) .)