Principal | Gráficos 3D | Gráficos 2D | Fractales | Math | Códigos | Tutoriales | Links
Para animar un modelo, utilizamos Double-Buffered, en el Ejemplo
1 realizo la animación básica, rotando el modelo en torno
al eje Y, en el Ejemplo 2 realizo lo mismo pero visualizo
la cantidad de frames por segundo (FPS) en la pantalla.
Código fuentes de ambos ejemplos aqui.
Ejemplo 1: animación simple |
Ejemplo 2: la misma pero con contador de fps |
//Autor : Ramiro //Email : valcoey@hotmail.com #include <GL/glut.h> #include <stdlib.h> //angulo de rotación GLfloat angulo = 0.0; //inicializo la fuente de luz y las demas variables void init(void) { GLfloat light_ambient[] = { 0.75, 0.75, 0.75, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 0.1, 0.25, 1.0, 0.0 }; glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glShadeModel(GL_SMOOTH); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); } void display(void) { //defino un material en este caso es un Rojo GLfloat mat_ambient[] = {0.1, 0.1, 0.1, 1.0f}; GLfloat mat_diffuse[] = {0.308, 0.615, 0.824, 1.0f}; GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0f}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,-10.0f); //aplico la rotacion al eje Y glRotatef(angulo, 0.0f, 1.0f, 0.0f); //aplico el material glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf (GL_FRONT, GL_SHININESS, 50.0f); //este objeto esta definido en GLUT glutSolidTeapot(2.5); //intercambio los Buffers glutSwapBuffers(); } //utilizo la perspectiva void reshape (int width, int height) { if (height==0) { height=1; } glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // salgo si se preciona ESC void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } //que hacer en ausencia de entrada void Idle(void) { //incrementa el angulo de rotación angulo += 0.5; //redibuja la ventana display(); } int main(int argc, char** argv) { glutInit(&argc, argv); //inicializa la ventana en el modo Doble Buffer glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (300, 300); glutInitWindowPosition (0, 0); glutCreateWindow ("Rotacion 3D"); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutIdleFunc(Idle); //inicia el proceso de eventos glutMainLoop(); return 0; } |
//Autor : Ramiro //Email : valcoey@hotmail.com #include <GL/glut.h> #include <stdlib.h> #include <stdio.h> #include <string.h> static char label[100]; int w,h; int frame=0, time=0, timebase=0; float fps; float angulo = 0.0; void inline drawString (char *s) { unsigned int i; for (i = 0; i < strlen (s); i++) glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, s[i]); } void setOrthographicProjection() { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, w, 0, h); glScalef(1, -1, 1); glTranslatef(0, -h, 0); glMatrixMode(GL_MODELVIEW); } void resetPerspectiveProjection() { glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); } void init(void) { GLfloat light_ambient[] = { 0.75, 0.75, 0.75, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 0.1, 0.25, 1.0, 0.0 }; glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } void display(void) { GLfloat mat_ambient[] = {0.1, 0.1, 0.1, 1.0f}; GLfloat mat_diffuse[] = {0.308, 0.615, 0.824, 1.0f}; GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0f}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glLoadIdentity(); glTranslatef(0.0f,0.0f,-10.0f); glRotatef(angulo, 0.0f, 1.0f, 0.0f); glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf (GL_FRONT, GL_SHININESS, 50.0f); glutSolidTeapot(2.5); glPopMatrix(); setOrthographicProjection(); glPushMatrix(); glPushAttrib( GL_LIGHTING_BIT ); glDisable( GL_LIGHTING ); glLoadIdentity(); glColor3f(1.0f,1.0f,1.0f); sprintf(label,"FPS:%5.2f",fps); glRasterPos2f(30.0,30.0); drawString (label); glPopAttrib(); glPopMatrix(); resetPerspectiveProjection(); glutSwapBuffers(); } void reshape (int width, int height) { if (height==0) height=1; w=width; h=height; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } void Idle(void) { angulo += 0.5; frame++; time=glutGet(GLUT_ELAPSED_TIME); if (time - timebase > 1000) { fps=frame*1000.0/(time-timebase); timebase = time; frame = 0; } display(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (300, 300); glutInitWindowPosition (0, 0); glutCreateWindow ("Rotacion 3D (FPS)"); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutIdleFunc(Idle); glutMainLoop(); return 0; } |
valcoey@hotmail.com
Ramiro, 2002
Principal | Gráficos 3D | Gráficos 2D | Fractales | Math | Códigos | Tutoriales | Links