Привязка текстуры к модели

This commit is contained in:
Ковалев Роман Евгеньевич 2022-11-14 19:47:54 +03:00 committed by R.E. Kovalev
parent da03a0744b
commit cc92dc8147
4 changed files with 22 additions and 4 deletions

View File

@ -3,6 +3,7 @@
"fstream": "cpp", "fstream": "cpp",
"iosfwd": "cpp", "iosfwd": "cpp",
"map": "cpp", "map": "cpp",
"atomic": "cpp" "atomic": "cpp",
"new": "cpp"
} }
} }

View File

@ -2,6 +2,7 @@
#define MODEL_H #define MODEL_H
#include "Buffers.h" #include "Buffers.h"
#include "Texture.h"
#include <GLM/glm.hpp> #include <GLM/glm.hpp>
@ -15,6 +16,7 @@ class Model
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); // Загрузка индексов в буфер
void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер
void set_texture(Texture& texture); // Привязка текстуры к модели
glm::vec3 position; // позиция модели glm::vec3 position; // позиция модели
glm::vec3 rotation; // поворот модели glm::vec3 rotation; // поворот модели
@ -27,7 +29,7 @@ class Model
BO texCoords_vbo; // буфер с текстурными координатами BO texCoords_vbo; // буфер с текстурными координатами
GLuint verteces_count; // Количество вершин GLuint verteces_count; // Количество вершин
GLuint indices_count; // Количество индексов GLuint indices_count; // Количество индексов
Texture texture_diffuse; // Диффузная текстура
}; };
#endif // MODEL_H #endif // MODEL_H

View File

@ -9,7 +9,7 @@ Model::Model() : verteces_count(0), indices_count(0), vertex_vbo(VERTEX), index_
} }
// Конструктор копирования // Конструктор копирования
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), texCoords_vbo(copy.texCoords_vbo), position(copy.position), rotation(copy.rotation), scale(copy.scale) 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), texCoords_vbo(copy.texCoords_vbo), texture_diffuse(copy.texture_diffuse), position(copy.position), rotation(copy.rotation), scale(copy.scale)
{ {
} }
@ -26,6 +26,9 @@ void Model::render(const GLuint &mvp_uniform)
glm::mat4 mvp = camera.getVP() * this->getTransformMatrix(); glm::mat4 mvp = camera.getVP() * this->getTransformMatrix();
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &mvp[0][0]); glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &mvp[0][0]);
// Подключаем текстуры
texture_diffuse.use();
// Подключаем VAO // Подключаем VAO
vao.use(); vao.use();
// Если есть индексы - рисуем с их использованием // Если есть индексы - рисуем с их использованием
@ -123,3 +126,15 @@ glm::mat4 Model::getTransformMatrix()
return transformMatrix; return transformMatrix;
} }
// Привязка текстуры к модели
void Model::set_texture(Texture& texture)
{
GLuint type = texture.getType();
switch(type)
{
case TEX_DIFFUSE:
texture_diffuse = texture;
break;
};
}

View File

@ -221,6 +221,7 @@ int main(void)
// Текстура травы // Текстура травы
Texture grass(TEX_DIFFUSE, "../resources/textures/grass.png"); Texture grass(TEX_DIFFUSE, "../resources/textures/grass.png");
rectangle.set_texture(grass);
// Установка цвета очистки буфера цвета // Установка цвета очистки буфера цвета
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@ -237,7 +238,6 @@ int main(void)
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// Тут производится рендер // Тут производится рендер
grass.use();
rectangle.render(mvp_uniform); rectangle.render(mvp_uniform);
// Представление содержимого буфера цепочки показа на окно // Представление содержимого буфера цепочки показа на окно