Skip to content

Commit

Permalink
Control vector refactor. Light tweaker
Browse files Browse the repository at this point in the history
  • Loading branch information
wivlaro committed Dec 13, 2024
1 parent 2fcce93 commit 805787c
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 53 deletions.
17 changes: 4 additions & 13 deletions src/MagnumGameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace MagnumGame {
.setSpecularColor(0x33000000_rgbaf)
.setLightPositions({{10.0f, 15.0f, 5.0f, 0.0f}});

;

_animatedTexturedShader = Shaders::PhongGL{Shaders::PhongGL::Configuration{}
.setJointCount(16, 4)
.setFlags(Shaders::PhongGL::Flag::DiffuseTexture | Shaders::PhongGL::Flag::ObjectId | Shaders::PhongGL::Flag::DynamicPerVertexJointCount)};
Expand Down Expand Up @@ -158,17 +158,8 @@ namespace MagnumGame {

Vector2 controlVector = getPlayerControlVector();

Vector3 cameraControlVector;
if (!controlVector.isZero()) {
cameraControlVector = _cameraObject->absoluteTransformationMatrix().transformVector({ controlVector.x(), 0, -controlVector.y() });
cameraControlVector.y() = 0;
cameraControlVector = cameraControlVector.normalized();
}
else {
cameraControlVector = {};
}
_gameState->getPlayer()->setControl(cameraControlVector);

auto cameraObjectMatrix = _cameraObject->absoluteTransformationMatrix();
_gameState->getPlayer()->setControl(controlVector, cameraObjectMatrix);

_gameState->update();

Expand Down Expand Up @@ -212,7 +203,7 @@ namespace MagnumGame {
_timeline.nextFrame();
redraw();

{ GL::Renderer::Error err; while ((err = GL::Renderer::error()) != GL::Renderer::Error::NoError) { Error() << __FILE__ << ":" << __LINE__ << "Error: " << err; } }
CHECK_GL_ERROR(__FILE__, __LINE__);
}


Expand Down
22 changes: 20 additions & 2 deletions src/MagnumGameApp_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Magnum/Trade/MaterialData.h>
#include <Magnum/Trade/MeshData.h>
#include <Magnum/Trade/SceneData.h>
#include <Magnum/Trade/LightData.h>
#include <Magnum/Trade/TextureData.h>
#include <Magnum/Trade/CameraData.h>
#include <Magnum/Trade/AnimationData.h>
Expand Down Expand Up @@ -48,7 +49,7 @@ namespace MagnumGame {
_playerBody = loadedModel.first();
_playerAnimator = loadedModel.second();
loadLevel(*gltfImporter);
_playerBody->translate({0,1,0});
_playerBody->translate({0,2,0});

_trackingCamera.emplace(*_cameraObject);

Expand Down Expand Up @@ -196,7 +197,7 @@ namespace MagnumGame {
Debug{} << "\tObject" << objectId << objectName;

for (auto cameraId: sceneData->camerasFor(objectId)) {
if (auto cameraData = importer.camera(cameraId)) {\
if (auto cameraData = importer.camera(cameraId)) {
if (auto matrix = sceneData->transformation3DFor(objectId)) {
_cameraObject->setTransformation(*matrix);
}
Expand All @@ -209,6 +210,23 @@ namespace MagnumGame {
}
}

for (auto light : sceneData->lightsFor(objectId)) {
auto lightData = importer.light(light);

Debug debug{};
debug << "\tLight" << objectId << light
<< lightData->type()
<< "atten" << lightData->attenuation()
<< "color" << lightData->color()
<< "intensity" << lightData->intensity()
<< "range" <<lightData->range()
<< "cone angles" << lightData->innerConeAngle() << lightData->outerConeAngle();

if (auto matrix = sceneData->transformation3DFor(objectId)) {
debug << "\tTransformation" << matrix->translation();
}
}

if (Utility::String::endsWith(objectName, colliderSuffix)) {
continue;
}
Expand Down
7 changes: 4 additions & 3 deletions src/MagnumGameApp_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ namespace MagnumGame {

_camera->draw(_debugDrawables);

_debugTextRenderer->AbstractRenderer::render(debugText);
auto textMatrix = Matrix3::translation(Vector2{windowSize()}*Vector2{-0.45f, 0.45f});
renderTextBuffer(textMatrix, 0xffffff_rgbf, 0x111111_rgbf, _debugTextRenderer->mesh());
std::tie(_textMesh, std::ignore) =
Text::Renderer2D::render(*_font, _fontGlyphCache, fontSmallSize, debugText, _textVertexBuffer, _textIndexBuffer, GL::BufferUsage::DynamicDraw, Text::Alignment::BottomLeft);
auto textMatrix = Matrix3::translation(Vector2{windowSize()}*Vector2{-0.45f, -0.45f});
renderTextBuffer(textMatrix, 0xffffff_rgbf, 0x111111_rgbf, _textMesh);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ namespace MagnumGame {

Vector3 Player::getPosition() const { return _pBody->transformation().translation(); }

void Player::setControl(Vector2 controlVector, const Math::Matrix4<Float> &cameraObjectMatrix) {

Vector3 cameraControlVector;
if (!controlVector.isZero()) {
cameraControlVector = cameraObjectMatrix.transformVector({ controlVector.x(), 0, -controlVector.y() });
cameraControlVector.y() = 0;
cameraControlVector = cameraControlVector.normalized();
}
else {
cameraControlVector = {};
}
setControl(cameraControlVector);
}

void Player::setControl(const Vector3 &control) {
if (_control != control) {
_control = control;
Expand Down
1 change: 1 addition & 0 deletions src/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace MagnumGame {

btVector3 hitPosition() const;

void setControl(Vector2 controlVector, const Math::Matrix4<Float>& cameraObjectMatrix);
void setControl(const Vector3& control);

void die();
Expand Down
7 changes: 7 additions & 0 deletions src/TexturedDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ namespace MagnumGame {
_shader.setProjectionMatrix(camera.projectionMatrix());
_shader.setTransformationMatrix(transformation);
_shader.setDiffuseColor(_color);
// _shader.setLightRanges({Constants::inf()});
_shader.setAmbientColor({ambientColour, ambientColour, ambientColour, 1.0f});
_shader.setLightColor({lightColour, lightColour, lightColour, 1.0f});
_shader.setSpecularColor(_color);
_shader.setShininess(shininess);
_shader.setSpecularColor({specular, specular,specular,1.0f});
_shader.setLightPositions({object().absoluteTransformationMatrix().inverted() * Vector4{lightDirection, 0.0f}.normalized()});
if (_texture) {
_shader.bindDiffuseTexture(*_texture);
{ GL::Renderer::Error err; while ((err = GL::Renderer::error()) != GL::Renderer::Error::NoError) { Error() << ("TexturedDrawable.cpp" ":" "67") << "Error: " << err << _texture << _texture->id(); } };
Expand Down
8 changes: 8 additions & 0 deletions src/TexturedDrawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ namespace MagnumGame {

btVector3 getPosition() const override;

void setWorldLightDirection(Quaternion direction);


void setSkin(Skin& skin, UnsignedInt perVertexJointCount, UnsignedInt secondaryPerVertexJointCount);

static inline float ambientColour = 0.2f;
static inline float lightColour = 0.8f;
static inline float shininess = 0.6f;
static inline float specular = 0.6f;
static inline Vector3 lightDirection = {0.1f, 0.9f, 0.1f};

private:
void draw(const Matrix4 &transformation, SceneGraph::Camera3D &) override;
static GL::Texture2D&& makeTexture(const Trade::ImageData2D&);
Expand Down
9 changes: 9 additions & 0 deletions src/Tweakables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ namespace MagnumGame {
TweakableValue{"Min distance", &MagnumGameApp::cameraMinDistance}
}});

_debugModes.emplace_back(DebugMode{"Lights", 0, std::vector{
TweakableValue{"Ambient", &TexturedDrawable::ambientColour},
TweakableValue{"Directional", &TexturedDrawable::lightColour},
TweakableValue{"Shininess", &TexturedDrawable::shininess},
TweakableValue{"Specular", &TexturedDrawable::specular},
TweakableValue{"Direction x", &TexturedDrawable::lightDirection.x()},
TweakableValue{"Direction y", &TexturedDrawable::lightDirection.y()},
TweakableValue{"Direction z", &TexturedDrawable::lightDirection.z()},
}});
}


Expand Down
90 changes: 55 additions & 35 deletions src/Tweakables.h
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
#pragma once
#include <functional>
#include <utility>
#include <vector>
#include <Corrade/Containers/Pointer.h>

namespace MagnumGame {

class Tweakables {
public:
Tweakables();

/**
* @brief Named pointer for tweaking a configurable float value at runtime
*/
struct TweakableValue {
const char* name;
float* pValue;
class Tweakables {
public:
Tweakables();

/**
* @brief Named pointer for tweaking a configurable float value at runtime
*/
class TweakableValue {
public:
const char *name;

TweakableValue(const char *name, float *value)
: TweakableValue{
name,
[value]() { return *value; },
[value](float v) { *value = v; }
} {
}

TweakableValue(const char *name,
std::function<float()> getter,
std::function<void(float)> setter)
: name(name), getter(std::move(getter)), setter(std::move(setter)) {
}

private:
std::function<void(float)> setter;
std::function<float()> getter;
};

/**
* @brief Group of tweakable values for tuning
*/
struct DebugMode {
const char *modeName;
size_t currentTweakIndex = 0;
std::vector<TweakableValue> tweakableValues;
};

const DebugMode &currentDebugMode() const { return _debugModes[_currentDebugModeIndex]; }
DebugMode &currentDebugMode() { return _debugModes[_currentDebugModeIndex]; }
bool hasActiveDebugMode() const { return _currentDebugModeIndex > 0; }

void addDebugMode(const char *modeName, size_t initialIndex, std::vector<TweakableValue> &&tweakableValues);

std::string getDebugText() const;

void changeDebugModeIndex(int delta);

private:
std::vector<DebugMode> _debugModes;
int _currentDebugModeIndex = 0;
};

/**
* @brief Group of tweakable values for tuning
*/
struct DebugMode {
const char* modeName;
size_t currentTweakIndex = 0;
std::vector<TweakableValue> tweakableValues;
};
const DebugMode& currentDebugMode() const { return _debugModes[_currentDebugModeIndex]; }
DebugMode& currentDebugMode() { return _debugModes[_currentDebugModeIndex]; }
bool hasActiveDebugMode() const {return _currentDebugModeIndex > 0;}

void addDebugMode(const char* modeName, size_t initialIndex, std::vector<TweakableValue>&& tweakableValues);
std::string getDebugText() const;

void changeDebugModeIndex(int delta);

private:
std::vector<DebugMode> _debugModes;
int _currentDebugModeIndex = 0;

};

}

0 comments on commit 805787c

Please sign in to comment.