From d2ab94fcdea988b442e799ce33d4ecd7392e4110 Mon Sep 17 00:00:00 2001 From: "re.kovalev" Date: Mon, 14 Nov 2022 19:53:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D1=83=D1=84=D0=B5=D1=80=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 | 3 ++- shaders/shader.vert | 1 + src/Model.cpp | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/Model.h b/include/Model.h index d79e677..907a13d 100644 --- a/include/Model.h +++ b/include/Model.h @@ -65,13 +65,14 @@ class Model : public Node void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер + void load_normals(glm::vec3* normals, GLuint count); // Загрузка нормалей в буфер void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов void set_texture(Texture& texture); // Привязка текстуры к модели private: VAO vao; BO vertex_vbo, index_vbo; // вершинный и индексный буферы - BO texCoords_vbo; // буфер с текстурными координатами + BO normals_vbo, texCoords_vbo; // буферы с нормалями и текстурными координатами GLuint verteces_count; // Количество вершин size_t first_index_byteOffset, indices_count; // Сдвиг в байтах для первого и количество индексов Texture texture_diffuse; // Диффузная текстура diff --git a/shaders/shader.vert b/shaders/shader.vert index caca2c7..4947387 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -2,6 +2,7 @@ layout(location = 0) in vec3 pos; layout(location = 1) in vec2 inTexCoord; +layout(location = 2) in vec3 normals; uniform mat4 vp; uniform mat4 model; diff --git a/src/Model.cpp b/src/Model.cpp index aebe24f..74254af 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -202,7 +202,7 @@ Node& Node::operator=(const Node& other) // Конструктор по умолчанию Model::Model(Node *parent) : Node(parent), verteces_count(0), first_index_byteOffset(0), indices_count(0), -vertex_vbo(VERTEX), index_vbo(ELEMENT), texCoords_vbo(VERTEX) +vertex_vbo(VERTEX), index_vbo(ELEMENT), normals_vbo(VERTEX), texCoords_vbo(VERTEX) { } @@ -211,7 +211,7 @@ vertex_vbo(VERTEX), index_vbo(ELEMENT), texCoords_vbo(VERTEX) 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), texCoords_vbo(copy.texCoords_vbo), +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) { @@ -332,7 +332,35 @@ void Model::load_texCoords(glm::vec2* texCoords, GLuint count) texCoords_vbo.load(texCoords, sizeof(glm::vec2)*count); texCoords_attrib_config(); } - + +// Функция для конфигурации атрибута вершинного буфера +void normals_attrib_config() +{ + // Устанавливаем связь между VAO и привязанным VBO + glVertexAttribPointer( 2 // индекс атрибута, должен совпадать с Layout шейдера + , 3 // количество компонент одного элемента + , GL_FLOAT // тип + , GL_FALSE // необходимость нормировать значения + , 0 // шаг + , (void *)0 // отступ с начала массива + ); + // Включаем необходимый атрибут у выбранного VAO + glEnableVertexAttribArray(2); +} + +// Загрузка нормалей в буфер +void Model::load_normals(glm::vec3* normals, GLuint count) +{ + // Подключаем VAO + vao.use(); + + normals_vbo.use(); + + // Загрузка вершин в память буфера + normals_vbo.load(normals, sizeof(glm::vec3)*count); + normals_attrib_config(); +} + // Ограничение диапазона из буфера индексов void Model::set_index_range(size_t first_byteOffset, size_t count) {