前面已经写过了,如果在OGL上使用CG了.
使用了如下关键代码,函数:
使用了如下关键代码,函数:
1.头文件
#include <Cg/cg.h>
#include <Cg/cgGL.h>
2.创建一个环境
CGcontext context = cgCreateContext();
3.编译一个程序
CGprogram program =
cgCreateProgram( context, //由cgCreateContext得到的环境
CG_SOURCE, //类型:源代码或目标代码
programString, //程序文本和数据
profile, //profile
"main" //入口函数名
args); //编译参数
#include <Cg/cg.h>
#include <Cg/cgGL.h>
2.创建一个环境
CGcontext context = cgCreateContext();
3.编译一个程序
CGprogram program =
cgCreateProgram( context, //由cgCreateContext得到的环境
CG_SOURCE, //类型:源代码或目标代码
programString, //程序文本和数据
profile, //profile
"main" //入口函数名
args); //编译参数
4.载入一个程序
cgGLLoadProgram(program);
5. 修改程序参数
CGparameter myParameter = cgGetNamedParameter( program, "myParameter" );
cgGLSetParameter4fv( myParameter, value);
6. 执行程序
激活一个profile cgGLEnableProfile(GL_PROFILE_ARBVP1);
绑定一个程序 cgGLBindProgram ( program );
禁止一个profile cgGLDisableProfile(CG_PROFILE_ARBVP1);
cgGLLoadProgram(program);
5. 修改程序参数
CGparameter myParameter = cgGetNamedParameter( program, "myParameter" );
cgGLSetParameter4fv( myParameter, value);
6. 执行程序
激活一个profile cgGLEnableProfile(GL_PROFILE_ARBVP1);
绑定一个程序 cgGLBindProgram ( program );
禁止一个profile cgGLDisableProfile(CG_PROFILE_ARBVP1);
7.释放资源
cgDestoryProgram( program );
cgDestoryContext( context );
cgDestoryProgram( program );
cgDestoryContext( context );
前面复习了光照模型,下面把这个顶点光照的CG代码贴出来.
uniform float4x4 modelViewProj : state.matrix.mvp;
void main(float4 position:POSITION, float3 normal:NORMAL, out float4 oPosition:POSITION, out float4 color:COLOR,
uniform float3 globalAmbient,
uniform float3 lightColor, uniform float3 lightPosition, uniform float3 eyePosition, uniform float3 Ke, uniform float3 Ka, uniform float3 Kd, uniform float3 Ks, uniform float shininess) { oPosition = mul(modelViewProj, position);
float3 P = position.xyz;
float3 N = normal; //计算自发光量 float3 emissive = Ke; //计算环境光量 float3 ambient = Ka * globalAmbient; //计算漫反射量 float3 L = normalize(lightPosition - P); float diffuseLight = max(dot(N, L), 0); float3 diffuse = Kd * lightColor * diffuseLight; //计算镜面反射光亮 float3 V = normalize(eyePosition - P); float3 H = normalize(L + V); float specularLight = pow(max(dot(N, H), 0), shininess); if(diffuseLight <= 0) specularLight = 0; float3 specular = Ks * lightColor * specularLight; //将所有的光加起来,:-> color.xyz = emissive + ambient + diffuse + specular; color.w = 1;
}[/code]
上面带有uniform前缀的参数,都是外部传入CG的参数 所以在外部我们定义几个全局变量. [code] float eyePosition[3]={0.0f, 0.0f, 10.0f}; float globalAmbient[3]={0.0f, 1.0f, 1.0f}; float Ka[3]={0.3f, 0.3f, 0.3f}; float Kd[3]={0.3f, 0.3f, 0.3f}; float Ke[3]={0.0f, 0.0f, 0.0f}; float Ks[3]={0.6f, 0.6f, 0.6f}; float lightcolor[3]={1.0f, 1.0f, 1.0f}; float lightPosition[3]={5.0f, 2.0f, 6.0f}; float shininess=5.0f;
CGparameter cg_globalAmbient;
CGparameter cg_lightcolor; CGparameter cg_lightPosition; CGparameter cg_eyePosition; CGparameter cg_Ke; CGparameter cg_Ka; CGparameter cg_Kd; CGparameter cg_Ks; CGparameter cg_shininess;
[/code]
在initShader()函数中我们需要告诉CG这些参数,加入
[code] cg_eyePosition=cgGetNamedParameter(g_CGprogram_vertex, "eyePosition");
cg_globalAmbient=cgGetNamedParameter(g_CGprogram_vertex, "globalAmbient"); cg_Ka=cgGetNamedParameter(g_CGprogram_vertex, "Ka"); cg_Kd=cgGetNamedParameter(g_CGprogram_vertex, "Kd"); cg_Ke=cgGetNamedParameter(g_CGprogram_vertex, "Ke"); cg_Ks=cgGetNamedParameter(g_CGprogram_vertex, "Ks"); cg_lightcolor=cgGetNamedParameter(g_CGprogram_vertex, "lightColor"); cg_lightPosition=cgGetNamedParameter(g_CGprogram_vertex, "lightPosition"); cg_shininess=cgGetNamedParameter(g_CGprogram_vertex, "shininess"); [/code]
最后在render函数部分,我们画一个球来测试CG写的光照程序的效果.
首先传入光照的各项参数.
[code] cgGLSetParameter3fv(cg_eyePosition, eyePosition);
cgGLSetParameter3fv(cg_globalAmbient, globalAmbient); cgGLSetParameter3fv(cg_Ka, Ka); cgGLSetParameter3fv(cg_Kd, Kd); cgGLSetParameter3fv(cg_Ke, Ke); cgGLSetParameter3fv(cg_Ks, Ks); cgGLSetParameter3fv(cg_lightcolor, lightcolor); cgGLSetParameter3fv(cg_lightPosition, lightPosition); cgGLSetParameter1f(cg_shininess, shininess); [/code]
开始执行CG程序
[code] cgGLBindProgram( g_CGprogram_vertex );
cgGLEnableProfile( g_CGprofile_vertex );
glTranslatef(0.0f, 0.0f, -10.0f);
gluSphere(quadratic,1.3f,32,32); cgGLDisableProfile( g_CGprofile_vertex ); |