diff --git a/11-lightmaps/resources/textures/container2.png b/11-lightmaps/resources/textures/container2.png new file mode 100644 index 0000000..596e8da Binary files /dev/null and b/11-lightmaps/resources/textures/container2.png differ diff --git a/11-lightmaps/resources/textures/container2_specular.png b/11-lightmaps/resources/textures/container2_specular.png new file mode 100644 index 0000000..681bf6e Binary files /dev/null and b/11-lightmaps/resources/textures/container2_specular.png differ diff --git a/11-lightmaps/shaders/11-frag.glsl b/11-lightmaps/shaders/11-frag.glsl new file mode 100644 index 0000000..767caab --- /dev/null +++ b/11-lightmaps/shaders/11-frag.glsl @@ -0,0 +1,46 @@ +#version 330 core +struct Material { + sampler2D diffuse; + sampler2D specular; + float shininess; +}; + +struct Light { + vec3 position; + vec3 diffuse; + vec3 specular; + vec3 ambient; +}; + +out vec4 FragColor; +in vec3 FragPos; +in vec3 Normal; +in vec2 TexCoords; + +uniform vec3 viewPos; +uniform Light light; +uniform Material material; + +void main() +{ + vec3 mat_tex = vec3(texture(material.diffuse, TexCoords)); + vec3 spec_tex = vec3(texture(material.specular, TexCoords)); + + vec3 ambient = light.ambient * mat_tex; + + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(light.position - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = light.diffuse * diff * mat_tex; + + vec3 viewDir = normalize(viewPos - FragPos); + vec3 reflectDir = reflect(-lightDir, norm); + + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + + vec3 specular = light.specular * spec * spec_tex; + + vec3 result = ambient + diffuse + specular; + + FragColor = vec4(result, 1.0); +} diff --git a/11-lightmaps/shaders/11-lightsource.frag.glsl b/11-lightmaps/shaders/11-lightsource.frag.glsl new file mode 100644 index 0000000..c208aa1 --- /dev/null +++ b/11-lightmaps/shaders/11-lightsource.frag.glsl @@ -0,0 +1,16 @@ +#version 330 core +out vec4 FragColor; + +struct Light { + vec3 position; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +uniform Light light; + +void main() +{ + FragColor = vec4(light.diffuse, 1.0); // set all 4 vector values to 1.0 +} diff --git a/11-lightmaps/shaders/11-vert.glsl b/11-lightmaps/shaders/11-vert.glsl new file mode 100644 index 0000000..276f393 --- /dev/null +++ b/11-lightmaps/shaders/11-vert.glsl @@ -0,0 +1,20 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec2 aTexCoords; + +out vec3 FragPos; +out vec3 Normal; +out vec2 TexCoords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0f); + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = mat3(transpose(inverse(model))) * aNormal; + TexCoords = aTexCoords; +} diff --git a/11-lightmaps/src/data.hpp b/11-lightmaps/src/data.hpp index 83245f2..a91f6ca 100644 --- a/11-lightmaps/src/data.hpp +++ b/11-lightmaps/src/data.hpp @@ -2,45 +2,46 @@ #include float vertices[] = { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + // positions // normals // texture coords + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -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0f, 0.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; diff --git a/11-lightmaps/src/main.cpp b/11-lightmaps/src/main.cpp index 42e8237..466e3f9 100644 --- a/11-lightmaps/src/main.cpp +++ b/11-lightmaps/src/main.cpp @@ -12,12 +12,10 @@ #include #include #include "data.hpp" -#include "physics.hpp" #include "model.hpp" #include "camera.hpp" void framebuffer_size_callback(GLFWwindow *window, int width, int height); -void processInput(GLFWwindow *window); const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; @@ -25,7 +23,6 @@ const unsigned int SCR_HEIGHT = 600; struct Config { Shader shader; Shader lightShader; - Model model; Light light{ .position={1.2f, 1.0f, 2.0f}, @@ -34,11 +31,19 @@ struct Config { .specular={1.0f, 1.0f, 1.0f}, }; - glm::vec3 boxPos{2.0f, 0.0f, 0.0f}; + glm::vec3 cubePos{2.0f, 0.0f, 0.0f}; + Camera camera{.movementSpeed=2.0f}; + + Material cubeMaterial{ + .ambient = {1.0f, 0.5f, 0.31f}, + .shininess = 32.0f, + .diffuseMap = 0, + .specularMap = 0, + }; + unsigned int cubeVAO = 0; unsigned int lightCubeVAO = 0; unsigned int VBO = 0; - Camera camera{.movementSpeed=2.0f}; float deltaTime = 0.0f; float lastFrame = 0.0f; @@ -89,20 +94,12 @@ GLFWwindow* create_window() { } -void processInput(GLFWwindow *window, BoxTest &box, Camera& camera) { +void processInput(GLFWwindow *window, Camera& camera) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } - if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { - b2Vec2 force(0.0f, 0.5f); - for(size_t i = 0; i < BODY_COUNT; i++) { - box.bodies[i]->ApplyForceToCenter(force, true); - box.bodies[i]->ApplyTorque(0.1f, true); - } - } - if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { camera.forward(); } @@ -159,9 +156,8 @@ Config setup() { glEnable(GL_DEPTH_TEST); Config config{ - .shader={"shaders/08-vert.glsl", "shaders/08-frag.glsl"}, - .lightShader={"shaders/08-vert.glsl", "shaders/08-lightsource.frag.glsl"}, - .model={"assets/popcorn_model_a.glb"} + .shader={"shaders/11-vert.glsl", "shaders/11-frag.glsl"}, + .lightShader={"shaders/11-vert.glsl", "shaders/11-lightsource.frag.glsl"}, }; unsigned int VBO = 0; @@ -178,59 +174,36 @@ Config setup() { glBindVertexArray(cubeVAO); // position attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float))); glEnableVertexAttribArray(1); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float))); + glEnableVertexAttribArray(2); + // light vao configure, this is the light source glGenVertexArrays(1, &lightCubeVAO); glBindVertexArray(lightCubeVAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); config.lightCubeVAO = lightCubeVAO; config.cubeVAO = cubeVAO; config.VBO = VBO; - return config; -} + config.cubeMaterial.diffuseMap = load_texture("resources/textures/container2.png"); + config.cubeMaterial.specularMap = load_texture("resources/textures/container2_specular.png"); -void draw_popcorn(Config& config, BoxTest& box, size_t box_i, glm::mat4& projection, glm::mat4& view) -{ config.shader.use(); + config.shader.setInt("material.diffuse", 0); + config.shader.setInt("material.specular", 1); - config.shader.setMat4("view", view); - config.shader.setMat4("projection", projection); - config.shader.setVec3("viewPos", config.camera.pos); - - config.shader.applyLight(config.light); - - glm::vec3 objectColor = {1.0, 0.94, 0.78}; - config.shader.applyMaterial({ - .ambient = objectColor, - .diffuse = objectColor, - .specular = objectColor, - .shininess = 100000.0f}); - - float time = glfwGetTime(); - // get position from box - b2Vec2 position = box.bodies[box_i]->GetPosition(); - float angle = box.bodies[box_i]->GetAngle(); - - glm::mat4 model = glm::mat4(1.0f); - model = glm::translate(model, glm::vec3(position.x, position.y, 0.0f)); - - model = glm::rotate(model, angle / 5.0f, glm::vec3(1.0f, 0.0f, 0.0f)); - model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(0.0f, 1.0f, 1.0f)); - - config.shader.setMat4("model", model); - - config.model.Draw(config.shader); + return config; } void draw_cube(Config& config, glm::mat4& projection, glm::mat4& view) { @@ -241,15 +214,19 @@ void draw_cube(Config& config, glm::mat4& projection, glm::mat4& view) { config.shader.setMat4("view", view); config.shader.setMat4("projection", projection); config.shader.setVec3("viewPos", config.camera.pos); - config.shader.applyMaterial({ - .ambient = {1.0f, 0.5f, 0.31f}, - .diffuse = {1.0f, 0.5f, 0.31f}, - .specular = config.light.specular, - .shininess = 32.0f}); + + config.shader.applyMaterial(config.cubeMaterial); glm::mat4 model = glm::mat4(1.0f); - model = glm::translate(model, config.boxPos); + model = glm::translate(model, config.cubePos); config.shader.setMat4("model", model); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.diffuseMap); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.specularMap); + glBindVertexArray(config.cubeVAO); glDrawArrays(GL_TRIANGLES, 0, 36); } @@ -270,15 +247,12 @@ void draw_light(Config& config, glm::mat4& projection, glm::mat4& view) { glDrawArrays(GL_TRIANGLES, 0, 36); } - -void render(GLFWwindow* window, Config& config, BoxTest& box, b2World& world) { +void render(GLFWwindow* window, Config& config) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); config.shader.use(); - Box2d_step_world(world); - // time is used for fake 3d rotation float time = glfwGetTime(); @@ -286,15 +260,13 @@ void render(GLFWwindow* window, Config& config, BoxTest& box, b2World& world) { glm::mat4 view = config.camera.lookAt(); glm::mat4 projection = glm::mat4(1.0f); + // fov, aspect, near plane, far plane projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); // update light color draw_cube(config, projection, view); - // the 0 is for which popcorn, so later when we have N for-loop - draw_popcorn(config, box, 0, projection, view); - draw_light(config, projection, view); glfwSwapBuffers(window); @@ -311,18 +283,14 @@ void cleanup(Config& config) { int main() { init_glfw(); - b2Vec2 gravity(0.0f, -10.0f); - b2World world(gravity); - BoxTest box = Box2d_setup(world); - auto window = create_window(); auto config = setup(); glfwSetWindowUserPointer(window, &config.camera); while(!glfwWindowShouldClose(window)) { - processInput(window, box, config.camera); - render(window, config, box, world); + processInput(window, config.camera); + render(window, config); config.updateDelta(); } diff --git a/11-lightmaps/src/shader.hpp b/11-lightmaps/src/shader.hpp index 947a7f9..2289c29 100644 --- a/11-lightmaps/src/shader.hpp +++ b/11-lightmaps/src/shader.hpp @@ -7,9 +7,9 @@ struct Material { glm::vec3 ambient{0.1f,0.1f,0.1f}; - glm::vec3 diffuse{0.5f,0.5f,0.5f}; - glm::vec3 specular{0.8f, 0.8f, 0.8f}; float shininess{32.0f}; + unsigned int diffuseMap = 0; + unsigned int specularMap = 0; }; struct Light { @@ -39,8 +39,6 @@ public: void applyMaterial(const Material& material) { setVec3("material.ambient", material.ambient); - setVec3("material.diffuse", material.diffuse); - setVec3("material.specular", material.specular); setFloat("material.shininess", material.shininess); }