From e9dddd4dbb18bb6d9649d616d6e42d70504e1d17 Mon Sep 17 00:00:00 2001 From: "re.kovalev" Date: Wed, 23 Nov 2022 14:11:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BC=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Model.h | 14 +++++++++++++- include/Scene.h | 2 +- shaders/shader.frag | 10 +++++++++- src/Model.cpp | 10 ++++++++-- src/Scene.cpp | 11 +++++++++-- src/main.cpp | 4 +++- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/include/Model.h b/include/Model.h index 907a13d..a63627e 100644 --- a/include/Model.h +++ b/include/Model.h @@ -51,6 +51,17 @@ class Node void invalidateParent(); // Проход потомков в глубину с изменением флага parent_changed }; +// Материал модели +struct Material +{ + alignas(16) glm::vec3 ka; // коэф. фонового отражения (цвет фонового освещения) + alignas(16) glm::vec3 kd; // коэф. диффузного отражения (цвет объекта) + alignas(16) glm::vec3 ks; // коэф. зеркального блика + float p; // показатель глянцевости + // Значения по умолчанию + Material() : ka(0.2f), kd(0.2f), ks(0.2f), p(1) { }; +}; + // Класс модели class Model : public Node { @@ -60,7 +71,7 @@ class Model : public Node Model& operator=(const Model& other); // Оператор присваивания virtual ~Model(); - void render(const GLuint &model_uniform); // Вызов отрисовки + void render(const GLuint &model_uniform, UBO &material_buffer); // Вызов отрисовки void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер @@ -69,6 +80,7 @@ class Model : public Node void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов void set_texture(Texture& texture); // Привязка текстуры к модели + Material material; // Материал модели private: VAO vao; BO vertex_vbo, index_vbo; // вершинный и индексный буферы diff --git a/include/Scene.h b/include/Scene.h index 0639c13..1f9162e 100644 --- a/include/Scene.h +++ b/include/Scene.h @@ -17,7 +17,7 @@ class Scene Scene(const Scene ©); // Конструктор копирования Scene& operator=(const Scene& other); // Оператор присваивания - void render(const GLuint &model_uniform); // Рендер сцены + void render(const GLuint &model_uniform, UBO &material_buffer); // Рендер сцены Node root; // Корневой узел diff --git a/shaders/shader.frag b/shaders/shader.frag index 801807d..ec3724e 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,7 +1,15 @@ -#version 330 core +#version 420 core in vec2 texCoord; +layout(std140, binding = 1) uniform Material +{ + vec3 ka; + vec3 kd; + vec3 ks; + float p; +}; + uniform sampler2D tex_diffuse; out vec4 color; diff --git a/src/Model.cpp b/src/Model.cpp index 74254af..7e4c9ec 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -212,7 +212,8 @@ Model::Model(const Model& copy) : Node(copy), vao(copy.vao), verteces_count(copy.verteces_count), first_index_byteOffset(copy.first_index_byteOffset), 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), -texture_diffuse(copy.texture_diffuse) +texture_diffuse(copy.texture_diffuse), +material(copy.material) { } @@ -233,6 +234,8 @@ Model& Model::operator=(const Model& other) texture_diffuse = other.texture_diffuse; + material = other.material; + return *this; } @@ -242,7 +245,7 @@ Model::~Model() } // Вызов отрисовки -void Model::render(const GLuint &model_uniform) +void Model::render(const GLuint &model_uniform, UBO &material_buffer) { // Загрузим матрицу трансформации glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &getTransformMatrix()[0][0]); @@ -250,6 +253,9 @@ void Model::render(const GLuint &model_uniform) // Подключаем текстуры texture_diffuse.use(); + // Загружаем данные о материале + material_buffer.load(&material, sizeof(material)); + // Подключаем VAO vao.use(); // Если есть индексы - рисуем с их использованием diff --git a/src/Scene.cpp b/src/Scene.cpp index 701c261..9219315 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -27,10 +27,10 @@ Scene& Scene::operator=(const Scene& other) } // Рендер сцены -void Scene::render(const GLuint &model_uniform) +void Scene::render(const GLuint &model_uniform, UBO &material_buffer) { for (auto & model : models) - model.render(model_uniform); + model.render(model_uniform, material_buffer); } // Перестройка узлов выбранного списка @@ -205,8 +205,15 @@ Scene loadOBJtoScene(const char* filename, const char* mtl_directory, const char auto s = --result.models.end(); s->set_index_range(materials_range[i]*sizeof(GLuint), materials_range[i+1]-materials_range[i]); + // Текстуры Texture diffuse(TEX_DIFFUSE, texture_directory + materials[materials_ids[i]].diffuse_texname); s->set_texture(diffuse); + + // Материал + s->material.ka = glm::vec3(materials[materials_ids[i]].ambient[0], materials[materials_ids[i]].ambient[1], materials[materials_ids[i]].ambient[2]); + s->material.kd = glm::vec3(materials[materials_ids[i]].diffuse[0], materials[materials_ids[i]].diffuse[1], materials[materials_ids[i]].diffuse[2]); + 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; } return result; diff --git a/src/main.cpp b/src/main.cpp index dba83c0..bc775c7 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,6 +93,7 @@ int main(void) const char* textures_base_shader_names[] = {"tex_diffuse"}; base.bindTextures(textures_base_shader_names, sizeof(textures_base_shader_names)/sizeof(const char*)); + // Загрузка сцены из obj файла Scene scene = loadOBJtoScene("../resources/models/cubes.obj", "../resources/models/", "../resources/textures/"); @@ -104,6 +105,7 @@ int main(void) // Uniform-буферы UBO cameraUB(sizeof(glm::mat4)*2, 0); + UBO material_data(sizeof(Material), 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Использование уменьшенных версий mipmap @@ -118,7 +120,7 @@ int main(void) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Тут производится рендер - scene.render(model_uniform); + scene.render(model_uniform, material_data); // Представление содержимого буфера цепочки показа на окно glfwSwapBuffers(window);