Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: delirio en 27 Junio 2011, 11:13 am



Título: Ayuda con eventos del mouse!!!
Publicado por: delirio en 27 Junio 2011, 11:13 am
Alguien que me ayude a mejorar mi programa de opengl......quiero mover mi figura con el mouse...no se muy bien manejar estos eventos del mouse...solo hice para que pueda moverlo con el teclado, pero quisiera tambien con el mouse..........
Ahi va mi codigo que he hecho:


Código
  1. #include <GL/freeglut.h>
  2. #include <GL/glu.h>
  3. #include <iostream>
  4.  
  5. int posx=0, posy=0;
  6.  
  7. void resize(int width, int height)
  8. {
  9.    glViewport(0, 0, width, height);
  10.  
  11.    glMatrixMode(GL_PROJECTION);
  12.    glLoadIdentity();
  13.    glOrtho(-50,50,-50,50,-10,10);
  14.    glMatrixMode(GL_MODELVIEW);
  15.    glLoadIdentity();
  16.  
  17.    glEnable(GL_DEPTH_TEST);
  18.    glDepthFunc(GL_LEQUAL);
  19. }
  20.  
  21.  
  22. //---------------------------------------------------------//
  23. void display()
  24. {
  25.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  26. glLoadIdentity();
  27. glBegin(GL_LINES);
  28. glColor3f(1.0f,1.0f,1.0f);
  29. glVertex3f( -50.0f, 0.0f,0.0f);
  30. glVertex3f(50.0f, 0.0f,0.0f);
  31. glColor3f(1.0f,1.0f,1.0f);
  32. glVertex3f( 0.0f, -50.0f,0.0f);
  33. glVertex3f(0.0f, 50.0f,0.0f);
  34. glEnd();
  35.  
  36. //----------------1º  TRIANGULO----------------//  
  37.  glPushMatrix();
  38. glTranslatef(posx, posy, 0);
  39.    glColor3f(1,0,0);
  40. glutSolidSphere(5, 20, 2);
  41.  glEnd();
  42.  glPopMatrix();
  43.  
  44.    glutSwapBuffers();
  45. }
  46. //---------------------------------------------------//
  47.  
  48. void key(unsigned char key, int x, int y)
  49. {
  50.    switch (key)
  51.    {
  52.        case 27 :
  53.            exit(0);
  54.            break;
  55.        default:
  56.            break;
  57.    }
  58.  
  59.    glutPostRedisplay();
  60. }
  61.  
  62.  
  63. void tecladoEspecial(int tecla, int x, int y) {
  64. switch(tecla) {
  65.   case GLUT_KEY_UP : posy++;break;
  66.   case GLUT_KEY_DOWN : posy--;break;
  67.   case GLUT_KEY_RIGHT : posx++;break;
  68.   case GLUT_KEY_LEFT : posx--;break;
  69. }
  70. glutPostRedisplay();
  71. }
  72.  
  73.  
  74.  
  75. void idle()
  76. {
  77.    glutPostRedisplay();
  78. }
  79.  
  80. int main(int argc, char *argv[])
  81. {
  82.    glutInit(&argc, argv);
  83.    glutInitWindowSize(640,480);
  84.    glutInitWindowPosition(10,10);
  85.    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  86.    glutCreateWindow("Transformacion 1");
  87.    glutReshapeFunc(resize);
  88.    glutDisplayFunc(display);
  89.    glutKeyboardFunc(key);
  90.    glutSpecialFunc(tecladoEspecial);
  91.  
  92.    glutIdleFunc(idle);
  93.  
  94.    glutMainLoop();
  95. return 0;
  96. }

Gracias de antemano....