From 54cd22ff57f9b4f0f605a897b1c740fc531cbe6d Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 17 Mar 2019 15:25:37 +0100 Subject: [PATCH] Add changes to the scene, using the displacement map in addition: fix the normals of the cube generator --- src/gamelogic.cpp | 56 +++++++++++++++++++++++------------ src/utilities/glutils.cpp | 6 ++-- src/utilities/glutils.h | 2 +- src/utilities/imageLoader.cpp | 15 +++++++++- src/utilities/imageLoader.hpp | 3 +- src/utilities/shapes.cpp | 3 ++ 6 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/gamelogic.cpp b/src/gamelogic.cpp index 2f3453f..2820a28 100644 --- a/src/gamelogic.cpp +++ b/src/gamelogic.cpp @@ -35,8 +35,6 @@ uint previousKeyFrame = 0; SceneNode* rootNode; SceneNode* plainNode; SceneNode* boxNode; -SceneNode* ballNode; -SceneNode* padNode; SceneNode* hudNode; SceneNode* textNode; @@ -70,8 +68,8 @@ PNGImage t_charmap = loadPNGFile("../res/textures/charmap.png"); PNGImage t_cobble_diff = loadPNGFile("../res/textures/cobble_diff.png"); PNGImage t_cobble_normal = loadPNGFile("../res/textures/cobble_normal.png"); PNGImage t_plain_diff = loadPNGFile("../res/textures/plain_diff.png"); -PNGImage t_plain_normal = loadPNGFile("../res/textures/plain_normal.png"); -PNGImage t_perlin = makePerlinNoisePNG(256, 256, {0.1, 0.2, 0.3}); +PNGImage t_plain_normal = loadPNGFile("../res/textures/plain_normal.png", true); +PNGImage t_perlin = makePerlinNoisePNG(256, 256, 0.05/16); void mouseCallback(GLFWwindow* window, double x, double y) { @@ -114,30 +112,46 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) { default_shader = new Gloom::Shader(); default_shader->makeBasicShader("../res/shaders/simple.vert", "../res/shaders/simple.frag"); - Mesh plain = generateSegmentedPlane(1000, 1000, 100, 100); + Mesh box = generateBox(50, 50, 50); + Mesh plain = generateSegmentedPlane(1000, 1000, 100, 100, 3); Mesh hello_world = generateTextGeometryBuffer("Skjer'a bagera?", 1.3, 2); + t_perlin.repeat_mirrored = true; rootNode = createSceneNode(); hudNode = createSceneNode(); - plainNode = createSceneNode(); - plainNode->setTexture(&t_plain_diff, &t_plain_normal); - plainNode->setMesh(&plain); - plainNode->position = {0, 0, 0}; - plainNode->shinyness = 30; - rootNode->children.push_back(plainNode); - - // add lights + // create and add lights to graph for (uint i = 0; ilightID = i; - rootNode->children.push_back(lightNode[0]); + rootNode->children.push_back(lightNode[i]); } - lightNode[0]->position = {200, 800, 600}; - lightNode[0]->color_emissive = vec3(0.2); - lightNode[0]->color_diffuse = vec3(0.8); - lightNode[0]->color_specular = vec3(0.0); - lightNode[0]->attenuation = vec3(1.0, 0.0, 0.000000); + + //create the scene: + plainNode = createSceneNode(); + plainNode->setTexture(&t_plain_diff, &t_plain_normal, &t_perlin); + plainNode->setMesh(&plain); + plainNode->position = {0, 0, 0}; + plainNode->shinyness = 20; + plainNode->displacementCoefficient = 40; + rootNode->children.push_back(plainNode); + + boxNode = createSceneNode(); + boxNode->setTexture(&t_cobble_diff, &t_cobble_normal); + boxNode->setMesh(&box); + boxNode->position = {500, 500, 40}; + boxNode->referencePoint = {25, 25, 25}; + boxNode->scale *= 2; + boxNode->shinyness = 20; + boxNode->displacementCoefficient = 40; + rootNode->children.push_back(boxNode); + + + lightNode[0]->position = {-600, 1400, 800}; + lightNode[0]->color_emissive = vec3(0.35); + lightNode[0]->color_diffuse = vec3(0.6); + lightNode[0]->color_specular = vec3(0.1); + lightNode[0]->attenuation = vec3(1.0, 0.0, 0.0); textNode = createSceneNode(); @@ -242,6 +256,10 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) { updateNodeTransformations(hudNode, mat4(1.0), cameraTransform, projection); // update positions of nodes (like the car) + plainNode->uvOffset.x += timeDelta*0.5; + plainNode->uvOffset.y -= timeDelta*0.5; + boxNode->rotation.z += timeDelta; + lightNode[1]->rotation.z -= timeDelta; } diff --git a/src/utilities/glutils.cpp b/src/utilities/glutils.cpp index fb69904..d5bf8c1 100644 --- a/src/utilities/glutils.cpp +++ b/src/utilities/glutils.cpp @@ -106,13 +106,13 @@ void addTangents(uint vaoID, Mesh& mesh) { glEnableVertexAttribArray(4); } -uint generateTexture(PNGImage& texture, bool mirrored) { +uint generateTexture(const PNGImage& texture) { uint id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (mirrored) ? GL_MIRRORED_REPEAT : GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (mirrored) ? GL_MIRRORED_REPEAT : GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (texture.repeat_mirrored) ? GL_MIRRORED_REPEAT : GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (texture.repeat_mirrored) ? GL_MIRRORED_REPEAT : GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); diff --git a/src/utilities/glutils.h b/src/utilities/glutils.h index aa72d6e..8a26b97 100644 --- a/src/utilities/glutils.h +++ b/src/utilities/glutils.h @@ -8,4 +8,4 @@ unsigned int generateBuffer(Mesh &mesh, bool isNormalMapped = false); void addTangents(unsigned int vaoID, Mesh& mesh); -unsigned int generateTexture(PNGImage& texture, bool mirrored = false); +unsigned int generateTexture(const PNGImage& texture); diff --git a/src/utilities/imageLoader.cpp b/src/utilities/imageLoader.cpp index bd4670f..643ad47 100644 --- a/src/utilities/imageLoader.cpp +++ b/src/utilities/imageLoader.cpp @@ -8,7 +8,7 @@ using std::vector; typedef unsigned int uint; // Original source: https://raw.githubusercontent.com/lvandeve/lodepng/master/examples/example_decode.cpp -PNGImage loadPNGFile(std::string fileName) { +PNGImage loadPNGFile(std::string fileName, bool flip_handedness) { vector png; vector pixels; //the raw pixels uint width, height; @@ -36,6 +36,19 @@ PNGImage loadPNGFile(std::string fileName) { } } + if (flip_handedness) { + for (uint xb = 0; xb < widthBytes; xb+=4) + for (uint y = 0; y < height; y++) { + unsigned char& r = pixels[y*widthBytes + xb + 0]; + unsigned char& g = pixels[y*widthBytes + xb + 1]; + unsigned char& b = pixels[y*widthBytes + xb + 2]; + unsigned char& a = pixels[y*widthBytes + xb + 3]; + + r = 255 - r; + g = 255 - g; + } + } + PNGImage image; image.width = width; image.height = height; diff --git a/src/utilities/imageLoader.hpp b/src/utilities/imageLoader.hpp index 5c65af8..47aa18f 100644 --- a/src/utilities/imageLoader.hpp +++ b/src/utilities/imageLoader.hpp @@ -8,10 +8,11 @@ typedef unsigned int uint; struct PNGImage { uint width, height; + bool repeat_mirrored = false; std::vector pixels; // RGBA }; -PNGImage loadPNGFile(std::string fileName); +PNGImage loadPNGFile(std::string fileName, bool flip_handedness=false); PNGImage makePerlinNoisePNG(uint w, uint h, float scale=0.1); diff --git a/src/utilities/shapes.cpp b/src/utilities/shapes.cpp index e1c15bd..0a9a6b3 100644 --- a/src/utilities/shapes.cpp +++ b/src/utilities/shapes.cpp @@ -197,6 +197,9 @@ Mesh generateBox(float width, float height, float depth, bool flipFaces) { normals[i + 2] *= -1; } } + + //2fix4u + for(vec3& normal : normals) normal *= -1; Mesh mesh; mesh.vertices = vertices;