From babe58f0a5a1fae4c0de79c40da01b5d5fb97722 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 1 Dec 2024 22:30:42 +0000 Subject: [PATCH] Start the lighting chapters --- shaders/frag.glsl | 9 +-- shaders/light_frag.glsl | 7 ++ shaders/vert.glsl | 4 - src/main.cc | 165 +++++++++++++++------------------------- 4 files changed, 72 insertions(+), 113 deletions(-) create mode 100644 shaders/light_frag.glsl diff --git a/shaders/frag.glsl b/shaders/frag.glsl index a8b171d..cc48556 100644 --- a/shaders/frag.glsl +++ b/shaders/frag.glsl @@ -1,13 +1,10 @@ #version 330 core -in vec2 tex_coords; - out vec4 color; -uniform float mix_factor; -uniform sampler2D texture0; -uniform sampler2D texture1; +uniform vec3 object_color; +uniform vec3 light_color; void main() { - color = mix(texture(texture0, tex_coords), texture(texture1, tex_coords), mix_factor); + color = vec4(object_color * light_color, 1.0); }; diff --git a/shaders/light_frag.glsl b/shaders/light_frag.glsl new file mode 100644 index 0000000..b460b8c --- /dev/null +++ b/shaders/light_frag.glsl @@ -0,0 +1,7 @@ +#version 330 core + +out vec4 color; + +void main() { + color = vec4(1.0); +} diff --git a/shaders/vert.glsl b/shaders/vert.glsl index 687ffbd..4560048 100644 --- a/shaders/vert.glsl +++ b/shaders/vert.glsl @@ -1,15 +1,11 @@ #version 330 core layout(location=0) in vec3 position; -layout(location=1) in vec2 uv; uniform mat4 model; uniform mat4 view; uniform mat4 projection; -out vec2 tex_coords; - void main() { - tex_coords = uv; gl_Position = projection * view * model * vec4(position, 1.0); }; diff --git a/src/main.cc b/src/main.cc index 7e4ab90..5926327 100644 --- a/src/main.cc +++ b/src/main.cc @@ -10,10 +10,6 @@ #include "glm/gtx/rotate_vector.hpp" #include "glm/gtx/string_cast.hpp" -// STB Image -#define STB_IMAGE_IMPLEMENTATION -#include "stb_image.h" - // SDL #include #include @@ -49,6 +45,9 @@ class Shader { Shader(const std::string &vert_file, const std::string &frag_file); ~Shader(); void activate(); + void set_float(const char *name, float value); + void set_vec3(const char *name, glm::vec3 vector); + void set_mat4(const char *name, glm::mat4 matrix); GLuint program; private: void link_program(GLuint vert, GLuint frag); @@ -86,34 +85,28 @@ int main() { SDL_SetRelativeMouseMode(SDL_TRUE); SDL_WarpMouseInWindow(window, WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT); - // Set texture options - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glEnable(GL_DEPTH_TEST); std::vector vertices = { - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // position, uv - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // position, uv - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // position, uv - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // position, uv - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // position, uv - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // position, uv - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // position, uv - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // position, uv - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // position, uv - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // position, uv - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // position, uv - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, // position, uv - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // position, uv - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // position, uv - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // position, uv - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // position, uv - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f // position, uv + 0.5f, -0.5f, 0.5f, // position + -0.5f, 0.5f, 0.5f, // position + 0.5f, 0.5f, 0.5f, // position + 0.5f, -0.5f, 0.5f, // position + -0.5f, -0.5f, 0.5f, // position + 0.5f, -0.5f, -0.5f, // position + -0.5f, 0.5f, -0.5f, // position + -0.5f, 0.5f, -0.5f, // position + -0.5f, 0.5f, 0.5f, // position + 0.5f, 0.5f, -0.5f, // position + 0.5f, -0.5f, -0.5f, // position + -0.5f, 0.5f, 0.5f, // position + 0.5f, 0.5f, 0.5f, // position + -0.5f, -0.5f, -0.5f, // position + -0.5f, -0.5f, -0.5f, // position + 0.5f, -0.5f, -0.5f, // position + -0.5f, 0.5f, -0.5f, // position }; std::vector indices = { @@ -145,68 +138,27 @@ int main() { glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void *)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *)0); + glEnableVertexAttribArray(0); + + GLuint light_vao = 0; + glGenVertexArrays(1, &light_vao); + glBindVertexArray(light_vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat))); - glEnableVertexAttribArray(1); glBindVertexArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - stbi_set_flip_vertically_on_load(true); - - // Load texture - int32_t width[2], height[2], channels[2]; - uint8_t *container = stbi_load("images/container.jpg", &width[0], &height[0], &channels[0], 0); - uint8_t *awesomeface = stbi_load("images/awesomeface.png", &width[1], &height[1], &channels[1], 0); - - GLuint textures[2]; - glGenTextures(2, textures); - - glActiveTexture(GL_TEXTURE0); // activate the texture unit first before binding texture - glBindTexture(GL_TEXTURE_2D, textures[0]); - if (container) { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width[0], height[0], 0, GL_RGB, GL_UNSIGNED_BYTE, container); - glGenerateMipmap(GL_TEXTURE_2D); - - stbi_image_free(container); - } else { - printf("Failed to load container texture \n"); - } - - glActiveTexture(GL_TEXTURE1); // activate the texture unit first before binding texture - glBindTexture(GL_TEXTURE_2D, textures[1]); - if (container) { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width[1], height[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, awesomeface); - glGenerateMipmap(GL_TEXTURE_2D); - - stbi_image_free(awesomeface); - } else { - printf("Failed to load container texture \n"); - } - - glBindTexture(GL_TEXTURE_2D, 0); - Shader main_shader {"shaders/vert.glsl", "shaders/frag.glsl"}; - main_shader.activate(); - glUniform1i(glGetUniformLocation(main_shader.program, "texture0"), 0); - glUniform1i(glGetUniformLocation(main_shader.program, "texture1"), 1); + Shader light_shader {"shaders/vert.glsl", "shaders/light_frag.glsl"}; - float texture_mix_factor = 0.2f; - - glm::vec3 cube_positions[] = { - glm::vec3( 0.0f, 0.0f, 0.0f), - glm::vec3( 4.0f, 6.0f, -15.0f), - glm::vec3(-3.5f, -3.2f, -2.5f), - glm::vec3(-5.8f, -3.0f, -12.3f), - glm::vec3( 4.4f, -1.4f, -3.5f), - glm::vec3(-3.7f, 4.0f, -7.5f), - glm::vec3( 3.3f, -3.0f, -2.5f), - glm::vec3( 3.5f, 3.0f, -2.5f), - glm::vec3( 3.5f, 1.2f, -1.5f), - glm::vec3(-3.3f, 2.0f, -1.5f) - }; + main_shader.set_vec3("object_color", glm::vec3(1.0f, 0.5f, 0.31f)); + main_shader.set_vec3("light_color", glm::vec3(1.0f, 1.0f, 1.0f)); const float camera_speed = 25.0f; glm::vec3 camera_position = glm::vec3(0.0f, 0.0f, 4.0f); @@ -220,7 +172,8 @@ int main() { 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); - glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + main_shader.set_mat4("projection", projection); + light_shader.set_mat4("projection", projection); const float sensitivity = 0.1f; int last_mouse_x = WINDOW_HALF_WIDTH; @@ -243,10 +196,6 @@ int main() { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) { running = false; - } else if (event.key.keysym.sym == SDLK_UP) { - texture_mix_factor += 0.1f; - } else if (event.key.keysym.sym == SDLK_DOWN) { - texture_mix_factor -= 0.1f; } else if (event.key.keysym.sym == SDLK_w) { camera_position += camera_speed * delta * camera_forward; } else if (event.key.keysym.sym == SDLK_s) { @@ -294,36 +243,31 @@ int main() { } } - texture_mix_factor = texture_mix_factor < 0.0f ? - 0.0f : texture_mix_factor > 1.0f ? - 1.0f : texture_mix_factor; - camera_forward.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); camera_forward.y = sin(glm::radians(pitch)); camera_forward.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); camera_forward = glm::normalize(camera_forward); view = glm::lookAt(camera_position, camera_position + camera_forward, world_up); - glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "view"), 1, GL_FALSE, glm::value_ptr(view)); + main_shader.set_mat4("view", view); + light_shader.set_mat4("view", view); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); main_shader.activate(); - glUniform1f(glGetUniformLocation(main_shader.program, "mix_factor"), texture_mix_factor); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, textures[0]); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, textures[1]); + main_shader.set_mat4("model", model); glBindVertexArray(vao); + glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "model"), 1, GL_FALSE, glm::value_ptr(model)); + glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void *)0); - for (int i = 0; i < sizeof(cube_positions) / sizeof(cube_positions[0]); ++i) { - float angle = 20.0f * i; - model = glm::translate(glm::mat4(1.0f), cube_positions[i]); - model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); - glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "model"), 1, GL_FALSE, glm::value_ptr(model)); - glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void *)0); - } + model = glm::translate(glm::mat4(1.0f), glm::vec3(1.2f, 1.0f, 2.0f)); + model = glm::scale(model, glm::vec3(0.2f)); + light_shader.activate(); + light_shader.set_mat4("model", model); + glBindVertexArray(light_vao); + glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void *)0); SDL_GL_SwapWindow(window); } @@ -356,6 +300,21 @@ void Shader::activate() { } } +void Shader::set_float(const char *name, float value) { + activate(); + glUniform1f(glGetUniformLocation(program, name), value); +} + +void Shader::set_vec3(const char *name, glm::vec3 vector) { + activate(); + glUniform3f(glGetUniformLocation(program, name), vector.x, vector.y, vector.z); +} + +void Shader::set_mat4(const char *name, glm::mat4 matrix) { + activate(); + glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, glm::value_ptr(matrix)); +} + void Shader::link_program(GLuint vert, GLuint frag) { program = glCreateProgram(); glAttachShader(program, vert);