трансформация модели с учетом камеры
This commit is contained in:
parent
93a3474872
commit
322cfb3020
@ -11,10 +11,15 @@ class Model
|
|||||||
Model(); // Конструктор без параметров
|
Model(); // Конструктор без параметров
|
||||||
Model(const Model& copy); // Конструктор копирования
|
Model(const Model& copy); // Конструктор копирования
|
||||||
~Model();
|
~Model();
|
||||||
void render(); // Вызов отрисовки
|
void render(const GLuint &mvp_uniform); // Вызов отрисовки
|
||||||
void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер
|
void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер
|
||||||
void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер
|
void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер
|
||||||
|
|
||||||
|
glm::vec3 position; // позиция модели
|
||||||
|
glm::vec3 rotation; // поворот модели
|
||||||
|
glm::vec3 scale; // мастабирование модели
|
||||||
|
glm::mat4 getTransformMatrix(); // Матрица трансформации модели
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VAO vao;
|
VAO vao;
|
||||||
BO vertex_vbo, index_vbo; // вершинный и индексный буферы
|
BO vertex_vbo, index_vbo; // вершинный и индексный буферы
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
#include "Model.h"
|
#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
|
||||||
vao.use();
|
vao.use();
|
||||||
// Если есть индексы - рисуем с их использованием
|
// Если есть индексы - рисуем с их использованием
|
||||||
@ -74,3 +80,21 @@ void Model::load_indices(GLuint* indices, GLuint count)
|
|||||||
// Запоминаем количество вершин для отрисовки
|
// Запоминаем количество вершин для отрисовки
|
||||||
indices_count = 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;
|
||||||
|
}
|
||||||
|
11
src/main.cpp
11
src/main.cpp
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Camera.h"
|
||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
|
|
||||||
#define WINDOW_WIDTH 800
|
#define WINDOW_WIDTH 800
|
||||||
@ -112,6 +113,9 @@ GLuint LoadShaders(const char *vertex_file, const char *fragment_file)
|
|||||||
return programID;
|
return programID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Camera camera(800.0f/600.0f);
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
GLFWwindow* window; // Указатель на окно GLFW3
|
GLFWwindow* window; // Указатель на окно GLFW3
|
||||||
@ -173,10 +177,15 @@ int main(void)
|
|||||||
|
|
||||||
// Загрузка индексов модели
|
// Загрузка индексов модели
|
||||||
rectangle.load_indices(indices, sizeof(indices));
|
rectangle.load_indices(indices, sizeof(indices));
|
||||||
|
|
||||||
|
rectangle.position.z = 2;
|
||||||
|
rectangle.rotation = glm::vec3(45);
|
||||||
|
|
||||||
// Установка цвета очистки буфера цвета
|
// Установка цвета очистки буфера цвета
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Расположение Uniform-переменной
|
||||||
|
GLuint mvp_uniform = glGetUniformLocation(shaderProgram, "mvp");
|
||||||
|
|
||||||
// Пока не произойдет событие запроса закрытия окна
|
// Пока не произойдет событие запроса закрытия окна
|
||||||
while(!glfwWindowShouldClose(window))
|
while(!glfwWindowShouldClose(window))
|
||||||
@ -185,7 +194,7 @@ int main(void)
|
|||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
// Тут производится рендер
|
// Тут производится рендер
|
||||||
rectangle.render();
|
rectangle.render(mvp_uniform);
|
||||||
|
|
||||||
// Представление содержимого буфера цепочки показа на окно
|
// Представление содержимого буфера цепочки показа на окно
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user