Загрузка данных


#include <GL/glut.h>
#pragma comment(lib, "freeglut.lib")
#include <stdlib.h>

GLfloat vertices[8][3] = {
    {-1.0, -1.0, -1.0},
    { 1.0, -1.0, -1.0},
    { 1.0,  1.0, -1.0},
    {-1.0,  1.0, -1.0},
    {-1.0, -1.0,  1.0},
    { 1.0, -1.0,  1.0},
    { 1.0,  1.0,  1.0},
    {-1.0,  1.0,  1.0}
};

GLfloat cubeColors[5][3] = {
    {1.0f, 0.2f, 0.2f},
    {0.2f, 1.0f, 0.2f},
    {0.2f, 0.4f, 1.0f},
    {1.0f, 0.8f, 0.2f},
    {0.8f, 0.2f, 1.0f}
};

int mainWindow;
int labWindow;

int mainWidth = 800;
int mainHeight = 800;

int labWidth = 800;
int labHeight = 600;

float eyeX = 5.0f;
float eyeY = 5.0f;
float eyeZ = 5.0f;

int labProjection = 1;

const float ISO_THETA = 35.264f;
const float ISO_PHI = 45.0f;

const float DIM_THETA = 20.7f;
const float DIM_PHI = 32.0f;

void drawFace(int a, int b, int c, int d) {
    glBegin(GL_POLYGON);
    glVertex3fv(vertices[a]);
    glVertex3fv(vertices[b]);
    glVertex3fv(vertices[c]);
    glVertex3fv(vertices[d]);
    glEnd();
}

void drawCube(int colorIndex) {
    glColor3fv(cubeColors[colorIndex]);

    drawFace(0, 3, 2, 1);
    drawFace(2, 3, 7, 6);
    drawFace(0, 4, 7, 3);
    drawFace(1, 2, 6, 5);
    drawFace(4, 5, 6, 7);
    drawFace(0, 1, 5, 4);
}

void renderCubeInViewport(int x, int y, int w, int h, int projType, int colorIndex) {
    glViewport(x, y, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (projType == 3) {
        double aspect = (double)w / (double)h;
        gluPerspective(45.0, aspect, 1.0, 20.0);
    } else {
        glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
    }

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    if (projType == 3) {
        gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    }

    switch (projType) {
    case 1:
        glRotatef(ISO_PHI, 0.0f, 1.0f, 0.0f);
        glRotatef(ISO_THETA, 1.0f, 0.0f, 0.0f);
        break;
    case 2:
        glRotatef(DIM_PHI, 0.0f, 1.0f, 0.0f);
        glRotatef(DIM_THETA, 1.0f, 0.0f, 0.0f);
        break;
    }

    drawCube(colorIndex);
}

void displayMain() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);

    int halfW = mainWidth / 2;
    int halfH = mainHeight / 2;

    renderCubeInViewport(0, halfH, halfW, halfH, 0, 0);
    renderCubeInViewport(0, 0, halfW, halfH, 2, 1);
    renderCubeInViewport(halfW, halfH, halfW, halfH, 1, 2);
    renderCubeInViewport(halfW, 0, halfW, halfH, 3, 3);

    glutSwapBuffers();
}

void displayLab() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);

    glViewport(0, 0, labWidth, labHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    float aspect = (float)labWidth / (float)labHeight;

    if (labProjection == 1) {
        glOrtho(-3.0 * aspect, 3.0 * aspect, -3.0, 3.0, 1.0, 20.0);
    } else {
        gluPerspective(60.0, aspect, 1.0, 20.0);
    }

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    drawCube(4);

    glutSwapBuffers();
}

void reshapeMain(int w, int h) {
    if (h == 0) h = 1;
    mainWidth = w;
    mainHeight = h;
    glViewport(0, 0, w, h);
}

void reshapeLab(int w, int h) {
    if (h == 0) h = 1;
    labWidth = w;
    labHeight = h;
    glViewport(0, 0, w, h);
}

void keyboard(unsigned char key, int x, int y) {
    float step = 0.3f;

    switch (key) {
    case '1':
        labProjection = 1;
        break;
    case '2':
        labProjection = 2;
        break;
    case 'w':
        eyeY += step;
        break;
    case 's':
        eyeY -= step;
        break;
    case 'a':
        eyeX -= step;
        break;
    case 'd':
        eyeX += step;
        break;
    case 'q':
        eyeZ += step;
        break;
    case 'e':
        eyeZ -= step;
        break;
    case 'r':
        eyeX = 5.0f;
        eyeY = 5.0f;
        eyeZ = 5.0f;
        break;
    case 27:
        exit(0);
    }

    glutSetWindow(mainWindow);
    glutPostRedisplay();

    glutSetWindow(labWindow);
    glutPostRedisplay();
}

void init() {
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowSize(mainWidth, mainHeight);
    glutInitWindowPosition(100, 100);
    mainWindow = glutCreateWindow("Four Cube Projections");
    init();
    glutDisplayFunc(displayMain);
    glutReshapeFunc(reshapeMain);
    glutKeyboardFunc(keyboard);

    glutInitWindowSize(labWidth, labHeight);
    glutInitWindowPosition(950, 100);
    labWindow = glutCreateWindow("Laboratory Work 4");
    init();
    glutDisplayFunc(displayLab);
    glutReshapeFunc(reshapeLab);
    glutKeyboardFunc(keyboard);

    glutMainLoop();

    return 0;
}