/
ArtemNech
/
simpleOpenGL
Обзор
Документация
Войти
/
ArtemNech
/
simpleOpenGL
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
backend/ogl_sys/drawing-sets/COglImageManager.cpp
158 строк
5 KB
Artem Nechitaylenko
initial
16 май 2026, 12:48
16 май 2026, 12:48
0d3ac6c
Код
Авторство
О чём код?
// // Created by artem on 12.04.26. // #include "COglImageManager.h" #include <QDebug> COglImageManager::COglImageManager() = default; COglImageManager::~COglImageManager() = default; void COglImageManager::init() { } static GLuint createTextureFromImageData(const std::vector<uint8_t> &imageData, int width, int height) { GLuint textureId; glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D, textureId); // :/palette/images/palette/17.png // ПРОВЕРКА ДАННЫХ if (imageData.empty()) { qDebug() << "ERROR: Image bytes are empty! Creating fallback texture."; // Создаем красную текстуру для теста std::vector<uint8_t> fallbackData(width * height * 4, 255); for (size_t i = 0; i < fallbackData.size(); i += 4) { fallbackData[i] = 255; // R fallbackData[i+1] = 0; // G fallbackData[i+2] = 0; // B fallbackData[i+3] = 255; // A } glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, fallbackData.data()); } else if (imageData.size() < width * height * 4) { qDebug() << "WARNING: Image data size smaller than expected!"; qDebug() << "Expected:" << width * height * 4 << "Got:" << imageData.size(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData.data()); } else { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData.data()); } // Настройки фильтрации glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Проверка ошибок GLenum error = glGetError(); if (error != GL_NO_ERROR) { qDebug() << "OpenGL error in 'COglImageManager' after texture creation:" << error; glDeleteTextures(1, &textureId); return 0; } return textureId; } void COglImageManager::loadTexture(const std::string &textureId, const ngraph::IImageElement &image) { if (!image.isValid()) { qDebug() << "Invalid image for texture:" << textureId.c_str(); return; } if (image.bytes.empty()) { qDebug() << "Image has no data:" << textureId.c_str(); return; } int width = static_cast<int>(image.rect.width()); int height = static_cast<int>(image.rect.height()); if (width <= 0 || height <= 0) { qDebug() << "Invalid image dimensions from rect:" << width << "x" << height; return; } // Проверяем, что количество байтов соответствует ожидаемому size_t expectedBytes = width * height * 4; // RGBA if (image.bytes.size() != expectedBytes) { qDebug() << "Warning: Image bytes size mismatch. Expected:" << expectedBytes << "Got:" << image.bytes.size() << "for texture:" << textureId.c_str(); } GLuint texture = createTextureFromImageData(image.bytes, width, height); m_textures[textureId] = texture; } void COglImageManager::explode_image(COglLayer *set, const ngraph::IImageElement &image) { if (!image.isValid()) { qDebug() << "Cannot draw invalid image:" << image.imageId.c_str(); return; } while (glGetError() != GL_NO_ERROR) { // Пустое тело - просто проглатываем ошибки } const std::string& textureId = image.imageId; // Ленивая загрузка при необходимости auto it = m_textures.find(textureId); if (it == m_textures.end()) { loadTexture(textureId, image); it = m_textures.find(textureId); if (it == m_textures.end()) { qDebug() << "Failed to load texture:" << textureId.c_str(); return; } } // СОЗДАЕМ БАТЧ С ТЕКСТУРОЙ set->startBatch(GL_TRIANGLES, 12,true, it->second); auto rect = image.rect; // Координаты вершин auto x1 = static_cast<float>(rect.left()); auto y1 = static_cast<float>(rect.top()); auto x2 = static_cast<float>(rect.right()); auto y2 = static_cast<float>(rect.bottom()); // Цвет с прозрачностью ngraph::NColor color(1.0, 1.0, 1.0, 1.0); color.setAlpha(static_cast<uint8_t>(image.opacity)); auto &m_vertices = set->vertices(); float Z = set->get_layer_Z(); // Первый треугольник m_vertices.emplace_back(x1, y1, Z, color, 0.0f, 0.0f); m_vertices.emplace_back(x2, y1, Z, color, 1.0f, 0.0f); m_vertices.emplace_back(x1, y2, Z, color, 0.0f, 1.0f); // Второй треугольник m_vertices.emplace_back(x1, y2, Z, color, 0.0f, 1.0f); m_vertices.emplace_back(x2, y1, Z, color, 1.0f, 0.0f); m_vertices.emplace_back(x2, y2, Z, color, 1.0f, 1.0f); set->endBatch(); }