|
|
安好cg toolkit
就能在include 里找到header files, library files啦,别忘了include 和link它们
bin里的dll文件,丢到运行的文件夹使得运行文件可以link到, 当然罗, windows专用, windows rocks! 哈 
- /**main.cpp*/
- #include <stdio.h>
- #include <stdlib.h>
- /**include cg headers*/
- #include <Cg/cg.h>
- #include <Cg/cgGL.h>
- #include <gl/glut.h>
- /**retain program reference for releasing*/
- CGprogram program;
- /**retain context reference for releasing*/
- CGcontext context;
- /**
- display callback function
- */
- void display(void)
- {
- glClear (GL_COLOR_BUFFER_BIT);/* clear all pixels */
- glColor3f (1.0, 1.0, 1.0);
- glBegin(GL_POLYGON);/* draw white polygon with corners at(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)*/
- glVertex2f (0.25, 0.25);
- glVertex2f (0.75, 0.25);
- glVertex2f (0.75, 0.75);
- glVertex2f (0.25, 0.75);
- glEnd();
- glFlush ();/* start processing buffered OpenGL routines */
- }
- /**
- output cg errors
- */
- static void CheckCgError(int no)
- {
- CGerror err = cgGetError();
- if (err != CG_NO_ERROR)
- {
- //output error number
- printf("%d: ",no);
- printf("CG error: %s\n", cgGetErrorString(err));
- }
- }
- void init (void)
- {
- //create cg context
- context=cgCreateContext();
- CheckCgError(1);
- //get a valid profile
- CGprofile vertexProfile;
- if (cgGLIsProfileSupported(CG_PROFILE_ARBVP1)){
- //store this profile
- vertexProfile=CG_PROFILE_ARBVP1;
- CheckCgError(2);
-
- //get cg source code from a file named "MYVP1.cg", its entry function is "program1"
- program=cgCreateProgramFromFile(context, CG_SOURCE,"MYVP1.cg",
- vertexProfile, "program1",NULL);
- CheckCgError(3);
- //load this program
- cgGLLoadProgram(program);
- CheckCgError(4);
- //output the compiled information
- printf("---- PROGRAM BEGIN ----\n%s---- PROGRAM END ----\n",
- cgGetProgramString(program, CG_COMPILED_PROGRAM));
- //enable the profile
- cgGLEnableProfile(vertexProfile);
- //bind the program
- cgGLBindProgram(program);
- }
-
- glClearColor (0.0, 0.0, 0.0, 0.0);/* select clearing color */
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);/* initialize viewing values */
- }
- //release cg resource before end of program
- void destoryApp(){
- cgDestroyProgram(program);
- cgDestroyContext(context);
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/*Declare initial display mode(single buffer and RGBA).*/
- glutInitWindowSize (250, 250); /*Declare initial window size.*/
- glutInitWindowPosition (100, 100);/*Declare initial window position.*/
- glutCreateWindow ("hello");/*Open window with "hello"in its title bar.*/
- init ();/*Call initialization routines.*/
- glutDisplayFunc(display); /*Register callback function to display graphics.*/
- glutMainLoop();/*Enter main loop and process events.*/
-
- destoryApp();
- return 0; /* ANSI C requires main to return int. */
- }
- //MYVP1.cg
- struct Output
- {
- float4 position : POSITION;
- float4 color : COLOR;
- };
- Output program1(float2 position : POSITION)
- {
- Output OUT;
- OUT.position = float4(position,0,1);
- if(position.x>0.5)
- OUT.color = float4(0,1,0,1);
- else
- OUT.color = float4(1,0,0,1);
- return OUT;
- } // main
复制代码 |
|