lunes, 24 de marzo de 2014

Programa rotación de figuras

#include <GL/gl.h>
 #include <GL/glu.h>
 #include <GL/glut.h>

GLfloat anio = 0, dia = 0, luna=0, anio2=0, luna2=0, anio3=0, luna3=0;
int angulo_z = 10, opc=0;

 void init(void){
     glClearColor (0.0, 0.0, 0.0, 0.0);
     glShadeModel (GL_FLAT);
 }

 void display(void){
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glClearDepth(1.0);
     glColor3f (1.0, 1.0, 0.0);// color del sol
     glPushMatrix();

         glutSolidSphere(0.7, 20, 16);/* Dibuja el Sol */
         glRotatef (angulo_z, 0.0, 0.0, 1.0);    //angulo constante de inclinacion en z
         glRotatef (anio, 0.0, 1.0, 0.0);    //angulo segun el numero de años
         glTranslatef (3.0, 0.0, 0.0);    //Distancia entre el sol y el primer planeta

             glPushMatrix();
             glRotatef ( dia, 0.0, 1.0, 0.0);    //angulo del planeta segun el dia
             glColor3f (0.0, 0.0, 1.0);                 // color del planeta
             glutWireSphere(0.3, 10, 8);                /* Dibuja el primer planeta */
             glPopMatrix();



         glRotatef (25, 0.0, 0.0, 1.0);               //Angulo constante de la luna respecto al primer planeta
         glRotatef (luna, 0.0, 1.0, 0.0);   //Angulo de la luna segun la hora
         glTranslatef (0.5, 0.0, 0.0);                //Distancia de la luna respecto al primer planeta
         glColor3f (0.7, 0.7, 0.7);
         glutWireSphere(0.07, 10, 8);                 // dibuja la luna del primer planeta

        glPopMatrix();

     glutSwapBuffers();
 }


 void display2(void){
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glClearDepth(1.0);
     glColor3f (1.0, 1.0, 0.0);// color del sol
     glPushMatrix();

         glutSolidSphere(0.7, 20, 16);/* Dibuja el Sol */
         glRotatef (angulo_z, 0.0, 0.0, 1.0);    //angulo constante de inclinacion en z
         glRotatef (anio2, 0.0, 1.0, 0.0);    //angulo segun el numero de años
         glTranslatef (3.0, 1.0, 0.0);    //Distancia entre el sol y el primer planeta

             glPushMatrix();
             glRotatef ( dia, 0.0, 1.0, 0.0);    //angulo del planeta segun el dia
             glColor3f (1.0, 1.0, 1.0);                 // color del planeta
             glutWireSphere(0.3, 10, 8);                /* Dibuja el primer planeta */
             glPopMatrix();



         glRotatef (25, 0.0, 0.0, 1.0);               //Angulo constante de la luna respecto al primer planeta
         glRotatef (luna2, 0.0, 1.0, 0.0);   //Angulo de la luna segun la hora
         glTranslatef (0.5, 0.0, 0.0);                //Distancia de la luna respecto al primer planeta
         glColor3f (0.7, 0.7, 0.7);
         glutWireSphere(0.07, 10, 8);                 // dibuja la luna del primer planeta

        glPopMatrix();

     glutSwapBuffers();
 }

 void display3(void){
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glClearDepth(1.0);
     glColor3f (1.0, 1.0, 0.0);// color del sol
     glPushMatrix();

         glutSolidSphere(0.7, 20, 16);/* Dibuja el Sol */
         glRotatef (angulo_z, 0.0, 0.0, 1.0);    //angulo constante de inclinacion en z
         glRotatef (anio3, 0.0, 1.0, 0.0);    //angulo segun el numero de años
         glTranslatef (3.0, -1.0, 0.0);    //Distancia entre el sol y el primer planeta

             glPushMatrix();
             glRotatef ( dia, 0.0, 1.0, 0.0);    //angulo del planeta segun el dia
             glColor3f (1.0, 0.0, 0.0);                 // color del planeta
             glutWireSphere(0.3, 10, 8);                /* Dibuja el primer planeta */
             glPopMatrix();



         glRotatef (25, 0.0, 0.0, 1.0);               //Angulo constante de la luna respecto al primer planeta
         glRotatef (luna3, 0.0, 1.0, 0.0);   //Angulo de la luna segun la hora
         glTranslatef (0.5, 0.0, 0.0);                //Distancia de la luna respecto al primer planeta
         glColor3f (0.7, 0.7, 0.7);
         glutWireSphere(0.07, 10, 8);                 // dibuja la luna del primer planeta

        glPopMatrix();

     glutSwapBuffers();
 }

 void reshape (int w, int h){
     glViewport (0, 0, (GLsizei) w, (GLsizei) h);
     glMatrixMode (GL_PROJECTION);
     glLoadIdentity ();
     gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     gluLookAt (0.0, 1.0, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 }

 void idle()
{
    display();
    display2();
    display3();
}



 void keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {

    case 'a':
        anio+=1.0f;
    break;


    case 'A':
        luna+=1.5f;
    break;

    case 's':
        anio2+=1.5f;
    break;

    case 'S':
        luna2+=1.5f;
    break;

    case 'd':
        anio3+=1.5f;
    break;

    case 'D':
        luna3+=1.5f;
    break;



    }
}




 int main(int argc, char** argv){
     glutInit(&argc, argv);
     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
     glutInitWindowSize (500, 500);
     glutInitWindowPosition (100, 100);
     glutCreateWindow (argv[0]);
     init ();
     glutDisplayFunc(display);
     glutDisplayFunc(display2);
     glutReshapeFunc(reshape);
     glutIdleFunc(idle);
     glutKeyboardFunc(keyboard);
     glutMainLoop();
     return 0;
 }

Programa Menus

#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;
float rx,ry;
float pri, seg;
int v[100]={0};
float vrx[100]={0};
float vry[100]={0};
int indi=0;
int b=0;





void menu(int opc)
{

    v[indi]=opc;
    vrx[indi]=(rx/(winWid/2))-1;
    vry[indi]=(ry/(winHeight/2))-1;


    switch(v[indi])
    {


        case 1:


            glPushMatrix();
            glTranslatef(vrx[indi],vry[indi],0.0);
            glColor3f(1.0,0.0,0.0);
            glutWireSphere(0.1,15,15);
            glPopMatrix();
            glFlush();


        break;

        case 2:

            glColor3f(1.0,0.0,0.0);
            glPushMatrix();
            glTranslatef(vrx[indi],vry[indi],0.0);
            glRotatef(55,0,1,1);
            glutWireCone(0.1,0.1,15,15);
            glPopMatrix();
            glFlush();


        break;

        case 3:

            glColor3f(1.0,0.0,0.0);
            glPushMatrix();
            glTranslatef(vrx[indi],vry[indi],0.0);
            glRotatef(55,0,1,1);
            glutWireCube(0.1);
            glPopMatrix();
            glFlush();

        break;

        case 4:

            glColor3f(1.0,0.0,0.0);
            glPushMatrix();
            glTranslatef(vrx[indi],vry[indi],0.0);
            glScalef(0.1,0.1,0.1);
            glRotatef(55,0,1,1);
            glutWireDodecahedron();
            glPopMatrix();
            glFlush();

        break;



    }
}


void dibuja(){
    //glClear(GL_COLOR_BUFFER_BIT);

    menu(v[indi]);


    glutSwapBuffers();
}
// en donde se de clic se movera el punto
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);
        indi++;

 //       redraw();

    }
}




int main(int argc, char *argv[]){
    winWid = 600.0;
    winHeight = 400.0;
    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();


//    glutMotionFunc(motion);
    glutDisplayFunc(dibuja);
    glutIdleFunc(dibuja);
    glutMouseFunc(mousebutton);
    glutCreateMenu(menu);
    glutAddMenuEntry("Opcion 1", 1);
    glutAddMenuEntry("Opcion 2", 2);
    glutAddMenuEntry("Opcion 3", 3);
    glutAddMenuEntry("Opcion 4", 4);
    glutAttachMenu(GLUT_RIGHT_BUTTON);


    glutMainLoop();

    return 0;
}

domingo, 9 de marzo de 2014

Disparo ;)

En este código jugamos un poco con lo que hemos aprendido del uso del teclado. Se muestran dos lineas perpendiculares y al presionar "a" aparece una esfera roja

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

#include <iostream>
#endif
using namespace std;


#include <stdlib.h>

float radio= 0.0;
float valor_x= 0.0;
float m[100]={2.0};
int b=1;
int i=1;





void keyInput(unsigned char key, int x, int y)
{
    switch(key){
        case 'q':
            exit(0);
        break;

        case 'a':

            if(b==0)
            {
                b=1;
                i=i+2;

            }

            glPushMatrix();
            glTranslatef(valor_x,radio,0.0);
            glColor3f(1.0,0.0,0.0);
            glutWireSphere(0.1,15,15);
            glPopMatrix();
            glFlush();



            m[i-1]=valor_x;

            m[i]=radio;

            if(b==1)
            {
                b=0;
            }



        break;

            }
}

void specialkeyInput(int key,int x, int y)
{
    if(key==GLUT_KEY_DOWN)
    {
        if(radio<-1.2)
        {
            radio=1;
        }
        else
        {
        radio-=0.1;
        glutPostRedisplay();
        }

    }
    if(key==GLUT_KEY_LEFT)
    {
        if(valor_x<-1.2)
        {
            valor_x=1;
        }
        else
        {
            valor_x-=0.1;
            glutPostRedisplay();
        }


    }
    if(key==GLUT_KEY_UP)
    {
        if(radio>1.2)
        {
            radio=-1;

        }
        radio+=0.1;
        glutPostRedisplay();


    }
    if(key==GLUT_KEY_RIGHT)
    {

        if(valor_x>1.2)
        {
            valor_x=-1;

        }
        else
        {
            valor_x+=0.1;
            glutPostRedisplay();
        }

    }
}

void dibuja()
{


    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if(b==0)
    {

    for(int j=1;j<=i;j=j+2)
    {
        cerr<<m[j-1];
            glPushMatrix();
            glColor3f(1.0,0.0,0.0);
            glTranslatef(m[j-1],m[j],0.0);
            glutWireSphere(0.1,15,15);
            glPopMatrix();

    }
    }


    glColor3f(1.0,1.0,0.0);
    glBegin(GL_LINES);

                        glVertex2f(valor_x, 1.0);
                        glVertex2f(valor_x, -1.0);
                        glVertex2f(1.0, radio);
                        glVertex2f(-1.0, radio);
    glEnd();
    glFlush();


}



int main()
{

    cerr << " main() \n";
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(550,550);
    glutCreateWindow("");
    glutKeyboardFunc(keyInput);
    glutDisplayFunc(dibuja);
    glutSpecialFunc(specialkeyInput);
    glutMainLoop();

}

Cambiar de posición una figura



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

#include <iostream>
#endif
using namespace std;


#include <stdlib.h>

float radio= 0.0;
float valor_x= 0.0;

void keyInput(unsigned char key, int x, int y)
{
    switch(key){
        case 'q':
            exit(0);
        break;
            }
}

void specialkeyInput(int key,int x, int y)
{
    if(key==GLUT_KEY_DOWN)
    {
        if(radio<-1.2)
        {
            radio=1;
        }
        else
        {
        radio-=0.1;
        glutPostRedisplay();
        }

    }
    if(key==GLUT_KEY_LEFT)
    {
        if(valor_x<-1.2)
        {
            valor_x=1;
        }
        else
        {
            valor_x-=0.1;
            glutPostRedisplay();
        }


    }
    if(key==GLUT_KEY_UP)
    {
        if(radio>1.2)
        {
            radio=-1;

        }
        radio+=0.1;
        glutPostRedisplay();


    }
    if(key==GLUT_KEY_RIGHT)
    {

        if(valor_x>1.2)
        {
            valor_x=-1;

        }
        else
        {
            valor_x+=0.1;
            glutPostRedisplay();
        }

    }
}

void dibuja()
{
 


    glClearColor(0,0,0,0);
    glPushMatrix();
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glTranslatef(valor_x,radio,0.0);
    glColor3f(1.0,1.0,0.0);
    glutWireSphere(0.2,15,15);
    glPopMatrix();
    glFlush();




}
int main()
{

    cerr << " main() \n";
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(550,550);
    glutCreateWindow("");
    glutKeyboardFunc(keyInput);
    glutDisplayFunc(dibuja);
    glutSpecialFunc(specialkeyInput);
    glutMainLoop();

}

Figuras primitivas GLUT

En este código se muestran la figuras primitivas así como algunas funciones como glRotate (para girar la figura), glTranslate (cambiar de posición la figura) y glutWire y glutSolid que cambia la consistencia de las figuras

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

#include <iostream>
#endif
using namespace std;


#include <stdlib.h>
void dibuja()
{
 


    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.8,0.8,0.0);
    glRotatef(45,0,1,1);
    glutWireSphere(0.2,15,15);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.4,0.8,0.0);
    glRotatef(55,0,1,1);
    glutWireCone(0.2,0.2,15,15);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.0,0.8,0.0);
    glRotatef(55,0,1,1);
    glutWireCube(0.2);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.4,0.8,0.0);
    glScalef(0.1,0.1,0.1);
    glRotatef(55,0,1,1);
    glutWireDodecahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.8,0.8,0.0);
    glScalef(0.2,0.2,0.2);
    glRotatef(55,0,1,1);
    glutWireIcosahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.8,0.4,0.0);
    glScalef(0.2,0.2,0.2);
    glRotatef(55,0,1,1);
    glutWireOctahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.4,0.4,0.0);
    glutWireTeapot(0.15);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.05,0.4,0.0);
    glScalef(0.2,0.2,0.2);
    glRotatef(55,0,1,1);
    glutWireTetrahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.4,0.4,0.0);
    glRotatef(55,0,1,1);
    glutWireTorus(0.1,0.1,15,15);
    glPopMatrix();
    glFlush();

    //SOLIDOS

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.8,0.0,0.0);
    glRotatef(45,0,1,1);
    glutSolidSphere(0.2,15,15);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.4,0.0,0.0);
    glRotatef(55,0,1,1);
    glutSolidCone(0.2,0.2,15,15);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.0,0.0,0.0);
    glRotatef(55,0,1,1);
    glutSolidCube(0.2);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.4,0.0,0.0);
    glScalef(0.1,0.1,0.1);
    glRotatef(55,0,1,1);
    glutSolidDodecahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.8,0.0,0.0);
    glScalef(0.2,0.2,0.2);
    glRotatef(55,0,1,1);
    glutSolidIcosahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.8,-0.4,0.0);
    glScalef(0.2,0.2,0.2);
    glRotatef(55,0,1,1);
    glutSolidOctahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(-0.4,-0.4,0.0);
    glutSolidTeapot(0.15);
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.05,-0.4,0.0);
    glScalef(0.2,0.2,0.2);
    glRotatef(55,0,1,1);
    glutSolidTetrahedron();
    glPopMatrix();
    glFlush();

    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    glTranslatef(0.4,-0.4,0.0);
    glRotatef(55,0,1,1);
    glutSolidTorus(0.1,0.1,15,15);
    glPopMatrix();
    glFlush();

}
int main()
{

    cerr << " main() \n";
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(550,550);
    glutCreateWindow("");
    glutDisplayFunc(dibuja);
    glutMainLoop();

}

lunes, 17 de febrero de 2014

Generación de cuadrados y triangulos

---Generación de cuadrados---

static void display(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);

    glLineWidth(1);//tamaño de la linea

    glBegin(GL_QUADS);//se define la figura primitiva a crear

for(float i=-1;i<=1;i+=0.1){

         glColor3f(1, i, i);
         glVertex2f(-1+i, 1-i);
         glVertex2f(1-i, 1-i);
         glVertex2f(1-i, -1+i);
         glVertex2f(-1+i, -1+i);
}

                    glEnd();
                    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Clase 3");
    glutDisplayFunc(display);
    glutMainLoop();

    return EXIT_SUCCESS;
}


---Generación de triángulos---

static void display(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);

    glLineWidth(1);//tamaño de la linea

    glBegin(GL_TRIANGLES);//se define la figura primitiva a crear

for(float i=-1;i<=1;i+=0.1){

         glColor3f(2, i, i);
         glVertex2f(0.0, 1-i);
         glVertex2f(1-i, -1+i);
         glVertex2f(-1+i, -1+i);
}

                    glEnd();
                    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Clase 4");
    glutDisplayFunc(display);
    glutMainLoop();

    return EXIT_SUCCESS;
}


Matrices de puntos y lineas

---Código de matrices de puntos---

static void display(void)
{

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(10);
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_POINTS);

        for(float x=-1.0;x<=1.0;x+=0.1){
            for(float y=-1.0;y<=1.0;y+=0.1){

                        glVertex2f(x, y);

                    }
                }
                    glEnd();
                    glFlush();

                }

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Clase 1");
    glutDisplayFunc(display);
    glutMainLoop();

    return EXIT_SUCCESS;
}


---Código de matrices de lineas---

static void display(void)
{

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);

    glLineWidth(1);//tamaño de la linea
    glColor3f(1.0,0.0,0.0);//color rojo
    glBegin(GL_LINES);//se define la figura primitiva a crear

     for(float x=-1.0;x<=1.0;x+=0.1){
            for(float y=-1.0;y<=1.0;y+=0.1){

                        glVertex2f(x, y);
                        glVertex2f(x, 0.0);
                        glVertex2f(x, y);
                        glVertex2f(0.0, y);

                    }
                }

        glEnd();
        glFlush();
                }


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Clase 2");
    glutDisplayFunc(display);
    glutMainLoop();

    return EXIT_SUCCESS;
}