Skip to content

Commit

Permalink
PoC for flight cam
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Sep 14, 2023
1 parent 2e97581 commit 218c8af
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 35 deletions.
4 changes: 2 additions & 2 deletions samples/golf/buildnumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef BUILD_NUMBER_H_
#define BUILD_NUMBER_H_

#define BUILDNUMBER 1699
#define BUILDNUMBER_STR "1699"
#define BUILDNUMBER 1712
#define BUILDNUMBER_STR "1712"

#endif /* BUILD_NUMBER_H_ */
3 changes: 3 additions & 0 deletions samples/golf/src/golf/GameConsts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ static constexpr float SkyCamRadius = 80.f;
static constexpr float GreenCamZoomFast = 2.5f;
static constexpr float GreenCamZoomSlow = 1.8f;
static constexpr float SkyCamZoomSpeed = 1.1f;// 3.f;
static constexpr float FlightCamFOV = 90.f;
static constexpr glm::vec3 FlightCamOffset = glm::vec3(0.f, 0.098f, 0.089f);
static constexpr float FlightCamRotation = -0.158f;

static constexpr glm::uvec2 MapSize(320u, 200u);
static constexpr glm::vec2 RangeSize(200.f, 250.f);
Expand Down
23 changes: 13 additions & 10 deletions samples/golf/src/golf/GolfState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ GolfState::GolfState(cro::StateStack& stack, cro::State::Context context, Shared
ballEntity = {};

registerDebugCommands();
registerDebugWindows();
#endif
registerDebugWindows();

cro::App::getInstance().resetFrameTime();
}
Expand Down Expand Up @@ -2011,15 +2011,15 @@ void GolfState::render()
m_greenBuffer.display();
m_gameScene.setActiveCamera(oldCam);
}
//else if (m_flightCam.getComponent<cro::Camera>().active)
//{
// //update the flight view
// auto oldCam = m_gameScene.setActiveCamera(m_flightCam);
// m_flightTexture.clear();
// m_gameScene.render();
// m_flightTexture.display();
// m_gameScene.setActiveCamera(oldCam);
//}
else if (m_flightCam.getComponent<cro::Camera>().active)
{
//update the flight view
auto oldCam = m_gameScene.setActiveCamera(m_flightCam);
m_flightTexture.clear(cro::Colour::Magenta);
m_gameScene.render();
m_flightTexture.display();
m_gameScene.setActiveCamera(oldCam);
}

#ifndef CRO_DEBUG_
if (m_roundEnded /* && !m_sharedData.tutorial */)
Expand Down Expand Up @@ -7085,6 +7085,9 @@ void GolfState::updateActor(const ActorInfo& update)
};
m_gameScene.getSystem<cro::CommandSystem>()->sendCommand(cmd2);

//set this ball as the flight cam target
m_flightCam.getComponent<cro::Callback>().setUserData<cro::Entity>(e);

//if the dest point moves the ball out of a certain radius
//then set the drone cam to follow (if it's active)
if (m_currentCamera == CameraID::Sky
Expand Down
64 changes: 44 additions & 20 deletions samples/golf/src/golf/GolfStateCameras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ source distribution.
#include "GolfState.hpp"
#include "FpsCameraSystem.hpp"
#include "CommandIDs.hpp"
#include "GameConsts.hpp"

#include <crogine/ecs/components/AudioListener.hpp>
#include <crogine/ecs/components/CommandTarget.hpp>
Expand Down Expand Up @@ -392,7 +393,7 @@ void GolfState::createCameras()
//unfortuantely this kills the FPS

const auto createFlightTexture =
[&]()
[&](cro::Camera& cam)
{
auto texSize = MapSize.y / 2u;

Expand Down Expand Up @@ -429,27 +430,50 @@ void GolfState::createCameras()
//}
//greenEnt.getComponent<cro::Transform>().setOrigin({ (texSize / 2), (texSize / 2) }); //must divide to a whole pixel!
//greenEnt.getComponent<cro::Callback>().getUserData<GreenCallbackData>().targetScale = targetScale.x;

cam.setPerspective(FlightCamFOV* cro::Util::Const::degToRad, 1.f, 0.001f, static_cast<float>(MapSize.x) * 1.25f);
cam.viewport = { 0.f, 0.f, 1.f, 1.f };
};
//createFlightTexture();

////follows the ball in flight
//camEnt = m_gameScene.createEntity();
//camEnt.addComponent<cro::Transform>();
//camEnt.addComponent<cro::Camera>().resizeCallback =
// [&, createFlightTexture](cro::Camera& cam)
// {
// createFlightTexture();

// cam.setPerspective(m_sharedData.fov * cro::Util::Const::degToRad, 1.f, 0.1f, static_cast<float>(MapSize.x) * 1.25f, m_shadowQuality.cascadeCount);
// cam.viewport = { 0.f, 0.f, 1.f, 1.f };
// };
//camEnt.getComponent<cro::Camera>().reflectionBuffer.create(ReflectionMapSize, ReflectionMapSize);
//camEnt.getComponent<cro::Camera>().reflectionBuffer.setSmooth(true);
//camEnt.getComponent<cro::Camera>().shadowMapBuffer.create(ShadowMapSize, ShadowMapSize);

//follows the ball in flight
camEnt = m_gameScene.createEntity();
camEnt.addComponent<cro::Transform>();
camEnt.addComponent<cro::Camera>().resizeCallback = createFlightTexture;
/*[&, createFlightTexture](cro::Camera& cam)
{
createFlightTexture();
cam.setPerspective(FlightCamFOV * cro::Util::Const::degToRad, 1.f, 0.001f, static_cast<float>(MapSize.x) * 1.25f);
cam.viewport = { 0.f, 0.f, 1.f, 1.f };
};*/
camEnt.getComponent<cro::Camera>().reflectionBuffer.create(ReflectionMapSize / 4, ReflectionMapSize / 4);
camEnt.getComponent<cro::Camera>().reflectionBuffer.setSmooth(true);
camEnt.getComponent<cro::Camera>().shadowMapBuffer.create(ShadowMapSize / 4, ShadowMapSize / 4);
//camEnt.getComponent<cro::Camera>().active = false;
//camEnt.getComponent<cro::Camera>().setMaxShadowDistance(m_shadowQuality.shadowFarDistance);
//camEnt.getComponent<cro::Camera>().setShadowExpansion(15.f);
//m_flightCam = camEnt;
LogI << "SET CAM FALSE BY DEFAULT!" << std::endl;
camEnt.getComponent<cro::Camera>().setMaxShadowDistance(m_shadowQuality.shadowFarDistance);
camEnt.getComponent<cro::Camera>().setShadowExpansion(1.f);

camEnt.addComponent<cro::Callback>().active = true;
camEnt.getComponent<cro::Callback>().setUserData<cro::Entity>(); //this is the target ball
camEnt.getComponent<cro::Callback>().function =
[](cro::Entity e, float)
{
if (e.getComponent<cro::Camera>().active)
{
auto target = e.getComponent<cro::Callback>().getUserData<cro::Entity>();
if (target.isValid())
{
auto pos = target.getComponent<cro::Transform>().getPosition() + FlightCamOffset;
e.getComponent<cro::Transform>().setPosition(pos);

glm::quat rot = glm::rotate(cro::Transform::QUAT_IDENTITY, FlightCamRotation, cro::Transform::X_AXIS);
e.getComponent<cro::Transform>().setRotation(rot);
}
}
};
createFlightTexture(camEnt.getComponent<cro::Camera>());
m_flightCam = camEnt;
}

void GolfState::setGreenCamPosition()
Expand Down
6 changes: 3 additions & 3 deletions samples/golf/src/golf/GolfStateDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void GolfState::registerDebugWindows()
{
registerWindow([&]()
{
/*if (ImGui::Begin("Ball Cam"))
if (ImGui::Begin("Ball Cam"))
{
glm::vec2 size(m_flightTexture.getSize());
ImGui::Image(m_flightTexture.getTexture(), { size.x, size.y }, { 0.f, 1.f }, { 1.f, 0.f });
Expand All @@ -325,7 +325,7 @@ void GolfState::registerDebugWindows()
static float fov = 60.f;
if (ImGui::SliderFloat("FOV", &fov, 40.f, 90.f))
{
cam.setPerspective(fov * cro::Util::Const::degToRad, 1.f, 0.1f, static_cast<float>(MapSize.x) * 1.25f, m_shadowQuality.cascadeCount);
cam.setPerspective(fov * cro::Util::Const::degToRad, 1.f, 0.001f, static_cast<float>(MapSize.x) * 1.25f/*, m_shadowQuality.cascadeCount*/);
}

static glm::vec3 pos(0.f);
Expand All @@ -344,7 +344,7 @@ void GolfState::registerDebugWindows()
m_flightCam.getComponent<cro::Transform>().setRotation(cro::Transform::X_AXIS, rotation);
}
}
ImGui::End();*/
ImGui::End();

//if (ImGui::Begin("Ach Track"))
//{
Expand Down

0 comments on commit 218c8af

Please sign in to comment.