Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DirectX12FromScratch/DirectX12FromScratch.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
<ClCompile Include="src\MiniEngine\SysClock.cpp" />
<ClCompile Include="src\MiniEngine\Time.cpp" />
<ClCompile Include="src\MiniEngine\Viewport.cpp" />
<ClCompile Include="src\Mouse.cpp" />
</ItemGroup>
<ItemGroup>
<FxCompile Include="Assets\shaders\camera.hlsl">
Expand Down
3 changes: 0 additions & 3 deletions DirectX12FromScratch/DirectX12FromScratch.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,6 @@
<ClCompile Include="src\MiniEngine\D3D12\D3D12RenderableModel.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="src\Mouse.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="src\MiniEngine\Material.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
Expand Down
9 changes: 1 addition & 8 deletions DirectX12FromScratch/include/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ class Event
int y;
};

struct MouseWheelEvent
{
int delta;
int x;
int y;
};

struct MouseWheelScrollEvent
{
Expand All @@ -82,14 +76,14 @@ class Event

enum EventType
{
Unknown,
Closed,
Resized,
LostFocus,
GainedFocus,
TextEntered,
KeyPressed,
KeyReleased,
MouseWheelMoved,
MouseWheelScrolled,
MouseButtonPressed,
MouseButtonReleased,
Expand Down Expand Up @@ -119,7 +113,6 @@ class Event
TextEvent text;
MouseMoveEvent mouseMove;
MouseButtonEvent mouseButton;
MouseWheelEvent mouseWheel;
MouseWheelScrollEvent mouseWheelScroll;
};
};
2 changes: 2 additions & 0 deletions DirectX12FromScratch/include/MainApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class MainApplication : public MiniEngine::Application
MiniEngine::SceneManager *_sceneManager;
MiniEngine::Camera *_camera;
MiniEngine::SceneNode *_node;

bool _hasFocus;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why _hasFocus is here? It's better to have a method in Window for that, don't you think?

};
8 changes: 4 additions & 4 deletions DirectX12FromScratch/include/MiniEngine/Quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ namespace MiniEngine
bool operator==(const Quaternion<T>& rhs) const
{
const Quaternion<T>& lhs = *this;
return (std::fabs(lhs.w - rhs.w) < EPSILON) && lhs.v == rhs.v;
return (std::fabs(lhs.w - rhs.w) < FLT_EPSILON) && lhs.v == rhs.v;
}

/**
Expand Down Expand Up @@ -492,7 +492,7 @@ namespace MiniEngine

T tr, s;
tr = m(1, 1) + m(2, 2) + m(3, 3);
if (tr >= epsilon)
if (tr >= FLT_EPSILON)
{
s = 0.5 / (T)sqrt(tr + 1.0);
q.w = 0.25 / s;
Expand Down Expand Up @@ -550,14 +550,14 @@ namespace MiniEngine
Quaternion<T> ret;
T cosTheta = w * q2.w + v.x * q2.v.x + v.y * q2.v.y + v.z * q2.v.z;
T theta = (T)acos(cosTheta);
if (fabs(theta) < epsilon)
if (fabs(theta) < FLT_EPSILON)
{
ret = *this;
}
else
{
T sinTheta = (T)sqrt(1.0 - cosTheta * cosTheta);
if (fabs(sinTheta) < epsilon)
if (fabs(sinTheta) < FLT_EPSILON)
{
ret.w = 0.5 * w + 0.5 * q2.w;
ret.v = v.lerp(0.5, q2.v);
Expand Down
2 changes: 2 additions & 0 deletions DirectX12FromScratch/include/MiniEngine/SceneNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace MiniEngine
Quatf &getDerivedRotation();
Vector3f &getDerivedScaling();

void setRotationMatrix(Matrix3f &m);

virtual void rotate(float w, Vector3f const &v, TransformSpace space = TS_LOCAL);
virtual void translate(Vector3f const &v, TransformSpace space = TS_LOCAL);
virtual void scale(Vector3f const &v);
Expand Down
12 changes: 7 additions & 5 deletions DirectX12FromScratch/include/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "Vector.h"

class Window;

////////////////////////////////////////////////////////////
/// \brief Give access to the real-time state of the mouse
///
Expand Down Expand Up @@ -67,7 +69,7 @@ class Mouse
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isButtonPressed(Button button);
virtual bool isButtonPressed(Button button) = 0;

////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
Expand All @@ -78,7 +80,7 @@ class Mouse
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getPosition();
virtual Vector2i getPosition() = 0;

////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
Expand All @@ -91,7 +93,7 @@ class Mouse
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
//static Vector2i getPosition(const Window& relativeTo);
virtual Vector2i getPosition(const Window& relativeTo) = 0;

////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
Expand All @@ -102,7 +104,7 @@ class Mouse
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setPosition(const Vector2i& position);
virtual void setPosition(const Vector2i& position) = 0;

////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
Expand All @@ -114,6 +116,6 @@ class Mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
//static void setPosition(const Vector2i& position, const Window& relativeTo);
virtual void setPosition(const Vector2i& position, const Window& relativeTo) = 0;
};

5 changes: 4 additions & 1 deletion DirectX12FromScratch/include/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class Window

virtual bool getEvent(Event &) = 0;

virtual bool isKeyPressed(Keyboard::Key key) = 0;
virtual Keyboard *getKeyboard() = 0;
virtual Mouse *getMouse() = 0;

virtual void setMouseCursorVisible(bool) = 0;
private:
};

61 changes: 46 additions & 15 deletions DirectX12FromScratch/src/MainApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "MiniEngine/D3D12/D3D12RenderSystem.h"
#include "MiniEngine/D3D12/D3D12RenderWindow.h"

MainApplication::MainApplication(const std::string &windowType, HINSTANCE hInstance) : MiniEngine::Application(), _window(nullptr)
MainApplication::MainApplication(const std::string &windowType, HINSTANCE hInstance) : MiniEngine::Application(), _window(nullptr), _hasFocus(true)
{
float clearColor[4] = { 0.2f, 0.2f, 0.2f, 0.0f };

Expand Down Expand Up @@ -49,6 +49,9 @@ MainApplication::MainApplication(const std::string &windowType, HINSTANCE hInsta
_node = _sceneManager->getRootNode()->createChild(_root->getRenderSystem()->loadModel("./Assets/models/teapot.txt"));
}
}

_window->getMouse()->setPosition({(int)(_window->getWidth() / 2), (int)(_window->getHeight() / 2) }, *_window);
_window->setMouseCursorVisible(false);
}
}

Expand Down Expand Up @@ -100,28 +103,43 @@ bool MainApplication::update(MiniEngine::Time elapsedTime)
return (false);

float elapsedSeconds = elapsedTime.getSeconds();
static Vector2f windowSize = Vector2f(_window->getWidth(), _window->getHeight());
static Vector2f lastPos = _window->getMouse()->getPosition(*_window);

if (_hasFocus) {
Vector2f mousePos = _window->getMouse()->getPosition(*_window);
Vector2f deltaPos = mousePos - lastPos;
if (lastPos != mousePos)
{
lastPos = mousePos;

if (_window->isKeyPressed(Keyboard::Left))
_camera->rotate(100 * elapsedSeconds, Vector3f(0, 1, 0), MiniEngine::TS_WORLD);
if (_window->isKeyPressed(Keyboard::Right))
_camera->rotate(100 * elapsedSeconds, Vector3f(0, -1, 0), MiniEngine::TS_WORLD);
if (_window->isKeyPressed(Keyboard::Up))
_camera->rotate(100 * elapsedSeconds, Vector3f(1, 0, 0), MiniEngine::TS_LOCAL);
if (_window->isKeyPressed(Keyboard::Down))
_camera->rotate(100 * elapsedSeconds, Vector3f(-1, 0, 0), MiniEngine::TS_LOCAL);
_camera->rotate(15 * elapsedSeconds * -deltaPos.x, Vector3f(0, 1, 0), MiniEngine::TS_WORLD);
_camera->rotate(15 * elapsedSeconds * -deltaPos.y, Vector3f(1, 0, 0), MiniEngine::TS_LOCAL);

if (_window->isKeyPressed(Keyboard::Z))
if (lastPos.x < windowSize.x * 0.05 || lastPos.x > windowSize.x * 0.95
|| lastPos.y < windowSize.y * 0.05 || lastPos.y > windowSize.y * 0.95)
{
Vector2f middle = Vector2f(windowSize.x / 2, windowSize.y / 2);
_window->setMouseCursorVisible(true);
_window->getMouse()->setPosition(middle, *_window);
_window->setMouseCursorVisible(false);
lastPos = middle;
}
}
}

if (_window->getKeyboard()->isKeyPressed(Keyboard::Z))
_camera->translate(Vector3f(0, 0, -100 * elapsedSeconds), MiniEngine::TS_LOCAL);
if (_window->isKeyPressed(Keyboard::S))
if (_window->getKeyboard()->isKeyPressed(Keyboard::S))
_camera->translate(Vector3f(0, 0, 100 * elapsedSeconds), MiniEngine::TS_LOCAL);
if (_window->isKeyPressed(Keyboard::Q))
if (_window->getKeyboard()->isKeyPressed(Keyboard::Q))
_camera->translate(Vector3f(-100 * elapsedSeconds, 0, 0), MiniEngine::TS_LOCAL);
if (_window->isKeyPressed(Keyboard::D))
if (_window->getKeyboard()->isKeyPressed(Keyboard::D))
_camera->translate(Vector3f(100 * elapsedSeconds, 0, 0), MiniEngine::TS_LOCAL);

if (_window->isKeyPressed(Keyboard::Space))
if (_window->getKeyboard()->isKeyPressed(Keyboard::Space))
_camera->translate(Vector3f(0, 100 * elapsedSeconds, 0), MiniEngine::TS_WORLD);
if (_window->isKeyPressed(Keyboard::LShift))
if (_window->getKeyboard()->isKeyPressed(Keyboard::LShift))
_camera->translate(Vector3f(0, -100 * elapsedSeconds, 0), MiniEngine::TS_WORLD);

Event event;
Expand All @@ -135,13 +153,26 @@ bool MainApplication::update(MiniEngine::Time elapsedTime)
_window->destroy();
return (false);
}
else if (event.key.code == Keyboard::LAlt && _hasFocus)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as _hasFocus

{
_window->setMouseCursorVisible(true);
_hasFocus = false;
}
}

if (event.type == Event::Closed)
{
_window->destroy();
return (false);
}

if (event.type == Event::MouseButtonPressed && !_hasFocus)
{
_window->setMouseCursorVisible(false);
_hasFocus = true;
Vector2f middle((int)(_window->getWidth() / 2), (int)(_window->getHeight() / 2));
_window->getMouse()->setPosition(middle, *_window);
}
}

return (true);
Expand Down
8 changes: 8 additions & 0 deletions DirectX12FromScratch/src/MiniEngine/SceneNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ Matrix4f &SceneNode::getTransformationMatrix()
return (_transform);
}

void SceneNode::setRotationMatrix(Matrix3f &m)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a TransformSpace?

{
Quatf q = Quatf::fromMatrix(m);
q.normalize();

_rotation = q;
}

void MiniEngine::SceneNode::rotate(float w, Vector3f const &v, TransformSpace space)
{
Quatf q = Quatf::fromAxisRot(v, w);
Expand Down
15 changes: 0 additions & 15 deletions DirectX12FromScratch/src/Mouse.cpp

This file was deleted.

8 changes: 4 additions & 4 deletions WINDOW_SFML/Keyboard_SFML.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <map>
#include <unordered_map>
#include <SFML/Window/Keyboard.hpp>
#include "Keyboard.h"

class Keyboard_SFML : Keyboard
class Keyboard_SFML : public Keyboard
{
public:
Keyboard_SFML();
Expand All @@ -14,6 +14,6 @@ class Keyboard_SFML : Keyboard
Keyboard::Key fromNative(sf::Keyboard::Key);

private:
std::map<sf::Keyboard::Key, Keyboard::Key> _fromNative;
std::map<Keyboard::Key, sf::Keyboard::Key> _toNative;
std::unordered_map<sf::Keyboard::Key, Keyboard::Key> _fromNative;
std::unordered_map<Keyboard::Key, sf::Keyboard::Key> _toNative;
};
Loading