sábado, 22 de marzo de 2014

Mover y dibujar

En esta ocasión se realizó un programa que permitiera mover la cruz y al momento de presionar un tecla especifica dibujará la figura programada y así sucesivamente. Esto se logra mediante el uso de la funciones anteriormente utilizadas glTranslate, glMatrixMode(GL_PROJECTION) y las instrucciones specialKeyInput y keyInput.


#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#endif

#include <stdlib.h>

using namespace std;
float radio=0.0;
float valor_x=0.0;
float valor_y=0.0;
int contador=0;
float posx[30];
float posy[30];
int cont=0;
int entra=0;


//La función que nos va estar dibujando
void Dib(){
    //if(entra==1){
    for(int cont=0;cont<contador;cont++){
        glTranslatef(posx[cont],posy[cont],0.0);
        glPushMatrix();
        glLineWidth(1);
        glRotatef(45,0,1,1);
        glColor3f(1.0,1.0,0.0);
        glutWireSphere(0.2,15,15);
        glPopMatrix();
        glFlush();
        //glutPostRedisplay();
        cerr <<" cont:" <<cont;
    }

    //}
   // entra=0;

    //glutPostRedisplay();
}

void Dibuja(){
    cerr << "Dibuja() " << " valor_x:" << valor_x << " valor_y:" << valor_y << "\n";
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    /*GL_PROJECTION permite mover el punto de referencia (0,0,0) a donde se le indique*/
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    cerr <<"antes:" <<cont;

    Dib();

    cerr <<"despues:" <<cont;
    glColor3f(0.0,1.0,1.0);
    glLineWidth(3);
    glTranslatef(valor_x,valor_y,0.0);
    glBegin(GL_LINE_STRIP);
    glVertex2d(valor_x+30,valor_y);
    glVertex2d(valor_x-30,valor_y);
    glVertex2d(valor_x,valor_y);
    glVertex2d(valor_x,valor_y+30);
    glVertex2d(valor_x,valor_y-30);
    glEnd();

    glFlush();

}


void keyInput(unsigned char key, int x, int y){

    switch(key){
    case 'q':
            exit(0);
    break;
    case 'a':
        cerr << "keyInput()" << " key:" << key;
        posx[contador]={valor_x};
        posy[contador]={valor_y};
        contador++;
        //entra=1;
    break;
    }

}

void specialKeyInput(int key, int x, int y){
    if(key==GLUT_KEY_DOWN){
        valor_y=valor_y-0.1;
        if(valor_y<=-1)
            valor_y=-1;
    }
    if(key == GLUT_KEY_LEFT){
        valor_x=valor_x-0.1;
        if(valor_x<=-1)
            valor_x=-1;
    }
    if(key == GLUT_KEY_UP){
        valor_y=valor_y+0.1;
        if(valor_y>=1)
            valor_y=1;
    }
    if(key == GLUT_KEY_RIGHT){
        valor_x=valor_x+0.1;
        if(valor_x>=1)
            valor_x=1;
    }

    glutPostRedisplay();
}




int main(){
    cerr <<"main() \n";
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(550,550);
    glutCreateWindow("");
    glutDisplayFunc(Dibuja);
    glutKeyboardFunc(keyInput);
    glutSpecialFunc(specialKeyInput);
    glutMainLoop();
}


No hay comentarios:

Publicar un comentario