Switch to translating the quad using a translation matrix
This commit is contained in:
		| @@ -3,8 +3,6 @@ | ||||
| in vec3 vert_colours; | ||||
| out vec4 color; | ||||
|  | ||||
| uniform float u_offset; | ||||
|  | ||||
| void main() { | ||||
|   color = vec4(vert_colours.r, vert_colours.g + u_offset, vert_colours.b, 1.0f); | ||||
|   color = vec4(vert_colours.r, vert_colours.g, vert_colours.b, 1.0f); | ||||
| }; | ||||
|   | ||||
| @@ -3,11 +3,11 @@ | ||||
| layout(location=0) in vec3 position; | ||||
| layout(location=1) in vec3 vert_colour; | ||||
|  | ||||
| uniform float u_offset; | ||||
| uniform mat4 u_model; | ||||
|  | ||||
| out vec3 vert_colours; | ||||
|  | ||||
| void main() { | ||||
|   vert_colours = vert_colour; | ||||
|   gl_Position = vec4(position.x, position.y + u_offset, position.z, 1.0f); | ||||
|   gl_Position = u_model * vec4(position, 1.0f); | ||||
| }; | ||||
|   | ||||
							
								
								
									
										35
									
								
								src/main.cc
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								src/main.cc
									
									
									
									
									
								
							| @@ -1,9 +1,13 @@ | ||||
| #include "glad/glad.h" | ||||
| #include "glm/gtc/type_ptr.hpp" | ||||
| #include <SDL2/SDL.h> | ||||
| #include <SDL2/SDL_error.h> | ||||
| #include <SDL2/SDL_video.h> | ||||
| #include <SDL2/SDL_events.h> | ||||
| #include <SDL2/SDL_keycode.h> | ||||
| #include <glm/vec3.hpp> | ||||
| #include <glm/mat4x4.hpp> | ||||
| #include <glm/ext.hpp> | ||||
| #include <cstdint> | ||||
| #include <cstdio> | ||||
| #include <string> | ||||
| @@ -41,13 +45,13 @@ struct App { | ||||
|   // OpenGL refers to as element, hence the name Element Buffer Object | ||||
|   GLuint        ebo; | ||||
|  | ||||
|   float         offset_modifier; | ||||
|   // offset value passed to shaders as uniform | ||||
|   float         offset; | ||||
|   // offset uniform index | ||||
|   GLint         u_offset_idx; | ||||
|  | ||||
|   GLuint        shader_program; | ||||
|  | ||||
|   float         speed; | ||||
|   // Model matrix to be sent as uniform to the vertex shader | ||||
|   glm::mat4     model_mat; | ||||
|   GLint         u_model_idx; | ||||
|  | ||||
|   bool          running; | ||||
| }; | ||||
|  | ||||
| @@ -119,7 +123,8 @@ int init(App &app) { | ||||
|          glGetString(GL_SHADING_LANGUAGE_VERSION) | ||||
|   ); | ||||
|  | ||||
|   app.offset_modifier = 0.02f; | ||||
|   app.speed = 0.02f; | ||||
|   app.model_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f)); | ||||
|  | ||||
|   return EXIT_CODE_SUCCESS; | ||||
| } | ||||
| @@ -200,10 +205,10 @@ void create_graphics_pipeline(App &app) { | ||||
|  | ||||
|   app.shader_program = create_shader_program(vs_source, fs_source); | ||||
|  | ||||
|   const char *u_offset = "u_offset"; | ||||
|   app.u_offset_idx = glGetUniformLocation(app.shader_program, u_offset); | ||||
|   if (app.u_offset_idx < 0) { | ||||
|     printf("Can't find uniform %s", u_offset); | ||||
|   const char *u_model = "u_model"; | ||||
|   app.u_model_idx = glGetUniformLocation(app.shader_program, u_model); | ||||
|   if (app.u_model_idx < 0) { | ||||
|     printf("Failed to find uniform %s\n", u_model); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @@ -218,9 +223,9 @@ void run_main_loop(App &app) { | ||||
|         break; | ||||
|       case SDL_KEYDOWN: | ||||
|         if (app.event.key.keysym.sym == SDLK_UP) { | ||||
|           app.offset += app.offset_modifier; | ||||
|           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.offset -= app.offset_modifier; | ||||
|           app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, -app.speed, 0.0f)); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
| @@ -234,8 +239,8 @@ void run_main_loop(App &app) { | ||||
|     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); | ||||
|  | ||||
|     glUseProgram(app.shader_program); | ||||
|     if (app.u_offset_idx >= 0) { | ||||
|       glUniform1f(app.u_offset_idx, app.offset); | ||||
|     if (app.u_model_idx >= 0) { | ||||
|       glUniformMatrix4fv(app.u_model_idx, 1, GL_FALSE, glm::value_ptr(app.model_mat)); | ||||
|     } | ||||
|     // End pre draw setup | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user