游戏开发论坛

 找回密码
 立即注册
搜索
查看: 25556|回复: 10

Cg编程入门编(OpenGL)

[复制链接]

24

主题

256

帖子

267

积分

中级会员

Rank: 3Rank: 3

积分
267
发表于 2005-10-26 15:29:00 | 显示全部楼层 |阅读模式
Cg ,你好!
------Cg编程入门编(OpenGL)

原注:B Alex D'Angelo (alexz@FusionIndustries.com)
译:  川流不息 (huawenguang@hotmail.com)未经本人同意,不能用于商业用途。

    本文将介绍如何在你现有的图形程序中加入对顶点和片段的高级着色。采用CG着色语言,将会另你的工作更加容易实现。在编程过程中,你可以通过调用一些已有的CG函数来完成你的工作。当然,当你学得足够深入后,你也可以自已编写一些着色器。最具有参考价值的资料,当然要数Fernando 和 Kilgard 编写的“The Cg Tutorial”了。在Nvidia公司的网页中可以下载到。

一、概述
    本文的程序中,结合OpenGL和GLUT在屏幂中画了一个立方体,程序中加入了CG着色程序代码。Listing1中包含了完整的原程序代码,其中CG部分用粗体字标识出来,本程序贯穿全文。Listing2是一个CG顶点着色的代码。

二、建立Cg编程环境
    在进行CG编程之前,首先要下载CG工具包“Cg Toolkit”,在以下网址可以找到:
http://developer.nvidia.com/CG ,请注意看一下你的显卡是否支持顶点着色编程。
    安装CG工具包,为了能让它在Visual C++ 中工作,你可用以下两种方法之一。第一种:把CG头文件和库文件中的内容,分别拷贝到Visual C++的头文件和库文件的文件夹中。
From: C:\Program Files\NVIDIA Corporation\Cg\lib
To:   C:\Program Files\Microsoft Visual Studio\VC98\Lib

From: C:\Program Files\NVIDIA Corporation\Cg\include
To:   C:\Program Files\Microsoft Visual Studio\VC98\Include
第二种方法就是在VC编译器中增加一个搜索路径:Tools ->Options -> projects ->"directiress"

    我们的程序中虽要连接“cg.lib”“cgGL.lib”这两个库文件,你可以把它写到连接路径中去:“properties ->Linker -> Input”,式者在程序开头加入以下代码:
#ifdef _MSC_VER
#pragma comment( lib, "cg.lib" )
#pragma comment( lib, "cgGL.lib" )
#endif
三、Cg编程细节
    为了使用CG着色器,我们必须在程序中建立一个着色上下文(CGcontext),一个程序,只需一个着色上下文就可以了。
    要为每个着色器建立一个CGprogram,而每个着色器你都可以为它指定一个单独的显卡适配参(CGprofile),CGprofile是用来告诉CG如何选择最适合你显卡的方式来对顶点或片段着色。
    着色器可以在任何你虽要用到的地方被载入,载入的时候只需要给程序传递一个着色器函数入口名,或是着色器文件名。本文例子中,着色器在绘图循环前被载入,并且,为了适应各种显卡的在求,我们为着色器指定了一个基础profile

1、设定着色器要用到的变量。
    首先,把CG头文件包含到你的程序中:
        #include <Cg/cg.h>
        #include <Cg/cgGL.h>
       
        接着,增加一些用来保存着色器入口地址的变量:
        static CGcontext Context = NULL;
        static CGprogram VertexProgram = NULL;
        以及一些着色参数的地址变量,在初始化着色器之后,这些地址变量将通过使用“CGparameters”与具体参数绑定在一起。
        static CGparameter KdParam = NULL;
        static CGparameter ModelViewProjParam = NULL;
        static CGparameter VertexColorParam = NULL;
       
        最后指定顶点着色用到的profile:
        static CGprofile VertexProfile = CG_PROFILE_VP20;
       
2、初始化CG
    程序中,初始化OpenGL这后,接着要对CG进行初始化。
    首先,要创建一个着色上下文,一个程序中有一个着色上下文(context)就可以了,所有要用到的着色器都将会共享这一个上下文。
        Context = cgCreateContext();
        其次,通过给上下文指定着色器文件名以及着色器类型(这里是顶点着色),把一个顶点着色器加到着色上下文中去。
        VertexProgram = cgCreateProgramFromFile(Context,
        CG_SOURCE, "vertexShader.cg",
        VertexProfile,
        NULL, NULL);
        只有在顶点着色器被成功创建之后,着色器的代码才被真正地载入到程序中来,与此同时,各种着色参数地址也最终与着色器中的参数绑定在一起。在这之后的程序中,你便可以自由地使用和改变这些参数的内容了。如例子中所示的:反射光颜色(kdParam),顶点颜色(vertexColorParam)和模型透视矩阵(modelViewProj),些变量都是可以改变的。
        在绘图主循环结束之后,所有着色器占用的资源都要及时释放,在CG中,提供了两个函数:cgDestroyProgram();和cgDestroyContext(); 让我们完成这一工作变得更简单。
3、绘图循环
        进入绘图循环之后,着色器必虽在实际绘图这前调用,绘图结束这后就应该被禁用。这个概念和glBegin(); 及 glEnd(); 的意思是相似的,实际上CG是不能在 glBegin(); 和 glEnd(); 之间被调用的。
        在所有绘图工作开妈之前,我们通过调用函数 cgGLBindProgram(); 把着色代码与OpenGL绘图代码关联起来,紧接着调用cgGLEnableProfile(); 函数来指定CG程序到底是进行顶点着色,还是片段着色。之后我们就可以利用cgGLSetParamter*();函数来传递或使用着色器中的参数了。
        在所有绘图工作完成之后,我们应该马上调用cgGLDisableProfile(); 来停止着色器的使用。
        着色器的参数是可以随时改变的,例如:立方体的顶点颜色可以通过函数cgGLSetParameter3f(vertexColorParam,0.0,1.0,0.00);来设定。
        既然我们知道了如何在绘图循环中使用一个着色器,那么扩展到在一个绘图循环中使用多个着色器的情况,那也是很容易实现的,简单地把着色绑定包在绘图代码的外层就可以了。
        例如:
        void draw()
        {
        cgGLBindProgram(program);
        cgGLEnableProfile(profile);
        drawing_code()
        cgGLDisableProfile(profile);
       
        cgGLBindProgram(program2);
        cgGLEnableProfile(profile2);
        drawing_code2()
        cgGLDisableProfile(profile2);
        }
       
四、结论
        现在我们已经学会了如何把着色代码加入到我们现有的程序中。在
        http://developer.nvidia.com/CG
和别的一些相关网站中,有许多着色代码提供给我们使用,同时也可我和他们一起分享你的经验。

sf_2005102615296.doc

57 KB, 下载次数:

24

主题

256

帖子

267

积分

中级会员

Rank: 3Rank: 3

积分
267
 楼主| 发表于 2005-10-26 15:31:00 | 显示全部楼层

Re: Cg编程入门编(OpenGL)



  1. Listing 1 : HelloCg.c
  2. Using Cg with your programs. Cg specific calls are in bold. Heavily based on the runtime_ogl
  3. project in the Cg toolkit.
  4. /* Minimalist program for using shaders, with no error checking */
  5. #ifdef _MSC_VER
  6. #pragma comment( lib, "cg.lib" )
  7. #pragma comment( lib, "cgGL.lib" )
  8. #endif
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #ifdef __APPLE__
  13. #include <GLUT/glut.h>
  14. #else
  15. #include <GL/glut.h>
  16. #endif
  17. #include <Cg/cg.h>
  18. #include <Cg/cgGL.h>
  19. /******************************************************************************/
  20. /*** Static Data ***/
  21. /******************************************************************************/
  22. /* New Cg global variables */
  23. static CGcontext Context = NULL;
  24. static CGprogram VertexProgram = NULL;
  25. static CGparameter KdParam = NULL;
  26. static CGparameter ModelViewProjParam = NULL;
  27. static CGparameter VertexColorParam = NULL;
  28. #ifdef __APPLE__
  29. static CGprofile VertexProfile = CG_PROFILE_ARBVP1;
  30. #else
  31. static CGprofile VertexProfile = CG_PROFILE_VP20;
  32. #endif
  33. /* End new Cg global variables */
  34. GLfloat CubeNormals[6][3] =
  35. {
  36.         {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  37.         {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  38. };
  39. GLint CubeFaces[6][4] =
  40. {
  41.         {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  42.         {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  43. };
  44. GLfloat CubeVertices[8][3];

  45. /******************************************************************************/
  46. static void DrawCube(void)
  47. {
  48.         int i;
  49.         cgGLBindProgram(VertexProgram);
  50.         /*
  51.         * Set various uniform parameters including the ModelViewProjection
  52.         * matrix for transforming the incoming position into HPOS.
  53.         */
  54.         if(KdParam != NULL)
  55.         cgGLSetParameter4f(KdParam, 1.0, 1.0, 0.0, 1.0);
  56.         /* Set the concatenate modelview and projection matrices */
  57.         if(ModelViewProjParam != NULL)
  58.         cgGLSetStateMatrixParameter(ModelViewProjParam,
  59.         CG_GL_MODELVIEW_PROJECTION_MATRIX,
  60.         CG_GL_MATRIX_IDENTITY);
  61.         cgGLEnableProfile(VertexProfile);
  62.         /*
  63.         * Create cube with per-vertex varying attributes
  64.         */
  65.         for(i = 0; i < 6; i++)
  66.         {
  67.         glBegin(GL_QUADS);
  68.         {
  69.         glNormal3fv(&CubeNormals[i][0]);
  70.         cgGLSetParameter3f(VertexColorParam, 1.0, 0.0, 0.0);
  71.         glVertex3fv(&CubeVertices[CubeFaces[i][0]][0]);
  72.         cgGLSetParameter3f(VertexColorParam, 0.0, 1.0, 0.0);
  73.         glVertex3fv(&CubeVertices[CubeFaces[i][1]][0]);
  74.         cgGLSetParameter3f(VertexColorParam, 0.0, 0.0, 1.0);
  75.         glVertex3fv(&CubeVertices[CubeFaces[i][2]][0]);
  76.         cgGLSetParameter3f(VertexColorParam, 1.0, 1.0, 1.0);
  77.         glVertex3fv(&CubeVertices[CubeFaces[i][3]][0]);
  78.         }
  79.         glEnd();
  80.         }
  81.         cgGLDisableProfile(VertexProfile);
  82. }
  83. static void Display(void)
  84. {
  85.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  86.         DrawCube();
  87.         glutSwapBuffers();
  88. }
  89. static void InitializeCube(GLfloat v[8][3])
  90. {
  91.         /* Setup cube vertex data. */
  92.         v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  93.         v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  94.         v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  95.         v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  96.         v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  97.         v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
  98. }
  99. static void InitializeGlut(int *argc, char *argv[])
  100. {
  101.         glutInit(argc, argv);
  102.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  103.         glutCreateWindow(argv[0]);
  104.         glutDisplayFunc(Display);
  105.         InitializeCube(CubeVertices);
  106.         /* Use depth buffering for hidden surface elimination. */
  107.         glEnable(GL_DEPTH_TEST);
  108.         /* Setup the view of the cube. */
  109.         glMatrixMode(GL_PROJECTION);
  110.         gluPerspective( /* field of view in degree */ 40.0,
  111.         /* aspect ratio */ 1.0,
  112.         /* Z near */ 1.0, /* Z far */ 10.0);
  113.         glMatrixMode(GL_MODELVIEW);
  114.         gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */
  115.         0.0, 0.0, 0.0, /* center is at (0,0,0) */
  116.         0.0, 1.0, 0.); /* up is in positive Y direction */
  117.         /* Adjust cube position to be asthetic angle. */
  118.         glTranslatef(0.0, 0.0, -1.0);
  119.         #if 1
  120.         glRotatef(60, 1.0, 0.0, 0.0);
  121.         glRotatef(-20, 0.0, 0.0, 1.0);
  122.         #endif
  123. }
  124. int main(int argc, char *argv[])
  125. {
  126.         InitializeGlut(&argc, argv);
  127.         /* Create one context which all shaders will use */
  128.         Context = cgCreateContext();
  129.         /* Adds shader to the context */
  130.         VertexProgram = cgCreateProgramFromFile(Context,
  131.         CG_SOURCE, "vertexShader.cg",
  132.         VertexProfile,
  133.         NULL, NULL);
  134.         if(VertexProgram != NULL)
  135.         {
  136.         /* Vertex shader only needs to be loaded once */
  137.         cgGLLoadProgram(VertexProgram);
  138.         /* Bind parameters to give access to variables in the shader */
  139.         KdParam = cgGetNamedParameter(VertexProgram, "Kd");
  140.         ModelViewProjParam = cgGetNamedParameter(VertexProgram, "ModelViewProj");
  141.         VertexColorParam = cgGetNamedParameter(VertexProgram, "IN.VertexColor");
  142.         }
  143.         glutMainLoop();
  144.         cgDestroyProgram(VertexProgram);
  145.         cgDestroyContext(Context);
  146.         return 0;
  147. }

复制代码

24

主题

256

帖子

267

积分

中级会员

Rank: 3Rank: 3

积分
267
 楼主| 发表于 2005-10-26 15:32:00 | 显示全部楼层

Re: Cg编程入门编(OpenGL)



  1. Listing 2 : vertexShader.cg
  2. Cg vertex shader code written. Heavily based on the runtime_ogl project in the Cg toolkit.
  3. struct appdata
  4. {
  5.         float4 position : POSITION;
  6.         float3 normal : NORMAL;
  7.         float3 color : DIFFUSE;
  8.         float3 VertexColor : SPECULAR;
  9. };
  10. struct vfconn
  11. {
  12.         float4 HPOS : POSITION;
  13.         float4 COL0 : COLOR0;
  14. };
  15. vfconn main(appdata IN,
  16. uniform float4 Kd,
  17. uniform float4x4 ModelViewProj)
  18. {
  19.         vfconn OUT;
  20.         OUT.HPOS = mul(ModelViewProj, IN.position);
  21.         OUT.COL0.xyz = Kd.xyz * IN.VertexColor.xyz;
  22.         OUT.COL0.w = 1.0;
  23.         return OUT;
  24. } // main

复制代码

24

主题

256

帖子

267

积分

中级会员

Rank: 3Rank: 3

积分
267
 楼主| 发表于 2005-10-26 15:37:00 | 显示全部楼层

Re: Cg编程入门编(OpenGL)

翻译是很辛苦,大家有空没空都别忘了支持一下。

原文:

sf_20051026153652.pdf

24.98 KB, 下载次数:

1

主题

42

帖子

52

积分

注册会员

Rank: 2

积分
52
发表于 2005-10-27 12:06:00 | 显示全部楼层

Re:Cg编程入门编(OpenGL)

顶下

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
发表于 2005-10-27 13:11:00 | 显示全部楼层

Re:Cg编程入门编(OpenGL)

顶~

39

主题

102

帖子

102

积分

注册会员

Rank: 2

积分
102
发表于 2005-10-28 08:21:00 | 显示全部楼层

Re:Cg编程入门编(OpenGL)

好,支持!!!!!!!!!!

0

主题

13

帖子

17

积分

新手上路

Rank: 1

积分
17
发表于 2005-10-31 12:09:00 | 显示全部楼层

Re:Cg编程入门编(OpenGL)

顶。。。。。。。。。。。。。。。。。。。。

1

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2007-10-27 16:09:00 | 显示全部楼层

Re:Cg编程入门编(OpenGL)

附件下不了呀!

2

主题

10

帖子

10

积分

新手上路

Rank: 1

积分
10
QQ
发表于 2007-10-30 20:56:00 | 显示全部楼层

Re:Cg编程入门编(OpenGL)

支持!!!!!!!!!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-17 11:07

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表