#include "GfxSolarSystem.hpp"
#include <osg/Group>
#include <osg/Node>
#include <osg/MatrixTransform>
#include <osg/LightSource>
#include <osg/Light>
#include <osg/Vec3>
#include <osg/Vec4>
#include <iostream>
#include <algorithm>
AST_NAMESPACE_BEGIN
GfxSolarSystem::GfxSolarSystem() : m_root(nullptr) {
}
GfxSolarSystem::~GfxSolarSystem() {
for (auto body : m_celestialBodies) {
delete body;
}
m_celestialBodies.clear();
}
bool GfxSolarSystem::initialize() {
m_root = new osg::Group();
if (!createCompleteSolarSystem()) {
return false;
}
return true;
}
void GfxSolarSystem::update(double deltaTime) {
for (auto body : m_celestialBodies) {
body->update(deltaTime);
}
}
osg::Node* GfxSolarSystem::getNode() {
return m_root;
}
void GfxSolarSystem::addCelestialBody(GfxCelestialBody* body) {
if (!body) {
return;
}
m_celestialBodies.push_back(body);
if (m_root) {
m_root->addChild(body->getNode());
osg::Node* orbitLine = body->createOrbitLine();
if (orbitLine) {
m_root->addChild(orbitLine);
m_orbitLines.push_back(orbitLine);
}
}
}
void GfxSolarSystem::removeCelestialBody(GfxCelestialBody* body) {
if (!body) {
return;
}
auto it = std::find(m_celestialBodies.begin(), m_celestialBodies.end(), body);
if (it != m_celestialBodies.end()) {
m_celestialBodies.erase(it);
}
if (m_root) {
m_root->removeChild(body->getNode());
}
}
GfxCelestialBody* GfxSolarSystem::getCelestialBody(const std::string& name) {
for (auto body : m_celestialBodies) {
if (body->getName() == name) {
return body;
}
}
return nullptr;
}
bool GfxSolarSystem::createSun() {
GfxCelestialBody* sun = new GfxCelestialBody("Sun", 1.0, osg::Vec4(1.0f, 0.9f, 0.0f, 1.0f));
if (!sun->initialize()) {
delete sun;
return false;
}
sun->setRotationSpeed(0.1);
addCelestialBody(sun);
return true;
}
bool GfxSolarSystem::createPlanet(const std::string& name, double radius, double orbitalRadius,
double orbitalSpeed, double rotationSpeed, const osg::Vec4& color) {
GfxCelestialBody* planet = new GfxCelestialBody(name, radius, color);
if (!planet->initialize()) {
delete planet;
return false;
}
planet->setOrbitalRadius(orbitalRadius);
planet->setOrbitalSpeed(orbitalSpeed);
planet->setRotationSpeed(rotationSpeed);
addCelestialBody(planet);
return true;
}
bool GfxSolarSystem::createCompleteSolarSystem() {
if (!createSun()) {
return false;
}
createPlanet("Mercury", 0.38, 2.0, 0.24, 0.01, osg::Vec4(0.7f, 0.7f, 0.7f, 1.0f));
createPlanet("Venus", 0.95, 3.0, 0.62, 0.001, osg::Vec4(0.9f, 0.8f, 0.6f, 1.0f));
createPlanet("Earth", 1.0, 4.0, 1.0, 0.041, osg::Vec4(0.2f, 0.3f, 0.8f, 1.0f));
createPlanet("Mars", 0.53, 5.0, 1.88, 0.039, osg::Vec4(0.8f, 0.3f, 0.2f, 1.0f));
createPlanet("Jupiter", 11.2, 7.0, 11.86, 0.41, osg::Vec4(0.8f, 0.7f, 0.5f, 1.0f));
createPlanet("Saturn", 9.45, 9.0, 29.46, 0.38, osg::Vec4(0.9f, 0.8f, 0.7f, 1.0f));
createPlanet("Uranus", 4.0, 11.0, 84.01, 0.16, osg::Vec4(0.5f, 0.7f, 0.8f, 1.0f));
createPlanet("Neptune", 3.88, 13.0, 164.8, 0.15, osg::Vec4(0.3f, 0.4f, 0.8f, 1.0f));
return true;
}
AST_NAMESPACE_END