diff --git a/include/Model.h b/include/Model.h index 3ba8af3..3e2fae3 100644 --- a/include/Model.h +++ b/include/Model.h @@ -59,12 +59,12 @@ class Model : public Node Model& operator=(const Model& other); // Оператор присваивания virtual ~Model(); - void render(); // Вызов отрисовки + void render(const GLuint &model_uniform); // Вызов отрисовки void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов - + private: VAO vao; BO vertex_vbo, index_vbo; // вершинный и индексный буферы diff --git a/include/Scene.h b/include/Scene.h index 6e6e92c..07638e9 100644 --- a/include/Scene.h +++ b/include/Scene.h @@ -14,7 +14,7 @@ class Scene Scene(const Scene ©); // Конструктор копирования Scene& operator=(const Scene& other); // Оператор присваивания - void render(); // Рендер сцены + void render(const GLuint &model_uniform); // Рендер сцены Node root; // Корневой узел diff --git a/shaders/shader.vert b/shaders/shader.vert index 16f3963..8a67d4d 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -2,8 +2,10 @@ layout(location = 0) in vec3 pos; +uniform mat4 vp; +uniform mat4 model; + void main() { - gl_Position.xyz = pos; - gl_Position.w = 1.0; + gl_Position = vp * model * vec4(pos, 1.0); } diff --git a/src/Model.cpp b/src/Model.cpp index 6f3094a..2daa600 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -238,8 +238,11 @@ Model::~Model() } // Вызов отрисовки -void Model::render() +void Model::render(const GLuint &model_uniform) { + // Загрузим матрицу трансформации + glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &getTransformMatrix()[0][0]); + // Подключаем VAO vao.use(); // Если есть индексы - рисуем с их использованием diff --git a/src/Scene.cpp b/src/Scene.cpp index 1a36fce..222efc8 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -27,10 +27,10 @@ Scene& Scene::operator=(const Scene& other) } // Рендер сцены -void Scene::render() +void Scene::render(const GLuint &model_uniform) { for (auto & model : models) - model.render(); + model.render(model_uniform); } // Перестройка узлов выбранного списка diff --git a/src/main.cpp b/src/main.cpp index 137bc83..24da569 100755 --- 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,7 @@ GLuint LoadShaders(const char *vertex_file, const char *fragment_file) return programID; } + int main(void) { GLFWwindow* window; // Указатель на окно GLFW3 @@ -174,19 +176,28 @@ int main(void) // Загрузка индексов модели rectangle.load_indices(indices, sizeof(indices)); + + rectangle.e_position().z = 2; + rectangle.e_rotation() = {0.733f, 0.462f, 0.191f, 0.462f}; // Установка цвета очистки буфера цвета glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + // Расположение Uniform-переменной + GLuint vp_uniform = glGetUniformLocation(shaderProgram, "vp"); + GLuint model_uniform = glGetUniformLocation(shaderProgram, "model"); // Пока не произойдет событие запроса закрытия окна while(!glfwWindowShouldClose(window)) { + // Загрузим матрицу проекции*вида + glUniformMatrix4fv(vp_uniform, 1, GL_FALSE, &Camera::current().getVP()[0][0]); + // Очистка буфера цвета glClear(GL_COLOR_BUFFER_BIT); // Тут производится рендер - rectangle.render(); + rectangle.render(model_uniform); // Представление содержимого буфера цепочки показа на окно glfwSwapBuffers(window);