Skip to content

Commit

Permalink
More code cleanup. Remove std::vector. Better level
Browse files Browse the repository at this point in the history
  • Loading branch information
wivlaro committed Dec 14, 2024
1 parent 4a07718 commit 10d9503
Show file tree
Hide file tree
Showing 30 changed files with 269 additions and 608 deletions.
Binary file modified models/levels-src/level-1.blend
Binary file not shown.
Binary file modified models/levels-src/level-1.blend1
Binary file not shown.
Binary file modified models/levels/level-1.glb
Binary file not shown.
5 changes: 0 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ add_executable(MagnumGameApp MagnumGameApp.cpp
Player.h
TexturedDrawable.cpp
TexturedDrawable.h
UnlitAlphaDrawable.cpp
UnlitAlphaDrawable.h
${MagnumGameApp_RESOURCES}
IEnableDrawable.h
MagnumGameApp.h
DebugLines.cpp
DebugLines.h
RandomUtil.cpp
RandomUtil.h
MagnumGameApp_init.cpp
MagnumGameApp_text.cpp
MagnumGameApp_input.cpp
MagnumGameApp_debug.cpp
Tweakables.cpp
Tweakables.h
GameState.cpp
Expand Down
26 changes: 21 additions & 5 deletions src/CameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

#include <Magnum/Math/Functions.h>
#include <Corrade/Containers/Optional.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/Trade/CameraData.h>
#include <Magnum/SceneGraph/Scene.h>

namespace MagnumGame {
CameraController::CameraController(Object3D &cameraObject, SceneGraph::Camera3D& camera)
: _cameraObject(cameraObject), _camera(camera), _targetObject{}, _distance(10) {
CameraController::CameraController(Scene3D &scene)
: _cameraObject(scene.addChild<Object3D>(nullptr))
, _camera(_cameraObject.addFeature<SceneGraph::Camera3D>())
, _targetObject{}
, _distance(10) {

_cameraObject
.translate(Vector3::zAxis(30.0f))
.rotateX(-90.0_degf);
_camera.setAspectRatioPolicy(SceneGraph::AspectRatioPolicy::Extend)
.setProjectionMatrix(Matrix4::perspectiveProjection(30.0_degf, 1.0f, 0.1f, 100.0f))
.setViewport(GL::defaultFramebuffer.viewport().size());
}

void CameraController::setupTargetFromCurrent(Object3D &target) {
Expand All @@ -25,8 +37,8 @@ namespace MagnumGame {
rotateBy(delta_yaw, delta_pitch);
}

void CameraController::loadCameraData(const Containers::Optional<Matrix4>& transformation,
const Trade::CameraData& cameraData) {
void CameraController::loadCameraData(const Containers::Optional<Matrix4> &transformation,
const Trade::CameraData &cameraData) {
if (transformation) {
_cameraObject.setTransformation(*transformation);
}
Expand All @@ -37,6 +49,10 @@ namespace MagnumGame {
cameraData.far()));
}

void CameraController::adjustZoom(float deltaY) {
_distance -= deltaY * 0.25f;
}

void CameraController::rotateBy(Deg deltaYaw, Deg deltaPitch) {
if (_targetObject == nullptr) return;

Expand Down Expand Up @@ -68,6 +84,6 @@ namespace MagnumGame {
// auto smoothHalfLife = 1.0f;
// cameraPosition = lerp(_cameraObject.transformation().translation(), cameraPosition, smoothHalfLife * deltaTime);

_cameraObject.setTransformation(Matrix4::lookAt(cameraPosition, targetPosition, {0,1,0}));
_cameraObject.setTransformation(Matrix4::lookAt(cameraPosition, targetPosition, {0, 1, 0}));
}
}
6 changes: 5 additions & 1 deletion src/CameraController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <Magnum/Platform/Sdl2Application.h>

#include "MagnumGameCommon.h"
#include <Magnum/SceneGraph/Camera.h>
#include <Magnum/SceneGraph/Object.h>
Expand All @@ -8,7 +10,7 @@
namespace MagnumGame {
class CameraController {
public:
explicit CameraController(Object3D &cameraObject, SceneGraph::Camera3D &camera);
explicit CameraController(Scene3D &camera);

void setupTargetFromCurrent(Object3D &target);

Expand All @@ -28,6 +30,8 @@ namespace MagnumGame {

void loadCameraData(const Containers::Optional<Matrix4>& transformation, const Trade::CameraData& camera_data);

void adjustZoom(float deltaY);

private:
Object3D &_cameraObject;
SceneGraph::Camera3D &_camera;
Expand Down
35 changes: 26 additions & 9 deletions src/DebugLines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,39 @@
#include "DebugLines.h"

#include <Corrade/Containers/GrowableArray.h>
#include <Magnum/Shaders/VertexColorGL.h>

namespace MagnumGame {
Containers::Array<DebugLines::DebugLinePoint> DebugLines::LineData{};
DebugLines::DebugLines(Shaders::VertexColorGL3D &shader)
: _debugLinesBuffer{GL::Buffer::TargetHint::Array}
, _debugLinesMesh{GL::MeshPrimitive::Lines}
, _shader(shader) {
_debugLinesBuffer.setData(_lineData, GL::BufferUsage::DynamicDraw);
_debugLinesMesh.setCount(static_cast<int>(_lineData.size()));

void DebugLines::Add(const Vector3 &p1, const Vector3 &p2, const Color4 &color) {
arrayAppend(LineData, DebugLinePoint{p1, color});
arrayAppend(LineData, DebugLinePoint{p2, color});
_debugLinesMesh.addVertexBuffer(_debugLinesBuffer, 0, 0, Shaders::VertexColorGL3D::Position{},
Shaders::VertexColorGL3D::Color4{});
}

void DebugLines::Add(const btVector3 &p1, const btVector3 &p2, const Color4 &color) {
arrayAppend(LineData, DebugLinePoint{Vector3(p1.x(), p1.y(), p1.z()), color});
arrayAppend(LineData, DebugLinePoint{Vector3(p2.x(), p2.y(), p2.z()), color});
void DebugLines::add(const Vector3 &p1, const Vector3 &p2, const Color4 &color) {
arrayAppend(_lineData, DebugLinePoint{p1, color});
arrayAppend(_lineData, DebugLinePoint{p2, color});
}

void DebugLines::Clear() {
arrayResize(LineData, 0);
void DebugLines::add(const btVector3 &p1, const btVector3 &p2, const Color4 &color) {
arrayAppend(_lineData, DebugLinePoint{Vector3(p1.x(), p1.y(), p1.z()), color});
arrayAppend(_lineData, DebugLinePoint{Vector3(p2.x(), p2.y(), p2.z()), color});
}

void DebugLines::clear() {
arrayResize(_lineData, 0);
}

void DebugLines::draw(const Matrix4 &transformationProjectionMatrix) {
_debugLinesBuffer.setData(_lineData, GL::BufferUsage::DynamicDraw);
_debugLinesMesh.setCount(_lineData.size());

_shader.setTransformationProjectionMatrix(transformationProjectionMatrix);
_shader.draw(_debugLinesMesh);
}
}
22 changes: 18 additions & 4 deletions src/DebugLines.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
//

#pragma once
#include <Corrade/Containers/Array.h>
#include <LinearMath/btVector3.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/Shaders/VertexColorGL.h>

namespace MagnumGame {

Expand All @@ -17,15 +21,25 @@ namespace MagnumGame {
*/
class DebugLines {
public:
explicit DebugLines(Shaders::VertexColorGL3D &shader);

void add(const Vector3 &p1, const Vector3 &p2, const Color4 &color);
void add(const btVector3& p1, const btVector3& p2, const Color4& color);
void clear();

void draw(const Matrix4 &transformationProjectionMatrix);

private:

struct DebugLinePoint {
Vector3 position;
Color4 color;
};
static Containers::Array<DebugLinePoint> LineData;
static void Add(const Vector3 &p1, const Vector3 &p2, const Color4 &color);
static void Add(const btVector3& p1, const btVector3& p2, const Color4& color);
static void Clear();

Containers::Array<DebugLinePoint> _lineData{};
GL::Buffer _debugLinesBuffer;
GL::Mesh _debugLinesMesh;
Shaders::VertexColorGL3D& _shader;
};

}
9 changes: 2 additions & 7 deletions src/GameAssets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace MagnumGame {

GameAssets::GameAssets(Trade::AbstractImporter& importer) {

_unlitAlphaShader = Shaders::FlatGL3D{Shaders::FlatGL3D::Configuration{}.setFlags(Shaders::FlatGL3D::Flag::Textured)};

_texturedShader = Shaders::PhongGL{Shaders::PhongGL::Configuration{}
.setFlags(Shaders::PhongGL::Flag::DiffuseTexture | Shaders::PhongGL::Flag::ObjectId )};
_texturedShader.setAmbientColor(0x111111_rgbf)
Expand All @@ -40,7 +38,6 @@ namespace MagnumGame {
.setLightPositions({{10.0f, 15.0f, 5.0f, 0.0f}});

_vertexColorShader = Shaders::VertexColorGL3D{};
_flatShader = Shaders::FlatGL3D{};

_playerAsset = loadAnimatedModel(importer, "characters/character-female-b.glb");
}
Expand Down Expand Up @@ -73,10 +70,8 @@ namespace MagnumGame {
}
}
if (outTexture) {
auto matOpt = gltfImporter.material(meshMat.second());
if (matOpt) {
auto textureId = matOpt->findAttribute<UnsignedInt>(Trade::MaterialAttribute::BaseColorTexture);
if (textureId) {
if (auto matOpt = gltfImporter.material(meshMat.second())) {
if (auto textureId = matOpt->findAttribute<UnsignedInt>(Trade::MaterialAttribute::BaseColorTexture)) {
Debug{} << "loadModel Material" << materialName << "has base color texture" << *textureId
<< "with name" << gltfImporter.textureName(*textureId);
if (auto textureOpt = gltfImporter.texture(*textureId)) {
Expand Down
6 changes: 2 additions & 4 deletions src/GameAssets.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace MagnumGame {
static Containers::Array<GL::Texture2D> loadTextures(Trade::AbstractImporter &importer);
static Containers::Array<MaterialAsset> loadMaterials(Trade::AbstractImporter &importer, Containers::Array<GL::Texture2D>& textures);

Containers::Pointer<AnimatorAsset> loadAnimatedModel(Trade::AbstractImporter &importer,
Containers::StringView fileName);
static Containers::Pointer<AnimatorAsset> loadAnimatedModel(Trade::AbstractImporter &importer,
Containers::StringView fileName);

btCapsuleShape& getPlayerShape() { return _bPlayerShape; }

Expand All @@ -36,9 +36,7 @@ namespace MagnumGame {

Shaders::PhongGL _texturedShader{NoCreate};
Shaders::PhongGL _animatedTexturedShader{NoCreate};
Shaders::FlatGL3D _unlitAlphaShader{NoCreate};
Shaders::VertexColorGL3D _vertexColorShader{NoCreate};
Shaders::FlatGL3D _flatShader{NoCreate};

btStaticPlaneShape _bGroundShape{{0,1,0},0};
btCapsuleShape _bPlayerShape{0.125, 0.5};
Expand Down
59 changes: 37 additions & 22 deletions src/GameState.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "GameState.h"

#include <sstream>
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/Path.h>
#include <Corrade/Utility/String.h>
Expand All @@ -11,13 +12,16 @@
#include <Magnum/Trade/SceneData.h>
#include <Magnum/GL/Renderer.h>
#include <Corrade/Containers/StructuredBindings.h>
#include <Magnum/DebugTools/ObjectRenderer.h>

#include "GameAssets.h"
#include "OnGroundQuery.h"
#include "Player.h"

namespace MagnumGame {
GameState::GameState(Timeline& timeline, GameAssets& assets) : _timeline(timeline), _assets(assets) {
GameState::GameState(const Timeline& timeline, GameAssets& assets)
: _timeline(timeline)
, _assets(assets) {
_bSphereQueryObject.setCollisionShape(&_bSphereQueryShape);

_bWorld.setGravity({0.0f, -10.0f, 0.0f});
Expand All @@ -26,16 +30,9 @@ namespace MagnumGame {
_debugDraw.setMode(BulletIntegration::DebugDraw::Mode::DrawWireframe);
_bWorld.setDebugDrawer(&_debugDraw);

_cameraController.emplace(_scene);

auto& cameraObject = _scene.addChild<Object3D>(nullptr)
.translate(Vector3::zAxis(30.0f))
.rotateX(-90.0_degf);
auto& camera = cameraObject.addFeature<SceneGraph::Camera3D>();
camera.setAspectRatioPolicy(SceneGraph::AspectRatioPolicy::Extend)
.setProjectionMatrix(Matrix4::perspectiveProjection(30.0_degf, 1.0f, 0.1f, 100.0f))
.setViewport(GL::defaultFramebuffer.viewport().size());

_cameraController.emplace(cameraObject, camera);
_debugResourceManager.set(DebugRendererGroup, DebugTools::ObjectRendererOptions{}.setSize(1.f));
}

void GameState::setControl(Vector2 controlVector) {
Expand All @@ -56,13 +53,17 @@ namespace MagnumGame {
return;
}
}
_levelShapes.clear();
_levelMeshes.clear();

arrayRemove(_levelShapes, 0, _levelShapes.size());
arrayRemove(_levelMeshes, 0, _levelMeshes.size());
arrayReserve(_levelShapes, importer.meshCount());
arrayReserve(_levelMeshes, importer.meshCount());

_meshToShapeMap.clear();

std::vector<UnsignedInt> meshToMeshCollider{};
Containers::Array<UnsignedInt> meshToMeshCollider{NoInit, importer.meshCount()};
for (auto meshId = 0U; meshId < importer.meshCount(); meshId++) {
meshToMeshCollider.push_back(meshId);
meshToMeshCollider[meshId] = meshId;
}

Debug{} << "Meshes:" << importer.meshCount();
Expand All @@ -83,25 +84,25 @@ namespace MagnumGame {
debug << "is a standalone collider";
}
auto meshPositions = meshData->positions3DAsArray();
_levelShapes.emplace_back(InPlaceInit, meshPositions.data()->data(),
static_cast<int>(meshPositions.size()), sizeof(Vector3));
_levelMeshes.emplace_back(InPlaceInit, NoCreate);
meshToMeshCollider[meshId] = normalMeshId;
arrayAppend(_levelShapes, InPlaceInit, InPlaceInit, meshPositions.data()->data(), static_cast<int>(meshPositions.size()), sizeof(Vector3));
arrayAppend(_levelMeshes, InPlaceInit, InPlaceInit, NoCreate);
} else {
auto colliderName = meshName + colliderSuffix;
auto colliderId = importer.meshForName(colliderName);
bool hasCollider = colliderId != -1;
if (hasCollider) {
meshToMeshCollider[meshId] = colliderId;
_levelShapes.emplace_back(InPlaceInit);
arrayAppend(_levelShapes, InPlaceInit);
debug << "has a collider" << colliderId << colliderName;
} else {
auto meshPositions = meshData->positions3DAsArray();
_levelShapes.emplace_back(InPlaceInit, meshPositions.data()->data(),
arrayAppend(_levelShapes, InPlaceInit, InPlaceInit, meshPositions.data()->data(),
static_cast<int>(meshPositions.size()), sizeof(Vector3));
debug << "has no explicit collider, creating from mesh";
}
[[maybe_unused]]
auto& mesh = _levelMeshes.emplace_back(InPlaceInit, MeshTools::compile(*meshData));
auto& mesh = arrayAppend(_levelMeshes, InPlaceInit, InPlaceInit, MeshTools::compile(*meshData));
#ifndef MAGNUM_TARGET_WEBGL
mesh->setLabel(meshName);
#endif
Expand Down Expand Up @@ -196,7 +197,9 @@ namespace MagnumGame {
animationOffset.setTransformation(Matrix4::translation({0, -0.4f, 0}));

//Prevent rotation in X & Z
rigidBody->rigidBody().setAngularFactor(btVector3(0.0f, 1.0f, 0.0f));
rigidBody->rigidBody().setAngularFactor(btVector3(0.0f, 0.5f, 0.0f));
Debug{} << "Friction: " << rigidBody->rigidBody().getFriction() << "rolling friction" << rigidBody->rigidBody().getRollingFriction();
rigidBody->rigidBody().setRollingFriction(0.5f);

_player.emplace("Someone", rigidBody, animator);
_player->resetToStart(Matrix4::translation({0,2,0}));
Expand All @@ -208,6 +211,10 @@ namespace MagnumGame {
void GameState::renderDebug(const Matrix4 &transformationProjectionMatrix) {
_debugDraw.setTransformationProjectionMatrix(transformationProjectionMatrix);
_bWorld.debugDrawWorld();

if (!_debugDrawables.isEmpty()) {
_cameraController->draw(_debugDrawables);
}
}

void GameState::start() {
Expand All @@ -217,14 +224,22 @@ namespace MagnumGame {
void GameState::update() {
if (_player) _player->update(_timeline.previousFrameDuration());


_bWorld.stepSimulation(_timeline.previousFrameDuration(), 5);

if (_cameraController) {
_cameraController->update(_timeline.previousFrameDuration());
}

//Doesn't actually draw, but updates the bone matrices
_cameraController->draw(_animatorDrawables);
}


void GameState::addDebugDrawable(SceneGraph::AbstractObject3D &playerRigidBody) {
//This is a feature added to playerRigidBody - it is deleted with that. Not nice to allocate in app code and delete in library code.
new DebugTools::ObjectRenderer3D{
_debugResourceManager, playerRigidBody, DebugRendererGroup, &_debugDrawables
};
}

}
Loading

0 comments on commit 10d9503

Please sign in to comment.