第一步:去官网上下载Cg ToolKit并安装
https://developer.nvidia.com/cg-toolkit-download
写此文时,最新版本是
Cg Toolkit 3.1 - April 2012 (3.1.0013)
- Windows 32/64-bit
- installer for
Windows XP, Vista and Win7.
- installer for
- Mac OS X ppc/i386/x86_64
- dmg for
Tiger, Leopard and Snow Leopard.
- dmg for
- Linux 32-bit
- Linux 64-bit
选择适合自己的操作系统,下载好了,就安装吧!
我的安装路径D:\Program Files\NVIDIA Corporation\Cg
第二步:设置路径
1.新建用户变量
变量:CG_HOME
变量值:D:\Program Files\NVIDIA Corporation\Cg
2.在path路径添加:D:\Program Files\NVIDIA Corporation\Cg\bin;
3.VS2008工具-选项-项目和解决方案-VC++目录
包含文件目录添加:$(CG_HOME)\include
库文件目录添加:$(CG_HOME)\lib
第三步:尝试在OPENGL里面使用(因为DX暂时没学。。。)
1.先打开D:\Program Files\NVIDIA Corporation\Cg\examples\OpenGL\basic\01_vertex_program文件夹。你可以看下里面的内容,这就是我们程序所需要的一些文件,这是书上的例子
2.新建-项目(我这里项目名是The_first_Cg_program),这里选择Win32控制台应用程序
3.项目-属性-配置属性-链接器-输入-附加依赖项 ,写上cg.lib cgGl.lib
4.新建源文件(我这里源文件名是main.cpp),将先前已打开的文件夹下面的01_vertex_program.c里面文件内容复制到源文件中
5.需要将cg源文件复制到项目目录下,将先前打开的文件夹下面的C2E1v_green.cg复制到你的项目文件夹(我这里是E:\《The Cg Turorial》\chap1\The_first_Cg_program\The_first_Cg_program)。
另外补充说明,其实这样就可以执行了,但是了方便的编辑Cg源文件,这里强烈推荐这样做
找到解决方案资源管理器,右键单击The_first_Cg_program项目,添加-新建筛选器,命名为Cg,右键点击Cg文件夹,添加-现有项,选择刚才拷贝过来的C2E1v_green.cg,这样以后就可以直接在vs2008里面编辑Cg源文件了
6.OK,编译执行。绿色三角形终于出来了,结果如下图:
最后,为了方便那些伸手党,我给main.cpp和C2E1v_green.cg的源码贴上
最最后,我遇到了 ”无法定位程序输入点_glutInitWithExit于动态链接库“ 的错误,
解决方法:
64位操作系统:将Cg安装目录下 ...\Cg\bin\glut32.dll 或 Cg\bin.x64\glut32.dll 覆盖复制到文件夹 C:\Windows\SysWOW64 中
32位操作系统:将Cg安装目录下 ...\Cg\bin\glut32.dll 覆盖复制到文件夹 C:\Windows\System32 中
main.cpp文件
/* 01_vertex_program.c - OpenGL-based very simple vertex program example
using Cg program from Chapter 2 of "The Cg Tutorial" (Addison-Wesley,
ISBN 0321194969). */
/* Requires the OpenGL Utility Toolkit (GLUT) and Cg runtime (version
1.0 or higher). */
#include <stdio.h> /* for printf and NULL */
#include <stdlib.h> /* for exit */
#if __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <Cg/cg.h> /* Can't include this? Is Cg Toolkit installed! */
#include <Cg/cgGL.h>
static CGcontext myCgContext;
static CGprofile myCgVertexProfile;
static CGprogram myCgVertexProgram;
static const char *myProgramName = "01_vertex_program",
*myVertexProgramFileName = "C2E1v_green.cg",
/* Page 38 */ *myVertexProgramName = "C2E1v_green";
static void checkForCgError(const char *situation)
{
CGerror error;
const char *string = cgGetLastErrorString(&error);
if (error != CG_NO_ERROR) {
printf("%s: %s: %s\n",
myProgramName, situation, string);
if (error == CG_COMPILER_ERROR) {
printf("%s\n", cgGetLastListing(myCgContext));
}
exit(1);
}
}
/* Forward declared GLUT callbacks registered by main. */
static void display(void);
static void keyboard(unsigned char c, int x, int y);
int main(int argc, char **argv)
{
glutInitWindowSize(400, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&argc, argv);
glutCreateWindow(myProgramName);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glClearColor(0.1, 0.3, 0.6, 0.0); /* Blue background */
myCgContext = cgCreateContext();
checkForCgError("creating context");
cgGLSetDebugMode(CG_FALSE);
cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);
myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(myCgVertexProfile);
checkForCgError("selecting vertex profile");
myCgVertexProgram =
cgCreateProgramFromFile(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
myVertexProgramFileName, /* Name of file containing program */
myCgVertexProfile, /* Profile: OpenGL ARB vertex program */
myVertexProgramName, /* Entry function name */
NULL); /* No extra compiler options */
checkForCgError("creating vertex program from file");
cgGLLoadProgram(myCgVertexProgram);
checkForCgError("loading vertex program");
glutMainLoop();
return 0;
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cgGLBindProgram(myCgVertexProgram);
checkForCgError("binding vertex program");
cgGLEnableProfile(myCgVertexProfile);
checkForCgError("enabling vertex profile");
/* Rendering code verbatim from Chapter 1, Section 2.4.1 "Rendering
a Triangle with OpenGL" (page 57). */
glBegin(GL_TRIANGLES);
glVertex2f(-0.8, 0.8);
glVertex2f(0.8, 0.8);
glVertex2f(0.0, -0.8);
glEnd();
cgGLDisableProfile(myCgVertexProfile);
checkForCgError("disabling vertex profile");
glutSwapBuffers();
}
static void keyboard(unsigned char c, int x, int y)
{
switch (c) {
case 27: /* Esc key */
/* Demonstrate proper deallocation of Cg runtime data structures.
Not strictly necessary if we are simply going to exit. */
cgDestroyProgram(myCgVertexProgram);
cgDestroyContext(myCgContext);
exit(0);
break;
}
}
C2E1v_green.cg源码,注意这里只用了顶点着色器
// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard. See page 38.
struct C2E1v_Output {
float4 position : POSITION;
float3 color : COLOR;
};
C2E1v_Output C2E1v_green(float2 position : POSITION)
{
C2E1v_Output OUT;
OUT.position = float4(position,0,1);
OUT.color = float3(0,1,0);
return OUT;
}