AtmosphereTestScene.cpp 10.6 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
#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>

Dario Seyb's avatar
Dario Seyb committed
26

Dario Seyb's avatar
Dario Seyb committed
27 28 29 30 31 32 33 34 35 36 37 38 39
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>();
Dario Seyb's avatar
Dario Seyb committed
40

Dario Seyb's avatar
Dario Seyb committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  consoleCamera = m_sceneGraph->create();
  consoleCamera.assign<Transform>();
  consoleCamera.assign<Camera>(50, 0.1, 50);

  m_renderer->addRenderPass(consoleCamera, "Console"_sh);
  m_renderer->setRenderPassOnlyTexture("Console"_sh, true);

  loadMainSceneResources();
  switchToMainScene();

  SDL_SetRelativeMouseMode(SDL_TRUE);
  m_player->setHasControl(false);
  m_player->setCockpitPosition(CAM_CONSOLE_POS);
  m_player->setRotation(CAM_CONSOLE_ROT);

  m_events->subscribe<SimulateEvent>([this](const SimulateEvent &e) {
    if (mainSceneRunning) {
      sunLight->dir = -glm::normalize(m_player->getWorldPosition());
      cockpitSunLight->dir =
        glm::vec3(glm::dvec4(sunLight->dir, 0) * m_player->getRotation());
    }

    if (isInterpolatingCamera) {
      double interpolationPercent = 1.0 - glm::pow(1 - interpolationTime / totalInterpolationTime, 2);
      m_player->setCockpitPosition(camStartPos + (camTargetPos-camStartPos)*interpolationPercent);

      interpolationPercent = 1.0 - glm::pow(1 - interpolationTime / totalInterpolationTime, 5);

      glm::quat rot = glm::slerp(camStartRot, camTargetRot, (float)interpolationPercent);
      m_player->setRotation(glm::mat4_cast(rot));
      interpolationTime += e.dt;
      if (interpolationTime >= totalInterpolationTime) {
        m_player->setCockpitPosition(camTargetPos);
        m_player->setRotation(glm::mat4_cast(camTargetRot));
        isInterpolatingCamera = false;
        m_player->setHasControl(true);
      }
    }
  });

  m_events->subscribe<KeyboardEvent>([this](const KeyboardEvent &e) {
    switch (e.originalEvent.key.keysym.scancode) {
    case SDL_SCANCODE_TAB:
        if (e.originalEvent.key.type == SDL_KEYDOWN) {
          m_player->attachToParent(earth);
          float r = earth.component<Transform>()->scale.x;
          m_player->setPosition(glm::vec3(0, 0, r * 1.2));
          m_player->setRotation(glm::mat4());
        }
      break;
    case SDL_SCANCODE_SPACE:
      if (e.originalEvent.key.type == SDL_KEYDOWN) {
        if (isInterpolatingCamera) break;
        m_player->setHasControl(false);

        if (isFocusedOnConsole) {
          camStartPos = m_player->getCockpitPosition();
          camTargetPos = { 0, 0 , 0 };
          camStartRot = glm::quat_cast(m_player->getRotation());
          camTargetRot = glm::quat();
        } else {
          camStartPos = m_player->getCockpitPosition();
          camTargetPos = CAM_CONSOLE_POS;
          camStartRot = glm::quat_cast(m_player->getRotation());
          camTargetRot =  glm::quat_cast(CAM_CONSOLE_ROT);
        }

        isFocusedOnConsole = !isFocusedOnConsole;
        interpolationTime = 0;
        totalInterpolationTime = 4;
        isInterpolatingCamera = true;

      }
    default:
      break;
    }
  });

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

    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::dvec3(0, 0, r * 1.2));
      m_player->setRotation(glm::dmat4());
    }

    if (ImGui::Button("Earth", glm::vec2(100, 20))) {
      m_player->attachToParent(earth);
      r = earth.component<Transform>()->scale.x;
      m_player->setPosition(glm::dvec3(0, 0, r * 1.2));
      m_player->setRotation(glm::dmat4());
    }
Dario Seyb's avatar
Dario Seyb committed
144

Dario Seyb's avatar
Dario Seyb committed
145 146
    ImGui::End();
  });
Dario Seyb's avatar
Dario Seyb committed
147

Dario Seyb's avatar
Dario Seyb committed
148 149 150 151 152 153
  return true;
}

void AtmosphereTestScene::switchToMainScene() {
  {
    auto skyboxPassId = m_renderer->getRenderPassId("Skybox"_sh);
Dario Seyb's avatar
Dario Seyb committed
154

Dario Seyb's avatar
Dario Seyb committed
155 156
    skybox = m_sceneGraph->create();
    skybox.assign<Transform>();
Dario Seyb's avatar
Dario Seyb committed
157 158 159
    skybox.assign<Drawable>(skyboxGeometry, skyboxMaterial, 0, skyboxPassId);
    skybox.assign<Light>(glm::vec4(1, 1, 1, 1), glm::vec3(1, 0, 0), false,
                         LightType::POINT, skyboxPassId);
Dario Seyb's avatar
Dario Seyb committed
160
  }
Dario Seyb's avatar
Dario Seyb committed
161

Dario Seyb's avatar
Dario Seyb committed
162 163 164 165 166 167 168 169
  {
    auto mainPassId = m_renderer->getRenderPassId("Main"_sh);

    // 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
Dario Seyb's avatar
Dario Seyb committed
170
    sun.assign<Drawable>(sunGeometry, sunMaterial, 0, mainPassId);
Dario Seyb's avatar
Dario Seyb committed
171 172
    float scale = m_orbitals->solarRadius * m_orbitals->scaleFactor * 0.1;
    sunTransform->scale = glm::dvec3(scale);
Dario Seyb's avatar
Dario Seyb committed
173 174 175 176 177 178 179 180 181 182 183 184
    sunLight = sun.assign<Light>(glm::vec4(1, 1, 1, 3), glm::vec3(1, 0, 0),
                                 false, LightType::DIRECTIONAL, mainPassId);

    // 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
185
  }
Dario Seyb's avatar
Dario Seyb committed
186

Dario Seyb's avatar
Dario Seyb committed
187
  {
Dario Seyb's avatar
Dario Seyb committed
188
    auto cockpitPassId = m_renderer->getRenderPassId("Cockpit"_sh);
Dario Seyb's avatar
Dario Seyb committed
189
    cockpit = m_sceneGraph->create();
Dario Seyb's avatar
Dario Seyb committed
190 191
    auto cockpitDrawable = cockpit.assign<Drawable>(
        cockpitGeometry, cockpitMaterial, 0, cockpitPassId);
Dario Seyb's avatar
Dario Seyb committed
192 193 194
    auto cockpitTransform = cockpit.assign<Transform>();
    cockpitTransform->position = glm::dvec3(0, 0, 0);
    cockpitDrawable->visible = true;
Dario Seyb's avatar
Dario Seyb committed
195 196 197
    auto cockpitLight =
        cockpit.assign<Light>(glm::vec4(1, 1, 1, 0.2), glm::vec3(1, 0, 0),
                              false, LightType::POINT, cockpitPassId);
Dario Seyb's avatar
Dario Seyb committed
198

Dario Seyb's avatar
merged  
Dario Seyb committed
199 200
    cockpitSun = m_sceneGraph->create();
    cockpitSun.assign<Transform>();
Dario Seyb's avatar
Dario Seyb committed
201 202 203
    cockpitSunLight =
        cockpitSun.assign<Light>(glm::vec4(1, 1, 1, 3), glm::vec3(1, 0, 0),
                                 true, LightType::DIRECTIONAL, cockpitPassId);
Dario Seyb's avatar
merged  
Dario Seyb committed
204

Dario Seyb's avatar
Dario Seyb committed
205
    auto blinkButton = m_sceneGraph->create();
Dario Seyb's avatar
Dario Seyb committed
206 207
    blinkButton.assign<Light>(glm::vec4(1, 0.2, 0.2, 5), glm::vec3(1, 0, 0),
                              false, LightType::POINT, cockpitPassId);
Dario Seyb's avatar
Dario Seyb committed
208 209
    auto transform = blinkButton.assign<Transform>();
    transform->parent = cockpitTransform;
Dario Seyb's avatar
Dario Seyb committed
210
    transform->position = {0.7655, -0.4545, -1.643};
Dario Seyb's avatar
Dario Seyb committed
211 212

    blinkButton = m_sceneGraph->create();
Dario Seyb's avatar
Dario Seyb committed
213 214
    blinkButton.assign<Light>(glm::vec4(0, 0.2, 1.0, 5), glm::vec3(1, 0, 0),
                              false, LightType::POINT, cockpitPassId);
Dario Seyb's avatar
Dario Seyb committed
215 216
    transform = blinkButton.assign<Transform>();
    transform->parent = cockpitTransform;
Dario Seyb's avatar
Dario Seyb committed
217 218 219 220 221 222 223 224 225 226 227 228
    transform->position = {-0.7655, -0.4545, -1.643};

    auto console = m_sceneGraph->create();
    auto consoleGeom = Geometry{ VertexArrayObjectCreator("console.obj").create() };
    consoleGeom.vao->setMode(GL_PATCHES);
    console.assign<Drawable>(
        consoleGeom,
        Material{glm::vec4{1, 1, 1, 1}, glm::vec4{1, 1, 1, 1},
                 m_renderer->getRenderPassTarget("Console"_sh), nullptr, nullptr,
                 skyboxMaterial.prog, false, RenderQueue::OPAQUE, GL_BACK},
        0, cockpitPassId);
    console.assign<Transform>();
Dario Seyb's avatar
Dario Seyb committed
229
  }
Dario Seyb's avatar
Dario Seyb committed
230
}
Dario Seyb's avatar
Dario Seyb committed
231

Dario Seyb's avatar
Dario Seyb committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
void AtmosphereTestScene::loadMainSceneResources() {
  {
    auto vaoSkybox = VertexArrayObjectCreator("skybox.obj").create();
    auto skyboxShader = ShaderProgramFileManager::the()->get(
        ShaderProgramCreator("Skybox")
            .attributeLocations(vaoSkybox->getAttributeLocations())
            .fragmentDataLocations(m_renderer->getGBufferLocations()));

    auto skyboxTexture = 
       Texture2DFileManager::the()->get(Texture2DCreator("skybox.png"));

    skyboxGeometry = {vaoSkybox};
    skyboxGeometry.vao->setMode(GL_PATCHES);
    skyboxMaterial = {glm::vec4{1, 1, 1, 1},
                      glm::vec4{1, 1, 1, 1},
                      skyboxTexture,
                      nullptr,
                      nullptr,
                      skyboxShader,
                      false,
                      RenderQueue::OPAQUE,
                      GL_BACK};
  }
Dario Seyb's avatar
Dario Seyb committed
255

Dario Seyb's avatar
Dario Seyb committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  {
    auto vaoSun = VertexArrayObjectCreator("uvsphere.obj").create();
    auto checkboardTexture =
        Texture2DFileManager::the()->get(Texture2DCreator("checkerboard.png"));

    auto sunShader =
        ShaderProgramCreator("PBR")
            .attributeLocations(vaoSun->getAttributeLocations())
            .fragmentDataLocations(m_renderer->getGBufferLocations())
            .create();

    sunGeometry = {vaoSun};
    sunGeometry.vao->setMode(GL_PATCHES);

    sunMaterial = {glm::vec4{1, 1, 1, 1},
                   glm::vec4{2, 2, 1, 20},
                   checkboardTexture,
                   nullptr,
                   nullptr,
                   sunShader,
                   false,
                   RenderQueue::OPAQUE,
                   GL_BACK};
  }
Dario Seyb's avatar
Dario Seyb committed
280

Dario Seyb's avatar
Dario Seyb committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  {
    // create cockpit
    auto vaoCockpit = VertexArrayObjectCreator("cockpit.obj").create();
    auto cockpitShader =
        ShaderProgramCreator("PBR")
            .attributeLocations(vaoCockpit->getAttributeLocations())
            .fragmentDataLocations(m_renderer->getGBufferLocations())
            .create();

    cockpitMaterial = {
        glm::vec4{0.4, 0.4, 0.4, 1}, glm::vec4{0, 0, 0, 1},
        Texture2DFileManager::the()->get(Texture2DCreator(
            "cockpit/cockpit_DefaultMaterial_AlbedoTransparency.png")),
        Texture2DFileManager::the()->get(
            Texture2DCreator("cockpit/cockpit_DefaultMaterial_Normal.png")),
        Texture2DFileManager::the()->get(Texture2DCreator(
            "cockpit/cockpit_DefaultMaterial_SpecularSmoothness.png")),
        cockpitShader, true, RenderQueue::OPAQUE, GL_BACK};

    cockpitGeometry = {vaoCockpit};
    cockpitGeometry.vao->setMode(GL_PATCHES);
  }
Dario Seyb's avatar
Dario Seyb committed
303

Dario Seyb's avatar
Dario Seyb committed
304
  resourceLoadingFinished = true;
Dario Seyb's avatar
Dario Seyb committed
305 306
}

Dario Seyb's avatar
Dario Seyb committed
307
void AtmosphereTestScene::shutdown() {}