From 322cfb30202e2156d41085eb309fc4cc4c6ff1d6 Mon Sep 17 00:00:00 2001 From: "re.kovalev" Date: Thu, 15 Dec 2022 13:45:24 +0300 Subject: [PATCH] =?UTF-8?q?=D1=82=D1=80=D0=B0=D0=BD=D1=81=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BC=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B8=20=D1=81=20=D1=83=D1=87=D0=B5=D1=82=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=BC=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Model.h | 7 ++++++- src/Model.cpp | 30 +++++++++++++++++++++++++++--- src/main.cpp | 11 ++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/include/Model.h b/include/Model.h index 103a29d..62d21df 100644 --- a/include/Model.h +++ b/include/Model.h @@ -11,10 +11,15 @@ class Model Model(); // Конструктор без параметров Model(const Model& copy); // Конструктор копирования ~Model(); - void render(); // Вызов отрисовки + void render(const GLuint &mvp_uniform); // Вызов отрисовки void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер + glm::vec3 position; // позиция модели + glm::vec3 rotation; // поворот модели + glm::vec3 scale; // мастабирование модели + glm::mat4 getTransformMatrix(); // Матрица трансформации модели + private: VAO vao; BO vertex_vbo, index_vbo; // вершинный и индексный буферы diff --git a/src/Model.cpp b/src/Model.cpp index ee5697e..094d084 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -1,13 +1,15 @@ #include "Model.h" +#include "Camera.h" +extern Camera camera; // Конструктор без параметров -Model::Model() : verteces_count(0), indices_count(0), vertex_vbo(VERTEX), index_vbo(ELEMENT) +Model::Model() : verteces_count(0), indices_count(0), vertex_vbo(VERTEX), index_vbo(ELEMENT), position(0), rotation(0), scale(1) { } // Конструктор копирования -Model::Model(const Model& copy) : vao(copy.vao), verteces_count(copy.verteces_count), indices_count(copy.indices_count), vertex_vbo(copy.vertex_vbo), index_vbo(copy.index_vbo) +Model::Model(const Model& copy) : vao(copy.vao), verteces_count(copy.verteces_count), indices_count(copy.indices_count), vertex_vbo(copy.vertex_vbo), index_vbo(copy.index_vbo), position(copy.position), rotation(copy.rotation), scale(copy.scale) { } @@ -18,8 +20,12 @@ Model::~Model() } // Вызов отрисовки -void Model::render() +void Model::render(const GLuint &mvp_uniform) { + // Расчитаем матрицу трансформации + glm::mat4 mvp = camera.getVP() * this->getTransformMatrix(); + glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &mvp[0][0]); + // Подключаем VAO vao.use(); // Если есть индексы - рисуем с их использованием @@ -74,3 +80,21 @@ void Model::load_indices(GLuint* indices, GLuint count) // Запоминаем количество вершин для отрисовки indices_count = count; } + +#include + +// Матрица трансформации модели +glm::mat4 Model::getTransformMatrix() +{ + glm::mat4 transformMatrix = glm::mat4(1.0f); + // Перемещение модели + transformMatrix = glm::translate(transformMatrix, position); + // Масштабирование + transformMatrix = glm::scale(transformMatrix, scale); + // Поворот модели + transformMatrix = glm::rotate(transformMatrix, glm::radians(rotation.x), glm::vec3(1.0, 0.0, 0.0)); + transformMatrix = glm::rotate(transformMatrix, glm::radians(rotation.y), glm::vec3(0.0, 1.0, 0.0)); + transformMatrix = glm::rotate(transformMatrix, glm::radians(rotation.z), glm::vec3(0.0, 0.0, 1.0)); + + return transformMatrix; +} diff --git a/src/main.cpp b/src/main.cpp index dff4431..dfeca98 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include +#include "Camera.h" #include "Model.h" #define WINDOW_WIDTH 800 @@ -112,6 +113,9 @@ GLuint LoadShaders(const char *vertex_file, const char *fragment_file) return programID; } + +Camera camera(800.0f/600.0f); + int main(void) { GLFWwindow* window; // Указатель на окно GLFW3 @@ -173,10 +177,15 @@ int main(void) // Загрузка индексов модели rectangle.load_indices(indices, sizeof(indices)); + + rectangle.position.z = 2; + rectangle.rotation = glm::vec3(45); // Установка цвета очистки буфера цвета glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + // Расположение Uniform-переменной + GLuint mvp_uniform = glGetUniformLocation(shaderProgram, "mvp"); // Пока не произойдет событие запроса закрытия окна while(!glfwWindowShouldClose(window)) @@ -185,7 +194,7 @@ int main(void) glClear(GL_COLOR_BUFFER_BIT); // Тут производится рендер - rectangle.render(); + rectangle.render(mvp_uniform); // Представление содержимого буфера цепочки показа на окно glfwSwapBuffers(window);