/* --------------------------------------------------------------- *\ 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_fprogram; CGprogram cg_vprogram; CGprofile cg_fprofile; CGprofile cg_vprofile; CGparameter param_t; /*displacement*/ CGparameter param_s; /*scale*/ CGparameter param_mv; /*modelview*/ CGparameter param_r; /*ramp texture id*/ /* 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 }; /* 1d texture */ unsigned int rampid; float ramp[]= { RED, GREEN, BLUE, WHITE }; /* 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(); cgSetErrorCallback(error); cg_vprofile= cgGLGetLatestProfile(CG_GL_VERTEX); cgGLSetOptimalOptions(cg_vprofile); cg_fprofile= cgGLGetLatestProfile(CG_GL_FRAGMENT); cg_vprogram= cgCreateProgramFromFile(cg_context, CG_SOURCE, "cganim2v.cg", cg_vprofile, NULL, NULL); cgGLLoadProgram(cg_vprogram); cg_fprogram= cgCreateProgramFromFile(cg_context, CG_SOURCE, "cganim2f.cg", cg_fprofile, NULL, NULL); cgGLLoadProgram(cg_fprogram); param_mv= cgGetNamedParameter(cg_vprogram, "mv"); param_t= cgGetNamedParameter(cg_vprogram, "t"); param_s= cgGetNamedParameter(cg_vprogram, "s"); param_r= cgGetNamedParameter(cg_fprogram, "r"); /* final opengl stuff */ glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_1D); glGenTextures(1, &rampid); glBindTexture(GL_TEXTURE_1D, rampid); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage1D(GL_TEXTURE_1D, 0, 3, 4, 0, GL_RGB, GL_FLOAT, ramp); glDisable(GL_TEXTURE_1D); 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); cgGLSetTextureParameter(param_r, rampid); cgGLBindProgram(cg_vprogram); cgGLBindProgram(cg_fprogram); cgGLEnableProfile(cg_vprofile); cgGLEnableProfile(cg_fprofile); glBegin(GL_QUADS); glColor3f(WHITE); glVertex3fv(&vertices[0]); glTexCoord3fv(&vertices[3]); glVertex3fv(&vertices[6]); glTexCoord3fv(&vertices[9]); glVertex3fv(&vertices[12]); glTexCoord3fv(&vertices[15]); glVertex3fv(&vertices[18]); glTexCoord3fv(&vertices[21]); glEnd(); cgGLDisableProfile(cg_vprofile); cgGLDisableProfile(cg_fprofile); glutPostRedisplay(); glutSwapBuffers(); return; } void idle() { usleep(25); value+= ( value < 360.0f ? 0.02f : -360.0f ); return; } /* --------------------------------------------------------------- *\ vim:ts=2:tw=78:sw=2 : \* --------------------------------------------------------------- */