AtmosphereTestScene.cpp 3.61 KB
Newer Older
Dario Seyb's avatar
Dario Seyb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <engine/scene/scenes/AtmosphereTestScene.hpp>

#include <engine/ui/UISystem.hpp>

#include <ACGL/ACGL.hh>

#include <ACGL/OpenGL/Objects.hh>
#include <ACGL/Base/Settings.hh>
#include <ACGL/Math/Math.hh>
#include <ACGL/OpenGL/Data/TextureLoadStore.hh>
#include <ACGL/OpenGL/Managers.hh>

#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
#include <ACGL/OpenGL/Creator/VertexArrayObjectCreator.hh>
#include <ACGL/OpenGL/Creator/Texture2DCreator.hh>

#include <engine/scene/Transform.hpp>
#include <engine/scene/Drawable.hpp>
#include <engine/scene/Planet.hpp>

#include <engine/events/MouseEvent.hpp>
#include <engine/events/KeyboardEvent.hpp>

#include <engine/graphics/BloomPostFX.hpp>

using namespace ACGL::OpenGL;
using namespace ACGL::Base;
using namespace ACGL::Utils;

bool AtmosphereTestScene::startup() {
  if (!Scene::startup()) {
    return false;
  }

  RESOLVE_DEPENDENCY(m_player);
  RESOLVE_DEPENDENCY(m_orbitals);

  m_renderer->addEffect<BloomPostFX>();

  auto vaoSun = VertexArrayObjectCreator("uvsphere.obj").create();

  // load a texture:
  auto checkboardTexture = Texture2DFileManager::the()->get(Texture2DCreator("checkerboard.png"));

  // look up all shader files starting with 'PBR' and build a ShaderProgram from it:
  auto pbrShader = ShaderProgramCreator("PBR")
    .attributeLocations(vaoSun->getAttributeLocations())
    .fragmentDataLocations(m_renderer->getGBufferLocations()).create();

  // Create geometry objects that point to the previously initialized vaos
  Geometry geom1 = { vaoSun };
	geom1.vao->setMode(GL_PATCHES);

  // Create a material that uses the loaded shader program
Dario Seyb's avatar
Dario Seyb committed
55
  Material sunMaterial = { glm::vec4{ 1, 1, 1, 1 }, glm::vec4{ 2, 2, 1, 1 }, checkboardTexture, pbrShader, false, RenderQueue::OPAQUE };
Dario Seyb's avatar
Dario Seyb committed
56 57 58 59 60 61 62 63 64

  // Let's create a placeholder sun
  sun = m_sceneGraph->create();
  // Add a transform component to it so we are able to position it in space
  auto sunTransform = sun.assign<Transform>();
  // Add a Drawable component to it so the renderer has something to draw
  sun.assign<Drawable>(geom1, sunMaterial);
  float scale = m_orbitals->solarRadius * m_orbitals->scaleFactor * 0.1;
  sunTransform->scale = glm::vec3(scale);
Dario Seyb's avatar
Dario Seyb committed
65
  sunLight = sun.assign<Light>(glm::vec4(1, 1, 1, 3), glm::vec3(1, 0, 0), false, LightType::DIRECTIONAL);
Dario Seyb's avatar
Dario Seyb committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  // create a planet! 
  // Mass in solar masses, Radius in solar radii, Eccentricity, Semimajor axis, Inclination, Ascending Node, Arg. of Periapsis, time at perihelion
  //																mass              radius            e         a        i          N          w
  earth = m_orbitals->addPlanet(sunTransform, "Earth", 0.000002988, 0.009153735632184, 0.016713, 1.000000, 0.0000000, 0.00000, 4.93533, 0);

  m_events->subscribe<SimulateEvent>([this](const SimulateEvent& e) {
    sunLight->dir = -glm::normalize(m_player->getWorldPosition());
  });

  // Subscribe to the UI drawing event.
  // This is called once per frame
  m_events->subscribe<"DrawUI"_sh>([this]() {

    ImGui::Begin("Camera Control", 0,
      ImGuiWindowFlags_::ImGuiWindowFlags_AlwaysAutoResize);

		float r;
    if (ImGui::Button("Sun/Global", glm::vec2(100, 20))) {
      // Reset the camera to sun coords
      m_player->attachToParent(sun);
			r = sun.component<Transform>()->scale.x;
      m_player->setPosition(glm::vec3(0, 0, r * 1.2));
      m_player->setRotation(glm::mat4());
    }


    if (ImGui::Button("Earth", glm::vec2(100, 20))) {
      m_player->attachToParent(earth);
			r = earth.component<Transform>()->scale.x;
			m_player->setPosition(glm::vec3(0, 0, r * 1.2));
      m_player->setRotation(glm::mat4());
    }

    ImGui::End();
  });

  return true;
}

void AtmosphereTestScene::shutdown() {
}