domingo, 23 de marzo de 2014

Práctica.- Menú básico

Esta práctica es una conjunción entre dos de loas trabajos subidos en este blog.
En primer lugar se utilizan las funciones del ratón. Y en segundo lugar se dibujan figuras predeterminadas.
El objetivo fundamental consiste en hacer clic derecho en determinado punto de la pantalla. Acto seguido debe desplegarse un menú en el cual existe la opción de seleccionar un figura para después dibujarla en la pantalla sobre el mismo punto en el que se ha hecho clic.
El código es el siguiente:
---------------------------------------------------------------------------------------------
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

#define NP 4000
#define AGE 200

float winWid,winHeight;
int rx,ry;
float valores[50];
int count=0;
int countfig=0;
int figure[20];


void redraw( void ){
      glClearColor(0.0,0.0,0.0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
     glColor3f(0.0,0.0,1.0);

        for(int i=0,j=0;i<count;i++,j++){
    glPushMatrix();
      double x=valores[i];
      i++;
      double y=valores[i];
        x=(x-310)/310;
        y=(y-250)/250;

       glTranslatef(x,y,0);
        glColor3f(0.0,0.0,1.0);
     if(figure[j]==1)
       {  glRotatef(60,1,1,0);
           glutWireCube(0.5);

       }
     if(figure[j]==2){
             glRotated(60,1,0,1);
        glutWireSphere(.5,16,16);
     }
     if(figure[j]==3){
      glRotatef(60,1,0,0);
      glutWireTorus(0.1,0.15,12.0,32.0);
     }
     if(figure[j]==4)
       glutWireTeapot(0.1);

     if(figure[j]==5){
      glRotatef(60,1,1,0);
       glutWireCone(0.3,0.5,22.0,5.0);

        }


      glPopMatrix();

    }
    glFlush();
}




void mousebutton(int button, int state, int x, int y){
    cerr << "mousebutton. x:" << x << ", y:" << y << " \n";
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
        rx = float(x);
        ry = winHeight - float(y);

    }
}
void menu(int opc){
   switch(opc)
     {
    case 1:
     valores[count]=rx;
      count++;
      valores[count]=ry;
      count++;
   figure[countfig]=1;
   countfig++;
        redraw();
        break;
            case 2:
     valores[count]=rx;
      count++;
      valores[count]=ry;

      count++;
   figure[countfig]=2;
   countfig++;
        redraw();
        break;
            case 3:
     valores[count]=rx;
      count++;
      valores[count]=ry;

      count++;
   figure[countfig]=3;
   countfig++;
        redraw();
        break;
            case 4:
     valores[count]=rx;
      count++;
      valores[count]=ry;

      count++;
   figure[countfig]=4;
   countfig++;
        redraw();
        break;

         case 5:
     valores[count]=rx;
      count++;
      valores[count]=ry;

      count++;
   figure[countfig]=5;
   countfig++;
        redraw();
        break;


     }

}


int main(int argc, char *argv[]){
    winWid = 620.0;
    winHeight = 500.0;
    glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutCreateWindow("Hola Mundo");
    glutPositionWindow(200,100);
    glutReshapeWindow(int(winWid),int(winHeight));
    glClearColor(0.0,0.0,0.0,1.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,winWid,0.0,winHeight, -100.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glutCreateMenu(menu);
glutAddMenuEntry("Cubo",1);
glutAddMenuEntry("Esfera",2);
glutAddMenuEntry("Dona",3);
glutAddMenuEntry("Tetera",4);
glutAddMenuEntry("Cono",5);

glutAttachMenu(GLUT_RIGHT_BUTTON);

    glutDisplayFunc(redraw);
  //  glutIdleFunc(redraw);
 //   glutMotionFunc(motion);
    glutMouseFunc(mousebutton);
    glutMainLoop();

    return 0;
}
-------------------------------------------------------------------------------------
Una vez implementado, obtendremos un resultado como el que se muestra en la imagen a continuación:

No hay comentarios.:

Publicar un comentario