Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
8.4 kB
7
Indexable
Never

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

#include<bits/stdc++.h>
#include <GL/gl.h>
#include<GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#endif

#include <stdlib.h>

static int slices = 16;
static int stacks = 16;

double ex=0, ey=5, ez=50;
double cx=0, cy=5, cz=0;
double ux=0, uy=1, uz=0;
double tx=0,ty=0,tz=0;
GLfloat ang = 0;
int rot = 1;
/* GLUT callback Handlers */

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar,-ar, ar, 2.0, 1000.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}



GLfloat arr[8][3]=
{

    {0,0,0},
    {1,0,0},
    {1,1,0},
    {0,1,0},

    {1,0,1},
    {1,1,1},
    {0,1,1},
    {0,0,1}
};

static GLubyte ind[6][4]=
{
    {0,1,2,3},
    {1,4,5,2},
    {4,7,6,5},

    {7,0,3,6},
    {3,2,5,6},
    {0,7,4,1}
};

void draw_cube(GLfloat c1,GLfloat c2,GLfloat c3 )
{
    GLfloat mat_ambient[]    = { 0.4*c1, 0.4*c2, 0.4*c3, 1.0f };
    GLfloat mat_diffuse[]    = { c1, c2, c3, 1.0f };
    GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat high_shininess[] = { 100.0f };

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);


    glBegin(GL_QUADS);
    for(int i=0; i<6; i++)
    {
        glVertex3fv(&arr[ind[i][0]][0]);
        glVertex3fv(&arr[ind[i][1]][0]);
        glVertex3fv(&arr[ind[i][2]][0]);
        glVertex3fv(&arr[ind[i][3]][0]);
    }
    glEnd();
}
int light_switch = 1;

void light0()
{
     GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
     GLfloat light_ambient[]  = { .5f, 0.5f, 0.5f, 1.0f};
     GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
     GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
     GLfloat light_position[] = {200, 200, 100, 0.0  };
    if(light_switch)
        glEnable(GL_LIGHT0);
    else
        glDisable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

}


void draw_A()
{
    glPushMatrix();

    glTranslatef(0,0,0);
    glRotatef(0, 0,1,0);
    // glScalef(3,3,3);
    glPushMatrix();
    glRotatef(-30,0,0,1);
    glScalef(2,16,2);
    draw_cube(0,1,0);
    glPopMatrix();
    glPopMatrix();


    glPushMatrix();
    glTranslatef(16,0,0);
    glRotatef(0, 0,1,0);
    // glScalef(3,3,3);
    glPushMatrix();
    glRotatef(30,0,0,1);
    glScalef(2,16,2);
    draw_cube(0,1,0);
    glPopMatrix();

    glPopMatrix();

    glPushMatrix();
    glTranslatef(4,4,0);
    glRotatef(0, 0,1,0);
    // glScalef(3,3,3);
    glPushMatrix();
    //glRotatef(30,0,0,1);
    glScalef(10,2,2);
    draw_cube(1,1,0);
    glPopMatrix();
    glPopMatrix();

}

void draw_N()
{
    glPushMatrix();

    glPushMatrix();
    glTranslatef(0,0,0);
    glRotatef(0,0,1,0);
        glPushMatrix();
        glTranslatef(0,0,0);
        glRotatef(0,0,0,0);
        glScalef(2,16,2);
        draw_cube(1,0,0);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0,0,0);
    glRotatef(0,0,1,0);
        glPushMatrix();
        glTranslatef(9,0,0);
        glRotatef(30,0,0,1);
        glScalef(2,16,2);
        draw_cube(0,1,0);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0,0,0);
    glRotatef(0,0,1,0);
        glPushMatrix();
        glTranslatef(9,0,0);
        glRotatef(0,0,0,0);
        glScalef(2,16,2);
        draw_cube(0,0,1);
        glPopMatrix();
    glPopMatrix();
    glPopMatrix();

}

static void display(void)
{
    const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double a = t*90.0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-5, 5,-5, 5, 2.0, 1000.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(ex,ey,ez,cx,cy,cz,ux,uy,uz);
        light0();

    glPushMatrix();
    glTranslatef(0,0,0);
    glRotatef(ang,0,1,0);
    draw_A();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(-21.00000,0,0);
    //glTranslatef(0,0,0);
    glRotatef(ang,0,1,0);
    draw_N();
    glPopMatrix();

    glutSwapBuffers();
}
double radius = 90, ay=90,ap=0,ar=90;

void yaw()
{
    cx=0;
    cz=0;
    cx= ex+radius*(cos(ay*3.1416/180));
    cz= ez+radius*(sin(ay*3.1416/180));

}


void pitch()
{
    cy=0;
    cz=0;
    cy= ey+radius*(cos(ap*3.1416/180));
    cz= ez+radius*(sin(ap*3.1416/180));

}
void roll()
{
    ux = radius*(cos(ar*3.1416/180));
    uy = radius*(sin(ar*3.1416/180));
}


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

    case '+':
        slices++;
        stacks++;
        break;
    case 'y':
        ay+=5;
        yaw();
        glutPostRedisplay();
        break;
    case 'Y':
        ay-=5;
        yaw();
        glutPostRedisplay();
        break;
    case 'p':
        ap+=5;
        pitch();
        glutPostRedisplay();
        break;
    case 'P':
        ap-=5;
        pitch();
        glutPostRedisplay();
        break;
    case 'r':
        ar+=5;
        roll();
        glutPostRedisplay();
        break;
    case 'R':
        ar-=5;
        roll();
        glutPostRedisplay();
        break;
    case '-':
        if (slices>3 && stacks>3)
        {
            slices--;
            stacks--;
        }
        break;

    case 'l':
      light_switch = !light_switch;
        break;
    case 'Z':
        tx-=1;
        printf("%lf %lf %lf\n",tx,ty,tz);
        break;
    case 'X':
        tx+=1;
        printf("%lf %lf %lf\n",tx,ty,tz);
        break;
    case 'C':
        ty-=.5;
        printf("%lf %lf %lf\n",tx,ty,tz);
        break;
    case 'V':
        ty+=.5;
        printf("%lf %lf %lf\n",tx,ty,tz);
        break;
    case 'B':
        tz-=1;
        printf("%lf %lf %lf\n",tx,ty,tz);
        break;
    case 'N':
        tz+=1;
        printf("%lf %lf %lf\n",tx,ty,tz);
        break;

    case 'w':
        ez--;
        cz--;

        break;
    case 's':
        ez++;
        cz++;
        break;

    case 'd':
        ex--;
        cx--;

        break;
    case 'a':
        ex++;
        cx++;
        break;

    case 'W':
        ey--;
        cy--;

        break;
    case 'S':
        ey++;
        cy++;
        break;

     case 'k':
         GLfloat tmp;
         tmp=ey;
         ey=ez;
         ez=ey;
        //swap(ey,ez);
        break;
    }
    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}


void animate()
{
    if(rot)
    {
        ang+=0.1;
        glutPostRedisplay();
    }
    if(rot==0)
    {

        ang = 0;
        glutPostRedisplay();
    }

}


const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);
    glutIdleFunc(animate);

    ///glClearColor(1,1,1,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);



    glutMainLoop();

    return EXIT_SUCCESS;
}