Adapting for web
This commit is contained in:
1 parent
cf1ec1e76f
commit
a59bcc0c4b
42 files changed
+843
-294
No files matched your search
@@ -1,4 +1,4 @@
|
||||
#include "render/TextureManager.h"
|
||||
#include "render/TextureManager.h"
|
||||
#include "render/OpenGlExtensions.h"
|
||||
#ifdef PNG_ENABLED
|
||||
#include "png.h"
|
||||
@@ -11,48 +11,68 @@ namespace ZL
|
||||
Texture::Texture(const TextureDataStruct& texData) {
|
||||
width = texData.width;
|
||||
height = texData.height;
|
||||
|
||||
glGenTextures(1, &texID);
|
||||
if (texID == 0) {
|
||||
throw std::runtime_error("glGenTextures did not work");
|
||||
glBindTexture(GL_TEXTURE_2D, texID);
|
||||
|
||||
GLint internalFormat;
|
||||
GLenum externalFormat;
|
||||
|
||||
switch (texData.format) {
|
||||
case TextureDataStruct::R8:
|
||||
// Для WebGL 1.0 лучше использовать GL_LUMINANCE вместо GL_RED
|
||||
// Если используешь WebGL 2.0, можно оставить GL_RED
|
||||
#if defined(EMSCRIPTEN) || defined(__ANDROID__)
|
||||
internalFormat = GL_LUMINANCE;
|
||||
externalFormat = GL_LUMINANCE;
|
||||
#else
|
||||
internalFormat = GL_RED;
|
||||
externalFormat = GL_RED;
|
||||
#endif
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
// Исправлено: используем GL_TEXTURE_2D
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
break;
|
||||
|
||||
case TextureDataStruct::RGB:
|
||||
internalFormat = GL_RGB;
|
||||
externalFormat = GL_RGB;
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // RGB может быть не выровнен по 4 байта
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
break;
|
||||
|
||||
default: // RGBA
|
||||
internalFormat = GL_RGBA;
|
||||
externalFormat = GL_RGBA;
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
break;
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
|
||||
static_cast<GLsizei>(width), static_cast<GLsizei>(height),
|
||||
0, externalFormat, GL_UNSIGNED_BYTE, texData.data.data());
|
||||
CheckGlError();
|
||||
|
||||
// ��������� ���������� � ����������� �� ���������� ����
|
||||
if (texData.mipmap == TextureDataStruct::GENERATE) {
|
||||
// ���������� ����������� ���������� ��� ���������� ��������
|
||||
// 3. Фильтрация и Мип-мапы
|
||||
// ВНИМАНИЕ: Для шрифтов (NPOT) в WebGL glGenerateMipmap работать НЕ будет!
|
||||
if (texData.mipmap == TextureDataStruct::GENERATE && texData.format != TextureDataStruct::R8) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
// ������� �������� ���������� (����� ������� �� ��������)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
CheckGlError();
|
||||
|
||||
// �������� ������ �������� ������ (Level 0)
|
||||
GLint format = (texData.bitSize == TextureDataStruct::BS_24BIT) ? GL_RGB : GL_RGBA;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format,
|
||||
static_cast<GLsizei>(width),
|
||||
static_cast<GLsizei>(height),
|
||||
0, format, GL_UNSIGNED_BYTE, texData.data.data());
|
||||
|
||||
CheckGlError();
|
||||
|
||||
// ��������� ���-�����, ���� ��� ����������
|
||||
if (texData.mipmap == TextureDataStruct::GENERATE) {
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
CheckGlError();
|
||||
}
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||||
}
|
||||
|
||||
Texture::Texture(const std::array<TextureDataStruct, 6>& texDataArray)
|
||||
{
|
||||
// ��������, ��� ��� ����� ����� ���������� �������
|
||||
width = texDataArray[0].width;
|
||||
height = texDataArray[0].height;
|
||||
|
||||
@@ -73,36 +93,28 @@ namespace ZL
|
||||
|
||||
CheckGlError();
|
||||
|
||||
// ��������� ���������� ��� Cubemap
|
||||
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
// ���������� GL_LINEAR ��� MIN_FILTER, ��� ��� ������� ����� �� ������������
|
||||
// ���� �� �������������� ������� (e.g., GL_LINEAR_MIPMAP_LINEAR), ����� ���� �� ������� glGenerateMipmap.
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
// ������������ ��������� ������� ��� Cubemap
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
|
||||
// GL_TEXTURE_WRAP_R �� �������������� � WebGL 1.0/OpenGL ES 2.0 � �������� ������.
|
||||
// ������������ ��� ����� ������ ��� ���������� ��������.
|
||||
#ifndef EMSCRIPTEN
|
||||
#ifndef __ANDROID__
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
CheckGlError(); // �������� ����� ��������� ����������
|
||||
|
||||
// �������� ������ ��� ������ �� 6 ������
|
||||
// GL_TEXTURE_CUBE_MAP_POSITIVE_X + i ���� �����: +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
|
||||
CheckGlError();
|
||||
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
GLint internalFormat;
|
||||
GLenum format;
|
||||
|
||||
// � WebGL 1.0/OpenGL ES 2.0 ���������� ������ (internalFormat)
|
||||
// ������ ������ ��������������� ������� ������ (format).
|
||||
if (texDataArray[i].bitSize == TextureDataStruct::BS_24BIT)
|
||||
|
||||
if (texDataArray[i].format == TextureDataStruct::RGB)
|
||||
{
|
||||
internalFormat = GL_RGB; // internalFormat
|
||||
format = GL_RGB; // format
|
||||
@@ -114,20 +126,19 @@ namespace ZL
|
||||
}
|
||||
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, // ������� �����
|
||||
0, // ������� MIP-��������
|
||||
internalFormat, // ���������� ������ (������ ��������� � ��������)
|
||||
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
|
||||
0,
|
||||
internalFormat,
|
||||
static_cast<GLsizei>(width),
|
||||
static_cast<GLsizei>(height),
|
||||
0, // ������� (������ 0)
|
||||
format, // ������ �������� ������
|
||||
GL_UNSIGNED_BYTE, // ��� ������
|
||||
texDataArray[i].data.data() // ��������� �� ������
|
||||
0,
|
||||
format,
|
||||
GL_UNSIGNED_BYTE,
|
||||
texDataArray[i].data.data()
|
||||
);
|
||||
CheckGlError();
|
||||
}
|
||||
|
||||
// ������� �������� ��� �������
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
|
||||
@@ -173,7 +184,7 @@ namespace ZL
|
||||
texData.width = *reinterpret_cast<uint32_t*>(&fileArr[18]);
|
||||
texData.height = *reinterpret_cast<uint32_t*>(&fileArr[22]);
|
||||
|
||||
texData.bitSize = TextureDataStruct::BS_24BIT;
|
||||
texData.format = TextureDataStruct::RGB;
|
||||
|
||||
size_t dataSize = texData.width * texData.height * 3;
|
||||
|
||||
@@ -222,7 +233,7 @@ namespace ZL
|
||||
texData.width = *reinterpret_cast<uint32_t*>(&fileArr[18]);
|
||||
texData.height = *reinterpret_cast<uint32_t*>(&fileArr[22]);
|
||||
|
||||
texData.bitSize = TextureDataStruct::BS_32BIT;
|
||||
texData.format = TextureDataStruct::RGBA;
|
||||
|
||||
size_t dataSize = texData.width * texData.height * 4;
|
||||
|
||||
@@ -255,48 +266,33 @@ namespace ZL
|
||||
|
||||
#ifdef PNG_ENABLED
|
||||
|
||||
// ��������� ��� �������� ������ � �����/������� � ������� ������� ������
|
||||
struct png_data_t {
|
||||
const char* data;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
// ���������������� ������� ������ ��� libpng
|
||||
// 'png_ptr' - ��������� �� ��������� png
|
||||
// 'out_ptr' - ���� ���������� ����������� ������
|
||||
// 'bytes_to_read' - ������� ���� ����� ���������
|
||||
void user_read_data(png_structp png_ptr, png_bytep out_ptr, png_size_t bytes_to_read) {
|
||||
// �������� ��������� �� ���� ��������� png_data_t, ������� �� ���������� � ������� png_set_read_fn
|
||||
png_data_t* data = (png_data_t*)png_get_io_ptr(png_ptr);
|
||||
|
||||
if (data->offset + bytes_to_read > data->size) {
|
||||
// ������� ��������� ������, ��� ���� � �������.
|
||||
// ������ ������ ����������� ������, �� ����� ������ ��������� ������� ��� ������� ������.
|
||||
// � ���� ������ �� ������� ������ libpng.
|
||||
png_error(png_ptr, "PNG Read Error: Attempted to read past end of data buffer.");
|
||||
bytes_to_read = data->size - data->offset; // �������������, ����� ��������� ����������
|
||||
bytes_to_read = data->size - data->offset;
|
||||
}
|
||||
|
||||
// �������� ������ �� ������ ������� � ����� libpng
|
||||
std::memcpy(out_ptr, data->data + data->offset, bytes_to_read);
|
||||
|
||||
// ��������� ��������
|
||||
data->offset += bytes_to_read;
|
||||
}
|
||||
|
||||
// ���������������� ������� �������������� (�� �������, ����� ������������ nullptr)
|
||||
void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg) {
|
||||
// ����� ����� ����������� ����������� ��������������
|
||||
//throw std::runtime_error();
|
||||
std::cout << "PNG Warning: " << warning_msg << std::endl;
|
||||
}
|
||||
|
||||
// ���������������� ������� ������ (����������� ��� setjmp)
|
||||
|
||||
void user_error_fn(png_structp png_ptr, png_const_charp error_msg) {
|
||||
// ����� ����� ����������� ����������� ������
|
||||
|
||||
std::cout << "PNG Error: " << error_msg << std::endl;
|
||||
// ����������� �������� longjmp ��� ������ �� �������� ������/������ PNG
|
||||
longjmp(png_jmpbuf(png_ptr), 1);
|
||||
}
|
||||
|
||||
@@ -304,7 +300,6 @@ namespace ZL
|
||||
{
|
||||
TextureDataStruct texData;
|
||||
|
||||
// ��������� ��� ���������� ������� �� �������
|
||||
png_data_t png_data = { fileArr.data(), fileArr.size(), 0 };
|
||||
|
||||
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
@@ -318,21 +313,15 @@ namespace ZL
|
||||
throw std::runtime_error("Could not create PNG info structure");
|
||||
}
|
||||
|
||||
// === ��������� ���������������� ������� ������ � ��������� ������ ===
|
||||
// 1. ��������� ����������� ������ � longjmp
|
||||
if (setjmp(png_jmpbuf(png))) {
|
||||
png_destroy_read_struct(&png, &info, nullptr);
|
||||
throw std::runtime_error("Error during PNG read (longjmp was executed)");
|
||||
}
|
||||
|
||||
// 2. ��������� ���������������� ������� ��� ��������� ������ � ��������������
|
||||
// ������ nullptr � error_ptr � warning_ptr ����� �������� ��������� �� ���� ��������� ������, ���� ����������
|
||||
png_set_error_fn(png, nullptr, user_error_fn, user_warning_fn);
|
||||
|
||||
// 3. ��������� ���������������� ������� ������ � �������� �� ����� ��������� png_data
|
||||
png_set_read_fn(png, &png_data, user_read_data);
|
||||
// ===================================================================
|
||||
|
||||
|
||||
png_read_info(png, info);
|
||||
|
||||
texData.width = png_get_image_width(png, info);
|
||||
@@ -340,7 +329,6 @@ namespace ZL
|
||||
png_byte color_type = png_get_color_type(png, info);
|
||||
png_byte bit_depth = png_get_bit_depth(png, info);
|
||||
|
||||
// === ���� �������������� (�������� ��� ���������) ===
|
||||
if (bit_depth == 16)
|
||||
png_set_strip_16(png);
|
||||
|
||||
@@ -363,9 +351,7 @@ namespace ZL
|
||||
png_set_gray_to_rgb(png);
|
||||
|
||||
png_read_update_info(png, info);
|
||||
// ====================================================
|
||||
|
||||
// === ������ �������� (�������� ��� ���������) ===
|
||||
|
||||
png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * texData.height);
|
||||
for (int y = 0; y < texData.height; y++) {
|
||||
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
|
||||
@@ -379,11 +365,11 @@ namespace ZL
|
||||
|
||||
if (has_alpha)
|
||||
{
|
||||
texData.bitSize = TextureDataStruct::BS_32BIT;
|
||||
texData.format = TextureDataStruct::RGBA;
|
||||
}
|
||||
else
|
||||
{
|
||||
texData.bitSize = TextureDataStruct::BS_24BIT;
|
||||
texData.format = TextureDataStruct::RGB;
|
||||
}
|
||||
|
||||
int channels = has_alpha ? 4 : 3;
|
||||
@@ -424,7 +410,6 @@ namespace ZL
|
||||
throw std::runtime_error("Could not read file data into memory");
|
||||
}
|
||||
|
||||
// �������� ����� �������, ������� �������� � �������� ����
|
||||
return CreateTextureDataFromPng(fileArr);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user