Restore pixel perfect camera code.

This commit is contained in:
2024-11-25 20:59:29 -06:00
parent a2c841288d
commit de55029356
19 changed files with 245 additions and 31 deletions

View File

@ -8,11 +8,11 @@
using namespace Dawn;
void SimpleComponent::onInit() {
this->initMethod(*this, events);
this->initMethod(*this, this->events);
}
void SimpleComponent::onDispose() {
for(auto &event : events) {
for(auto &event : this->events) {
event();
}
}

View File

@ -8,9 +8,6 @@
namespace Dawn {
class SimpleComponent final : public SceneComponent {
private:
std::vector<std::function<void()>> events;
public:
std::function<void(
SceneComponent&,

View File

@ -56,6 +56,20 @@ float_t Camera::getAspect() {
return rt->getWidth() / rt->getHeight();
}
float_t Camera::lookAtPixelPerfect(
const glm::vec3 &position,
const glm::vec3 &look,
const float_t &scale
) {
return this->getItem()->lookAtPixelPerfect(
position,
look,
this->getRenderTarget()->getHeight(),
this->fov,
scale
);
}
void Camera::setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) {
onResizeListener();
this->renderTarget = renderTarget;

View File

@ -57,6 +57,20 @@ namespace Dawn {
*/
glm::mat4 getProjection();
/**
* Shorthand for getItem()->lookAtPixelPerfect()
*
* @param position Position to look from.
* @param look Position to look at.
* @param scale Scale to use. At scale 1 then the centerest pixel is 1:1.
* @return The Z distance that was calculated.
*/
float_t lookAtPixelPerfect(
const glm::vec3 &position,
const glm::vec3 &look,
const float_t &scale = 1.0f
);
/**
* Sets the render target for this camera.
*

View File

@ -35,6 +35,7 @@ void IGame::init() {
void IGame::update() {
this->assetManager->update();
this->inputManager.update();
if(nextFrameScene) {
nextFrameScene->stage();

View File

@ -5,17 +5,17 @@
#pragma once
#include "util/Math.hpp"
#include "input/InputBind.hpp"
namespace Dawn {
class DawnGame;
typedef int_fast16_t inputbind_t;
template<typename T>
class IInputManager {
protected:
std::unordered_map<inputbind_t, std::vector<T>> binds;
std::unordered_map<inputbind_t, float_t> valuesLeft;
std::unordered_map<inputbind_t, float_t> valuesRight;
std::unordered_map<enum InputBind, std::vector<T>> binds;
std::unordered_map<enum InputBind, float_t> valuesLeft;
std::unordered_map<enum InputBind, float_t> valuesRight;
bool_t currentIsLeft = true;
/**
@ -34,7 +34,7 @@ namespace Dawn {
* @param bind Bind to bind the axis to.
* @param axis Axis to use for this bind.
*/
void bind(const inputbind_t bind, const T axis) {
void bind(const enum InputBind bind, const T axis) {
this->binds[bind].push_back(axis);
}
@ -43,7 +43,7 @@ namespace Dawn {
*
* @param bind Bind to remove all binds from.
*/
void unbind(const inputbind_t bind) {
void unbind(const enum InputBind bind) {
this->binds[bind].clear();
}
@ -61,7 +61,7 @@ namespace Dawn {
* @param bind Bind to get the value of.
* @return The current input state (between 0 and 1).
*/
float_t getValue(const inputbind_t bind) {
float_t getValue(const enum InputBind bind) {
if(this->currentIsLeft) {
auto exist = this->valuesLeft.find(bind);
return exist == this->valuesLeft.end() ? 0.0f : exist->second;
@ -77,7 +77,7 @@ namespace Dawn {
* @param bind Bind to get the value of.
* @return The value of the bind, last frame.
*/
float_t getValueLastUpdate(const inputbind_t bind) {
float_t getValueLastUpdate(const enum InputBind bind) {
if(this->currentIsLeft) {
auto exist = this->valuesRight.find(bind);
return exist == this->valuesRight.end() ? 0.0f : exist->second;
@ -99,15 +99,15 @@ namespace Dawn {
* @param positive Bind to use for the positive axis.
* @return A value between -1 and 1.
*/
float_t getAxis(const inputbind_t negative, const inputbind_t positive) {
float_t getAxis(const enum InputBind negative, const enum InputBind positive) {
return -getValue(negative) + getValue(positive);
}
glm::vec2 getAxis2D(
const inputbind_t negativeX,
const inputbind_t positiveX,
const inputbind_t negativeY,
const inputbind_t positiveY
const enum InputBind negativeX,
const enum InputBind positiveX,
const enum InputBind negativeY,
const enum InputBind positiveY
) {
return glm::vec2(
getAxis(negativeX, positiveX),
@ -122,7 +122,7 @@ namespace Dawn {
* @param y Y Axis bind.
* @return 2D vector of the two given input binds.
*/
glm::vec2 getAxis2D(const inputbind_t x, const inputbind_t y) {
glm::vec2 getAxis2D(const enum InputBind x, const enum InputBind y) {
return glm::vec2(getValue(x), getValue(y));
}
@ -133,7 +133,7 @@ namespace Dawn {
* @param bind Bind to check if pressed.
* @return True if value is non-zero, or false for zero.
*/
bool_t isDown(const inputbind_t bind) {
bool_t isDown(const enum InputBind bind) {
return this->getValue(bind) != 0.0f;
}
@ -144,7 +144,7 @@ namespace Dawn {
* @param bind Bind to check if pressed.
* @return True if down this frame and not down last frame.
*/
bool_t isPressed(const inputbind_t bind) {
bool_t isPressed(const enum InputBind bind) {
return this->getValue(bind) != 0 && this->getValueLastUpdate(bind) == 0;
}
@ -155,7 +155,7 @@ namespace Dawn {
* @param bind Bind to check if released.
* @return True if up this frame, and down last frame.
*/
bool_t wasReleased(const inputbind_t bind) {
bool_t wasReleased(const enum InputBind bind) {
return this->getValue(bind) == 0 && this->getValueLastUpdate(bind) != 0;
}

View File

@ -20,6 +20,7 @@ void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
sceneComponentState,
SCENE_COMPONENT_STATE_INIT
);
this->events.clear();
this->item = item;
this->onInit();
}
@ -39,6 +40,11 @@ void SceneComponent::dispose() {
sceneComponentState,
SCENE_COMPONENT_STATE_DISPOSED
);
for(auto &event : this->events) {
event();
}
this->events.clear();
this->onDispose();
this->item.reset();
}

View File

@ -20,6 +20,8 @@ namespace Dawn {
uint_fast8_t sceneComponentState = 0;
protected:
std::vector<std::function<void()>> events;
/**
* Custom component listener that is invoked when the component is meant
* to initialize.

View File

@ -168,6 +168,25 @@ void SceneItemTransform::lookAt(
this->setWorldTransform(glm::lookAt(position, target, up));
}
float_t SceneItemTransform::lookAtPixelPerfect(
const glm::vec3 &position,
const glm::vec3 &look,
const float_t &viewportHeight,
const float_t &fov,
const float_t &scale
) {
float_t z = (
tanf((glm::radians(180.0f) - fov) / 2.0f) *
(viewportHeight/2.0f)
) / scale;
this->lookAt(
glm::vec3(position.x, position.y, position.z + z),
look,
glm::vec3(0, 1, 0)
);
return z;
}
SceneItemTransform::~SceneItemTransform() {
std::for_each(
this->children.begin(),

View File

@ -164,6 +164,24 @@ namespace Dawn {
const glm::vec3 up
);
/**
* Shorthand combined for lookAt and perspectivePixelPerfectDistance
* to allow you to create pixel perfect lookAt camera view matricies.
*
* @param position Position of the camera. Z is for an offset.
* @param look Position in world space this transform looks at.
* @param viewportHeight Height of the viewport.
* @param fov Field of view (in radians).
* @return The Z distance that was calculated.
*/
float_t lookAtPixelPerfect(
const glm::vec3 &position,
const glm::vec3 &look,
const float_t &viewportHeight,
const float_t &fov,
const float_t &scale = 1.0f
);
virtual ~SceneItemTransform();
};
}