Add translation, rotation and scale matrices
This commit is contained in:
		
							
								
								
									
										54
									
								
								src/main.cc
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								src/main.cc
									
									
									
									
									
								
							| @@ -1,4 +1,5 @@ | ||||
| #include "glad/glad.h" | ||||
| #include "glm/ext/matrix_transform.hpp" | ||||
| #include "glm/gtc/type_ptr.hpp" | ||||
| #include "glm/trigonometric.hpp" | ||||
| #include <SDL2/SDL.h> | ||||
| @@ -49,9 +50,14 @@ struct App { | ||||
|   GLuint        shader_program; | ||||
|  | ||||
|   float         speed; | ||||
|   glm::mat4     translation; | ||||
|   glm::mat4     rotation; | ||||
|   glm::mat4     scale; | ||||
|  | ||||
|   // Model matrix to be sent as uniform to the vertex shader | ||||
|   glm::mat4     model_mat; | ||||
|   GLint         u_model_idx; | ||||
|   bool          recalc_model_mat; | ||||
|  | ||||
|   // Projection matrix to be sent as uniform to the vertex shader | ||||
|   glm::mat4     projection_mat; | ||||
| @@ -129,7 +135,10 @@ int init(App &app) { | ||||
|   ); | ||||
|  | ||||
|   app.speed          = 0.02f; | ||||
|   app.model_mat      = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f)); | ||||
|   app.translation    = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.5f)); | ||||
|   app.rotation       = glm::mat4(1.0f); | ||||
|   app.scale          = glm::mat4(1.0f); | ||||
|   app.model_mat      = app.translation * app.rotation * app.scale; | ||||
|   app.projection_mat = glm::perspective(glm::radians(60.0f), | ||||
|                                         (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, | ||||
|                                         0.1f, | ||||
| @@ -142,16 +151,16 @@ int init(App &app) { | ||||
| void create_vertex_spec(App &app) { | ||||
|   const std::vector<GLfloat> vertices = { | ||||
|     // vert0 | ||||
|     -0.5f, -0.5f, -1.5f, // position | ||||
|     -0.5f, -0.5f, 0.0f, // position | ||||
|      1.0f,  0.0f, 0.0f, // colour | ||||
|     // vert1 | ||||
|      0.5f, -0.5f, -1.5f, // position | ||||
|      0.5f, -0.5f, 0.0f, // position | ||||
|      0.0f,  1.0f, 0.0f, // colour | ||||
|     // vert2 | ||||
|     -0.5f,  0.5f, -1.5f, // position | ||||
|     -0.5f,  0.5f, 0.0f, // position | ||||
|      0.0f,  0.0f, 1.0f, // colour | ||||
|     // vert3 | ||||
|      0.5f,  0.5f, -1.5f, // position | ||||
|      0.5f,  0.5f, 0.0f, // position | ||||
|      1.0f,  1.0f, 0.0f, // colour | ||||
|   }; | ||||
|  | ||||
| @@ -238,11 +247,38 @@ void run_main_loop(App &app) { | ||||
|         app.running = false; | ||||
|         break; | ||||
|       case SDL_KEYDOWN: | ||||
|         if (app.event.key.keysym.sym == SDLK_UP) { | ||||
|           app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, app.speed, 0.0f)); | ||||
|         } else if (app.event.key.keysym.sym == SDLK_DOWN) { | ||||
|           app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, -app.speed, 0.0f)); | ||||
|         if (app.event.key.keysym.sym == SDLK_w) { | ||||
|           app.translation = glm::translate(app.translation, glm::vec3(0.0f, 0.0f, app.speed)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_s) { | ||||
|           app.translation = glm::translate(app.translation, glm::vec3(0.0f, 0.0f, -app.speed)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_d) { | ||||
|           app.translation = glm::translate(app.translation, glm::vec3(app.speed, 0.0f, 0.0f)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_a) { | ||||
|           app.translation = glm::translate(app.translation, glm::vec3(-app.speed, 0.0f, 0.0f)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_RIGHT) { | ||||
|           app.rotation = glm::rotate(app.rotation, glm::radians(10.0f), glm::vec3(0.0f, 1.0f, 0.0f)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_LEFT) { | ||||
|           app.rotation = glm::rotate(app.rotation, glm::radians(-10.0f), glm::vec3(0.0f, 1.0f, 0.0f)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_r) { | ||||
|           app.scale = glm::scale(app.scale, glm::vec3(1.2f)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } else if (app.event.key.keysym.sym == SDLK_e) { | ||||
|           app.scale = glm::scale(app.scale, glm::vec3(0.8f)); | ||||
|           app.recalc_model_mat = true; | ||||
|         } | ||||
|  | ||||
|         if (app.recalc_model_mat) { | ||||
|           app.recalc_model_mat = false; | ||||
|           app.model_mat = app.translation * app.rotation * app.scale; | ||||
|         } | ||||
|  | ||||
|         break; | ||||
|       } | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user