• Jueves 28 de Marzo de 2024, 14:26

Autor Tema:  Pez 3D  (Leído 1708 veces)

politico6

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Pez 3D
« en: Jueves 26 de Mayo de 2011, 20:12 »
0
Hola a todos, necesito a partir de un pez hecho en 2D con transformaciones geométricas, con su zoom, movimientos de translación..etc.. a partir de eso hacerlo en 3D... y no se como hacerlo ni que funciones utilizar... alguien me puede echar una mano????? Gracias..
Os dejo el codigo:
Código: C
  1.  #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6. #include <GL/glut.h>
  7.  
  8. int alto=800;
  9. int ancho=800;
  10. float zoom=1.0, tx=0.0, ty=0.0;
  11.  
  12. static void Init( void )
  13. {
  14.     glClearIndex( 0.0 );
  15.     glShadeModel( GL_FLAT );
  16. }
  17.  
  18. static void Reshape( int width, int height )
  19. {   
  20.     ancho=width;
  21.     alto=height;
  22.  
  23.  
  24.     glMatrixMode(GL_PROJECTION);
  25.     glLoadIdentity();   
  26.     glViewport(0.1, 0.1, (GLint)width/2, (GLint)height/2);
  27.     glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  28.     glScalef(zoom,zoom,1.0);
  29.         glTranslatef(tx,ty,0.0);
  30.  
  31.     glClearColor(0.0,0,0.7,0.0);
  32.     glClear( GL_COLOR_BUFFER_BIT );
  33.     glColor3f(0.0,0.0,0.0);
  34.         glMatrixMode(GL_MODELVIEW); 
  35.     glLoadIdentity();
  36.     glCallList(2);
  37.                 //primera figura izquierda abajo
  38.  
  39.  
  40.     glMatrixMode(GL_PROJECTION);
  41.     glLoadIdentity();   
  42.     glViewport((GLint)width/2, 0.0, (GLint)width/2, (GLint)height/2);
  43.     glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  44.     glScalef(zoom,zoom,1.0);
  45.     glTranslatef(tx,ty,0.0);
  46.  
  47.         glMatrixMode(GL_MODELVIEW); 
  48.     glLoadIdentity();
  49.     glCallList(2);
  50.             //segunda figura derecha abajo
  51.  
  52.  
  53.     glMatrixMode(GL_PROJECTION);
  54.     glLoadIdentity();   
  55.     glViewport(0, (GLint)width/2, (GLint)width/2, (GLint)height/2);
  56.     glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  57.     glScalef(zoom,zoom,1.0);
  58.     glTranslatef(tx,ty,0.0);
  59.  
  60.         glMatrixMode(GL_MODELVIEW); 
  61.     glLoadIdentity();
  62.     glCallList(2);
  63.             // tercera figura izquierda arriba
  64.  
  65.  
  66.     glMatrixMode(GL_PROJECTION);
  67.     glLoadIdentity();   
  68.     glViewport((GLint)width/2, (GLint)width/2, (GLint)width/2, (GLint)height/2);
  69.     glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  70.     glScalef(zoom,zoom,1.0);
  71.     glTranslatef(tx,ty,0.0);
  72.  
  73.         glMatrixMode(GL_MODELVIEW); 
  74.     glLoadIdentity();
  75.     glCallList(2);
  76.             //cuarta figura derecha arriba
  77.  
  78.  
  79.     glFlush();
  80.  
  81. }
  82.  
  83.  
  84.  
  85. static void Key_Esc(unsigned char key, int x, int y )
  86. {
  87.     if (key==27)
  88.             exit(0);
  89.  
  90.     if (key ==43){
  91.         zoom*=1.1;
  92.         Reshape(ancho,alto);
  93.     }
  94.     if (key==45){
  95.         zoom*=0.9;
  96.         Reshape(ancho,alto);
  97.     }
  98. }
  99.  
  100.  
  101. static void Special(int key, int x, int y )
  102. {
  103.     if (key==GLUT_KEY_LEFT){
  104.                 tx=tx-0.2;
  105.                 Reshape(ancho,alto);
  106.     }   
  107.         if (key==GLUT_KEY_RIGHT){
  108.         tx=tx+0.2;
  109.         Reshape(ancho,alto);
  110.     }
  111.     if (key==GLUT_KEY_UP){
  112.         ty=ty+0.2;
  113.         Reshape(ancho,alto);
  114.     }
  115.     if (key==GLUT_KEY_DOWN){
  116.         ty=ty-0.2;
  117.         Reshape(ancho,alto);
  118.     }
  119. }
  120.  
  121.  
  122. static void triangulo ()
  123. {   
  124.     glNewList(1, GL_COMPILE);
  125.    
  126.         glBegin(GL_TRIANGLES);
  127.         // Dibujamos un triángulo
  128.         glColor3f (1.0,0.6, 0.0);
  129.         // Color
  130.         glVertex3f(0.0,0.0,0.0);
  131.         // Coordenadas del primer vértice
  132.         glVertex3f(0.0,0.4,0.0);
  133.         // Coordenadas del segundo vértice
  134.         glVertex3f(0.4,0.0,0.0);
  135.         // Coordenadas del tercer vértice
  136.                 glEnd();
  137.  
  138.     glEndList();    // Terminamos de dibujar
  139.  
  140. }
  141.  
  142. static void cuadrado()
  143. {   
  144.     glBegin(GL_QUADS);
  145.     glColor3f (1.0,0.6, 0.0);
  146.         //color
  147.     glVertex3f(0.0,0.0,0.0);
  148.     glVertex3f(0.0,0.2,0.0);
  149.     glVertex3f(0.2,0.2,0.0);   
  150.     glVertex3f(0.2,0.0,0.0);
  151.  
  152.     glEnd();
  153. }
  154.  
  155. void pez(){
  156.  
  157.     glNewList(2, GL_COMPILE);
  158.     
  159.     glPushMatrix();
  160.     glTranslatef(0.0,0.0,0.0);
  161.     glRotatef(0.0,0.0,0.0,0.0);
  162.     glCallList(1);
  163.         glPopMatrix();
  164.         // cuerpo de pez 1
  165.  
  166.     glPushMatrix();
  167.     glRotatef(180.0,1.0,0.0,0.0);
  168.     glCallList(1);
  169.     glPopMatrix();
  170.         // cuerpo de pez 2
  171.  
  172.     glPushMatrix();
  173.     glTranslatef(0.0,0.2,0.0);
  174.     glRotatef(180.0,0.0,1.0,0.0);                                  
  175.     glScalef(0.5,0.5,0.0);
  176.     glCallList(1);
  177.     glPopMatrix();
  178.         // ala de arriba 1
  179.  
  180.     glPushMatrix();
  181.     glTranslatef(-0.2,0.2,0.0);
  182.     glRotatef(180.0,1.0,0.0,0.0);                                  
  183.     glScalef(0.5,0.5,0.0);
  184.     glCallList(1);
  185.     glPopMatrix();
  186.         // ala de arriba 2
  187.  
  188.     glPushMatrix();
  189.     glTranslatef(-0.2,-0.2,0.0);
  190.     glRotatef(0.0,0.0,0.0,0.0);                                
  191.     glScalef(0.5,0.5,0.0);
  192.     glCallList(1);
  193.     glPopMatrix();
  194.         // ala de abajo 1
  195.    
  196.     glPushMatrix();
  197.     glTranslatef(0.0,-0.2,0.0);
  198.     glRotatef(180.0,0.0,0.0,1.0);                                  
  199.     glScalef(0.5,0.5,0.0);
  200.     glCallList(1);
  201.     glPopMatrix();
  202.         // ala de abajo 2
  203.  
  204.     glPushMatrix();
  205.     glTranslatef(-0.4,0.0,0.0);
  206.  
  207.     glRotatef(0.0,0.0,0.0,0.0);                                
  208.     glScalef(0.5,0.5,0.0);
  209.     glCallList(1);
  210.     glPopMatrix();
  211.         // cola 1
  212.    
  213.     glPushMatrix();
  214.     glTranslatef(-0.4,0.0,0.0);
  215.     glRotatef(180.0,1.0,0.0,0.0);                                  
  216.     glScalef(0.5,0.5,0.0);
  217.     glCallList(1);
  218.     glPopMatrix();
  219.         // cola 2
  220.  
  221.     glPushMatrix();
  222.     glTranslatef(0.5,0.1,0.0);
  223.     glRotatef(45.0,0.0,0.0,1.0);                                   
  224.     glScalef(0.5,0.5,0.0);
  225.     glCallList(1);
  226.     glPopMatrix();
  227.         // gota 1
  228.  
  229.     glPushMatrix();
  230.     glTranslatef(0.5,0.38,0.0);
  231.     glRotatef(225.0,0.0,0.0,1.0);                                  
  232.     glScalef(0.5,0.5,0.0);
  233.     glCallList(1);
  234.     glPopMatrix();
  235.         // gota 2
  236.    
  237.     glEndList();
  238. }
  239.  
  240. static void Display( void )
  241. {
  242.     Reshape(ancho,alto);
  243. }
  244.  
  245.  
  246. int main( int argc, char **argv )
  247. {
  248.    
  249.    glutInit(&argc, argv);
  250.    int a;
  251.    glutInitDisplayMode( GLUT_RGB );
  252.  
  253.    glutInitWindowPosition(100,100 );
  254.    glutInitWindowSize(800, 800 );
  255.    glutCreateWindow("Pez con transformaciones geometricas");
  256.  
  257.    Init();
  258.    triangulo();
  259.    pez();
  260.  
  261.    glutReshapeFunc(Reshape);
  262.    glutDisplayFunc(Display);
  263.    glutKeyboardFunc(Key_Esc);
  264.    glutSpecialFunc(Special);
  265.    
  266.    glutMainLoop( );
  267.    
  268.    return 0;
  269. }
  270.  
  271.