X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2Fworld.cpp;h=32ffeb954a965da4fd6bc344864ce93e52f16a57;hb=0734615e546059679f1827c35fe1928ffea2fc56;hp=682eb9bb3763b8019da31fd206cb4a5bb483aeed;hpb=0e061ce526fe46dd3e894223e5d646eb2e30f826;p=blobs.git diff --git a/src/world/world.cpp b/src/world/world.cpp index 682eb9b..32ffeb9 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -42,7 +42,6 @@ Body::Body() , mass(1.0) , radius(1.0) , orbit() -, surface_tilt(0.0, 0.0) , axis_tilt(0.0, 0.0) , rotation(0.0) , angular(0.0) @@ -115,6 +114,24 @@ double Body::RotationalPeriod() const noexcept { } } +double Body::DayLength() const noexcept { + if (!HasParent()) { + return RotationalPeriod(); + } + double year = OrbitalPeriod(); + double sidereal = RotationalPeriod(); + double grade = (angular < 0.0 ? -1.0 : 1.0) * (std::abs(axis_tilt.x) > PI * 0.5 ? -1.0 : 1.0); + return std::abs((year * sidereal) / ( year + (grade * sidereal))); +} + +double Body::SphereOfInfluence() const noexcept { + if (HasParent()) { + return orbit.SemiMajorAxis() * std::pow(Mass() / Parent().Mass(), 2.0 / 5.0); + } else { + return std::numeric_limits::infinity(); + } +} + glm::dmat4 Body::ToUniverse() const noexcept { glm::dmat4 m(1.0); const Body *b = this; @@ -171,12 +188,8 @@ void Body::Cache() noexcept { orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y); inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x); } - local = - glm::eulerAngleY(rotation) - * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y); - inverse_local = - glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x) - * glm::eulerAngleY(-rotation); + local = glm::eulerAngleY(rotation); + inverse_local = glm::eulerAngleY(-rotation); } void Body::CheckCollision() noexcept { @@ -184,13 +197,13 @@ void Body::CheckCollision() noexcept { collisions.clear(); auto end = Creatures().end(); for (auto i = Creatures().begin(); i != end; ++i) { - math::AABB i_box((*i)->CollisionBox()); + math::AABB i_box((*i)->CollisionBounds()); glm::dmat4 i_mat((*i)->CollisionTransform()); for (auto j = (i + 1); j != end; ++j) { glm::dvec3 diff((*i)->GetSituation().Position() - (*j)->GetSituation().Position()); double max_dist = ((*i)->Size() + (*j)->Size()) * 1.74; if (glm::length2(diff) > max_dist * max_dist) continue; - math::AABB j_box((*j)->CollisionBox()); + math::AABB j_box((*j)->CollisionBounds()); glm::dmat4 j_mat((*j)->CollisionTransform()); glm::dvec3 normal; double depth; @@ -200,11 +213,12 @@ void Body::CheckCollision() noexcept { } } for (auto &c : collisions) { + c.A().OnCollide(c.B()); + c.B().OnCollide(c.A()); c.A().GetSituation().Move(c.Normal() * (c.Depth() * -0.5)); c.B().GetSituation().Move(c.Normal() * (c.Depth() * 0.5)); c.A().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.AVel())); c.B().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.BVel())); - // TODO: notify participants so they can be annoyed } } @@ -455,16 +469,22 @@ glm::dvec3 Planet::TileCenter(int srf, int x, int y, double e) const noexcept { return glm::normalize(cubeunmap(srf, u, v)) * (Radius() + e); } -void Planet::BuildVAO(const Set &ts) { +void Planet::BuildVAO() { vao.reset(new graphics::SimpleVAO); vao->Bind(); vao->BindAttributes(); vao->EnableAttribute(0); vao->EnableAttribute(1); vao->EnableAttribute(2); + vao->EnableAttribute(3); + vao->EnableAttribute(4); + vao->EnableAttribute(5); vao->AttributePointer(0, false, offsetof(Attributes, position)); vao->AttributePointer(1, false, offsetof(Attributes, normal)); vao->AttributePointer(2, false, offsetof(Attributes, tex_coord)); + vao->AttributePointer(3, false, offsetof(Attributes, shiny)); + vao->AttributePointer(4, false, offsetof(Attributes, glossy)); + vao->AttributePointer(5, false, offsetof(Attributes, metallic)); vao->ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW); { auto attrib = vao->MapAttributes(GL_WRITE_ONLY); @@ -476,21 +496,22 @@ void Planet::BuildVAO(const Set &ts) { for (int index = 0, surface = 0; surface < 6; ++surface) { for (int y = 0; y < sidelength; ++y) { for (int x = 0; x < sidelength; ++x, ++index) { - glm::vec3 pos[5]; - pos[0][(surface + 0) % 3] = x + 0 - offset; - pos[0][(surface + 1) % 3] = y + 0 - offset; + glm::vec3 pos[4]; + pos[0][(surface + 0) % 3] = float(x + 0) - offset; + pos[0][(surface + 1) % 3] = float(y + 0) - offset; pos[0][(surface + 2) % 3] = offset; - pos[1][(surface + 0) % 3] = x + 0 - offset; - pos[1][(surface + 1) % 3] = y + 1 - offset; + pos[1][(surface + 0) % 3] = float(x + 0) - offset; + pos[1][(surface + 1) % 3] = float(y + 1) - offset; pos[1][(surface + 2) % 3] = offset; - pos[2][(surface + 0) % 3] = x + 1 - offset; - pos[2][(surface + 1) % 3] = y + 0 - offset; + pos[2][(surface + 0) % 3] = float(x + 1) - offset; + pos[2][(surface + 1) % 3] = float(y + 0) - offset; pos[2][(surface + 2) % 3] = offset; - pos[3][(surface + 0) % 3] = x + 1 - offset; - pos[3][(surface + 1) % 3] = y + 1 - offset; + pos[3][(surface + 0) % 3] = float(x + 1) - offset; + pos[3][(surface + 1) % 3] = float(y + 1) - offset; pos[3][(surface + 2) % 3] = offset; - float tex = ts[TileAt(surface, x, y).type].texture; + const TileType &t = TypeAt(surface, x, y); + float tex = t.texture; const float tex_v_begin = surface < 3 ? 1.0f : 0.0f; const float tex_v_end = surface < 3 ? 0.0f : 1.0f; @@ -499,24 +520,36 @@ void Planet::BuildVAO(const Set &ts) { attrib[4 * index + 0].tex_coord[0] = 0.0f; attrib[4 * index + 0].tex_coord[1] = tex_v_begin; attrib[4 * index + 0].tex_coord[2] = tex; + attrib[4 * index + 0].shiny = t.shiny; + attrib[4 * index + 0].glossy = t.glossy; + attrib[4 * index + 0].metallic = t.metallic; attrib[4 * index + 1].position = glm::normalize(pos[1]) * (surface < 3 ? offset : -offset); attrib[4 * index + 1].normal = pos[1]; attrib[4 * index + 1].tex_coord[0] = 0.0f; attrib[4 * index + 1].tex_coord[1] = tex_v_end; attrib[4 * index + 1].tex_coord[2] = tex; + attrib[4 * index + 1].shiny = t.shiny; + attrib[4 * index + 1].glossy = t.glossy; + attrib[4 * index + 1].metallic = t.metallic; attrib[4 * index + 2].position = glm::normalize(pos[2]) * (surface < 3 ? offset : -offset); attrib[4 * index + 2].normal = pos[2]; attrib[4 * index + 2].tex_coord[0] = 1.0f; attrib[4 * index + 2].tex_coord[1] = tex_v_begin; attrib[4 * index + 2].tex_coord[2] = tex; + attrib[4 * index + 2].shiny = t.shiny; + attrib[4 * index + 2].glossy = t.glossy; + attrib[4 * index + 2].metallic = t.metallic; attrib[4 * index + 3].position = glm::normalize(pos[3]) * (surface < 3 ? offset : -offset); attrib[4 * index + 3].normal = pos[3]; attrib[4 * index + 3].tex_coord[0] = 1.0f; attrib[4 * index + 3].tex_coord[1] = tex_v_end; attrib[4 * index + 3].tex_coord[2] = tex; + attrib[4 * index + 3].shiny = t.shiny; + attrib[4 * index + 3].glossy = t.glossy; + attrib[4 * index + 3].metallic = t.metallic; } } } @@ -587,7 +620,7 @@ void GenerateEarthlike(const Set &tiles, Planet &p) noexcept { constexpr double highland_thresh = 0.4; constexpr double mountain_thresh = 0.5; - const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y)); + const glm::dvec3 axis(0.0, 1.0, 0.0); const double cap_thresh = std::abs(std::cos(p.AxialTilt().x)); const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0; const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0; @@ -666,7 +699,7 @@ void GenerateEarthlike(const Set &tiles, Planet &p) noexcept { } } } - p.BuildVAO(tiles); + p.BuildVAO(); } void GenerateTest(const Set &tiles, Planet &p) noexcept { @@ -681,12 +714,14 @@ void GenerateTest(const Set &tiles, Planet &p) noexcept { } } } - p.BuildVAO(tiles); + p.BuildVAO(); } Sun::Sun() -: Body() { +: Body() +, color(1.0) +, luminosity(1.0) { } Sun::~Sun() {