shader学习笔记(1)-basic cg in openGL

shader学习笔记(1)-basic cg in openGL

因为要往nsg中填加shader框架,所以从昨天开始着手学习cg,上午跟lzd问了一下传参的问题,发现了glColor设置的颜色其实只对COLOR的语义变量起作用,那DIFFUSE和SPECULAR的就不好使了,所以每次都要set一下parameter.

下边是我看到的cg使用的流程。


cg 使用流程:

  1. #ifdef __APPLE__

static CGprofile VertexProfile = CG_PROFILE_ARBVP1;

#else

static CGprofile VertexProfile = CG_PROFILE_VP20;

#endif

  1. CGContext context = cgCreateContext() ;
  2. CGProgram VertexProgram = cgCreateProgramFromFile(Context,CG_SOURCE, "vertexShader.cg",VertexProfile,NULL, NULL );
  3. 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");

}

  1. // 上边做一次初始化工作即可,下边是每次循环要用到的
  2. cgGLBindProgram(VertexProgram);
  3. 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);

  1. cgGLEnableProfile(VertexProfile);
  2. 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();

  1. cgGLDisableProfile(VertexProfile);

// 后边是收尾工作

  1. cgDestroyProgram(vertexProgram)
  2. cgDestroyContext(context);

 

你可能感兴趣的:(shader学习笔记(1)-basic cg in openGL)