Cod sursa(job #1110284)

Utilizator RyuzakiL Lawliet Ryuzaki Data 17 februarie 2014 22:07:17
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 4.63 kb
#include <iostream>
#include <GL/glut.h>
//#include <cmath>
using namespace std;

float rotX=0.0, rotY=0.0;

void cub(float w, float h, float d)
{
    w /= 2;
    h /= 2;
    d /= 2;
    glRotatef(rotX+=0.1, 1.f, 0.5f, 0.0f);

    glBegin(GL_QUADS);
    //fata din spate
        glColor3f(1.0 , 1.0 , 1.0);
        glVertex3f(-w, -h, -d);
        glVertex3f( w, -h, -d);
        glVertex3f( w,  h, -d);
        glVertex3f(-w,  h, -d);
    //fata din fata
        glColor3f(0.0 , 0.0 , 0.0);
         glVertex3f(-w, -h,  d);
        glVertex3f( w, -h,  d);
        glVertex3f( w,  h,  d);
        glVertex3f(-w,  h,  d);
    //fata de sus
        glColor3f(1.0 , 0.0 , 0.0);
        glVertex3f(-w,  h, -d);
        glVertex3f( w,  h, -d);
        glVertex3f( w,  h,  d);
         glVertex3f(-w,  h,  d);
    //fata de jos
        glColor3f(0.0 , 1.0 , 0.0);
        glVertex3f(-w, -h, -d);
        glVertex3f( w, -h, -d);
        glVertex3f( w, -h,  d);
        glVertex3f(-w, -h,  d);
    //fata din stanga
        glColor3f(0.0 , 0.0 , 1.0);
        glVertex3f(-w, -h, -d);
        glVertex3f(-w,  h, -d);
         glVertex3f(-w,  h,  d);
        glVertex3f(-w, -h,  d);
    //fata din dreapta
        glColor3f(1.0 , 0.0 , 1.0);
        glVertex3f( w, -h, -d);
        glVertex3f( w,  h, -d);
        glVertex3f( w,  h,  d);
        glVertex3f( w, -h,  d);
    glEnd();
 }

void om()
{
    //fata
    glBegin(GL_QUADS);
        glColor3f(1.0f, 0.0f, 0.0f);

        glVertex3f(-1.0f,  1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glVertex3f( 1.0f, -1.0f, 0.0f);
        glVertex3f( 1.0f,  1.0f, 0.0f);
    glEnd();

    //nas
    glBegin(GL_TRIANGLES);
        glColor3f(0.0f , 0.0f , 1.0f);

        glVertex3f(0.0f , -0.0f , 0.0f);
        glVertex3f(0.05f , 0.09f , 0.0f);
        glVertex3f(-0.05f , 0.09f , 0.0f);
    glEnd();
    glTranslatef(0.0 , -0.60 , 0.0);

    //gura
    glBegin(GL_QUADS);
        glColor3f(0.0 , 0.5 , 0.5);

        glVertex3f(-0.5 , -0.1 , 0.0);
        glVertex3f(0.5 , -0.1 , 0.0);
        glVertex3f(0.5 , 0.1 , 0.0);
        glVertex3f(-0.5 , 0.1 , 0.0);

    glEnd();
    glTranslatef(0.0 , 1.60 , 0.0);

    //par
    glBegin(GL_QUADS);
    glColor3f(0.0 , 0.0 , 0.0);
        for(int i=-2;i<=2;i++){
            float x=i/2;
            glVertex3f(x      , 0.0 , 0.0);
            glVertex3f(x      , 1.0 , 0.0);
            glVertex3f(x+0.01 , 1.0 , 0.0);
            glVertex3f(x+0.01 , 0.0 , 0.0);
        }
    glEnd();

    //ochiul stang
    glPushMatrix();
    glTranslatef(-0.5 , -0.5 , 0.0);
    glRotatef(rotX+=0.5, 0.0f, 0.0f, 1.0f);

    glBegin(GL_QUADS);

        glColor3f(0.0 , 1.0 , 0.0);

        glVertex3f(-0.1 , -0.1 , 0.0);
        glVertex3f(0.1, -0.1 , 0.0);
        glVertex3f(0.1 , 0.1 , 0.0);
        glVertex3f(-0.1 , 0.1 , 0.0);
    glEnd();
    glPopMatrix();

    //ochiul drept
    glPushMatrix();
    glTranslatef(0.5 , -0.5 , 0.0);
    glRotatef(rotY+=0.5, 0.0f, 0.0f, -1.0f);

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

        glVertex3f(-0.1 , -0.1 , 0.0);
        glVertex3f(0.1 , -0.1 , 0.0);
        glVertex3f(0.1 , 0.1 , 0.0);
        glVertex3f(-0.1 , 0.1 , 0.0);
    glEnd();
    glPopMatrix();



}

void render()
{
    //coloreaza backgroundul si sterge ce era inainte pe ecran
    glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //alte functii necesare functionarii...
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, 1.25, 0.4f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //seteaza axele de coordonate
    glTranslatef(0.0f, 0.0f, -7.0f);
    //aici se deseneaza

    om();

    //pana aici se deseneaza
    glutSwapBuffers();
}

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

    switch (key)
    {
        //daca apasam pe ESC fereastra noua se inchide
        case 27:
        {
            exit(0);
        }
    }
}

int main(int argc, char** argv)
{
    //initializarea ferestrei noi cu ajutorul unor functii create de GLUT
    glutInit(&argc, argv);
    glutInitWindowSize(800, 600);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
    glutCreateWindow("Salut!");
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);

    //functii speciale pt afisare, miscare si interactionare cu USER-ul

    glutKeyboardFunc(keyboard);
    glutDisplayFunc(render);
  //  glutFullScreen();
    glutIdleFunc(render);

    //tine programul intr-o bucla infinita
    glutMainLoop();


     return 0;
}