From 0b71aad1f7685718f5babfb22fd3a1485336bb83 Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Fri, 30 Sep 2016 23:15:51 +0200 Subject: [PATCH] planet distribution is nice now and one sun is always placed with bigger radius and space nearby. --- data/shader/gameobjects.frag | 29 +++---- game/main.cpp | 6 +- .../renderer_polygon_3d.cpp | 13 ++- game/state/state.cpp | 81 ++++++++++++++----- game/state/state.hpp | 2 +- 5 files changed, 86 insertions(+), 45 deletions(-) diff --git a/data/shader/gameobjects.frag b/data/shader/gameobjects.frag index 9d78938..afc73bf 100644 --- a/data/shader/gameobjects.frag +++ b/data/shader/gameobjects.frag @@ -6,8 +6,9 @@ varying vec3 lightDirection; uniform vec3 lightColor; uniform vec3 lightPosition; -uniform float materialKind; uniform vec3 materialColor; +uniform int materialKind; +uniform int materialSeed; void main() { @@ -23,27 +24,15 @@ void main() } // TODO: add noise texture - vec3 IDiffuse = vec3(materialColor) * lightColor * max(dot(normal, lightDirection), 0.0); + vec3 color = materialColor; + color = max(vec3(0.0), min(vec3(1.0), color)); + + vec3 IDiffuse = vec3(color) * lightColor * max(dot(normal, lightDirection), 0.0); // TODO make instensity/exponent as parameter //vec3 ISpecular = lightColor * 5.0 * pow(max(dot(Reflected, Eye), 0.0), 2.0); - vec3 ISpecular = lightColor * pow(max(dot(Reflected, Eye), 0.0), 2.0); + //vec3 ISpecular = lightColor * pow(max(dot(Reflected, Eye), 0.0), 2.0); - if (dot(lightDirection, normal) <= 0.0) { - ISpecular = vec3(0.0, 0.0, 0.0); - } - - // p: point of the fragment on the surface - //vec3 v_eye = normalize(pos_eye - pos_space); // point from p to eye - //vec3 v_sun = normalize(sun_light_dir); // points from p to the sun - - //float specular = spec_int * pow( - // clamp(dot((v_eye + v_sun)/2.0, normal), 0.0, 1.0), - // 2.0*specular_exponent); - - //ISpecular = vec3(1.0, 0.0, 0.0) * specular; - - //gl_FragColor = vec4(ISpecular, 1.0); - gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular, 1.0); - //gl_FragColor = vec4(0.5+0.5*normal, 1.0); + //gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular, 1.0); + gl_FragColor = vec4((IAmbient + IDiffuse), 1.0); } \ No newline at end of file diff --git a/game/main.cpp b/game/main.cpp index da4a2cf..6460cab 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -10,6 +10,8 @@ #include "network/server.hpp" #include "options.hpp" +#include + uint64_t optionsFlags; using asio::ip::tcp; @@ -71,6 +73,8 @@ int main(int argc, char *argv[]) } } + srand(time(NULL)); + Game game; game.state()->setDeveloperMode(devMode); @@ -78,7 +82,7 @@ int main(int argc, char *argv[]) Server s(io_service, game.state(), atoi(port) ); //GameWindow window(500, 500, &game); - GameWindow window(750, 750, &game); + GameWindow window(500, 500, &game); window.set_maxfps(60.0); window.open(); diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index 8c35070..81d2495 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -171,7 +171,8 @@ namespace endofthejedi { glm::vec3 c = planet->getColor(); glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z); - glUniform1f(m_shader.location("materialKind"), (float) planet->material); + glUniform1i(m_shader.location("materialSeed"), planet->seed); + glUniform1i(m_shader.location("materialKind"), (int) planet->material); m_planetModel->render(); } @@ -294,16 +295,22 @@ namespace endofthejedi { void RendererPolygon3d::configureLightningInShader() { - glm::vec3 c = glm::vec3(1.0, 1.0, 0.8); - glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z); + // TODO: add a few small lights for explosions so they lit the + // surroundsings + // TODO: use the sun planet color for this! + glm::vec3 c = glm::vec3(1.0, 1.0, 0.8); glm::vec3 p = glm::vec3(0.3f, 0.4f, 0.0f); + for (const game::Planet *planet : m_state->planets) { if (planet->material == game::Planet::Material::Sun) { p = glm::vec3(planet->position, 0.0); + c = planet->getColor(); break; } } + glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z); + glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z); } } diff --git a/game/state/state.cpp b/game/state/state.cpp index 6b72060..a094071 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -19,7 +19,7 @@ #include "util.hpp" namespace game { - void State::init(bool devMode) + void State::init(int numPlanets, bool devMode) { m_nextId = 0; m_time = 0.0; @@ -30,31 +30,72 @@ namespace game { m_maxNumTraces = 10; m_developerMode = devMode; - bool planetsOnCircle = false; + Planet::Material mat = Planet::Material::Rock; - int numPlanets = 10; for (int i=0; i(numPlanets); - t *= 2.0*M_PI; + switch(i) { + case 0: + mat = Planet::Material::Sun; + break; - // distribute but not in the center - int tries = 0; - glm::vec2 pos; - do { - float cr = 0.7; - if (planetsOnCircle) { - pos = cr * glm::vec2(std::sin(t), std::cos(t)); - } else { - pos = cr * util::randv2_m1_1(); - } - } while(glm::length(pos) < 0.2 && tries++ < 1000); + case 1: + case 2: + mat = Planet::Material::Water; + break; - Planet::Material mat = Planet::Material::Rock; - if (i == 0) { - mat = Planet::Material::Sun; + case 3: + case 4: + mat = Planet::Material::Sand; + break; + + case 5: + mat = Planet::Material::Metal; + break; + + default: + mat = Planet::Material::Rock; } - planets.push_back(new Planet(pos, i, 0.03 + 0.07*util::randf_0_1(), mat)); + glm::vec2 pos; + float radius = 0.03 + 0.07*util::randf_0_1(); + if (i == 0) { + // sun is bigger but not too big + radius += 0.05; + if (radius > 0.9) { + radius = 0.9; + } + + } + + bool tooNearToCenter = true; + bool collidesWithOtherPlanet = true; + + // distribute but not in the center and not next to other planets + int tries = 0; + do { + pos = util::randv2_m1_1(); + + collidesWithOtherPlanet = false; + tooNearToCenter = glm::length(pos) < 0.1; + + if (!tooNearToCenter) { + for (const Planet *other : planets) { + float d = glm::distance(other->position, pos); + + float extraDist = (other->material == Planet::Material::Sun) + ? 4.0 + : 1.0; + + if (d < extraDist*other->radius + radius) { + collidesWithOtherPlanet = true; + break; + } + } + } + + } while((collidesWithOtherPlanet || tooNearToCenter) && tries++ < 1000); + + planets.push_back(new Planet(pos, i, radius, mat)); } } diff --git a/game/state/state.hpp b/game/state/state.hpp index 50e84e0..aab4e81 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -40,7 +40,7 @@ namespace game { // called to setup the state (randomize planets, kill // traces/missiles/ships etc.) - void init(bool devMode=false); + void init(int numPlanets=15, bool devMode=false); // main method to advance the simulation by the given timestamp in // seconds.