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


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

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

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

GLuint textureID;

int imageWidth, imageHeight, imageChannels;
unsigned char* imageData = NULL;

float angleX = 20.0f;
float angleY = 30.0f;

int windowWidth = 800;
int windowHeight = 600;

void drawFace(int a, int b, int c, int d) {
    glBegin(GL_QUADS);

    glTexCoord2f(0.0f, 0.0f); glVertex3fv(vertices[a]);
    glTexCoord2f(1.0f, 0.0f); glVertex3fv(vertices[b]);
    glTexCoord2f(1.0f, 1.0f); glVertex3fv(vertices[c]);
    glTexCoord2f(0.0f, 1.0f); glVertex3fv(vertices[d]);

    glEnd();
}

void drawCube() {
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureID);

    glColor3f(1.0f, 1.0f, 1.0f);

    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);

    glDisable(GL_TEXTURE_2D);
}

void loadTexture(const char* filename) {
    imageData = stbi_load(filename, &imageWidth, &imageHeight, &imageChannels, 3);

    if (!imageData) {
        printf("Ошибка загрузки изображения\n");
        exit(1);
    }

    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGB,
        imageWidth,
        imageHeight,
        0,
        GL_RGB,
        GL_UNSIGNED_BYTE,
        imageData
    );
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    glViewport(0, 0, windowWidth, windowHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    double aspect = (double)windowWidth / (double)windowHeight;
    gluPerspective(45.0, aspect, 1.0, 20.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(
        4.0, 4.0, 6.0,
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0
    );

    glRotatef(angleX, 1.0f, 0.0f, 0.0f);
    glRotatef(angleY, 0.0f, 1.0f, 0.0f);

    drawCube();

    glutSwapBuffers();
}

void reshape(int w, int h) {
    if (h == 0) h = 1;

    windowWidth = w;
    windowHeight = h;

    glViewport(0, 0, w, h);
}

void idle() {
    angleY += 0.2f;
    if (angleY > 360.0f) angleY -= 360.0f;

    glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y) {
    switch (key) {
    case 'a': angleY -= 5.0f; break;
    case 'd': angleY += 5.0f; break;
    case 'w': angleX -= 5.0f; break;
    case 's': angleX += 5.0f; break;
    case 27:
        if (imageData) stbi_image_free(imageData);
        exit(0);
    }

    glutPostRedisplay();
}

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

    loadTexture("texture.bmp");
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("Laboratory Work 5 - Textured Cube");

    init();

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

    glutMainLoop();

    return 0;
}