04/src/Model.cpp

98 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Model.h"
#include "Camera.h"
extern Camera camera;
// Конструктор без параметров
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), position(copy.position), rotation(copy.rotation), scale(copy.scale)
{
}
Model::~Model()
{
}
// Вызов отрисовки
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();
// Если есть индексы - рисуем с их использованием
if (indices_count)
glDrawElements(GL_TRIANGLES, indices_count, GL_UNSIGNED_INT, (void*)0);
// Если есть вершины - рисуем на основании массива вершин
else if (verteces_count)
glDrawArrays(GL_TRIANGLES, 0, verteces_count);
}
// Функция для конфигурации атрибута вершинного буфера
void vertex_attrib_config()
{
// Включаем необходимый атрибут у выбранного VAO
glEnableVertexAttribArray(0);
// Устанавливаем связь между VAO и привязанным VBO
glVertexAttribPointer( 0 // индекс атрибута, должен совпадать с Layout шейдера
, 3 // количество компонент одного элемента
, GL_FLOAT // тип
, GL_FALSE // нормализованность значений
, 0 // шаг
, (void *)0 // отступ с начала массива
);
}
// Загрузка вершин в буфер
void Model::load_verteces(glm::vec3* verteces, GLuint count)
{
// Подключаем VAO и вершинный буфер
vao.use();
vertex_vbo.use();
// Загрузка вершин в память буфера
vertex_vbo.load(verteces, sizeof(glm::vec3)*count);
vertex_attrib_config();
// Запоминаем количество вершин для отрисовки
verteces_count = count;
}
// Загрузка индексов в буфер
void Model::load_indices(GLuint* indices, GLuint count)
{
// Подключаем VAO и индексный буфер
vao.use();
index_vbo.use();
// Загрузка вершин в память буфера
index_vbo.load(indices, sizeof(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::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));
// Масштабирование
transformMatrix = glm::scale(transformMatrix, scale);
return transformMatrix;
}