jueves, 13 de marzo de 2014

Draw Figures with GLUT using mouse buttons


Para poder dibujar las figuras predeterminadas de "glut" haremos clic izquierdo sobre queramos dibujar la figura y paso siguiente al presionar el botón secundario se desplegará un menú de las figuras y haremos clic en la figura que queramos dibujar.

Código Fuente:

#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 = 600.0,winHeight = 400.0;
float rx=winWid/2,ry=winHeight/2;
float Arreglo[3][500]={0.0};
int cont=0;
int a;

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


for(int i=0;i<cont;i++)
{
    a=Arreglo[2][i];
switch(a)
{

case 1:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(0,1,1);
glScalef(0.8,0.8,0.8);
glRotatef(60,0,1,1);
glutWireCube(0.2);
glPopMatrix();
break;

case 2:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(1,1,0);
glScalef(0.8,0.8,0.8);
glRotatef(90,1,0,1);
glutWireCone(0.2,0.5,10,10);
glPopMatrix();
break;

case 3:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(1,1,0);
glScalef(0.8,0.8,0.8);
glRotatef(60,0,1,1);
glutSolidTeapot(0.2);
glPopMatrix();
break;

case 4:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(1,0,1);
glScalef(0.8,0.8,0.8);
glRotatef(45,0,1,1);
glutWireSphere(0.2,15,15);
glPopMatrix();
break;

case 5:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(0,0,1);
glScalef(0.8,0.8,0.8);
glRotatef(60,0,1,1);
glutWireTorus(0.1,0.1,10,10);
glPopMatrix();
break;


case 6:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(1,1,1);
glScalef(0.2,0.2,0.2);
glRotatef(30,0,1,1);
glutWireOctahedron();
glPopMatrix();
break;


case 7:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(0,0,1);
glScalef(0.1,0.1,0.1);
glRotatef(30,0,1,1);
glutWireDodecahedron();
glPopMatrix();
break;


case 8:
glPushMatrix();
glTranslatef(Arreglo[0][i],Arreglo[1][i],0.0);
glColor3f(1,0,0);
glScalef(0.2,0.2,0.2);
glRotatef(60,0,1,1);
glutWireIcosahedron();
glPopMatrix();
break;

}

}
glutSwapBuffers();
}


void motion(int x, int y){
    cerr << "motion. x:" << x << ", y:" << y << " \n";
     rx = float(x);
     ry = winHeight - float(y);
}


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);
        redraw();
    }
}

void menu(int opc)
{


    switch(opc)
    {
    case 1:
    Arreglo[0][cont]=(rx/(winWid/2))-1;
    Arreglo[1][cont]=(ry/(winHeight/2))-1;
    Arreglo[2][cont]=1;
    cont+=1;
    redraw();
     break;

     case 2:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=2;
     cont+=1;
     redraw();
     break;

     case 3:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=3;
     cont+=1;
     redraw();
     break;

     case 4:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=4;
     cont+=1;
     redraw();
     break;

     case 5:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=5;
     cont+=1;
     redraw();
     break;

     case 6:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=6;
     cont+=1;
     redraw();
     break;


     case 7:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=7;
     cont+=1;
     redraw();
     break;

     case 8:
     Arreglo[0][cont]=(rx/(winWid/2))-1;
     Arreglo[1][cont]=(ry/(winHeight/2))-1;
     Arreglo[2][cont]=8;
     cont+=1;
     redraw();
     break;
    }
}



int main(int argc, char *argv[]){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("");
    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();
    glutDisplayFunc(redraw);
    glutIdleFunc(redraw);
    glutMotionFunc(motion);
    glutMouseFunc(mousebutton);
    glutCreateMenu(menu);
    glutAddMenuEntry("Cube",1);
    glutAddMenuEntry("Cone",2);
    glutAddMenuEntry("Teapot",3);
    glutAddMenuEntry("Sphere",4);
    glutAddMenuEntry("Torus",5);
    glutAddMenuEntry("Octahedron",6);
    glutAddMenuEntry("Dodecahedron",7);
    glutAddMenuEntry("Icosahedron",8);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glutMainLoop();
    return 0;
}

No hay comentarios:

Publicar un comentario