www.pudn.com > colorvary1.rar > colorvary1.cpp


#include  
#include  
#include  
 
#include  
#include  
#include  
 
static CGcontext Context = NULL; 
static CGprogram Program = NULL; 
static CGparameter PosParam = NULL; 
static CGparameter ColorParam = NULL; 
 
static CGprofile profile; 
 
static void CheckCgError(void) 
{ 
  CGerror err = cgGetError(); 
 
  if (err != CG_NO_ERROR) 
   { 
     printf("CG error: %s\n", cgGetErrorString(err)); 
     exit(1); 
   } 
} 
 
static void RenderScene(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
 
	cgGLBindProgram(Program); 
	 
//	if(PosParam != NULL) 
//		cgGLSetParameter2f(PosParam, 1.0, 0.0); 
 
	cgGLEnableProfile(profile); 
 
	glBegin(GL_TRIANGLES); 
 
	cgGLSetParameter4f(ColorParam, 1.0f, 0.0f, 0.0f, 1.0f); 
//	glVertex2f(-0.8, 0.8); 
	cgGLSetParameter2f(PosParam, -0.8, 0.2); 
	cgGLSetParameter4f(ColorParam, 0.0f, 1.0f, 0.0f, 1.0f); 
	glVertex2f(0.8, 0.8); 
	cgGLSetParameter4f(ColorParam, 0.0f, 0.0f, 1.0f, 1.0f); 
	glVertex2f(0.0, -0.5); 
 
	glEnd(); 
 
	cgGLDisableProfile(profile); 
 
	// Flush drawing commands 
	glutSwapBuffers(); 
 
} 
 
void SetupRC() 
{ 
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); 
	 
//	glEnable(GL_DEPTH_TEST);	 
//    glEnable(GL_DITHER); 
//    glShadeModel(GL_FLAT); 
//	glFrontFace(GL_CCW);	//ÄæÊ±ÕëΪÕý 
} 
 
void ChangeSize(int w, int h) 
{ 
	GLfloat nRange = 100.0f; 
 
	if(h == 0) 
		h = 1; 
 
    glViewport(0, 0, w, h); 
 
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
 
	// Establish clipping volume (left, right, bottom, top, near, far) 
    if (w <= h)  
		glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); 
    else  
		glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); 
 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
} 
 
int main(int argc, char* argv[]) 
{ 
	glutInit(&argc, argv); 
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
	glutCreateWindow("colorvary1"); 
	glutReshapeFunc(ChangeSize); 
	glutDisplayFunc(RenderScene); 
	SetupRC(); 
 
	profile = CG_PROFILE_VP20; 
	Context = cgCreateContext(); 
	CheckCgError(); 
 
	/* Test adding source text to context */ 
	Program = cgCreateProgramFromFile(Context, 
                                    CG_SOURCE,  
									"varying.cg", 
                                    profile, 
                                    "C3E2v_varying",  
									NULL); 
	CheckCgError(); 
 
	fprintf(stderr, "LAST LISTING----%s----\n", cgGetLastListing(Context)); 
 
	fprintf(stderr, "---- PROGRAM BEGIN ----\n%s---- PROGRAM END ----\n", 
        cgGetProgramString(Program, CG_COMPILED_PROGRAM)); 
 
	if(Program != NULL) 
	{ 
		cgGLLoadProgram(Program); 
		CheckCgError(); 
 
		PosParam = cgGetNamedParameter(Program, "position"); 
		CheckCgError(); 
 
		ColorParam = cgGetNamedParameter(Program, "color"); 
		CheckCgError(); 
	 
	} 
 
	glutMainLoop(); 
 
	cgDestroyProgram(Program); 
	cgDestroyContext(Context); 
 
	return 0; 
}