Hello,CG

第一个CG程序

1. 首先,自然是包含头文件

#include <cg/cg.h>
#include <cg/cgGL.h>
#pragma comment(lib,"cg.lib")
#pragma comment(lib,"cgGL.lib")

2. 创建着色器程序所需的变量

着色上下文CGcontext,着色器程序CGprogram,

CGprofile是用来告诉CG如何选择最适合你显卡的方式来处理顶点或片段着色,

CGparameter,参数传递,从你所写的程序中向着色器程序中传递参数.

static CGcontext Context = NULL;
static CGprogram VertexProgram = NULL;
static CGprofile VertexProfile;
static CGparameter KdParam = NULL;
static CGparameter ModelViewProjParam = NULL;
static CGparameter VertexColorParam = NULL;
//反射光颜色(kdParam),顶点颜色(vertexColorParam)和模型透视矩阵(modelViewProj)

3. 着色器初始化,在opengl程序的init函数中进行

Context = cgCreateContext();
VertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
VertexProgram = cgCreateProgramFromFile(Context,CG_SOURCE, "vertexShader.cg",VertexProfile,NULL, NULL);
cgGLLoadProgram(VertexProgram);
KdParam = cgGetNamedParameter(VertexProgram, "Kd");
ModelViewProjParam = cgGetNamedParameter(VertexProgram, "ModelViewProj");
VertexColorParam = cgGetNamedParameter(VertexProgram, "IN.VertexColor");

4. 着色器的参数传入,在display函数中进行

cgGLBindProgram(VertexProgram);
cgGLSetParameter4f(KdParam, 1.0, 1.0, 0.0, 1.0);
cgGLSetStateMatrixParameter(ModelViewProjParam,CG_GL_MODELVIEW_PROJECTION_MATRIX,CG_GL_MATRIX_IDENTITY);
cgGLEnableProfile(VertexProfile);

5. 最后别忘记了释放资源

cgDestroyProgram(VertexProgram);
cgDestroyContext(Context);

完整代码如下

View Code
#include <stdio.h>
#include <gl/glut.h>
#include <cg/cg.h>
#include <cg/cgGL.h>
#pragma comment(lib,"cg.lib")
#pragma comment(lib,"cgGL.lib")

#define WINDOW_SIZE 800

static CGcontext Context = NULL;
static CGprogram VertexProgram = NULL;
static CGprofile VertexProfile;
static CGparameter KdParam = NULL;
static CGparameter ModelViewProjParam = NULL;
static CGparameter VertexColorParam = NULL;
//反射光颜色(kdParam),顶点颜色(vertexColorParam)和模型透视矩阵(modelViewProj)

GLfloat CubeNormals[6][3] =
{
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
};
GLint CubeFaces[6][4] =
{
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
};
GLfloat CubeVertices[8][3];


static void InitializeCube(GLfloat v[8][3])
{
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
}

static void DrawCube(void)
{
int i;

cgGLBindProgram(VertexProgram);
cgGLSetParameter4f(KdParam, 1.0, 1.0, 0.0, 1.0);
cgGLSetStateMatrixParameter(ModelViewProjParam,CG_GL_MODELVIEW_PROJECTION_MATRIX,CG_GL_MATRIX_IDENTITY);
cgGLEnableProfile(VertexProfile);

for(i = 0; i < 6; i++)
{
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);
}

void init()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

InitializeCube(CubeVertices);


//-------------------------------cg init-------------------------------------------------------
Context = cgCreateContext();
VertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
VertexProgram = cgCreateProgramFromFile(Context,CG_SOURCE, "vertexShader.cg",VertexProfile,NULL, NULL);
cgGLLoadProgram(VertexProgram);
KdParam = cgGetNamedParameter(VertexProgram, "Kd");
ModelViewProjParam = cgGetNamedParameter(VertexProgram, "ModelViewProj");
VertexColorParam = cgGetNamedParameter(VertexProgram, "IN.VertexColor");
}

void display()
{
static float rotate=0.0;
rotate+=1.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0.0,0.0,-6.0);
glRotatef(30,1.0,0.0,0.0);
glRotatef(rotate,0.0,1.0,0.0);

DrawCube();
glutSwapBuffers();
}

void reshape(int w,int h)
{
if (h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.01f,400.0f);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutCreateWindow(argv[0]);
glutReshapeWindow(WINDOW_SIZE,WINDOW_SIZE);

glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);

init();
glutMainLoop();
cgDestroyProgram(VertexProgram);
cgDestroyContext(Context);
return 0;
}






posted @ 2011-10-17 20:06  wangwh0910  阅读(444)  评论(0)    收藏  举报