AtmosphereTestScene.cpp 4.51 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
#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:
Dario Seyb's avatar
Dario Seyb committed
46
  auto sunShader = ShaderProgramCreator("PBR")
Dario Seyb's avatar
Dario Seyb committed
47 48 49 50 51 52 53 54
    .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, sunShader, false, RenderQueue::OPAQUE };
Dario Seyb's avatar
Dario Seyb committed
56 57 58 59 60 61 62 63

  // 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;
David Gilbert's avatar
David Gilbert committed
64
  sunTransform->scale = glm::dvec3(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

  // 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);

Dario Seyb's avatar
Dario Seyb committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  // create cockpit 
  auto vaoCockpit = VertexArrayObjectCreator("cockpit.obj").create();
  auto cockpitShader = ShaderProgramCreator("PBR")
    .attributeLocations(vaoCockpit->getAttributeLocations())
    .fragmentDataLocations(m_renderer->getGBufferLocations()).create();

  Material cockpitMaterial{ glm::vec4{1, 1, 1, 1}, glm::vec4{0.05, 0.05, 0.05, 1}, checkboardTexture, cockpitShader, true, RenderQueue::OPAQUE };
  Geometry cockpitGeom{ vaoCockpit };
  cockpitGeom.vao->setMode(GL_PATCHES);
  cockpit = m_sceneGraph->create();
  auto cockpitDrawable = cockpit.assign<Drawable>(cockpitGeom, cockpitMaterial);
  auto cockpitTransform = cockpit.assign<Transform>();
  cockpitTransform->position = glm::dvec3(0, 0, 0);
  cockpitDrawable->renderPassIndex = 1;
  cockpitDrawable->visible = true;
  auto cockpitLight = cockpit.assign<Light>(glm::vec4(1, 1, 1, 1), glm::vec3(1, 0, 0), false, LightType::POINT, 1);

Dario Seyb's avatar
Dario Seyb committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  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;
David Gilbert's avatar
David Gilbert committed
106 107
      m_player->setPosition(glm::dvec3(0, 0, r * 1.2));
      m_player->setRotation(glm::dmat4());
Dario Seyb's avatar
Dario Seyb committed
108 109 110 111 112 113
    }


    if (ImGui::Button("Earth", glm::vec2(100, 20))) {
      m_player->attachToParent(earth);
			r = earth.component<Transform>()->scale.x;
David Gilbert's avatar
David Gilbert committed
114 115
			m_player->setPosition(glm::dvec3(0, 0, r * 1.2));
      m_player->setRotation(glm::dmat4());
Dario Seyb's avatar
Dario Seyb committed
116 117 118 119 120 121 122 123 124 125
    }

    ImGui::End();
  });

  return true;
}

void AtmosphereTestScene::shutdown() {
}