Entity kezdő, kamerával kiegészítve
This commit is contained in:
parent
2e9f2d45d9
commit
89ca072b2d
75
Cam.cpp
75
Cam.cpp
@ -6,12 +6,15 @@
|
||||
#include "utils.h"
|
||||
|
||||
eg3d::Cam::Cam() {
|
||||
|
||||
loadDefaults();
|
||||
}
|
||||
|
||||
void eg3d::Cam::loadDefaults() {
|
||||
mProjParams.loadDefaults();
|
||||
mViewParams.loadDefaults();
|
||||
|
||||
constructViewMatrix();
|
||||
constructProjMatrix();
|
||||
}
|
||||
|
||||
const XMFLOAT4X4 &eg3d::Cam::getViewMatrix() const {
|
||||
@ -23,17 +26,40 @@ const XMFLOAT4X4 &eg3d::Cam::getProjMatrix() const {
|
||||
}
|
||||
|
||||
void eg3d::Cam::constructViewMatrix() {
|
||||
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // felfelé irány
|
||||
XMMATRIX r = XMMatrixRotationX(mViewParams.pitch) * XMMatrixRotationY(mViewParams.yaw); // forgatások (yaw, pitch)
|
||||
XMMATRIX t = XMMatrixTranslation(mViewParams.position.x, mViewParams.position.y, mViewParams.position.z); // eltolás
|
||||
XMStoreFloat4x4(&mMView, MT(t*r)); // view mátrix letárolása
|
||||
|
||||
XMVECTOR dir = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f); // (0,0,-1) irányba néz alapból a kamera
|
||||
XMStoreFloat3(&mVViewDirection, XMVector3Transform(dir, r)); // forgatott vektor kiszámítása és letárolása
|
||||
XMVECTOR dir = XMVectorSet(0.0f, 0.0f, 1.0, 1.0f); // (0,0,-1) irányba néz alapból a kamera
|
||||
dir = XMVector3Transform(dir, r);
|
||||
XMStoreFloat3(&mVViewDirection, dir); // forgatott vektor kiszámítása és letárolása
|
||||
|
||||
XMVECTOR position = XMLoadFloat3(&mViewParams.position);
|
||||
XMMATRIX v = XMMatrixLookAtRH(position, position + dir, up);
|
||||
|
||||
XMStoreFloat4x4(&mMView, XMMatrixTranspose(v)); // view mátrix letárolása
|
||||
|
||||
XMVECTOR strafe = XMVector3Cross(dir, up); // oldalazás irányának kiszámítása
|
||||
XMStoreFloat3(&mStrafeDirection, strafe);
|
||||
|
||||
//LOG(std::to_string(mViewParams.position.x) + ", " + std::to_string(mViewParams.position.y) + ", " + std::to_string(mViewParams.position.z));
|
||||
//LOG(std::to_string(mViewParams.pitch));
|
||||
}
|
||||
|
||||
void eg3d::Cam::constructProjMatrix() {
|
||||
XMMATRIX p = XMMatrixPerspectiveFovLH(mProjParams.FOV, mProjParams.AspectRatio, mProjParams.NearPlane, mProjParams.FarPlane);
|
||||
XMStoreFloat4x4(&mMProj, MT(p));
|
||||
XMMATRIX p; // vetítés mátrixa
|
||||
|
||||
// vetítés mátrixának megadása
|
||||
switch (mProjParams.projType)
|
||||
{
|
||||
case ProjectionType::PERSPECTIVE:
|
||||
p = XMMatrixPerspectiveFovRH(mProjParams.FOV_ViewHeight, mProjParams.AspectRatio, mProjParams.NearPlane, mProjParams.FarPlane);
|
||||
break;
|
||||
case ProjectionType::ORTHOGRAPHIC:
|
||||
p = XMMatrixOrthographicRH( mProjParams.FOV_ViewHeight * mProjParams.AspectRatio, mProjParams.FOV_ViewHeight, mProjParams.NearPlane, mProjParams.FarPlane);
|
||||
break;
|
||||
}
|
||||
|
||||
XMStoreFloat4x4(&mMProj, XMMatrixTranspose(p));
|
||||
}
|
||||
|
||||
void eg3d::Cam::setProjParams(const eg3d::ProjParams &pp) {
|
||||
@ -54,7 +80,7 @@ eg3d::ViewParams eg3d::Cam::getViewParams() const {
|
||||
return mViewParams;
|
||||
}
|
||||
|
||||
void eg3d::Cam::advance(float ds) {
|
||||
void eg3d::Cam::walk(float ds) {
|
||||
XMVECTOR s = XMLoadFloat3(&mViewParams.position);
|
||||
XMVECTOR dir = XMLoadFloat3(&mVViewDirection);
|
||||
s = XMVectorAdd(s, XMVectorScale(dir, ds));
|
||||
@ -63,13 +89,32 @@ void eg3d::Cam::advance(float ds) {
|
||||
constructViewMatrix();
|
||||
}
|
||||
|
||||
void eg3d::Cam::strafe(float ds)
|
||||
{
|
||||
XMVECTOR s = XMLoadFloat3(&mViewParams.position);
|
||||
XMVECTOR strafe = XMLoadFloat3(&mStrafeDirection);
|
||||
s = XMVectorAdd(s, XMVectorScale(strafe, ds));
|
||||
XMStoreFloat3(&mViewParams.position, s);
|
||||
|
||||
constructViewMatrix();
|
||||
}
|
||||
|
||||
void eg3d::Cam::yaw(float dphi) {
|
||||
mViewParams.yaw += dphi;
|
||||
mViewParams.yaw -= dphi; // BALkezes koordináta-rendszer miatt
|
||||
|
||||
if (mViewParams.yaw < -XM_2PI)
|
||||
{
|
||||
mViewParams.yaw += XM_2PI;
|
||||
} else if (mViewParams.yaw > XM_2PI)
|
||||
{
|
||||
mViewParams.yaw -= XM_2PI;
|
||||
}
|
||||
|
||||
constructViewMatrix();
|
||||
}
|
||||
|
||||
void eg3d::Cam::pitch(float drho) {
|
||||
mViewParams.pitch += drho;
|
||||
mViewParams.pitch = min(max(mViewParams.pitch + drho, -XM_PIDIV2 + 0.001f), XM_PIDIV2 - 0.001f);
|
||||
constructViewMatrix();
|
||||
}
|
||||
|
||||
@ -80,13 +125,17 @@ eg3d::ProjParams::ProjParams() {
|
||||
}
|
||||
|
||||
void eg3d::ProjParams::loadDefaults() {
|
||||
FOV = XM_PIDIV2; // 90 fokos látószög
|
||||
FOV_ViewHeight = XM_PIDIV2; // 90 fokos látószög
|
||||
AspectRatio = 1.0f; // 1:1-es képarány
|
||||
NearPlane = 1.0f; // közeli sík
|
||||
FarPlane = 10.0f;
|
||||
FarPlane = 10.0f; // távoli sík
|
||||
projType = ProjectionType::PERSPECTIVE; // perspektivikus vetítés
|
||||
}
|
||||
|
||||
eg3d::ViewParams::ViewParams() {}
|
||||
eg3d::ViewParams::ViewParams()
|
||||
{
|
||||
loadDefaults();
|
||||
}
|
||||
|
||||
void eg3d::ViewParams::loadDefaults() {
|
||||
position.x = position.y = position.z = 0.0f; // (0,0,0), origó
|
||||
|
12
Cam.h
12
Cam.h
@ -12,15 +12,19 @@ using namespace DirectX;
|
||||
|
||||
namespace eg3d {
|
||||
|
||||
enum class ProjectionType { PERSPECTIVE, ORTHOGRAPHIC };
|
||||
|
||||
// vetítés tulajdonságai
|
||||
struct ProjParams : public IHasDefault {
|
||||
float FOV; // látószög
|
||||
float FOV_ViewHeight; // látószög
|
||||
float AspectRatio; // képarány
|
||||
float NearPlane; // közeli sík
|
||||
float FarPlane; // távoli sík
|
||||
|
||||
ProjectionType projType; // vetítés típusa
|
||||
|
||||
ProjParams(); // konstr.
|
||||
void loadDefaults(); // alapértelmezett paraméterek betöltése (FOV: 90 fok, AR: 1:1, N: 1.0, F: 10.0)
|
||||
void loadDefaults(); // alapértelmezett paraméterek betöltése (FOV_ViewHeight: 90 fok, AR: 1:1, N: 1.0, F: 10.0)
|
||||
};
|
||||
|
||||
// nézet tulajdonságai
|
||||
@ -39,6 +43,7 @@ namespace eg3d {
|
||||
ViewParams mViewParams; // nézet paraméterei
|
||||
|
||||
XMFLOAT3 mVViewDirection; // nézet iránya
|
||||
XMFLOAT3 mStrafeDirection; // oldalazás iránya
|
||||
|
||||
XMFLOAT4X4 mMView, mMProj; // view és proj mátrixok
|
||||
void constructViewMatrix(); // view mátrix összeállítása
|
||||
@ -55,7 +60,8 @@ namespace eg3d {
|
||||
void setViewParams(const ViewParams& vp); // nézet paramétereinek beállítása
|
||||
ViewParams getViewParams() const; // nézetparaméterek elkérése
|
||||
|
||||
void advance(float ds); // kamera mozgatása a nézet irányába
|
||||
void walk(float ds); // kamera mozgatása a nézet irányába
|
||||
void strafe(float ds); // kamera mozgatása a nézetre merõlegesen
|
||||
void yaw(float dphi); // kamera forgatása
|
||||
void pitch(float drho); // kamera billentése
|
||||
};
|
||||
|
16
DXWindow.cpp
16
DXWindow.cpp
@ -9,6 +9,8 @@ namespace eg3d {
|
||||
{
|
||||
init();
|
||||
initDrawObjects();
|
||||
|
||||
pCam = nullptr;
|
||||
}
|
||||
|
||||
|
||||
@ -375,23 +377,21 @@ namespace eg3d {
|
||||
{
|
||||
DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, XMMatrixTranspose(XMMatrixRotationX(-XM_PIDIV2)));
|
||||
|
||||
// TODO vetítés mátrixának kicserélése
|
||||
float viewHeight = 1.5;
|
||||
DirectX::XMStoreFloat4x4(&constantBuffer.projMatrix,
|
||||
DirectX::XMMatrixTranspose(XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 1000)));
|
||||
|
||||
// TODO nézet mátrixának átadása
|
||||
if (pCam != nullptr) {
|
||||
constantBuffer.viewMatrix = pCam->getViewMatrix();
|
||||
constantBuffer.projMatrix = pCam->getProjMatrix();
|
||||
}
|
||||
|
||||
bufferMapCopy(cbResource, static_cast<void *>(&constantBuffer), sizeof(constantBuffer));
|
||||
}
|
||||
|
||||
void DXWindow::createRootSignature()
|
||||
{
|
||||
// TODO root paraméterek megadása
|
||||
|
||||
// root paraméterek létrehozása
|
||||
CD3DX12_ROOT_PARAMETER slotParams[1];
|
||||
|
||||
// TODO worldMatrix konstansbufferének hozzáadása
|
||||
|
||||
// konstansbuffer-TÍPUS beállítása
|
||||
slotParams[0].InitAsConstantBufferView(0);
|
||||
|
||||
|
18
Entity.cpp
18
Entity.cpp
@ -8,22 +8,40 @@ eg3d::Entity::Entity(const ComPtr<ID3D12Device> &device) : Geometry(device) {
|
||||
loadDefaults();
|
||||
}
|
||||
|
||||
eg3d::Entity::Entity(const Entity& other): Geometry(other.device)
|
||||
{
|
||||
setEntitySRT(other.getEntitySRT());
|
||||
}
|
||||
|
||||
void eg3d::Entity::loadDefaults() {
|
||||
XMStoreFloat4x4(&mWorldMatrix, XMMatrixIdentity()); // egységmátrix betöltése a világ-mátrixba
|
||||
mSRTProps.loadDefaults();
|
||||
}
|
||||
|
||||
void eg3d::Entity::setEntitySRT(const eg3d::SRTProps &srtProps) {
|
||||
mSRTProps = srtProps;
|
||||
constructWorldMatrix();
|
||||
}
|
||||
|
||||
eg3d::SRTProps eg3d::Entity::getEntitySRT() const {
|
||||
return mSRTProps;
|
||||
}
|
||||
|
||||
void eg3d::Entity::draw(ComPtr<ID3D12GraphicsCommandList> commandList) const
|
||||
{
|
||||
// TODO konstansbuffer becsatolása
|
||||
}
|
||||
|
||||
void eg3d::Entity::constructWorldMatrix() {
|
||||
// TODO (Cam alapján)
|
||||
// XMMATRIX w = s * r * t
|
||||
// XMStoreFloat4x4(...)
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ----------------------
|
||||
|
||||
eg3d::SRTProps::SRTProps() {
|
||||
|
5
Entity.h
5
Entity.h
@ -26,13 +26,18 @@ namespace eg3d {
|
||||
SRTProps mSRTProps; // STR-tulajdonságok
|
||||
|
||||
void constructWorldMatrix(); // világ-mátrix újragenerálása
|
||||
|
||||
// TODO konstansbuffer létrehozása, feltöltése
|
||||
public:
|
||||
Entity(const ComPtr<ID3D12Device> &device);
|
||||
explicit Entity(const Entity& other); // másolókonstruktor
|
||||
|
||||
void loadDefaults() override;
|
||||
|
||||
void setEntitySRT(const SRTProps& srtProps); // SRT-tulajdonságok beállítása
|
||||
SRTProps getEntitySRT() const; // SRT-tualjdonságok elkérése
|
||||
|
||||
void draw(ComPtr<ID3D12GraphicsCommandList> commandList) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -6,12 +6,12 @@
|
||||
#include "Logger.h"
|
||||
#include <hidusage.h>
|
||||
|
||||
eg3d::CB_AssigmentData::CB_AssigmentData()
|
||||
eg3d::CB_AssignmentData::CB_AssignmentData()
|
||||
{
|
||||
loadDefaults();
|
||||
}
|
||||
|
||||
void eg3d::CB_AssigmentData::loadDefaults()
|
||||
void eg3d::CB_AssignmentData::loadDefaults()
|
||||
{
|
||||
eventType = ET_None;
|
||||
pcbFunction = nullptr;
|
||||
@ -24,23 +24,56 @@ eg3d::EventHandler::EventHandler() {
|
||||
|
||||
void eg3d::EventHandler::init()
|
||||
{
|
||||
mCamVelocity = 0.0f; // kamera sebességének inicializációja
|
||||
mMouseLocked = false; // nincs befogva az egér
|
||||
}
|
||||
|
||||
float eg3d::EventHandler::getCamVelocity() const
|
||||
void eg3d::EventHandler::regKeyCB(char key, const CB_AssignmentData& cbAData)
|
||||
{
|
||||
return mCamVelocity;
|
||||
mKeyAssignments.insert(std::pair<char, CB_AssignmentData>(key, cbAData));
|
||||
}
|
||||
|
||||
void eg3d::EventHandler::regKeyCB(char key, const CB_AssigmentData& cbAData)
|
||||
void eg3d::EventHandler::regKeyCB(char key, CB_EH* pcbFunction, void* pUser,
|
||||
std::initializer_list<EventType> eventTypes)
|
||||
{
|
||||
mKeyAssigments.insert(std::pair<char, CB_AssigmentData>(key, cbAData));
|
||||
if (eventTypes.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// hozzárendelési adatok
|
||||
CB_AssignmentData cbAData;
|
||||
cbAData.pUser = pUser;
|
||||
cbAData.pcbFunction = pcbFunction;
|
||||
|
||||
// regisztráció minden egyes eseménytípusra
|
||||
for (const EventType * pET = eventTypes.begin(); pET <= eventTypes.end(); pET++)
|
||||
{
|
||||
cbAData.eventType = *pET;
|
||||
regKeyCB(key, cbAData);
|
||||
}
|
||||
}
|
||||
|
||||
void eg3d::EventHandler::regMouseCB(EventType et, const CB_AssigmentData& cbAData)
|
||||
void eg3d::EventHandler::regMouseCB(const CB_AssignmentData& cbAData)
|
||||
{
|
||||
mMouseAssignments.insert(std::pair<EventType, CB_AssigmentData>(et, cbAData));
|
||||
mMouseAssignments.insert(std::pair<EventType, CB_AssignmentData>(cbAData.eventType, cbAData));
|
||||
}
|
||||
|
||||
void eg3d::EventHandler::regMouseCB(CB_EH* pcbFunction, void* pUser, std::initializer_list<EventType> eventTypes)
|
||||
{
|
||||
if (eventTypes.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// hozzárendelési adatok
|
||||
CB_AssignmentData cbAData;
|
||||
cbAData.pUser = pUser;
|
||||
cbAData.pcbFunction = pcbFunction;
|
||||
|
||||
// regisztráció minden egyes eseménytípusra
|
||||
for (const EventType * pET = eventTypes.begin(); pET <= eventTypes.end(); pET++)
|
||||
{
|
||||
cbAData.eventType = *pET;
|
||||
regMouseCB(cbAData);
|
||||
}
|
||||
}
|
||||
|
||||
eg3d::MouseDisplacement eg3d::EventHandler::getMouseDisplacement()
|
||||
@ -53,10 +86,19 @@ eg3d::MouseDisplacement eg3d::EventHandler::getMouseDisplacement()
|
||||
return md;
|
||||
}
|
||||
|
||||
bool eg3d::EventHandler::isMouseLocked() const
|
||||
{
|
||||
return mMouseLocked;
|
||||
}
|
||||
|
||||
bool eg3d::EventHandler::isKeyPressed(WPARAM vkey) const
|
||||
{
|
||||
return mPressedKeySet.find(vkey) != mPressedKeySet.end();
|
||||
}
|
||||
|
||||
// -----------------------------------
|
||||
|
||||
int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
|
||||
// TODO eseménykezelés megvalósítása
|
||||
Window * pWindow = static_cast<Window *>((void *)GetWindowLongPtrA(hwnd, GWLP_USERDATA));
|
||||
EventHandler * pThis_EH = pWindow->getEventHandler(); // ablakhoz rendelt eseménykezelõ elkérése
|
||||
|
||||
@ -145,6 +187,7 @@ int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPA
|
||||
case WM_KEYDOWN:
|
||||
{
|
||||
et = ET_KeyDown; // esemény típusának beállítása
|
||||
mPressedKeySet.emplace(wParam); // billentyûlenyomás letárolása
|
||||
|
||||
if (wParam == 'L') {
|
||||
pThis_EH->mMouseLocked = !pThis_EH->mMouseLocked;
|
||||
@ -175,10 +218,11 @@ int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPA
|
||||
} // DIRECT NINCS BREAK
|
||||
case WM_KEYUP: {
|
||||
et = ET_KeyUp; // esemény típusának beállítása
|
||||
mPressedKeySet.erase(wParam); // billentyû törlése a halmazból
|
||||
|
||||
KEYBOARD_CONT:
|
||||
|
||||
auto assigments = mKeyAssigments.equal_range(wParam); // hozzárendelések elkérése
|
||||
auto assigments = mKeyAssignments.equal_range(wParam); // hozzárendelések elkérése
|
||||
auto rangeBegin = assigments.first;
|
||||
auto rangeEnd = assigments.second;
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <Windows.h>
|
||||
#include <map>
|
||||
#include "IHasDefault.h"
|
||||
#include <set>
|
||||
|
||||
namespace eg3d {
|
||||
|
||||
@ -27,13 +28,13 @@ namespace eg3d {
|
||||
|
||||
typedef void (CB_EH)(EventHandlerCBData *); // callback-függvény típusa
|
||||
|
||||
struct CB_AssigmentData : public IHasDefault // hozzárendelési struktúra
|
||||
struct CB_AssignmentData : public IHasDefault // hozzárendelési struktúra
|
||||
{
|
||||
EventType eventType; // esemény típusa, melyhez hozzárendelünk
|
||||
CB_EH * pcbFunction; // callback-függvény pointere
|
||||
void * pUser; // tetszõleges adat pointere
|
||||
|
||||
CB_AssigmentData(); // konstr.
|
||||
CB_AssignmentData(); // konstr.
|
||||
void loadDefaults() override;
|
||||
};
|
||||
|
||||
@ -44,24 +45,28 @@ namespace eg3d {
|
||||
|
||||
class EventHandler {
|
||||
private:
|
||||
float mCamVelocity; // kamera mozgási sebessége
|
||||
bool mMouseLocked; // lockolvan van az egér?
|
||||
|
||||
void init(); // osztály inicializálása
|
||||
|
||||
std::multimap<EventType, CB_AssigmentData> mMouseAssignments; // egér-hozzárendelések
|
||||
std::multimap<char, CB_AssigmentData> mKeyAssigments; // billentyû-hozzárendelések
|
||||
std::multimap<EventType, CB_AssignmentData> mMouseAssignments; // egér-hozzárendelések
|
||||
std::multimap<char, CB_AssignmentData> mKeyAssignments; // billentyû-hozzárendelések
|
||||
MouseDisplacement mMouseDisplacement; //
|
||||
|
||||
std::set<WPARAM> mPressedKeySet; // a lenyomott billentyûket tárolja
|
||||
public:
|
||||
static constexpr float cCAM_VELOCITY = 0.05;
|
||||
static constexpr float cCAM_VELOCITY = 0.05f;
|
||||
|
||||
EventHandler(); // konstr.
|
||||
int processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); // esemény feldolgozása (0 -> feldolgozva, 1 -> nincs feldolgozva)
|
||||
float getCamVelocity() const; // kamera sebességének lekérése
|
||||
|
||||
void regKeyCB(char key, const CB_AssigmentData& cbAData); // billentyûesemények hozzárendelése
|
||||
void regMouseCB(EventType et, const CB_AssigmentData& cbAData); // egéresemények hozzárendelése
|
||||
void regKeyCB(char key, const CB_AssignmentData& cbAData); // billentyûesemények hozzárendelése
|
||||
void regKeyCB(char key, CB_EH * pcbFunction, void * pUser, std::initializer_list<EventType> eventTypes); // feliratkozás több eseményre
|
||||
void regMouseCB(const CB_AssignmentData& cbAData); // egéresemények hozzárendelése
|
||||
void regMouseCB(CB_EH * pcbFunction, void * pUser, std::initializer_list<EventType> eventTypes); // egéresemények hozzárendelése
|
||||
MouseDisplacement getMouseDisplacement(); // egér elmozdulásának elkérése
|
||||
bool isMouseLocked() const; // lekéri, hogy lockolva van-e az egér
|
||||
bool isKeyPressed(WPARAM vkey) const; // lekéri, hogy az adott billentyû le van-e nyomva
|
||||
};
|
||||
|
||||
}
|
||||
|
44
Geometry.cpp
44
Geometry.cpp
@ -6,12 +6,30 @@
|
||||
|
||||
namespace eg3d {
|
||||
|
||||
Geometry::Geometry(ComPtr<ID3D12Device> device) : mDevice(device) {
|
||||
int ImportAdapters::IAD_Default(XMFLOAT3& vertex)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ImportAdapters::IAD_SwapYZ_RH(XMFLOAT3& vertex)
|
||||
{
|
||||
float y = vertex.y;
|
||||
vertex.y = vertex.z;
|
||||
vertex.z = -y;
|
||||
//vertex.x = vertex.x;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -------------------
|
||||
|
||||
Geometry::Geometry(ComPtr<ID3D12Device> device) : device(device) {
|
||||
// topológia beállítása
|
||||
setTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
}
|
||||
|
||||
Geometry::Geometry(const Geometry &other) : mDevice(other.mDevice) {
|
||||
Geometry::Geometry(const Geometry &other) : device(other.device) {
|
||||
setTopology(other.getTopology());
|
||||
setVertices(other.getVertices());
|
||||
setIndices(other.getIndices());
|
||||
@ -23,7 +41,7 @@ namespace eg3d {
|
||||
size_t bufferSize = mVertices.size() * sizeof(Vertex); // buffer mérete
|
||||
|
||||
mVertexBuffer.Reset(); // buffer törlése
|
||||
mVertexBuffer = createBuffer_UH(mDevice, bufferSize); // buffer létrehozása
|
||||
mVertexBuffer = createBuffer_UH(device, bufferSize); // buffer létrehozása
|
||||
bufferMapCopy(mVertexBuffer, (void *) mVertices.data(), bufferSize); // buffer feltöltése
|
||||
|
||||
// vertex buffer view beállítása
|
||||
@ -42,7 +60,7 @@ namespace eg3d {
|
||||
size_t bufferSize = mIndices.size() * sizeof(uint32_t); // buffer mérete
|
||||
|
||||
mIndexBuffer.Reset(); // buffer törlése
|
||||
mIndexBuffer = createBuffer_UH(mDevice, bufferSize); // buffer létrehozása
|
||||
mIndexBuffer = createBuffer_UH(device, bufferSize); // buffer létrehozása
|
||||
bufferMapCopy(mIndexBuffer, (void *) mIndices.data(), bufferSize); // buffer feltöltése
|
||||
|
||||
// index buffer view beállítása
|
||||
@ -99,7 +117,7 @@ namespace eg3d {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Geometry::loadBinarySTL(const std::string &fileName) {
|
||||
void Geometry::loadBinarySTL(const std::string &fileName, ImportAdapter_fn * pIAD) {
|
||||
auto inFile = std::ifstream(fileName, std::ios::in | std::ios::binary); // bináris fájl megnyitása olvasásra
|
||||
|
||||
if (!inFile.is_open()) { // megnézzük, hogy sikerült-e megnyitni a fájlt
|
||||
@ -123,15 +141,19 @@ namespace eg3d {
|
||||
STLTriangle triangle;
|
||||
inFile.read(reinterpret_cast<char *>(&triangle), 50); // egy háromszög betöltése
|
||||
|
||||
vertices.emplace_back(triangle.v1);
|
||||
vertices.emplace_back(triangle.v2);
|
||||
vertices.emplace_back(triangle.v3);
|
||||
for (auto& vertex : triangle.vertices)
|
||||
{
|
||||
if (pIAD != nullptr) {
|
||||
pIAD(vertex);
|
||||
}
|
||||
vertices.emplace_back(vertex);
|
||||
}
|
||||
|
||||
// indexek vektorának feltöltése
|
||||
uint32_t firstIndex = 3 * i;
|
||||
indices.push_back(firstIndex);
|
||||
indices.push_back(firstIndex + 1);
|
||||
indices.push_back(firstIndex + 2);
|
||||
indices.push_back(firstIndex + 1);
|
||||
}
|
||||
|
||||
inFile.close();
|
||||
@ -142,4 +164,8 @@ namespace eg3d {
|
||||
LOG("Successful geometry loading from file '" + fileName + "' of " + std::to_string(triN) + " triangles.");
|
||||
}
|
||||
|
||||
ComPtr<ID3D12Device> Geometry::getDevice() const
|
||||
{
|
||||
return device;
|
||||
}
|
||||
}
|
||||
|
20
Geometry.h
20
Geometry.h
@ -13,14 +13,21 @@ namespace eg3d {
|
||||
|
||||
struct STLTriangle {
|
||||
XMFLOAT3 normal;
|
||||
XMFLOAT3 v1, v2, v3;
|
||||
XMFLOAT3 vertices[3];
|
||||
uint16_t attributeByteCount;
|
||||
};
|
||||
|
||||
typedef int (ImportAdapter_fn)(XMFLOAT3&); // importáláskor a vertexek átalakítására szolgál
|
||||
|
||||
class ImportAdapters
|
||||
{
|
||||
public:
|
||||
static int IAD_Default(XMFLOAT3& vertex); // alapértelmezett
|
||||
static int IAD_SwapYZ_RH(XMFLOAT3& vertex); // y-z tengelyek cseréje
|
||||
};
|
||||
|
||||
class Geometry : public IDrawable {
|
||||
private:
|
||||
ComPtr<ID3D12Device> mDevice; // device
|
||||
|
||||
std::vector<Vertex> mVertices; // vertexek
|
||||
std::vector<uint32_t> mIndices; // indexek
|
||||
|
||||
@ -36,6 +43,8 @@ namespace eg3d {
|
||||
// leírók
|
||||
D3D12_VERTEX_BUFFER_VIEW mVertexBufferView;
|
||||
D3D12_INDEX_BUFFER_VIEW mIndexBufferView;
|
||||
protected:
|
||||
ComPtr<ID3D12Device> device; // device
|
||||
public:
|
||||
explicit Geometry(ComPtr<ID3D12Device> device); // konstr.
|
||||
Geometry(const Geometry& other); // másolókonstruktor
|
||||
@ -46,10 +55,11 @@ namespace eg3d {
|
||||
void setTopology(D3D_PRIMITIVE_TOPOLOGY topology); // topológia beállítása
|
||||
D3D_PRIMITIVE_TOPOLOGY getTopology() const; // topológia lekérése
|
||||
void setupInputAssembler(ComPtr<ID3D12GraphicsCommandList> commandList) const; // input assembler beállítása
|
||||
void draw(ComPtr<ID3D12GraphicsCommandList> commandList) const override; // kirajzolás
|
||||
virtual void draw(ComPtr<ID3D12GraphicsCommandList> commandList) const override; // kirajzolás
|
||||
void merge(const Geometry& other); // két geometria egyesítése
|
||||
Geometry& operator+=(const Geometry& other); // két geometria egyesítése
|
||||
void loadBinarySTL(const std::string& fileName); // bináris STL fájl betöltése
|
||||
void loadBinarySTL(const std::string& fileName, ImportAdapter_fn * pIAD = nullptr); // bináris STL fájl betöltése
|
||||
ComPtr<ID3D12Device> getDevice() const; // device elkérése
|
||||
};
|
||||
|
||||
// geometriatároló típusa
|
||||
|
BIN
RUNTIME/assets/dust2.stl
Normal file
BIN
RUNTIME/assets/dust2.stl
Normal file
Binary file not shown.
@ -21,7 +21,7 @@ VSOutput vs_main(VSInput input)
|
||||
{
|
||||
VSOutput output; // vertex shader kimenete
|
||||
|
||||
output.position_H = mul(float4(input.position_D, 1.0), mul(mul(transformMatrix, viewMatrix), projMatrix));
|
||||
output.position_H = mul(float4(input.position_D, 1.0), mul(viewMatrix, projMatrix));
|
||||
output.color = float3(1.0, 1.0, 1.0);
|
||||
|
||||
return output;
|
||||
|
168
main.cpp
168
main.cpp
@ -16,20 +16,125 @@
|
||||
|
||||
// FPS-mérés
|
||||
size_t gFrames = 0;
|
||||
std::string gFPS_FT_msg = "";
|
||||
void CB_FPSaux(eg3d::Timer *pTimer, const double *pTrigTimeElapsed, void *ptr) {
|
||||
// TODO képkockaidő és FPS kiszámítása; képkockaszámláló nullázása; eredmények kiírása
|
||||
|
||||
double fps = gFrames / *pTrigTimeElapsed;
|
||||
gFrames = 0;
|
||||
|
||||
LOG(std::to_string(1 / fps * 1000) + "ms, " + std::to_string(fps) + "FPS");
|
||||
gFPS_FT_msg = std::to_string(1 / fps * 1000) + " ms, " + std::to_string(fps) + " FPS";
|
||||
}
|
||||
|
||||
using namespace eg3d;
|
||||
|
||||
Cam gCam; // virtuális kamera
|
||||
DrawablePool gDrawables; // geometriák halmaza
|
||||
|
||||
// ÁLLAPOTVÁLTOZÓK
|
||||
bool gMoveForward = false;
|
||||
bool gMoveBackward = false;
|
||||
bool gStrafeLeft = false;
|
||||
bool gStrafeRight = false;
|
||||
|
||||
void keyCB(EventHandlerCBData * pCBData)
|
||||
{
|
||||
LOG("Key pressed!");
|
||||
std::string message = "Switching projection to ";
|
||||
|
||||
ProjParams projParams = gCam.getProjParams();
|
||||
|
||||
if (projParams.projType == ProjectionType::PERSPECTIVE) {
|
||||
projParams.projType = ProjectionType::ORTHOGRAPHIC;
|
||||
projParams.FOV_ViewHeight = 1000.0f;
|
||||
message += "orthographic!";
|
||||
} else
|
||||
{
|
||||
projParams.projType = ProjectionType::PERSPECTIVE;
|
||||
projParams.FOV_ViewHeight = XM_PIDIV2; // 90°
|
||||
message += "perspectivic!";
|
||||
}
|
||||
|
||||
gCam.setProjParams(projParams);
|
||||
|
||||
LOG(message);
|
||||
}
|
||||
|
||||
void resetCam_H(EventHandlerCBData * pCBData)
|
||||
{
|
||||
gCam.setViewParams(ViewParams());
|
||||
|
||||
LOG("Cam reset!");
|
||||
}
|
||||
|
||||
void mouseCB(EventHandlerCBData * pCBData)
|
||||
{
|
||||
LOG("Mouse clicked!");
|
||||
|
||||
// TODO új entitás behozása a kamera pozíciójára
|
||||
}
|
||||
|
||||
void moveForward_H(EventHandlerCBData * pCBData)
|
||||
{
|
||||
if (pCBData->eventType == ET_KeyDown)
|
||||
{
|
||||
gMoveForward = true;
|
||||
} else if(pCBData->eventType == ET_KeyUp)
|
||||
{
|
||||
gMoveForward = false;
|
||||
}
|
||||
}
|
||||
|
||||
void moveBackward_H(EventHandlerCBData * pCBData)
|
||||
{
|
||||
if (pCBData->eventType == ET_KeyDown)
|
||||
{
|
||||
gMoveBackward = true;
|
||||
}
|
||||
else if (pCBData->eventType == ET_KeyUp)
|
||||
{
|
||||
gMoveBackward = false;
|
||||
}
|
||||
}
|
||||
|
||||
void strafeLeft_H(EventHandlerCBData * pCBData)
|
||||
{
|
||||
if (pCBData->eventType == ET_KeyDown)
|
||||
{
|
||||
gStrafeLeft = true;
|
||||
}
|
||||
else if (pCBData->eventType == ET_KeyUp)
|
||||
{
|
||||
gStrafeLeft = false;
|
||||
}
|
||||
}
|
||||
|
||||
void strafeRight_H(EventHandlerCBData * pCBData)
|
||||
{
|
||||
if (pCBData->eventType == ET_KeyDown)
|
||||
{
|
||||
gStrafeRight = true;
|
||||
}
|
||||
else if (pCBData->eventType == ET_KeyUp)
|
||||
{
|
||||
gStrafeRight = false;
|
||||
}
|
||||
}
|
||||
|
||||
void regEventHandlers(EventHandler * pEH)
|
||||
{
|
||||
pEH->regKeyCB('P', keyCB, nullptr, { ET_KeyDown });
|
||||
|
||||
// kattintás
|
||||
pEH->regMouseCB(mouseCB, nullptr, { ET_LMouseDown });
|
||||
|
||||
// mozgás
|
||||
pEH->regKeyCB('W', moveForward_H, nullptr, { ET_KeyDown, ET_KeyUp }); // előre
|
||||
pEH->regKeyCB('S', moveBackward_H, nullptr, { ET_KeyDown, ET_KeyUp }); // hátra
|
||||
pEH->regKeyCB('A', strafeLeft_H, nullptr, { ET_KeyDown, ET_KeyUp }); // balra
|
||||
pEH->regKeyCB('D', strafeRight_H, nullptr, { ET_KeyDown, ET_KeyUp }); // jobbra
|
||||
|
||||
// kamera visszahelyezése az origóba
|
||||
pEH->regKeyCB('R', resetCam_H, nullptr, { ET_KeyDown });
|
||||
}
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgs, int nWinMode) {
|
||||
@ -40,12 +145,19 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
|
||||
// TODO eseménykezelő létrehozása és ablakhoz rendelése
|
||||
EventHandler eventHandler;
|
||||
win.setEventHandler(&eventHandler);
|
||||
regEventHandlers(&eventHandler);
|
||||
|
||||
CB_AssigmentData cbAData;
|
||||
cbAData.pcbFunction = keyCB;
|
||||
cbAData.pUser = nullptr;
|
||||
cbAData.eventType = ET_KeyDown;
|
||||
eventHandler.regKeyCB('Z', cbAData);
|
||||
win.setCam(&gCam);
|
||||
|
||||
// vetítési paraméterek beállítása
|
||||
ProjParams projParams;
|
||||
projParams.projType = ProjectionType::PERSPECTIVE;
|
||||
projParams.AspectRatio = win.getAspectRatio();
|
||||
projParams.FOV_ViewHeight = XM_PIDIV2;
|
||||
projParams.FarPlane = 10000;
|
||||
projParams.NearPlane = 0.5;
|
||||
|
||||
gCam.setProjParams(projParams);
|
||||
|
||||
// FPS-mérő callback-függvény regisztrálása
|
||||
TimerCallbackData timerCBData;
|
||||
@ -66,17 +178,19 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
|
||||
pGeo->setIndices(indices);
|
||||
|
||||
auto pGeo2 = std::shared_ptr<Geometry>(new Geometry(win.getDevice()));
|
||||
pGeo2->loadBinarySTL("assets/utahteapot.stl");
|
||||
pGeo2->loadBinarySTL("assets/dust2.stl", ImportAdapters::IAD_SwapYZ_RH);
|
||||
|
||||
(*pGeo) += (*pGeo2);
|
||||
|
||||
DrawablePool drawables; // geometriák halmaza
|
||||
drawables.push_back(pGeo); // pGeo geometria hozzáadása
|
||||
gDrawables.push_back(pGeo); // pGeo geometria hozzáadása
|
||||
//drawables.push_back(pGeo);
|
||||
|
||||
|
||||
// ------------------------------------
|
||||
|
||||
const float cCamVelocity = 10.0f;
|
||||
const float cAngularVelocity = 0.001f;
|
||||
|
||||
MSG msg = { };
|
||||
|
||||
while (msg.message != WM_QUIT)
|
||||
@ -89,7 +203,39 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
|
||||
{
|
||||
Sleep(15);
|
||||
|
||||
win.Draw(drawables); // kirajzolás
|
||||
/*MouseDisplacement md = eventHandler.getMouseDisplacement();
|
||||
LOG(std::string("(") + std::to_string(md.dx) + ", " + std::to_string(md.dy) + ")");*/
|
||||
|
||||
//LOG(std::string("forward: ") + std::to_string(gMoveForward) + ", backward: " + std::to_string(gMoveBackward));
|
||||
|
||||
// KAMERA MOZGATÁSA
|
||||
if (gMoveForward == true) {
|
||||
gCam.walk(cCamVelocity);
|
||||
} else if (gMoveBackward == true) {
|
||||
gCam.walk(-cCamVelocity);
|
||||
}
|
||||
|
||||
if (gStrafeLeft == true) {
|
||||
gCam.strafe(-cCamVelocity);
|
||||
} else if (gStrafeRight == true) {
|
||||
gCam.strafe(cCamVelocity);
|
||||
}
|
||||
|
||||
MouseDisplacement md = eventHandler.getMouseDisplacement();
|
||||
if (eventHandler.isMouseLocked()) {
|
||||
gCam.pitch(md.dy * cAngularVelocity);
|
||||
gCam.yaw(md.dx * cAngularVelocity);
|
||||
}
|
||||
|
||||
auto viewParams = gCam.getViewParams();
|
||||
auto position = viewParams.position;
|
||||
auto pitch = viewParams.pitch * 180.0f / XM_PI;
|
||||
auto yaw = viewParams.yaw * 180.0f / XM_PI;
|
||||
|
||||
std::string winTitle = "Open world game - P: (" + std::to_string(position.x) + "; " + std::to_string(position.y) + "; " + std::to_string(position.z) + "), " +
|
||||
"yaw: " + std::to_string(yaw) + "°, pitch: " +std::to_string(pitch) + "° ---- " + gFPS_FT_msg;
|
||||
win.setTitle(winTitle);
|
||||
win.Draw(gDrawables); // kirajzolás
|
||||
|
||||
gFrames++; // képkockaszám léptetése
|
||||
}
|
||||
|
3
utils.h
3
utils.h
@ -78,6 +78,9 @@ inline void bufferMapCopy(ComPtr<ID3D12Resource> bufferResource, void * data, si
|
||||
}
|
||||
|
||||
// feltételes transzponálás (ha definiálva van, akkor automatikusan transzponál minden mátrixot)
|
||||
// KÖVETKEZÕ SOR CSAK ITT JÓ!!
|
||||
#define MT_ON
|
||||
|
||||
#ifdef MT_ON
|
||||
#define MT(m) XMMatrixTranspose(m)
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user