/* --------------------------------------------------------------- *\ file : cganim.c author : mathias gumz about : demonstrates linear vertexanimation calculated on : the gpu. usage of cg-language : : basicly the src-vertices are stored as normal : vertices and the dst-vertices are stored as : texcoords. additionally we need a scalar v which : tells the shader-program the position between : src- and dst-positions : $Id : $ \* --------------------------------------------------------------- */ /* ----------------------------------------------------- *\ includes \* ----------------------------------------------------- */ #include #include #include #include #include #include /* ----------------------------------------------------- *\ some constants \* ----------------------------------------------------- */ #define WHITE 1.0f, 1.0f, 1.0f #define RED 1.0f, 0.0f, 0.0f #define GREEN 0.2f, 1.0f, 0.4f #define BLUE 0.2f, 0.4f, 1.0f /* ----------------------------------------------------- *\ globals \* ----------------------------------------------------- */ /* window */ int w_width= 300; int w_height= 300; /* cg stuff */ CGcontext cg_context; CGprogram cg_program; CGprofile cg_profile; CGparameter param_t; CGparameter param_s; CGparameter param_mv; /* vertex data */ float vertices[]= { /* src quad */ /* dst quad */ -5.0f, 0.0f, -5.0f, -5.0f, 5.0f, -5.0f, 5.0f , 0.0f, -5.0f, 5.0f , 9.0f, -5.0f, 5.0f , 0.0f, 5.0f , 5.0f , 6.0f, 5.0f , -5.0f, 0.0f, 5.0f , -5.0f, 7.0f, 5.0f }; /* 0 <= value < 360 */ float value; /* ----------------------------------------------------- *\ prototypes \* ----------------------------------------------------- */ /* basic stuff */ int main(int argc, char* argv[]); int initGl(); int cleanGl(); /* glut */ void display(); void reshape(int w, int h); void idle(); void keys(unsigned char k, int x, int y); /* cg */ void error(); /* ----------------------------------------------------- *\ the real program \* ----------------------------------------------------- */ int main(int argc, char* argv[]) { glutInit(&argc, argv); initGl(); glutMainLoop(); cleanGl(); return 0; } int initGl() { /* setup glut first */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(w_width, w_height); glutCreateWindow("cganim"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keys); glutIdleFunc(idle); /* setup cg */ cg_context= cgCreateContext(); if (cgGLIsProfileSupported(CG_PROFILE_VP30)) cg_profile= CG_PROFILE_VP30; else if (cgGLIsProfileSupported(CG_PROFILE_VP20)) cg_profile= CG_PROFILE_VP20; else if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) cg_profile= CG_PROFILE_ARBFP1; else { printf("CGerror: no vertexprofil for your graphicsystem available, exit\n."); exit(1); } cgSetErrorCallback(error); cg_program= cgCreateProgramFromFile(cg_context, CG_SOURCE, "cganim.cg", cg_profile, "main", NULL); cgGLLoadProgram(cg_program); if (!cgIsProgram(cg_program)) { printf("CGerror: no cg-program loaded, exit.\n"); exit(1); } param_mv= cgGetNamedParameter(cg_program, "mv"); param_t= cgGetNamedParameter(cg_program, "t"); param_s= cgGetNamedParameter(cg_program, "s"); /* final opengl stuff */ glEnable(GL_DEPTH_TEST); return 0; } int cleanGl() { return 0; } void keys(unsigned char k, int x, int y) { switch (k) { case 27: cleanGl(); exit(0); break; } return; } void reshape(int w, int h) { glViewport(0, 0, w, h); w_width= w; w_height= h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(75.0f, (float)w/(float)h, 0.05, 40.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(2.0f, 15.0f, -10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glutPostRedisplay(); return; } void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /* coords */ glBegin(GL_LINES); glColor3f(WHITE); glVertex3f(-10.0f, 0.0f, 0.0f); glVertex3f(+10.0f, 0.0f, 0.0f); glColor3f(RED); glVertex3f( 0.0f,10.0f, 0.0f); glVertex3f( 0.0f,-10.0f, 0.0f); glColor3f(GREEN); glVertex3f( 0.0f, 0.0f,10.0f); glVertex3f( 0.0f, 0.0f,-10.0f); glEnd(); /* src */ glBegin(GL_LINE_LOOP); glColor3f(GREEN); glVertex3fv(&vertices[0]); glVertex3fv(&vertices[6]); glVertex3fv(&vertices[12]); glVertex3fv(&vertices[18]); glEnd(); /* dst */ glBegin(GL_LINE_LOOP); glColor3f(BLUE); glVertex3fv(&vertices[3]); glVertex3fv(&vertices[9]); glVertex3fv(&vertices[15]); glVertex3fv(&vertices[21]); glEnd(); cgGLSetParameter1f(param_t, 0.5 + 0.5 * sin(value)); cgGLSetParameter1f(param_s, 1.0f); cgGLSetStateMatrixParameter(param_mv, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); cgGLBindProgram(cg_program); cgGLEnableProfile(cg_profile); glBegin(GL_LINE_LOOP); glColor3f(WHITE); glVertex3fv(&vertices[0]); glTexCoord3fv(&vertices[3]); glVertex3fv(&vertices[6]); glTexCoord3fv(&vertices[9]); glColor3f(RED); glVertex3fv(&vertices[12]); glTexCoord3fv(&vertices[15]); glVertex3fv(&vertices[18]); glTexCoord3fv(&vertices[21]); glEnd(); cgGLDisableProfile(cg_profile); glutPostRedisplay(); glutSwapBuffers(); return; } void idle() { usleep(25); value+= ( value < 360.0f ? 0.02f : -360.0f ); return; } /* --------------------------------------------------------------- *\ vim:ts=2:tw=78:sw=2 : \* --------------------------------------------------------------- */