From 674887d5cebcf88c408a3f150558f2c5643e2423 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 5 Apr 2020 17:48:49 +0300 Subject: [PATCH] final data push --- src/Environment.cpp | 17 ++++++ src/Environment.h | 53 ++++++++++++++++++ src/planet/PlanetFace.cpp | 113 +++++++++++++++++++++++++++++++++++--- src/planet/PlanetFace.h | 2 +- 4 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 src/Environment.cpp create mode 100644 src/Environment.h diff --git a/src/Environment.cpp b/src/Environment.cpp new file mode 100644 index 0000000..2886015 --- /dev/null +++ b/src/Environment.cpp @@ -0,0 +1,17 @@ +#include "Environment.h" + +void Environment::draw (Shader* shader) +{ + for (int i = 0; i < MAX_LIGHTS; i++) { + /*std::string indx = std::to_string(i); + if (m_lights[i]) { + shader->setUniform("lightColor[" + indx + "]", m_lights[i].color); + shader->setUniform("lightPosition[" + indx + "]", m_lights[i].position); + shader->setUniform("attenuation[" + indx + "]", m_lights[i].attenuation); + } else { + shader->setUniform("lightColor[" + indx + "]", glm::vec3(0.0, 0.0, 0.0)); + shader->setUniform("lightPosition[" + indx + "]", glm::vec3(0.0, 0.0, 0.0)); + shader->setUniform("attenuation[" + indx + "]", glm::vec3(1.0, 0.0, 0.0)); + }*/ + } +} diff --git a/src/Environment.h b/src/Environment.h new file mode 100644 index 0000000..e064a2c --- /dev/null +++ b/src/Environment.h @@ -0,0 +1,53 @@ +#ifndef __ENVIRONMENT_H__ +#define __ENVIRONMENT_H__ + +#include "util/Common.h" +#include "util/Singleton.h" +#include "Shader.h" + +#define MAX_LIGHTS 4 + +struct Light { + Light (glm::vec3 position, glm::vec3 color, glm::vec3 attenuation = glm::vec3(1.0, 0.0, 0.0)) : + position(position), color(color), attenuation(attenuation) {} + glm::vec3 position; + glm::vec3 color; + glm::vec3 attenuation; +}; + +struct SpotLight : public Light { + SpotLight (glm::vec3 position, glm::vec3 dir, glm::vec3 color, glm::vec3 attenuation) : + Light(position, color, attenuation), direction(dir) {} + glm::vec3 direction; +}; + +struct DirectionalLight : public SpotLight { + DirectionalLight (glm::vec3 position, glm::vec3 dir, glm::vec3 color) : + SpotLight(position, dir, color, glm::vec3(1.0, 0.0, 0.0)) {} +}; + +class Environment : public Singleton +{ + public: + void draw (Shader* shader); + + inline void setAmbientColor (glm::vec3 color) { m_ambient = color; } + inline void setSun (Light light) { m_sun = light; } + + friend class Singleton; + private: + Light m_sun; + + // Ambient color + glm::vec3 m_ambient; + + // Fog + glm::vec3 m_fogColor; + float m_fogStart; + float m_fogEnd; + + // All lights + std::vector m_lights; +}; + +#endif // __ENVIRONMENT_H__ diff --git a/src/planet/PlanetFace.cpp b/src/planet/PlanetFace.cpp index ceb90cb..06304f8 100644 --- a/src/planet/PlanetFace.cpp +++ b/src/planet/PlanetFace.cpp @@ -56,11 +56,11 @@ void PlanetFaceNode::tick(Camera* camera, GLfloat dtime, bool& tickUpdatedLOD, b else if (camToOrigin > splitDistance * 2 && !m_leaf) tickUpdatedLOD = this->merge(); + // TODO: fix weird vanishing meshes + if (tickUpdatedLOD) return; - // TODO: fix weird vanishing meshes - if (m_leaf && m_generated == false) { tickGeneratedFace = true; @@ -135,14 +135,20 @@ void PlanetFaceNode::generate() vertices.push_back({ pos, vertNormal, glm::vec2(j * (1.0 / RESOLUTION), i * (1.0 / RESOLUTION)) }); // Set center - if ((i == RESOLUTION / 2 and j == RESOLUTION / 2)) + if ((i == RESOLUTION / 2 && j == RESOLUTION / 2)) m_center = pos; } } + bool fanTop = true; + bool fanLeft = true; + bool fanRight = true; + bool fanBottom = true; + // Create indices for mesh for (int gz = 0; gz < RESOLUTION - 1; gz++) { + bool slantLeft = gz % 2 == 0; for (int gx = 0; gx < RESOLUTION - 1; gx++) { int topLeft = (gz * RESOLUTION) + gx; @@ -150,12 +156,101 @@ void PlanetFaceNode::generate() int bottomLeft = ((gz + 1) * RESOLUTION) + gx; int bottomRight = bottomLeft + 1; - indices.push_back(bottomLeft); - indices.push_back(bottomRight); - indices.push_back(topRight); - indices.push_back(topRight); - indices.push_back(topLeft); - indices.push_back(bottomLeft); + bool uTri1 = true; + bool uTri2 = true; + int tri1[3]; + int tri2[3]; + + if (slantLeft) + { + tri1[0] = topLeft; + tri1[1] = bottomLeft; + tri1[2] = bottomRight; + tri2[0] = topLeft; + tri2[1] = bottomRight; + tri2[2] = topRight; + } + else + { + tri1[0] = topLeft; + tri1[1] = bottomLeft; + tri1[2] = topRight; + tri2[0] = bottomLeft; + tri2[1] = bottomRight; + tri2[2] = topRight; + } + + if (fanTop && gz == 0) + { + if (gx % 2 == 0) + { + tri2[0] = topLeft; + tri2[1] = bottomRight; + tri2[2] = topRight + 1; + } + else + { + uTri1 = false; + } + } + + if (fanRight && gx == RESOLUTION - 2) + { + if (gz % 2 == 0) + { + tri2[0] = topRight; + tri2[1] = bottomLeft; + tri2[2] = bottomRight + RESOLUTION; + } + else + { + uTri2 = false; + } + } + + if (fanBottom && gz == RESOLUTION - 2) + { + if (gx % 2 == 0) + { + tri2[0] = bottomLeft; + tri2[1] = bottomRight + 1; + tri2[2] = topRight; + } + else + { + uTri1 = false; + } + } + + if (fanLeft && gx == 0) + { + if (gz % 2 == 0) + { + tri1[0] = topLeft; + tri1[1] = bottomLeft + RESOLUTION; + tri1[2] = bottomRight; + } + else + { + uTri1 = false; + } + } + + if (uTri1) + { + indices.push_back(tri1[0]); + indices.push_back(tri1[1]); + indices.push_back(tri1[2]); + } + + if (uTri2) + { + indices.push_back(tri2[0]); + indices.push_back(tri2[1]); + indices.push_back(tri2[2]); + } + + slantLeft = !slantLeft; } } diff --git a/src/planet/PlanetFace.h b/src/planet/PlanetFace.h index aa475ce..61858cf 100644 --- a/src/planet/PlanetFace.h +++ b/src/planet/PlanetFace.h @@ -6,7 +6,7 @@ #include "Shader.h" #include "Camera.h" -#define RESOLUTION 64 +#define RESOLUTION 61 enum Face {