Cg之重影效果范例

这一节主要讲了三点内容:

  1. out类型限定符的作用。该限定词限定的形式参数被称为call-by-result参数。也就是说,Cg必须把由out限定的形式参数的最终值传递给它对应的调用参数。
  2. 顶点程序中如何实现对纹理坐标的偏移。
  3. 片断程序中如何实现对两个纹理的混合。

附上经自己整理后的源代码:

  1. //顶点程序:twoTextures_vertex.cg
  2. void main(float2 position : POSITION,
  3.           float2 texCoord : TEXCOORD0,
  4.           
  5.           out float4 oPosition : POSITION,
  6.           out float2 leftTexCoord : TEXCOORD0,
  7.           out float2 rightTexCoord : TEXCOORD1,
  8.           
  9.           uniform float2 leftSeparation,
  10.           uniform float2 rightSeparation)
  11. {
  12.      oPosition = float4(position,0,1);
  13.      leftTexCoord = texCoord + leftSeparation;
  14.      rightTexCoord = texCoord + rightSeparation; 
  15. }

 

  1. //片断程序:twoTextures_fragment.cg
  2. void main(float2 leftTexCoord : TEXCOORD0,
  3.           float2 rightTexCoord : TEXCOORD1,
  4.           
  5.           out float4 color : COLOR,
  6.           uniform sampler2D decal)
  7. {
  8.     float4 leftColor = tex2D(decal,leftTexCoord);
  9.     float4 rightColor = tex2D(decal,rightTexCoord);
  10.     color = lerp(leftColor,rightColor,0.5);
  11. }

 

  1. //整理后的应用程序 2008-9-9 13:24:48
  2. #ifdef _MSC_VER
  3. #pragma comment( lib, "cg.lib" )
  4. #pragma comment( lib, "cgGL.lib" )
  5. #endif
  6. #include <stdio.h>    /* for printf and NULL */
  7. #include <stdlib.h>   /* for exit */
  8. #if __APPLE__
  9. #include <GLUT/glut.h>
  10. #else
  11. #include <GL/glut.h>
  12. #endif
  13. #include <Cg/cg.h>    /* Can't include this?  Is Cg Toolkit installed! */
  14. #include <Cg/cgGL.h>
  15. static const char *myProgramName = "twoTextures";
  16. static CGcontext   myCgContext;//Cg上下文环境
  17. static CGprofile   myCgVertexProfile;//顶点profile
  18. static CGprogram   myCgVertexProgram;//顶点程序
  19. static CGparameter myCgVertexParam_leftSeparation;//顶点程序参数
  20. static CGparameter myCgVertexParam_rightSeparation;//顶点程序参数
  21. static const char  *myVertexProgramFileName = "twoTextures_vertex.cg",//顶点Cg源文件
  22.                    *myVertexProgramName = "main";//顶点程序入口函数
  23. static CGprofile   myCgFragmentProfile;//片断profile
  24. static CGprogram   myCgFragmentProgram;//片断程序
  25. static CGparameter myCgFragmentParam_decal;//片断程序参数
  26. static const char  *myFragmentProgramFileName = "twoTextures_fragment.cg",//片断Cg源文件
  27.                    *myFragmentProgramName = "main";//片断程序入口函数
  28. static const GLubyte  //纹理图像数据
  29. myDemonTextureImage[3*(128*128)] = {
  30. /* RGB8 image data for a mipmapped 128x128 demon texture */
  31. #include "demon_image.h"
  32. };
  33. static void checkForCgError(const char *situation)
  34. {
  35.   CGerror error;
  36.   const char *string = cgGetLastErrorString(&error);
  37.   if (error != CG_NO_ERROR) {
  38.     printf("%s: %s: %s/n",
  39.       myProgramName, situation, string);
  40.     if (error == CG_COMPILER_ERROR) {
  41.       printf("%s/n", cgGetLastListing(myCgContext));
  42.     }
  43.     exit(1);
  44.   }
  45. }
  46. /* Forward declared GLUT callbacks registered by main. */
  47. static void display(void);
  48. static void keyboard(unsigned char c, int x, int y);
  49. static void initTexture(void);
  50. static void initCg(void);
  51. static void initCgVertexProgram(void);
  52. static void initCgFragmentProgram(void);
  53. int main(int argc, char **argv)
  54. {
  55.       glutInitWindowSize(400, 400);
  56.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  57.     glutInit(&argc, argv);
  58.     glutCreateWindow(myProgramName);
  59.     glutDisplayFunc(display);
  60.     glutKeyboardFunc(keyboard);
  61.     glClearColor(0.1, 0.3, 0.6, 0.0);  /* Blue background */
  62.     initTexture();
  63.   initCg();
  64.   glutMainLoop();
  65.   return 0;
  66. }
  67.     static void initTexture(void){
  68.           glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /*Tightly packed texture data.*/
  69.       glBindTexture(GL_TEXTURE_2D, 666);
  70.       /* Load demon decal texture with mipmaps. */
  71.             gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8,
  72.             128, 128, GL_RGB, GL_UNSIGNED_BYTE, myDemonTextureImage);
  73.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  74.             GL_LINEAR_MIPMAP_LINEAR);
  75.     }
  76.     
  77.     static void initCg(void){
  78.           myCgContext = cgCreateContext();//得到当前上下文环境
  79.           checkForCgError("creating context");
  80.           cgGLSetDebugMode( CG_FALSE );
  81.           cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);
  82.           
  83.           initCgVertexProgram();
  84.           initCgFragmentProgram();
  85. }
  86. static void initCgVertexProgram(void){
  87.           //-----------------------------选择顶点和片断的profile---------------------------------
  88.           myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
  89.           cgGLSetOptimalOptions(myCgVertexProfile);
  90.           checkForCgError("selecting vertex profile");
  91.           //-----------------------------选择顶点和片断的profile-----------------------------
  92.           
  93.           //-----------------------------载入顶点程序和参数-----------------------------
  94.           myCgVertexProgram =
  95.             cgCreateProgramFromFile(
  96.               myCgContext,              /* Cg runtime context */
  97.               CG_SOURCE,                /* Program in human-readable form */
  98.               myVertexProgramFileName,  /* Name of file containing program */
  99.               myCgVertexProfile,        /* Profile: OpenGL ARB vertex program */
  100.               myVertexProgramName,      /* Entry function name */
  101.               NULL);                    /* No extra compiler options */
  102.           checkForCgError("creating vertex program from file");
  103.           cgGLLoadProgram(myCgVertexProgram);
  104.           checkForCgError("loading vertex program");
  105.           
  106.           myCgVertexParam_leftSeparation =
  107.             cgGetNamedParameter(myCgVertexProgram, "leftSeparation");
  108.           myCgVertexParam_rightSeparation =
  109.             cgGetNamedParameter(myCgVertexProgram, "rightSeparation");
  110.           cgSetParameter2f(myCgVertexParam_leftSeparation,  -0.3, 0);
  111.           cgSetParameter2f(myCgVertexParam_rightSeparation, 0.3, 0);
  112.      //-----------------------------载入顶点程序和参数-----------------------------  
  113.     }
  114.     
  115.     static void initCgFragmentProgram(void){
  116.           myCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
  117.           cgGLSetOptimalOptions(myCgFragmentProfile);
  118.           checkForCgError("selecting fragment profile");
  119.         
  120.           myCgFragmentProgram =
  121.             cgCreateProgramFromFile(
  122.               myCgContext,                /* Cg runtime context */
  123.               CG_SOURCE,                  /* Program in human-readable form */
  124.               myFragmentProgramFileName,  /* Name of file containing program */
  125.               myCgFragmentProfile,        /* Profile: OpenGL ARB vertex program */
  126.               myFragmentProgramName,      /* Entry function name */
  127.               NULL);        
  128.           checkForCgError("creating fragment program from file");
  129.         
  130.           cgGLLoadProgram(myCgFragmentProgram);
  131.           checkForCgError("loading fragment program");
  132.         
  133.           myCgFragmentParam_decal =
  134.             cgGetNamedParameter(myCgFragmentProgram, "decal");
  135.           checkForCgError("getting decal parameter");   
  136.           cgGLSetTextureParameter(myCgFragmentParam_decal, 666);    
  137.           checkForCgError("setting decal 2D texture");      
  138.     }
  139. static void display(void)
  140. {
  141.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  142.   
  143.   cgGLBindProgram(myCgVertexProgram);
  144.   checkForCgError("binding vertex program");
  145.   cgGLEnableProfile(myCgVertexProfile);
  146.   checkForCgError("enabling vertex profile");  
  147.   cgGLBindProgram(myCgFragmentProgram);
  148.   checkForCgError("binding Fragment program");
  149.   cgGLEnableProfile(myCgFragmentProfile);
  150.   checkForCgError("enabling Fragment profile");
  151.   glBegin(GL_TRIANGLES);
  152.     glTexCoord2f(0, 0);
  153.     glVertex2f(-0.8, 0.8);
  154.     glTexCoord2f(1, 0);
  155.     glVertex2f(0.8, 0.8);
  156.     glTexCoord2f(0.5, 1);
  157.     glVertex2f(0.0, -0.8);
  158.   glEnd();
  159.   cgGLDisableProfile(myCgVertexProfile);
  160.   cgGLDisableProfile(myCgFragmentProfile);
  161.   checkForCgError("disabling vertex profile");
  162.   glutSwapBuffers();
  163. }
  164. static void keyboard(unsigned char c, int x, int y)
  165. {
  166.   switch (c) {
  167.   case 27:  /* Esc key */
  168.     /* Demonstrate proper deallocation of Cg runtime data structures.
  169.        Not strictly necessary if we are simply going to exit. */
  170.     cgDestroyProgram(myCgVertexProgram);
  171.     cgDestroyContext(myCgContext);
  172.     exit(0);
  173.     break;
  174.   }
  175. }
展开阅读全文

没有更多推荐了,返回首页