博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一章 在VS2008下如何配置好CG环境
阅读量:4041 次
发布时间:2019-05-24

本文共 4826 字,大约阅读时间需要 16 分钟。

第一步:去官网上下载Cg ToolKit并安装

写此文时,最新版本是

Cg Toolkit 3.1 - April 2012 (3.1.0013)

  • Windows 32/64-bit
    •  for Windows XP, Vista and Win7.
  • Mac OS X ppc/i386/x86_64
    •  for Tiger, Leopard and Snow Leopard.
  • Linux 32-bit
    •  tarball
    •  for RedHat
    •  for Debian and Ubuntu.
  • Linux 64-bit
    •  tarball
    •  for RedHat
    •  for Debian and Ubuntu.
选择适合自己的操作系统,下载好了,就安装吧!

我的安装路径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的源码贴上

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 
/* for printf and NULL */#include
/* for exit */#if __APPLE__#include
#else#include
#endif#include
/* Can't include this? Is Cg Toolkit installed! */#include
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;	}

转载地址:http://raxdi.baihongyu.com/

你可能感兴趣的文章
Hibernate设置主键自增,执行HQL语句
查看>>
设置MYSQL最大连接数与WAIT_TIMEOUT
查看>>
java根据ip地址获取详细地域信息
查看>>
解决s:iterator嵌套s:radio的传值问题
查看>>
位运算-不用加减乘除做加法。
查看>>
C++继承的三种方式(公有,私有,保护)
查看>>
待修改:C++多线程编程学习笔记
查看>>
冒泡、选择、插入、归并
查看>>
QTextEdit显示超链接
查看>>
使用socket下载文件(C++)
查看>>
cent os6.5静默安装oracle
查看>>
cent os6.5搭建oracle-dataguard
查看>>
使easyui-tree显示到指定层次
查看>>
给easyui-input元素添加js原生方法
查看>>
动态规划-最长公共子序列LCS
查看>>
动态规划-矩阵最小路径和
查看>>
动态规划-最长递增子序列
查看>>
spdlog输出格式设置
查看>>
ffmpeg-设置推流,拉流使用的协议类型(TCP/UDP)
查看>>
ffmpeg- 部分错误码-av_interleaved_write_frame/av_write_frame
查看>>