From 5d15619c96a8a8ecf6786b093656222e49277ea8 Mon Sep 17 00:00:00 2001 From: "re.kovalev" Date: Fri, 3 Feb 2023 13:20:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D0=BA=D1=81=D1=82=D1=83=D1=80?= =?UTF-8?q?=D1=8B=20=D0=B2=D1=8B=D1=81=D0=BE=D1=82=20=D0=B8=20=D0=BD=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Model.h | 2 ++ include/Texture.h | 2 ++ src/Model.cpp | 16 ++++++++++-- src/Texture.cpp | 66 ++++++++++++++++++++++++++++++++++++++++------- src/main.cpp | 2 +- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/include/Model.h b/include/Model.h index 946dd34..4a2e3a5 100644 --- a/include/Model.h +++ b/include/Model.h @@ -65,6 +65,8 @@ class Model : public Movable Texture texture_diffuse; // Диффузная текстура Texture texture_ambient; // Текстура фонового освщения Texture texture_specular; // Текстура зеркального отражения + Texture texture_heights; // Текстура высот + Texture texture_normals; // Текстура нормалей }; // Класс сгруппированной модели diff --git a/include/Texture.h b/include/Texture.h index 1c9c6a1..95bb306 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -10,6 +10,8 @@ enum TexType { TEX_DIFFUSE, TEX_AMBIENT, TEX_SPECULAR, + TEX_HEIGHTS, + TEX_NORMAL, TEX_AVAILABLE_COUNT }; diff --git a/src/Model.cpp b/src/Model.cpp index 8e03e71..c9e5fde 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -20,7 +20,8 @@ vao(copy.vao), verteces_count(copy.verteces_count), first_index(copy.first_index), indices_count(copy.indices_count), vertex_vbo(copy.vertex_vbo), index_vbo(copy.index_vbo), normals_vbo(copy.normals_vbo), texCoords_vbo(copy.texCoords_vbo), tangent_vbo(copy.tangent_vbo), bitangent_vbo(copy.bitangent_vbo), -texture_diffuse(copy.texture_diffuse), texture_ambient(copy.texture_ambient), texture_specular(copy.texture_specular), material(copy.material) +texture_diffuse(copy.texture_diffuse), texture_ambient(copy.texture_ambient), texture_specular(copy.texture_specular), +texture_heights(copy.texture_heights), texture_normals(copy.texture_normals), material(copy.material) { } @@ -55,7 +56,8 @@ void Model::render(ShaderProgram &shaderProgram, UBO &material_buffer, const glm texture_diffuse.use(); texture_ambient.use(); texture_specular.use(); - + texture_heights.use(); + texture_normals.use(); // Загружаем данные о материале material_buffer.load(&material, sizeof(material)); @@ -194,6 +196,12 @@ void Model::set_texture(Texture& texture) case TEX_SPECULAR: texture_specular = texture; break; + case TEX_HEIGHTS: + texture_heights = texture; + break; + case TEX_NORMAL: + texture_normals = texture; + break; }; } @@ -435,6 +443,10 @@ GrouptedModel loadOBJtoGroupted(const char* filename, const char* mtl_directory, s->set_texture(ambient); Texture specular(TEX_SPECULAR, texture_directory + materials[materials_ids[i]].specular_texname); s->set_texture(specular); + Texture normal(TEX_NORMAL, texture_directory + materials[materials_ids[i]].normal_texname); + s->set_texture(normal); + Texture heights(TEX_HEIGHTS, texture_directory + materials[materials_ids[i]].bump_texname); + s->set_texture(heights); // Материал s->material.ka = pow(glm::vec3(materials[materials_ids[i]].ambient[0], materials[materials_ids[i]].ambient[1], materials[materials_ids[i]].ambient[2]), glm::vec3(1/inv_gamma)); diff --git a/src/Texture.cpp b/src/Texture.cpp index 51126b9..966a9ec 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -28,11 +28,34 @@ Texture::Texture(GLuint t, const std::string& filename) // Если изображение успешно считано if (image) { - // Загрузка данных с учетом прозрачности - if (channels == 3) // RGB - glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); - else if (channels == 4) // RGBA - glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); + // Выбор формата с учетом типа текстуры (нормали не sRGB) и числа каналов + GLuint internalformat = GL_RGB, format = GL_RGB; + switch (channels) + { + case 1: + internalformat = format = GL_RED; + break; + case 2: + internalformat = format = GL_RG; + break; + case 3: + format = GL_RGB; + if (type == TEX_NORMAL || type == TEX_HEIGHTS) + internalformat = GL_RGB; + else + internalformat = GL_SRGB; + break; + case 4: + format = GL_RGBA; + if (type == TEX_NORMAL || type == TEX_HEIGHTS) + internalformat = GL_RGBA; + else + internalformat = GL_SRGB_ALPHA; + break; + + } + // Загрузка данных с учетом формата + glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); // Генерация мипмапа для активной текстуры glBindTexture(GL_TEXTURE_2D, 0); // Отвязка активной текстуры @@ -239,11 +262,34 @@ TextureCube::TextureCube(GLuint t, const std::string (&filename)[6]) // Если изображение успешно считано if (image) { - // Загрузка данных с учетом прозрачности - if (channels == 3) // RGB - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_SRGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); - else if (channels == 4) // RGBA - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_SRGB_ALPHA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); + // Выбор формата с учетом типа текстуры (нормали не sRGB) и числа каналов + GLuint internalformat = GL_RGB, format = GL_RGB; + switch (channels) + { + case 1: + internalformat = format = GL_RED; + break; + case 2: + internalformat = format = GL_RG; + break; + case 3: + format = GL_RGB; + if (type == TEX_NORMAL || type == TEX_HEIGHTS) + internalformat = GL_RGB; + else + internalformat = GL_SRGB; + break; + case 4: + format = GL_RGBA; + if (type == TEX_NORMAL || type == TEX_HEIGHTS) + internalformat = GL_RGBA; + else + internalformat = GL_SRGB_ALPHA; + break; + + } + // Загрузка данных с учетом формата + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalformat, width, height, 0, format, GL_UNSIGNED_BYTE, image); stbi_image_free(image); // Освобождение оперативной памяти } diff --git a/src/main.cpp b/src/main.cpp index 3803fbe..54e350c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,7 +98,7 @@ int main(void) gShader.load(GL_FRAGMENT_SHADER, "shaders/gshader.frag"); gShader.link(); // Установим значения текстур - const char* textures_base_shader_names[] = {"tex_diffuse", "tex_ambient", "tex_specular"}; + const char* textures_base_shader_names[] = {"tex_diffuse", "tex_ambient", "tex_specular", "tex_heights", "tex_normal"}; gShader.bindTextures(textures_base_shader_names, sizeof(textures_base_shader_names)/sizeof(const char*));