因为要往nsg中填加shader框架,所以从昨天开始着手学习cg,上午跟lzd问了一下传参的问题,发现了glColor设置的颜色其实只对COLOR的语义变量起作用,那DIFFUSE和SPECULAR的就不好使了,所以每次都要set一下parameter.
下边是我看到的cg使用的流程。
cg 使用流程:
- #ifdef __APPLE__
static CGprofile VertexProfile = CG_PROFILE_ARBVP1;
#else
static CGprofile VertexProfile = CG_PROFILE_VP20;
#endif
- CGContext context = cgCreateContext() ;
- CGProgram VertexProgram = cgCreateProgramFromFile(Context,CG_SOURCE, "vertexShader.cg",VertexProfile,NULL, NULL );
- if(VertexProgram != NULL)
{
/* Vertex shader only needs to be loaded once */
cgGLLoadProgram(VertexProgram);
/* Bind parameters to give access to variables in the shader */
KdParam = cgGetNamedParameter(VertexProgram, "Kd");
ModelViewProjParam = cgGetNamedParameter(VertexProgram, "ModelViewProj");
VertexColorParam = cgGetNamedParameter(VertexProgram, "IN.VertexColor");
}
- // 上边做一次初始化工作即可,下边是每次循环要用到的
- cgGLBindProgram(VertexProgram);
- if(KdParam != NULL)
cgGLSetParameter4f(KdParam, 1.0, 1.0, 0.0, 1.0);
if(ModelViewProjParam != NULL)
cgGLSetStateMatrixParameter(ModelViewProjParam,CG_GL_MODELVIEW_PROJECTION_MATRIX,CG_GL_MATRIX_IDENTITY);
- cgGLEnableProfile(VertexProfile);
- glBegin(GL_QUADS);
{
glNormal3fv(&CubeNormals[i][0]);
cgGLSetParameter3f(VertexColorParam, 1.0, 0.0, 0.0);
glVertex3fv(&CubeVertices[CubeFaces[i][0]][0]);
cgGLSetParameter3f(VertexColorParam, 0.0, 1.0, 0.0);
glVertex3fv(&CubeVertices[CubeFaces[i][1]][0]);
cgGLSetParameter3f(VertexColorParam, 0.0, 0.0, 1.0);
glVertex3fv(&CubeVertices[CubeFaces[i][2]][0]);
cgGLSetParameter3f(VertexColorParam, 1.0, 1.0, 1.0);
glVertex3fv(&CubeVertices[CubeFaces[i][3]][0]);
}
glEnd();
- cgGLDisableProfile(VertexProfile);
// 后边是收尾工作
- cgDestroyProgram(vertexProgram)
- cgDestroyContext(context);