您的位置:首页 > 其它

CG学习笔记①

2012-03-04 15:05 274 查看
头文件库文件包含:

#include <cg/cg.h>
#include <cg/cgD3D9.h>		//direct3d9环境
#pragma comment(lib,"cg.lib")
#pragma comment(lib,"cgD3D9.lib")


一些CG变量定义

static CGcontext myCgContext;

static CGprofile myCgVertexProfile;	//profile
static CGprogram myCgVertexProgram;	//顶点程序

static const WCHAR *myProgramNameW = L"CGDemo";
static const char *myProgramName = "CGDemo",					//程序名
*myVertexProgrameFileName = "CGv_green.cg",	//CG文件名
*myVertexProgrameName = "v_main";		//CG入口函数名


一、创建一个环境:

一个context是一个CG程序的容器。它包含了加载的所有的CG程序和它们共享的数据
myCgContext = cgCreateContext();	//创建一个CG环境
cgSetParameterSettingMode(myCgContext,CG_DEFERRED_PARAMETER_SETTING);	//设置参数设置模式


二、创建编译一个CG程序

使用cgCreateProgramFromFile函数把一个CG程序从文件加载到环境中。

const char **profilesopts;
/* Determine the best profile once a device to be set. */
myCgVertexProfile = cgD3D9GetLatestVertexProfile();
profilesopts = cgD3D9GetOptimalOptions(myCgVertexProfile);

myCgVertexProgram = cgCreateProgramFromFile(myCgContext,		//由cgCreateContext得到的环境
CG_SOURCE,			//类型:源代码或目标代码
myVertexProgrameFileName,	//程序文本的数据
myCgVertexProfile,	        //profile
myVertexProgrameName,       //入口函数名
profilesopts);	        //编译参数


三、加载一个CG程序

一个CG程序编译完成后需要将目标代码传递给3D API使用
//d3d设备传给CG运行库
cgD3D9SetDevice(pd3dDevice);
cgD3D9LoadProgram(myCgVertexProgram, //CGprogram
false,	     //参数阴影
0)  //汇编标记


四、执行CG程序

讲程序绑定到当前的3DAPI ,在之后的渲染中为每个vertext/fragment加载执行。

cgD3D9BindProgram(myCgVertexProgram);


五、资源释放

释放程序资源和环境资源

cgD3D9SetDevice(NULL);
//释放为一个单独程序分配的资源
cgDestroyProgram(myCgVertexProgram);
checkForCgError("destroying vertex program");
//释放为一个环境分配的所有资源
cgDestroyContext(myCgContext);


六、错误处理

char buffer[4096];
CGerror error;
const char *string = cgGetLastErrorString(&error);

if (error != CG_NO_ERROR)
{
if(error == CG_COMPILER_ERROR)
{
sprintf(buffer,
"Program:%s\n"
"Situation:%s\n"
"Error:%s\n"
"Cg compiler output...\n\n",
myProgramName,situation,string);
OutputDebugStringA(buffer);
OutputDebugStringA(cgGetLastListing(myCgContext));
sprintf(buffer,
"Program:%s\n"
"Situation:%s\n"
"Error:%s\n\n"
"Check debug output for Cg compiler output...\n",
myProgramName,situation,string);
MessageBoxA(0,buffer,"Cg compilation error",MB_OK | MB_ICONSTOP | MB_TASKMODAL);
}
else
{
sprintf(buffer,
"Program: %s \n"
"Situation: %s\n"
"Error: %s",
myProgramName, situation, string);
MessageBoxA(0, buffer,
"Cg runtime error", MB_OK | MB_ICONSTOP | MB_TASKMODAL);
}
exit(1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: