Browse Source

Add a camera and focus entity and fix camera transform in render system

master
C. J. Howard 5 years ago
parent
commit
1caa468dd1
Signed by: cjhoward GPG Key ID: 03E1FABA9C3EC195
5 changed files with 44 additions and 16 deletions
  1. +1
    -1
      src/entity/components/camera-component.hpp
  2. +8
    -3
      src/entity/systems/render-system.cpp
  3. +21
    -0
      src/game.cpp
  4. +2
    -0
      src/game.hpp
  5. +12
    -12
      src/states/title-state.cpp

+ 1
- 1
src/entity/components/camera-component.hpp View File

@ -31,7 +31,7 @@ class CameraComponent: public Component
public:
virtual ComponentBase* clone() const;
Camera camera;
Camera* camera;
};
#endif // CAMERA_COMPONENT_HPP

+ 8
- 3
src/entity/systems/render-system.cpp View File

@ -40,7 +40,12 @@ void RenderSystem::update(float t, float dt)
CameraComponent* camera = std::get<0>(member->components);
TransformComponent* transform = std::get<1>(member->components);
camera->camera.setTransform(transform->transform);
Vector3 forward = glm::normalize(transform->transform.rotation * Vector3(0.0f, 0.0f, -1.0f));
Vector3 up = glm::normalize(transform->transform.rotation * Vector3(0.0f, 1.0f, 0.0f));
Vector3 right = glm::normalize(glm::cross(forward, up));
camera->camera->lookAt(transform->transform.translation, transform->transform.translation + forward, up);
//camera->camera->setTransform(transform->transform);
}
// Update transform of all model instances
@ -56,13 +61,13 @@ void RenderSystem::update(float t, float dt)
void RenderSystem::memberRegistered(const CameraGroup::Member* member)
{
CameraComponent* camera = std::get<0>(member->components);
scene->addObject(&camera->camera);
scene->addObject(camera->camera);
}
void RenderSystem::memberUnregistered(const CameraGroup::Member* member)
{
CameraComponent* camera = std::get<0>(member->components);
scene->removeObject(&camera->camera);
scene->removeObject(camera->camera);
}
void RenderSystem::memberRegistered(const ModelGroup::Member* member)

+ 21
- 0
src/game.cpp View File

@ -39,6 +39,8 @@
#include "game/brush.hpp"
#include "entity/component-manager.hpp"
#include "entity/components/transform-component.hpp"
#include "entity/components/camera-component.hpp"
#include "entity/components/orbit-constraint-component.hpp"
#include "entity/components/model-component.hpp"
#include "entity/components/terrain-patch-component.hpp"
#include "entity/entity-manager.hpp"
@ -540,6 +542,25 @@ void Game::setup()
systemManager->addSystem(constraintSystem);
systemManager->addSystem(renderSystem);
// Create focus entity
focusEntity = createInstance();
componentManager->addComponent(focusEntity, new TransformComponent());
setTranslation(focusEntity, {0, 0, 0});
// Create camera entity
cameraEntity = createInstance();
CameraComponent* cameraComponent = new CameraComponent();
cameraComponent->camera = &camera;
TransformComponent* transformComponent = new TransformComponent();
OrbitConstraintComponent* orbitConstraintComponent = new OrbitConstraintComponent();
orbitConstraintComponent->target = 0;
orbitConstraintComponent->elevation = 0.0f;
orbitConstraintComponent->azimuth = 0.0f;
orbitConstraintComponent->distance = 0.0f;
componentManager->addComponent(cameraEntity, cameraComponent);
componentManager->addComponent(cameraEntity, transformComponent);
componentManager->addComponent(cameraEntity, orbitConstraintComponent);
// Load navmesh
TriangleMesh* navmesh = resourceManager->load<TriangleMesh>("sidewalk.mesh");

+ 2
- 0
src/game.hpp View File

@ -477,6 +477,8 @@ public:
Camera camera;
Camera sunlightCamera;
Camera uiCamera;
EntityID focusEntity;
EntityID cameraEntity;
// Animation
Animator animator;

+ 12
- 12
src/states/title-state.cpp View File

@ -22,6 +22,10 @@
#include "menu.hpp"
#include "debug/command-interpreter.hpp"
#include "game/camera-rig.hpp"
#include "entity/component-manager.hpp"
#include "entity/components/transform-component.hpp"
#include "entity/components/camera-component.hpp"
#include "entity/components/orbit-constraint-component.hpp"
void Game::enterTitleState()
{
@ -31,18 +35,14 @@ void Game::enterTitleState()
setTranslation(antHill, antHillTranslation);
// Setup camera
cameraRig = orbitCam;
orbitCam->setTargetFocalPoint(antHillTranslation);
orbitCam->setTargetFocalDistance(0.0f);
orbitCam->setTargetElevation(glm::radians(80.0f));
orbitCam->setTargetAzimuth(0.0f);
orbitCam->setFocalPoint(orbitCam->getTargetFocalPoint());
orbitCam->setFocalDistance(orbitCam->getTargetFocalDistance());
orbitCam->setElevation(orbitCam->getTargetElevation());
orbitCam->setAzimuth(orbitCam->getTargetAzimuth());
float fov = glm::radians(30.0f);
orbitCam->getCamera()->setPerspective(fov, (float)w / (float)h, 1.0f, 1000.0f);
float fov = radians(30.0f);
CameraComponent* cameraComponent = componentManager->getComponent<CameraComponent>(cameraEntity);
cameraComponent->camera->setPerspective(fov, (float)w / (float)h, 1.0f, 1000.0f);
OrbitConstraintComponent* orbitConstraintComponent = componentManager->getComponent<OrbitConstraintComponent>(cameraEntity);
orbitConstraintComponent->target = focusEntity;
orbitConstraintComponent->elevation = radians(45.0f);
orbitConstraintComponent->azimuth = 0.0f;
orbitConstraintComponent->distance = 40.0f;
// Begin fade-in
fadeIn(6.0f, {0, 0, 0}, nullptr);

Loading…
Cancel
Save