Skip to content

Commit

Permalink
Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
fcturan20 committed Mar 19, 2023
1 parent fe26bb4 commit 7fc52a2
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 36 deletions.
63 changes: 63 additions & 0 deletions .clang-format
@@ -0,0 +1,63 @@
---
BasedOnStyle: Microsoft
AccessModifierOffset: -1
AlignArrayOfStructures: Left
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCompound: true
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations:
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCompound: true
PadOperators: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortFunctionsOnASingleLine: All
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterUnion: true
BeforeLambdaBody: true
BreakBeforeBraces: Allman
ColumnLimit: 80
IncludeBlocks: Regroup
IncludeCategories:
- Regex: DebugNew.h
Priority: 4
- Regex: <([A-Za-z0-9\Q/-_\E])+>
Priority: 3
- Regex: <([A-Za-z0-9.\Q/-_\E])+>
Priority: 2
- Regex: '"([A-Za-z0-9.\Q/-_\E])+"'
Priority: 1
IndentPPDirectives: BeforeHash
IndentWidth: 2
NamespaceIndentation: All
PointerAlignment: Left
QualifierAlignment: Left
SeparateDefinitionBlocks: Always
SpaceAfterCStyleCast: true
SpaceBeforeCpp11BracedList: true
SpacesInContainerLiterals: false
Standard: c++17
TabWidth: 1
TypenameMacros:
- TKResourceType
- TKComponentType
- TKDeclareParam
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -13,6 +13,10 @@ set(XGFX_API "DIRECTX12")
set_property(TARGET DX12Renderer PROPERTY C_STANDARD 17)
set_property(TARGET DX12Renderer PROPERTY CXX_STANDARD 17)
target_include_directories(DX12Renderer PUBLIC "${CMAKE_SOURCE_DIR}/Dependency/CrossWindow/src")
if(MSVC)
target_compile_options(DX12Renderer PUBLIC "/ZI")
target_link_options(DX12Renderer PUBLIC "/INCREMENTAL")
endif()

target_link_libraries(
DX12Renderer
Expand Down
98 changes: 98 additions & 0 deletions src/AppWindow.cpp
@@ -1,8 +1,14 @@
#include "AppWindow.h"
#include "Utils.h"
#include "Main.h"
#include "Camera.h"

namespace WoohooDX12
{
bool isRightClicked = false, buttonPressed[4] = {};
double frameDelta = 0.0f;

static std::chrono::high_resolution_clock::time_point lastTime;
int AppWindow::CreateAppWindow(int width, int height)
{
m_width = width;
Expand All @@ -19,20 +25,112 @@ namespace WoohooDX12

m_eventQueue = new xwin::EventQueue();
m_window = new xwin::Window();
lastTime = std::chrono::high_resolution_clock::now();

if (!m_window->create(wdesc, *m_eventQueue))
{
Log("Window creation has failed.", LogType::LT_ERROR);
return -1;
};

m_cam = new Camera;

return 0;
}

void AppWindow::CloseAppWindow()
{
m_window->close();
}

void AppWindow::HandleEvents() {
m_eventQueue->update();
auto now = std::chrono::high_resolution_clock::now();
frameDelta = ((now - lastTime).count()) / pow(10.0f, 6.0f);
lastTime = now;

DirectX::XMVECTOR mouseOffset = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
bool isFirstRight = false;
while (!m_eventQueue->empty())
{
const xwin::Event& event = m_eventQueue->front();

if (event.type == xwin::EventType::Resize)
{
const xwin::ResizeData data = event.data.resize;
m_app->m_renderer->Resize(data.width, data.height);
m_cam->SetProperties(45.0f,
data.width,
data.height,
CONST_MOUSESENSITIVITY);
shouldRender = false;
}
else if (event.type == xwin::EventType::Close)
{
CloseAppWindow();
shouldRender = false;
m_app->m_quit = true;
}
else {
shouldRender = true;
}

if (event.type == xwin::EventType::MouseInput && event.data.mouseInput.button == xwin::MouseInput::Right) {
if (event.data.mouseInput.state == xwin::ButtonState::Pressed)
{
isRightClicked = true;
}
else
{
isRightClicked = false;
}
}
if (event.type == xwin::EventType::MouseMove && isRightClicked) {
mouseOffset = DirectX::XMVectorAdd(mouseOffset, DirectX::XMVectorSet(event.data.mouseMove.deltax, event.data.mouseMove.deltay, 0.0f, 0.0f));
}
if (event.type == xwin::EventType::Keyboard) {
WhButtons key = WhButtons::INVALID;
switch (event.data.keyboard.key)
{
case xwin::Key::W:
key = WhButtons::W;
break;
case xwin::Key::A:
key = WhButtons::A;
break;
case xwin::Key::S:
key = WhButtons::S;
break;
case xwin::Key::D:
key = WhButtons::D;
break;
}
if (key == WhButtons::INVALID) {
break;
}

if (event.data.keyboard.state == xwin::ButtonState::Pressed)
{
buttonPressed[(int) key] = true;
}
else
{
buttonPressed[(int) key] = false;
}
}

m_eventQueue->pop();
}

if (isRightClicked) {
DirectX::XMFLOAT2 offset2D;
DirectX::XMStoreFloat2(&offset2D ,mouseOffset);
m_cam->TakeInputs(offset2D.x, offset2D.y);
}
}
bool AppWindow::ShouldRender() {
return shouldRender;
}
xwin::WindowDesc AppWindow::GetDesc()
{
return m_window->getDesc();
Expand Down
16 changes: 16 additions & 0 deletions src/AppWindow.h
Expand Up @@ -8,15 +8,31 @@ namespace WoohooDX12
{
public:
int CreateAppWindow(int width, int height);
void HandleEvents();
bool ShouldRender();
void CloseAppWindow();

xwin::WindowDesc GetDesc();

private:
bool shouldRender = true;
public:
int m_width = 640;
int m_height = 640;

xwin::Window* m_window = nullptr;
xwin::EventQueue* m_eventQueue = nullptr;
};

enum class WhButtons : unsigned char
{
W = 0,
A = 1,
S = 2,
D = 3,
INVALID = 255
};

extern bool isRightClicked, buttonPressed[4];
extern double frameDelta;
}
108 changes: 108 additions & 0 deletions src/Camera.cpp
@@ -0,0 +1,108 @@
#include "Camera.h"

#include "RendererUtils.h"
#include "Utils.h"
#include "d3dx12.h"
#include "AppWindow.h"

using namespace DirectX;

namespace WoohooDX12
{
Camera* m_cam = nullptr;

Vec3 worldUp(0, 1, 0), reverse(-1.0f, -1.0f, -1.0f);

Camera::Camera(Vec3 i_target)
{
target = XMLoadFloat3(&i_target);
XMFLOAT3 initPos = XMFLOAT3(0.0, 0.0, 10.0f);
pos = XMLoadFloat3(&initPos);
rot = XMVECTOR();
scal = XMVECTOR();
fov = 45.0f;
}

// Getters
Mat Camera::CalculateProjMat()
{
projMat = XMMatrixPerspectiveFovLH(XMConvertToRadians(float(fov)),
float(viewWidth / viewHeight),
nearPlane,
farPlane);
return projMat;
}

Mat Camera::CalculateViewMat()
{
XMVECTOR frontVec = XMVectorMultiply(target, XMLoadFloat3(&reverse));

viewMat = XMMatrixLookAtLH(pos,
XMVectorAdd(frontVec, pos),
XMLoadFloat3(&worldUp));
return viewMat;
}

// Setters
void Camera::SetProperties(unsigned short fov_in_Angle,
float aspect_Width,
float aspect_Height,
float sensitivity)
{
fov = fov_in_Angle;
viewWidth = aspect_Width;
viewHeight = aspect_Height;
nearPlane = CONST_CAMNEAR;
farPlane = CONST_CAMFAR;
mouseSensitivity = sensitivity;
}

void Camera::TakeInputs(float mouseOffsetX, float mouseOffsetY)
{
float multiplier = mouseSensitivity * frameDelta;
mouseYaw += mouseOffsetX * -multiplier;
mousePitch += mouseOffsetY * multiplier;

// Limit Pitch direction to 90 degrees, because we don't want our camera to
// upside-down!
if (mousePitch > 89.0f)
{
mousePitch = 89.0f;
}
else if (mousePitch < -89.0f)
{
mousePitch = -89.0f;
}

// Set target direction
double radPitch = XMConvertToRadians(mousePitch);
double radYaw = XMConvertToRadians(mouseYaw);
target = XMVectorSet(cos(radPitch) * cos(radYaw),
sin(radPitch),
cos(radPitch) * sin(radYaw),
0.0f);

XMVECTOR front, right, up;
front = XMVector3Normalize(target);
right = XMVector3Cross(XMVector3Normalize(target), XMLoadFloat3(&worldUp));
up = XMVectorNegate(XMVector3Normalize(XMVector3Cross(front, right)));

if (buttonPressed[(int)WhButtons::W]) {
pos -= front * multiplier;
}
if (buttonPressed[(int)WhButtons::S]) {
pos += front * multiplier;
}
if (buttonPressed[(int)WhButtons::D]) {
pos += right * multiplier;
}
if (buttonPressed[(int)WhButtons::A]) {
pos -= right * multiplier;
}
XMFLOAT3 fPos;
XMStoreFloat3(&fPos, pos);


CalculateViewMat();
}
} // namespace WoohooDX12
36 changes: 36 additions & 0 deletions src/Camera.h
@@ -0,0 +1,36 @@
#pragma once

#include "RendererUtils.h"

#include <d3d12.h>

namespace WoohooDX12
{
static constexpr double CONST_CAMNEAR = 0.001, CONST_CAMFAR = 10000.0, CONST_MOUSESENSITIVITY = 0.1f;

class Camera
{
DirectX::XMVECTOR pos, rot, scal, target;
Mat viewMat, projMat;
// Fov is in angle
unsigned short fov = 45.0f, viewWidth = 1920.0f, viewHeight = 1080.0f;
float nearPlane = CONST_CAMNEAR, farPlane = CONST_CAMFAR,
mouseSensitivity = 0.001f, mouseYaw = 90.0f, mousePitch = 0.0f;

public:
Camera(Vec3 target = Vec3(0, 0, 1));

// Getters
Mat CalculateProjMat();
Mat CalculateViewMat();

// Setters
void SetProperties(unsigned short fov_in_Angle,
float aspect_Width,
float aspect_Height,
float sensitivity);
void TakeInputs(float mouseOffsetX, float mouseOffsetY);
};

extern Camera* m_cam;
} // namespace WoohooDX12

0 comments on commit 7fc52a2

Please sign in to comment.