Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #if defined(__APPLE__)
  4.     #include <OpenGL/OpenGL.h>
  5.     #include <GLUT/GLUT.h>
  6. #else
  7.     #include <GL/gl.h>
  8.     #include <GL/freeglut.h>
  9. #endif
  10.  
  11. using namespace std;
  12.  
  13. bool boto_f = false;
  14. bool boto_v = false;
  15. double y_anterior_t = 0.0;
  16. double x_anterior_t = 0.0;
  17. double alfa_t = 0.0;
  18. double beta_t = 0.0;
  19. double y_anterior_f = 0.0;
  20. double x_anterior_f = 0.0;
  21. double alfa_f = 0.0;
  22. double beta_f = 0.0;
  23.  
  24.  
  25. void teclat(unsigned char c, int x, int y) {
  26.     switch(c) {
  27.         case 'h': cout << "Ayuda: " << endl;
  28.               cout << "h: consultar ayuda" << endl;
  29.               cout << "f: rotacio" << endl;
  30.               cout << "v: scalar" << endl;
  31.               cout << "ESC: salir del programa" << endl;
  32.              
  33.             break;
  34.            
  35.         case 'f': boto_f = true;
  36.             break;
  37.            
  38.         case 'v': boto_v = true;
  39.             break;
  40.         case 27: exit(0);
  41.             break;
  42.     }
  43. }
  44.  
  45. void iniMovRaton(int x, int y) {
  46.     if (boto_f) {
  47.         alfa_f += x - x_anterior_f;
  48.         x_anterior_f = alfa_f;
  49.         beta_f += y - y_anterior_f;
  50.         y_anterior_f = beta_f;
  51.         glutPostRedisplay();
  52.     }
  53.     if (boto_v) {
  54.         double x_nueva = x;
  55.         double y_nueva = y;
  56.         x_nueva = ((2.0*double(x))/glutGet(GLUT_WINDOW_WIDTH))-1.0;
  57.         y_nueva = 1.0-((2.0*double(y))/glutGet(GLUT_WINDOW_HEIGHT));
  58.         alfa_t += x_nueva - x_anterior_t;
  59.         x_anterior_t = alfa_t;
  60.         beta_t += y_nueva - y_anterior_t;
  61.         y_anterior_t = beta_t;
  62.         glutPostRedisplay();
  63.     }
  64. }
  65.  
  66. void iniRaton(int button, int state, int x, int y) {
  67.     if (state == GLUT_UP) {
  68.         boto_f = false;
  69.         boto_v = false;
  70.     }
  71.     glutPostRedisplay();
  72. }
  73.  
  74. void reshape(int w, int h) {
  75.         if (w > h) glViewport((w-h)/2,0,h,h);
  76.         else glViewport(0,(h-w)/2,w,w);
  77. }
  78.  
  79. void pinta_ninot() {
  80.     glPushMatrix();
  81.     glTranslated(0,0,0);
  82.     glutWireSphere(0.4,16,16);
  83.     glPopMatrix();
  84.    
  85.     glPushMatrix();
  86.     glTranslated(0,0.6,0);
  87.     glutWireSphere(0.2,16,16);
  88.     glPopMatrix();
  89.    
  90.     glPushMatrix();
  91.     glColor3f(0.0,0.0,1.0);
  92.     glTranslated(0.1,0.6,0);
  93.     glRotatef(90,0,1.0,0);
  94.     glutWireCone(0.1,0.2,8,8);
  95.     glPopMatrix();
  96. }
  97.  
  98. void refresh(void) {
  99.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  100.    
  101.     glMatrixMode(GL_MODELVIEW);
  102.    
  103.     //glLoadIdentity();
  104.    
  105.     glPushMatrix();
  106.     glColor3d(1,1,1);
  107.     glRotated(alfa_f,0,1,0);
  108.     glRotated(beta_f,1,0,0);
  109.     //glScaled(1.0-alfa_t,1.0-beta_t,0);
  110.         pinta_ninot();
  111.     glPopMatrix();
  112.    
  113.     glutSwapBuffers();
  114. }
  115.  
  116. void initGL() {
  117.     glClearColor(0,0,0,1);
  118.    
  119.     glMatrixMode(GL_PROJECTION);
  120.     glLoadIdentity();
  121.     glOrtho(-1,1,-1,1,-1,1);
  122.     glMatrixMode(GL_MODELVIEW);
  123.    
  124. }
  125.  
  126. int main(int argc, const char *argv []) {
  127.     //inicializaciones
  128.     glutInit(&argc, (char**)argv);
  129.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  130.     glutInitWindowSize(600,600);
  131.     glutCreateWindow("IDI: Practiques OpenGL");
  132.     initGL();
  133.  
  134.     //callbacks
  135.     glutDisplayFunc(refresh);
  136.     glutMouseFunc(iniRaton);
  137.     glutMotionFunc(iniMovRaton);
  138.     glutKeyboardFunc(teclat);
  139.         glutReshapeFunc(reshape);
  140.    
  141.                
  142.     //bucle procesamiento eventos
  143.     glutMainLoop();
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement