Пример использования UBO
This commit is contained in:
		
							parent
							
								
									54d4301e5f
								
							
						
					
					
						commit
						48d8b47a88
					
				| @ -31,7 +31,7 @@ class Model : public Movable | ||||
|         Model(); // Конструктор без параметров
 | ||||
|         Model(const Model& copy); // Конструктор копирования
 | ||||
|         ~Model(); | ||||
|         void render(const GLuint &mvp_uniform); // Вызов отрисовки
 | ||||
|         void render(const GLuint &model_uniform); // Вызов отрисовки
 | ||||
|         void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер
 | ||||
|         void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер
 | ||||
|         void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер
 | ||||
| @ -52,7 +52,7 @@ class Model : public Movable | ||||
| class GrouptedModel: public Movable | ||||
| { | ||||
|     public: | ||||
|         void render(const GLuint &mvp_uniform); // Вызов отрисовки
 | ||||
|         void render(const GLuint &model_uniform); // Вызов отрисовки
 | ||||
|         std::vector<Model> parts; | ||||
| 
 | ||||
|         Model& operator[](int index); // Оператор[i] для простого доступа к частям модели
 | ||||
|  | ||||
| @ -1,15 +1,21 @@ | ||||
| #version 330 core  | ||||
| #version 420 core  | ||||
| 
 | ||||
| layout(location = 0) in vec3 pos;  | ||||
| layout(location = 1) in vec2 inTexCoord; | ||||
| layout(location = 2) in vec3 normals;  | ||||
| 
 | ||||
| uniform mat4 mvp; | ||||
| layout(std140, binding = 0) uniform Camera | ||||
| { | ||||
|     mat4 projection; | ||||
|     mat4 view; | ||||
| }; | ||||
| 
 | ||||
| uniform mat4 model; | ||||
| 
 | ||||
| out vec2 texCoord; | ||||
| 
 | ||||
| void main()  | ||||
| {  | ||||
|     gl_Position = mvp * vec4(pos, 1.0); | ||||
|     gl_Position = projection * view * model * vec4(pos, 1.0); | ||||
|     texCoord = inTexCoord; | ||||
| }  | ||||
|  | ||||
| @ -29,11 +29,11 @@ Model::~Model() | ||||
| } | ||||
| 
 | ||||
| // Вызов отрисовки
 | ||||
| void Model::render(const GLuint &mvp_uniform)  | ||||
| void Model::render(const GLuint &model_uniform)  | ||||
| { | ||||
|     // Расчитаем матрицу трансформации
 | ||||
|     glm::mat4 mvp = camera.getVP() * this->getTransformMatrix(); | ||||
|     glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &mvp[0][0]); | ||||
|     glm::mat4 model = this->getTransformMatrix(); | ||||
|     glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &model[0][0]); | ||||
| 
 | ||||
|     // Подключаем текстуры
 | ||||
|     texture_diffuse.use(); | ||||
| @ -309,14 +309,14 @@ GrouptedModel loadOBJtoGroupted(const char* filename, const char* mtl_directory, | ||||
| } | ||||
| 
 | ||||
| // Вызов отрисовки групповой модели
 | ||||
| void GrouptedModel::render(const GLuint &mvp_uniform) | ||||
| void GrouptedModel::render(const GLuint &model_uniform) | ||||
| { | ||||
|     for (auto& model : parts) | ||||
|     { | ||||
|         model.position += position; | ||||
|         model.rotation += rotation; | ||||
|         model.scale *= scale; | ||||
|         model.render(mvp_uniform); | ||||
|         model.render(model_uniform); | ||||
|         model.position -= position; | ||||
|         model.rotation -= rotation; | ||||
|         model.scale /= scale; | ||||
|  | ||||
							
								
								
									
										11
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/main.cpp
									
									
									
									
									
								
							| @ -196,7 +196,10 @@ int main(void) | ||||
|     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | ||||
| 
 | ||||
|     // Расположение Uniform-переменной
 | ||||
|     GLuint mvp_uniform = glGetUniformLocation(shaderProgram, "mvp"); | ||||
|     GLuint model_uniform = glGetUniformLocation(shaderProgram, "model"); | ||||
| 
 | ||||
|     // Uniform-буферы
 | ||||
|     UBO cameraUB(sizeof(glm::mat4)*2, 0); | ||||
| 
 | ||||
|     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Использование уменьшенных версий mipmap  
 | ||||
| 
 | ||||
| @ -206,8 +209,12 @@ int main(void) | ||||
|         // Очистка буфера цвета
 | ||||
|         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||||
| 
 | ||||
|         // Загрузка данных о камере
 | ||||
|         cameraUB.loadSub(&camera.getProjection(), sizeof(glm::mat4), 0); | ||||
|         cameraUB.loadSub(&camera.getView(), sizeof(glm::mat4), sizeof(glm::mat4)); | ||||
| 
 | ||||
|         // Тут производится рендер
 | ||||
|         scene.render(mvp_uniform); | ||||
|         scene.render(model_uniform); | ||||
| 
 | ||||
|         // Представление содержимого буфера цепочки показа на окно
 | ||||
|         glfwSwapBuffers(window); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user