трансформация модели с учетом камеры

This commit is contained in:
Ковалев Роман Евгеньевич 2022-12-15 13:45:24 +03:00 committed by R.E. Kovalev
parent 93a3474872
commit 322cfb3020
3 changed files with 43 additions and 5 deletions

View File

@ -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; // вершинный и индексный буферы

View File

@ -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/gtc/matrix_transform.hpp>
// Матрица трансформации модели
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;
}

View File

@ -5,6 +5,7 @@
#include <iostream>
#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);