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


#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}
};

float eyeX = 5.0f, eyeY = 5.0f, eyeZ = 5.0f;
float centerX = 0.0f, centerY = 0.0f, centerZ = 0.0f;
float upX = 0.0f, upY = 1.0f, upZ = 0.0f;

int windowWidth = 800;
int windowHeight = 800;

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

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

GLfloat cubeColors[4][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}
};

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(1, 2, 6, 5);
    drawFace(5, 6, 7, 4);
    drawFace(0, 4, 7, 3);
    drawFace(3, 7, 6, 2);
    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, centerX, centerY, centerZ, upX, upY, upZ);
    }

    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 display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);

    int halfW = windowWidth / 2;
    int halfH = windowHeight / 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 reshape(int w, int h) {
    windowWidth = w;
    windowHeight = h;
    glViewport(0, 0, w, h);
}

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

    switch (key) {
    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);
    }

    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(windowWidth, windowHeight);
    glutCreateWindow("Cube Projections");

    init();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
    return 0;
}