diff --git a/include/Model.h b/include/Model.h index 8c77920..7358103 100644 --- a/include/Model.h +++ b/include/Model.h @@ -15,12 +15,13 @@ void calc_tb(const GLuint* indices, const int indices_count, const glm::vec3* ve struct Material { - alignas(16) glm::vec3 ka; // коэф. фонового отражения (цвет фонового освещения) - alignas(16) glm::vec3 kd; // коэф. диффузного отражения (цвет объекта) - alignas(16) glm::vec3 ks; // коэф. зеркального блика - float p; // показатель глянцевости + alignas(16) glm::vec3 base_color; // Базовый цвет материала + float roughness; // Шероховатость поверхности + float metallic; // Металличность поверхности + float specular; // Интенсивность блика диэлектриков + alignas(16) glm::vec3 emitted; // Излучаемый поверхностью свет // Значения по умолчанию - Material() : ka(0.2f), kd(0.2f), ks(0.2f), p(1) { }; + Material() : base_color(0.8f), roughness(0.5f), metallic(0.0f), specular(0.5f), emitted(0.0f) { }; }; // Идентификатор модели @@ -89,9 +90,11 @@ class Model : public Movable BO *tangent_vbo, *bitangent_vbo; // буферы с касательными и бикасательными векторами GLuint verteces_count; // Количество вершин GLuint first_index, indices_count; // Первый и количество индексов - Texture texture_diffuse; // Диффузная текстура - Texture texture_ambient; // Текстура фонового освщения - Texture texture_specular; // Текстура зеркального отражения + Texture texture_albedo; // Текстура альбедо (цвет поверхности) + Texture texture_roughness; // Текстура шероховатостей + Texture texture_metallic; // Текстура металличности + Texture texture_specular; // Текстура интенсивности блика диэлектриков + Texture texture_emitted; // Текстура излучаемого света Texture texture_heights; // Текстура высот Texture texture_normals; // Текстура нормалей }; diff --git a/include/Texture.h b/include/Texture.h index 95bb306..ff20ece 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -7,9 +7,11 @@ #include enum TexType { - TEX_DIFFUSE, - TEX_AMBIENT, + TEX_ALBEDO, + TEX_ROUGHNESS, + TEX_METALLIC, TEX_SPECULAR, + TEX_EMITTED, TEX_HEIGHTS, TEX_NORMAL, TEX_AVAILABLE_COUNT @@ -36,8 +38,8 @@ class Texture : public BaseTexture { public: Texture(GLuint type = TEX_AVAILABLE_COUNT, const std::string& filename = ""); // Загрузка текстуры с диска или использование "пустой" - Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере - Texture(GLuint width, GLuint height, void* data, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера без привязки к буферу с загрузкой пикселей по указателю + Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере + Texture(GLuint width, GLuint height, void* data, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера без привязки к буферу с загрузкой пикселей по указателю Texture(const Texture& other); // Конструктор копирования Texture& operator=(const Texture& other); // Оператор присваивания @@ -49,7 +51,7 @@ class Texture : public BaseTexture class TextureArray : public BaseTexture { public: - TextureArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере + TextureArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере TextureArray(const TextureArray& other); // Конструктор копирования TextureArray& operator=(const TextureArray& other); // Оператор присваивания @@ -62,7 +64,7 @@ class TextureCube : public BaseTexture { public: TextureCube(GLuint type = TEX_AVAILABLE_COUNT, const std::string (&filename)[6] = {""}); // Загрузка текстуры с диска или использование "пустой" - TextureCube(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере + TextureCube(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере TextureCube(const TextureCube& other); // Конструктор копирования TextureCube& operator=(const TextureCube& other); // Оператор присваивания @@ -74,7 +76,7 @@ class TextureCube : public BaseTexture class TextureCubeArray : public BaseTexture { public: - TextureCubeArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере + TextureCubeArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере TextureCubeArray(const TextureCubeArray& other); // Конструктор копирования TextureCubeArray& operator=(const TextureCubeArray& other); // Оператор присваивания diff --git a/src/Lights.cpp b/src/Lights.cpp index e93557c..cf019ca 100644 --- a/src/Lights.cpp +++ b/src/Lights.cpp @@ -51,7 +51,7 @@ void BulbDebug::render(ShaderProgram &shaderProgram, UBO &material_buffer) glUniform3fv(direction_uniform, 1, &data->direction[0]); // Зададим параметры материала сфере действия - bulb_model.parts[0].material.ka = data->color; + bulb_model.parts[0].material.base_color = data->color; bulb_model.parts[0].position = data->position; bulb_model.parts[0].scale = glm::vec3(data->radius); @@ -67,7 +67,7 @@ void BulbDebug::render(ShaderProgram &shaderProgram, UBO &material_buffer) glUniform1f(angle_uniform, 180); // Зададим параметры материала сфере действия // Зададим цвет для колбы (первая в составе модели) - bulb_model.parts[1].material.ka = data->color; + bulb_model.parts[1].material.base_color = data->color; bulb_model.position = data->position; glm::mat4 transform = bulb_model.getTransformMatrix(); diff --git a/src/Model.cpp b/src/Model.cpp index 76213cf..555ef91 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -35,7 +35,7 @@ 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), +texture_albedo(copy.texture_albedo), texture_roughness(copy.texture_roughness), texture_metallic(copy.texture_metallic), texture_specular(copy.texture_specular), texture_emitted(copy.texture_emitted), texture_heights(copy.texture_heights), texture_normals(copy.texture_normals), material(copy.material), normalmapped(copy.normalmapped), parallaxmapped(copy.parallaxmapped), displacementmapped(copy.displacementmapped) { @@ -92,9 +92,11 @@ void Model::render(ShaderProgram &shaderProgram, UBO &material_buffer, const glm glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &model[0][0]); // Подключаем текстуры - texture_diffuse.use(); - texture_ambient.use(); + texture_albedo.use(); + texture_roughness.use(); + texture_metallic.use(); texture_specular.use(); + texture_emitted.use(); texture_heights.use(); texture_normals.use(); @@ -248,14 +250,25 @@ void Model::set_texture(Texture& texture) GLuint type = texture.getType(); switch(type) { - case TEX_DIFFUSE: - texture_diffuse = texture; + case TEX_ALBEDO: + texture_albedo = texture; + material.base_color.r = -1; break; - case TEX_AMBIENT: - texture_ambient = texture; + case TEX_ROUGHNESS: + texture_roughness = texture; + material.roughness = -1; + break; + case TEX_METALLIC: + texture_metallic = texture; + material.metallic = -1; break; case TEX_SPECULAR: texture_specular = texture; + material.specular = -1; + break; + case TEX_EMITTED: + texture_emitted = texture; + material.emitted.r = -1; break; case TEX_HEIGHTS: texture_heights = texture; @@ -501,23 +514,39 @@ GrouptedModel loadOBJtoGroupted(const char* filename, const char* mtl_directory, auto s = --result.parts.end(); s->set_index_range(materials_range[i], materials_range[i+1]-materials_range[i]); - // Текстуры - Texture diffuse(TEX_DIFFUSE, texture_directory + materials[materials_ids[i]].diffuse_texname); - s->set_texture(diffuse); - Texture ambient(TEX_AMBIENT, texture_directory + materials[materials_ids[i]].ambient_texname); - 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)); - s->material.kd = pow(glm::vec3(materials[materials_ids[i]].diffuse[0], materials[materials_ids[i]].diffuse[1], materials[materials_ids[i]].diffuse[2]), glm::vec3(1/inv_gamma)); - s->material.ks = glm::vec3(materials[materials_ids[i]].specular[0], materials[materials_ids[i]].specular[1], materials[materials_ids[i]].specular[2]); - s->material.p = (materials[materials_ids[i]].shininess > 0.0f) ? 1000.0f / materials[materials_ids[i]].shininess : 1000.0f; + s->material.base_color = pow(glm::vec3(materials[materials_ids[i]].diffuse[0], materials[materials_ids[i]].diffuse[1], materials[materials_ids[i]].diffuse[2]), glm::vec3(1/inv_gamma)); + s->material.roughness = 1 - sqrt(materials[materials_ids[i]].shininess/1000); // шероховатость поверхности + s->material.metallic = (materials[materials_ids[i]].ambient[0] + materials[materials_ids[i]].ambient[1] + materials[materials_ids[i]].ambient[2]) / 3.0f; + s->material.specular = (materials[materials_ids[i]].specular[0] + materials[materials_ids[i]].specular[1] + materials[materials_ids[i]].specular[2]) / 3.0f; + s->material.emitted = pow(glm::vec3(materials[materials_ids[i]].emission[0], materials[materials_ids[i]].emission[1], materials[materials_ids[i]].emission[2]), glm::vec3(1/inv_gamma)); + + // Текстуры + if (!materials[materials_ids[i]].diffuse_texname.empty()) + { + Texture diffuse(TEX_ALBEDO, texture_directory + materials[materials_ids[i]].diffuse_texname); + s->set_texture(diffuse); + } + if (!materials[materials_ids[i]].ambient_texname.empty()) + { + Texture ambient(TEX_METALLIC, texture_directory + materials[materials_ids[i]].ambient_texname); + s->set_texture(ambient); + } + if (!materials[materials_ids[i]].specular_texname.empty()) + { + Texture specular(TEX_SPECULAR, texture_directory + materials[materials_ids[i]].specular_texname); + s->set_texture(specular); + } + if (!materials[materials_ids[i]].normal_texname.empty()) + { + Texture normal(TEX_NORMAL, texture_directory + materials[materials_ids[i]].normal_texname); + s->set_texture(normal); + } + if (!materials[materials_ids[i]].bump_texname.empty()) + { + Texture heights(TEX_HEIGHTS, texture_directory + materials[materials_ids[i]].bump_texname); + s->set_texture(heights); + } } return result; diff --git a/src/main.cpp b/src/main.cpp index 1fd1917..c897758 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,8 +119,6 @@ int main(void) GrouptedModel scene = loadOBJtoGroupted("../resources/models/blob.obj", "../resources/models/", "../resources/textures/"); scene.scale = glm::vec3(0.01); scene.position.z = 1; - scene.parts[0].material.kd = {0.5,0.5,0.5}; - scene.parts[0].material.ka = {0.05,0.05,0.05}; scene.set_group_id((GLuint64) &scene); // Установка цвета очистки буфера цвета @@ -320,7 +318,7 @@ int main(void) rectangle.scale = glm::vec3(4); // Текстуры для прямоугольника - Texture rectangle_diffuse(TEX_DIFFUSE, "../resources/textures/rekovalev_diffusemap.png"); + Texture rectangle_diffuse(TEX_ALBEDO, "../resources/textures/rekovalev_diffusemap.png"); rectangle.set_texture(rectangle_diffuse); Texture rectangle_normal(TEX_NORMAL, "../resources/textures/rekovalev_normalmap.png"); rectangle.set_texture(rectangle_normal); @@ -375,7 +373,7 @@ int main(void) // Модель скайбокса Model skybox; skybox.load_verteces(skybox_verticies, sizeof(skybox_verticies)/sizeof(glm::vec3)); - TextureCube skybox_texture(TEX_DIFFUSE, { "../resources/textures/skybox/px.jpg" + TextureCube skybox_texture(TEX_ALBEDO, { "../resources/textures/skybox/px.jpg" , "../resources/textures/skybox/nx.jpg" , "../resources/textures/skybox/py.jpg" , "../resources/textures/skybox/ny.jpg"