Add changes to the scene, using the displacement map

in addition: fix the normals of the cube generator
This commit is contained in:
Peder Bergebakken Sundt 2019-03-17 15:25:37 +01:00
parent c9e4332a65
commit 54cd22ff57
6 changed files with 60 additions and 25 deletions

View File

@ -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; i<N_LIGHTS; i++) {
lightNode[i] = createSceneNode(POINT_LIGHT);
lightNode[i]->lightID = 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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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<unsigned char> png;
vector<unsigned char> 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;

View File

@ -8,10 +8,11 @@ typedef unsigned int uint;
struct PNGImage {
uint width, height;
bool repeat_mirrored = false;
std::vector<unsigned char> 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);

View File

@ -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;