Rotate camera around scene
This commit is contained in:
		
							
								
								
									
										42
									
								
								src/main.cc
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								src/main.cc
									
									
									
									
									
								
							| @@ -16,6 +16,7 @@ | |||||||
|  |  | ||||||
| // SDL | // SDL | ||||||
| #include <SDL2/SDL.h> | #include <SDL2/SDL.h> | ||||||
|  | #include <SDL2/SDL_timer.h> | ||||||
| #include <SDL2/SDL_stdinc.h> | #include <SDL2/SDL_stdinc.h> | ||||||
| #include <SDL2/SDL_error.h> | #include <SDL2/SDL_error.h> | ||||||
| #include <SDL2/SDL_video.h> | #include <SDL2/SDL_video.h> | ||||||
| @@ -193,29 +194,31 @@ int main() { | |||||||
|  |  | ||||||
|   glm::vec3 cube_positions[] = { |   glm::vec3 cube_positions[] = { | ||||||
|       glm::vec3( 0.0f,  0.0f,  0.0f),  |       glm::vec3( 0.0f,  0.0f,  0.0f),  | ||||||
|       glm::vec3( 5.0f,  8.0f, -25.0f),  |       glm::vec3( 4.0f,  6.0f, -15.0f),  | ||||||
|       glm::vec3(-4.5f, -5.2f, -12.5f),   |       glm::vec3(-3.5f, -3.2f, -2.5f),   | ||||||
|       glm::vec3(-6.8f, -5.0f, -22.3f),   |       glm::vec3(-5.8f, -3.0f, -12.3f),   | ||||||
|       glm::vec3( 5.4f, -3.4f, -13.5f),   |       glm::vec3( 4.4f, -1.4f, -3.5f),   | ||||||
|       glm::vec3(-4.7f,  6.0f, -17.5f),   |       glm::vec3(-3.7f,  4.0f, -7.5f),   | ||||||
|       glm::vec3( 4.3f, -5.0f, -12.5f),   |       glm::vec3( 3.3f, -3.0f, -2.5f),   | ||||||
|       glm::vec3( 4.5f,  5.0f, -12.5f),  |       glm::vec3( 3.5f,  3.0f, -2.5f),  | ||||||
|       glm::vec3( 4.5f,  3.2f, -11.5f),  |       glm::vec3( 3.5f,  1.2f, -1.5f),  | ||||||
|       glm::vec3(-4.3f,  4.0f, -11.5f)   |       glm::vec3(-3.3f,  2.0f, -1.5f)   | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   glm::mat4 model = glm::mat4(1.0f); |   glm::vec3 camera_target    = glm::vec3(0.0f, 0.0f, 0.0f); | ||||||
|  |   glm::vec3 world_up         = glm::vec3(0.0f, 1.0f, 0.0f); | ||||||
|   glm::mat4 view = glm::mat4(1.0f); |  | ||||||
|   view = glm::translate(view, glm::vec3(0.0f, 0.0f, -4.0f)); |  | ||||||
|  |  | ||||||
|  |   glm::mat4 model      = glm::mat4(1.0f); | ||||||
|  |   glm::mat4 view       = glm::mat4(1.0f); | ||||||
|   glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f); |   glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f); | ||||||
|  |  | ||||||
|   glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "view"), 1, GL_FALSE, glm::value_ptr(view)); |  | ||||||
|   glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); |   glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); | ||||||
|  |  | ||||||
|   bool running = true; |   float ticks        = 0; | ||||||
|   SDL_Event event = {}; |   float radius       = 10.0f; | ||||||
|  |   bool running       = true; | ||||||
|  |   SDL_Event event    = {}; | ||||||
|  |  | ||||||
|   while (running) { |   while (running) { | ||||||
|     while (SDL_PollEvent(&event)) { |     while (SDL_PollEvent(&event)) { | ||||||
|       switch (event.type) { |       switch (event.type) { | ||||||
| @@ -250,6 +253,13 @@ int main() { | |||||||
|                          0.0f : texture_mix_factor > 1.0f ? |                          0.0f : texture_mix_factor > 1.0f ? | ||||||
|                          1.0f : texture_mix_factor; |                          1.0f : texture_mix_factor; | ||||||
|  |  | ||||||
|  |     ticks                     = (float)SDL_GetTicks() / 1000.0f; | ||||||
|  |     float camera_x            = sin(ticks) * radius; | ||||||
|  |     float camera_z            = cos(ticks) * radius; | ||||||
|  |     glm::vec3 camera_position = glm::vec3(camera_x, 0.0f, camera_z); | ||||||
|  |     view                      = glm::lookAt(camera_position, camera_target, world_up); | ||||||
|  |     glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "view"), 1, GL_FALSE, glm::value_ptr(view)); | ||||||
|  |  | ||||||
|     glClearColor(0.2f, 0.3f, 0.3f, 1.0f); |     glClearColor(0.2f, 0.3f, 0.3f, 1.0f); | ||||||
|     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user