grafika-entity kezdő
This commit is contained in:
parent
0d63bbd662
commit
cf18cd4e84
@ -3,6 +3,6 @@ project(WinApi)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
add_executable(WinApi WIN32 main.cpp d3dx12.h DXWindow.cpp DXWindow.h Logger.cpp Logger.h utils.cpp utils.h Window.cpp Window.h Timer.cpp Timer.h IDrawable.h Geometry.h EventHandler.cpp EventHandler.h Vertex.cpp Vertex.h)
|
add_executable(WinApi WIN32 main.cpp d3dx12.h DXWindow.cpp DXWindow.h Logger.cpp Logger.h utils.cpp utils.h Window.cpp Window.h Timer.cpp Timer.h IDrawable.h Geometry.h EventHandler.cpp EventHandler.h Vertex.cpp Vertex.h Cam.cpp Cam.h Geometry.cpp const_bufs.h Entity.cpp Entity.h IHasDefault.h)
|
||||||
|
|
||||||
target_link_libraries(WinApi d3d12.lib dxgi.lib dxguid.lib d3dcompiler.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib)
|
target_link_libraries(WinApi d3d12.lib dxgi.lib dxguid.lib d3dcompiler.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib)
|
95
Cam.cpp
Normal file
95
Cam.cpp
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 16..
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Cam.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
eg3d::Cam::Cam() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::loadDefaults() {
|
||||||
|
mProjParams.loadDefaults();
|
||||||
|
mViewParams.loadDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
const XMFLOAT4X4 &eg3d::Cam::getViewMatrix() const {
|
||||||
|
return mMView;
|
||||||
|
}
|
||||||
|
|
||||||
|
const XMFLOAT4X4 &eg3d::Cam::getProjMatrix() const {
|
||||||
|
return mMProj;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::constructViewMatrix() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::constructProjMatrix() {
|
||||||
|
XMMATRIX p = XMMatrixPerspectiveFovLH(mProjParams.FOV, mProjParams.AspectRatio, mProjParams.NearPlane, mProjParams.FarPlane);
|
||||||
|
XMStoreFloat4x4(&mMProj, MT(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::setProjParams(const eg3d::ProjParams &pp) {
|
||||||
|
mProjParams = pp;
|
||||||
|
constructProjMatrix(); // projekciós mátrix újragenerálása
|
||||||
|
}
|
||||||
|
|
||||||
|
eg3d::ProjParams eg3d::Cam::getProjParams() const {
|
||||||
|
return mProjParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::setViewParams(const eg3d::ViewParams &vp) {
|
||||||
|
mViewParams = vp;
|
||||||
|
constructViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
eg3d::ViewParams eg3d::Cam::getViewParams() const {
|
||||||
|
return mViewParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::advance(float ds) {
|
||||||
|
XMVECTOR s = XMLoadFloat3(&mViewParams.position);
|
||||||
|
XMVECTOR dir = XMLoadFloat3(&mVViewDirection);
|
||||||
|
s = XMVectorAdd(s, XMVectorScale(dir, ds));
|
||||||
|
XMStoreFloat3(&mViewParams.position, s);
|
||||||
|
|
||||||
|
constructViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::yaw(float dphi) {
|
||||||
|
mViewParams.yaw += dphi;
|
||||||
|
constructViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Cam::pitch(float drho) {
|
||||||
|
mViewParams.pitch += drho;
|
||||||
|
constructViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------
|
||||||
|
|
||||||
|
eg3d::ProjParams::ProjParams() {
|
||||||
|
loadDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::ProjParams::loadDefaults() {
|
||||||
|
FOV = 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
eg3d::ViewParams::ViewParams() {}
|
||||||
|
|
||||||
|
void eg3d::ViewParams::loadDefaults() {
|
||||||
|
position.x = position.y = position.z = 0.0f; // (0,0,0), origó
|
||||||
|
yaw = 0.0f; // nincs forgatás
|
||||||
|
pitch = 0.0f; // nincs billentés
|
||||||
|
}
|
66
Cam.h
Normal file
66
Cam.h
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 16..
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef CAM
|
||||||
|
#define CAM
|
||||||
|
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
#include "IHasDefault.h"
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
|
namespace eg3d {
|
||||||
|
|
||||||
|
// vetítés tulajdonságai
|
||||||
|
struct ProjParams : public IHasDefault {
|
||||||
|
float FOV; // látószög
|
||||||
|
float AspectRatio; // képarány
|
||||||
|
float NearPlane; // közeli sík
|
||||||
|
float FarPlane; // távoli sík
|
||||||
|
|
||||||
|
ProjParams(); // konstr.
|
||||||
|
void loadDefaults(); // alapértelmezett paraméterek betöltése (FOV: 90 fok, AR: 1:1, N: 1.0, F: 10.0)
|
||||||
|
};
|
||||||
|
|
||||||
|
// nézet tulajdonságai
|
||||||
|
struct ViewParams : public IHasDefault {
|
||||||
|
XMFLOAT3 position; // nézet pozíciója
|
||||||
|
float yaw; // forgatás
|
||||||
|
float pitch; // billentés
|
||||||
|
|
||||||
|
ViewParams(); // konstr.
|
||||||
|
void loadDefaults() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Cam : IHasDefault {
|
||||||
|
private:
|
||||||
|
ProjParams mProjParams; // vetítés paraméterei
|
||||||
|
ViewParams mViewParams; // nézet paraméterei
|
||||||
|
|
||||||
|
XMFLOAT3 mVViewDirection; // nézet iránya
|
||||||
|
|
||||||
|
XMFLOAT4X4 mMView, mMProj; // view és proj mátrixok
|
||||||
|
void constructViewMatrix(); // view mátrix összeállítása
|
||||||
|
void constructProjMatrix(); // projekciós mátrix összeállítása
|
||||||
|
|
||||||
|
void loadDefaults() override;
|
||||||
|
public:
|
||||||
|
Cam();
|
||||||
|
const XMFLOAT4X4& getViewMatrix() const; // view mátrix elkérése
|
||||||
|
const XMFLOAT4X4& getProjMatrix() const; // proj mátrix elkérése
|
||||||
|
|
||||||
|
void setProjParams(const ProjParams& pp); // projekciós paraméterek beállítása
|
||||||
|
ProjParams getProjParams() const; // vetítési paraméterek elkérése
|
||||||
|
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 yaw(float dphi); // kamera forgatása
|
||||||
|
void pitch(float drho); // kamera billentése
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //CAM
|
28
DXWindow.cpp
28
DXWindow.cpp
@ -376,24 +376,15 @@ namespace eg3d {
|
|||||||
|
|
||||||
void DXWindow::updateConstantBuffers()
|
void DXWindow::updateConstantBuffers()
|
||||||
{
|
{
|
||||||
// transzformáció frissítése
|
DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, XMMatrixRotationX(-XM_PIDIV2));
|
||||||
//DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixScaling(2.0, 1.0, 1.0));
|
|
||||||
|
|
||||||
rotAngle = rotAngle + 0.01f;
|
// TODO vetítés mátrixának kicserélése
|
||||||
if (rotAngle > XM_2PI)
|
|
||||||
{
|
|
||||||
rotAngle -= XM_2PI;
|
|
||||||
// rotAngle = rotAngle - XM_2PI;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixTranspose(XMMatrixRotationZ(rotAngle) * XMMatrixRotationX(-XM_PIDIV2)));
|
|
||||||
|
|
||||||
XMStoreFloat4x4(&constantBuffer.viewMatrix, XMMatrixTranspose(XMMatrixTranslation(0.0f, -4.0f, camPosition)));
|
|
||||||
|
|
||||||
float viewHeight = 1.5;
|
float viewHeight = 1.5;
|
||||||
DirectX::XMStoreFloat4x4(&constantBuffer.projMatrix,
|
DirectX::XMStoreFloat4x4(&constantBuffer.projMatrix,
|
||||||
DirectX::XMMatrixTranspose(XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 1000)));
|
DirectX::XMMatrixTranspose(XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 1000)));
|
||||||
|
|
||||||
|
// TODO nézet mátrixának átadása
|
||||||
|
|
||||||
bufferMapCopy(cbResource, static_cast<void *>(&constantBuffer), sizeof(constantBuffer));
|
bufferMapCopy(cbResource, static_cast<void *>(&constantBuffer), sizeof(constantBuffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -463,9 +454,10 @@ namespace eg3d {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DXWindow::moveCam(float deltaX)
|
void DXWindow::setCam(Cam *pCam) {
|
||||||
{
|
this->pCam = pCam;
|
||||||
camPosition += deltaX;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
22
DXWindow.h
22
DXWindow.h
@ -17,14 +17,9 @@
|
|||||||
#include <DirectXMath.h>
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
|
#include "Cam.h"
|
||||||
|
|
||||||
// konstansbuffer struktúrája
|
#include "const_bufs.h"
|
||||||
struct ConstantBuffer
|
|
||||||
{
|
|
||||||
DirectX::XMFLOAT4X4 transformMatrix;
|
|
||||||
DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix
|
|
||||||
DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace eg3d {
|
namespace eg3d {
|
||||||
|
|
||||||
@ -88,9 +83,9 @@ namespace eg3d {
|
|||||||
// TODO konstansbuffer példány és DX-objektum
|
// TODO konstansbuffer példány és DX-objektum
|
||||||
ComPtr<ID3D12Resource> cbResource; // konstansbuffer DX-objektuma
|
ComPtr<ID3D12Resource> cbResource; // konstansbuffer DX-objektuma
|
||||||
ConstantBuffer constantBuffer; // konstansbuffer
|
ConstantBuffer constantBuffer; // konstansbuffer
|
||||||
// TODO TRANSZFORMÁCIÓS TULAJDONSÁGOK
|
|
||||||
float rotAngle; // forgatás szöge
|
// virtuális kamera
|
||||||
float camPosition; // kamera pozíciója
|
Cam * pCam;
|
||||||
|
|
||||||
void initDrawObjects(); // kirajzolandó objektumok inicializálása
|
void initDrawObjects(); // kirajzolandó objektumok inicializálása
|
||||||
|
|
||||||
@ -125,12 +120,15 @@ namespace eg3d {
|
|||||||
public:
|
public:
|
||||||
DXWindow();
|
DXWindow();
|
||||||
virtual ~DXWindow();
|
virtual ~DXWindow();
|
||||||
|
|
||||||
|
// TODO paraméter átírása
|
||||||
void Draw(const DrawablePool &drawables);
|
void Draw(const DrawablePool &drawables);
|
||||||
|
|
||||||
ComPtr<ID3D12Device> getDevice() const; // device elkérése
|
ComPtr<ID3D12Device> getDevice() const; // device elkérése
|
||||||
|
|
||||||
void moveCam(float deltaX); // kamera mozgatása
|
void setCam(Cam * pCam); // kamera beállítása
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
37
Entity.cpp
Normal file
37
Entity.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 19..
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Entity.h"
|
||||||
|
|
||||||
|
eg3d::Entity::Entity(const ComPtr<ID3D12Device> &device) : Geometry(device) {
|
||||||
|
loadDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Entity::loadDefaults() {
|
||||||
|
XMStoreFloat4x4(&mWorldMatrix, XMMatrixIdentity()); // egységmátrix betöltése a világ-mátrixba
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Entity::setEntitySRT(const eg3d::SRTProps &srtProps) {
|
||||||
|
mSRTProps = srtProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
eg3d::SRTProps eg3d::Entity::getEntitySRT() const {
|
||||||
|
return mSRTProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::Entity::constructWorldMatrix() {
|
||||||
|
// TODO (Cam alapján)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------
|
||||||
|
|
||||||
|
eg3d::SRTProps::SRTProps() {
|
||||||
|
loadDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
void eg3d::SRTProps::loadDefaults() {
|
||||||
|
scaling = { 1.0f, 1.0f, 1.0f };
|
||||||
|
rotation = { 0.0f, 0.0f, 0.0f };
|
||||||
|
translation = {0.0f, 0.0f, 0.0f };
|
||||||
|
}
|
41
Entity.h
Normal file
41
Entity.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 19..
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef ENTITY
|
||||||
|
#define ENTITY
|
||||||
|
|
||||||
|
#include "Geometry.h"
|
||||||
|
#include "IHasDefault.h"
|
||||||
|
|
||||||
|
namespace eg3d {
|
||||||
|
|
||||||
|
// nagyítás, forgatás, eltolás
|
||||||
|
struct SRTProps : public IHasDefault {
|
||||||
|
XMFLOAT3 scaling; // nagyítás
|
||||||
|
XMFLOAT3 rotation; // forgatás
|
||||||
|
XMFLOAT3 translation; // pozíció
|
||||||
|
|
||||||
|
SRTProps(); // konstr.
|
||||||
|
void loadDefaults() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Entity : public Geometry, public IHasDefault {
|
||||||
|
private:
|
||||||
|
DirectX::XMFLOAT4X4 mWorldMatrix; // világ-mátrix
|
||||||
|
SRTProps mSRTProps; // STR-tulajdonságok
|
||||||
|
|
||||||
|
void constructWorldMatrix(); // világ-mátrix újragenerálása
|
||||||
|
public:
|
||||||
|
Entity(const ComPtr<ID3D12Device> &device);
|
||||||
|
|
||||||
|
void loadDefaults() override;
|
||||||
|
|
||||||
|
void setEntitySRT(const SRTProps& srtProps); // SRT-tulajdonságok beállítása
|
||||||
|
SRTProps getEntitySRT() const; // SRT-tualjdonságok elkérése
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ENTITY
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#include "EventHandler.h"
|
#include "EventHandler.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include <hidusage.h>
|
||||||
eg3d::EventHandler::EventHandler() {
|
eg3d::EventHandler::EventHandler() {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
@ -12,6 +12,9 @@ eg3d::EventHandler::EventHandler() {
|
|||||||
void eg3d::EventHandler::init()
|
void eg3d::EventHandler::init()
|
||||||
{
|
{
|
||||||
mCamVelocity = 0.0f; // kamera sebességének inicializációja
|
mCamVelocity = 0.0f; // kamera sebességének inicializációja
|
||||||
|
mMouseLocked = false; // nincs befogva az egér
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float eg3d::EventHandler::getCamVelocity() const
|
float eg3d::EventHandler::getCamVelocity() const
|
||||||
@ -25,33 +28,57 @@ int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPA
|
|||||||
// TODO eseménykezelés megvalósítása
|
// TODO eseménykezelés megvalósítása
|
||||||
EventHandler * pThis_EH = static_cast<Window *>((void *)GetWindowLongPtrA(hwnd, GWLP_USERDATA))->getEventHandler(); // ablakhoz rendelt eseménykezelõ elkérése
|
EventHandler * pThis_EH = static_cast<Window *>((void *)GetWindowLongPtrA(hwnd, GWLP_USERDATA))->getEventHandler(); // ablakhoz rendelt eseménykezelõ elkérése
|
||||||
|
|
||||||
|
UINT rawDataLength = 64;
|
||||||
|
BYTE pRawData[64];
|
||||||
|
|
||||||
switch (message)
|
switch (message)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
case WM_INPUT:
|
||||||
|
{
|
||||||
|
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, pRawData, &rawDataLength, sizeof(RAWINPUTHEADER));
|
||||||
|
|
||||||
|
RAWINPUT * pRawInput = (RAWINPUT *)pRawData;
|
||||||
|
|
||||||
|
if (pRawInput->header.dwType == RIM_TYPEMOUSE)
|
||||||
|
{
|
||||||
|
int displacementX = pRawInput->data.mouse.lLastX;
|
||||||
|
int displacementY = pRawInput->data.mouse.lLastY;
|
||||||
|
|
||||||
|
LOG(std::string("(") + std::to_string(displacementX) + ", " + std::to_string(displacementY) + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case WM_KEYDOWN:
|
case WM_KEYDOWN:
|
||||||
{
|
{
|
||||||
switch (wParam)
|
switch (wParam)
|
||||||
{
|
{
|
||||||
case 'A':
|
case 'W':
|
||||||
pThis_EH->mCamVelocity = -cCAM_VELOCITY;
|
pThis_EH->mCamVelocity = -cCAM_VELOCITY;
|
||||||
break;
|
break;
|
||||||
case 'D':
|
case 'S':
|
||||||
pThis_EH->mCamVelocity = cCAM_VELOCITY;
|
pThis_EH->mCamVelocity = cCAM_VELOCITY;
|
||||||
break;
|
break;
|
||||||
|
case 'L':
|
||||||
|
pThis_EH->mMouseLocked = !pThis_EH->mMouseLocked;
|
||||||
|
LOG(std::string("Mouse ") + (pThis_EH->mMouseLocked ? "" : "un") + "locked");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WM_KEYUP:
|
case WM_KEYUP:
|
||||||
{
|
{
|
||||||
if (wParam == 'A' || wParam == 'D')
|
if (wParam == 'W' || wParam == 'S')
|
||||||
{
|
{
|
||||||
pThis_EH->mCamVelocity = 0.0f;
|
pThis_EH->mCamVelocity = 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,9 @@ namespace eg3d {
|
|||||||
class EventHandler {
|
class EventHandler {
|
||||||
private:
|
private:
|
||||||
float mCamVelocity; // kamera mozgási sebessége
|
float mCamVelocity; // kamera mozgási sebessége
|
||||||
|
bool mMouseLocked; // lockolvan van az egér?
|
||||||
void init(); // osztály inicializálása
|
|
||||||
|
void init(); // osztály inicializálása
|
||||||
public:
|
public:
|
||||||
static constexpr float cCAM_VELOCITY = 0.05;
|
static constexpr float cCAM_VELOCITY = 0.05;
|
||||||
|
|
||||||
|
145
Geometry.cpp
Normal file
145
Geometry.cpp
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 16..
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Geometry.h"
|
||||||
|
|
||||||
|
namespace eg3d {
|
||||||
|
|
||||||
|
Geometry::Geometry(ComPtr<ID3D12Device> device) : mDevice(device) {
|
||||||
|
// topológia beállítása
|
||||||
|
setTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||||
|
}
|
||||||
|
|
||||||
|
Geometry::Geometry(const Geometry &other) : mDevice(other.mDevice) {
|
||||||
|
setTopology(other.getTopology());
|
||||||
|
setVertices(other.getVertices());
|
||||||
|
setIndices(other.getIndices());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::setVertices(const std::vector<Vertex> &vertices) {
|
||||||
|
mVertices = vertices;
|
||||||
|
|
||||||
|
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
|
||||||
|
bufferMapCopy(mVertexBuffer, (void *) mVertices.data(), bufferSize); // buffer feltöltése
|
||||||
|
|
||||||
|
// vertex buffer view beállítása
|
||||||
|
mVertexBufferView.BufferLocation = mVertexBuffer->GetGPUVirtualAddress();
|
||||||
|
mVertexBufferView.StrideInBytes = sizeof(Vertex);
|
||||||
|
mVertexBufferView.SizeInBytes = bufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Vertex> Geometry::getVertices() const {
|
||||||
|
return mVertices;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::setIndices(const std::vector<uint32_t> &indices) {
|
||||||
|
mIndices = indices;
|
||||||
|
|
||||||
|
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
|
||||||
|
bufferMapCopy(mIndexBuffer, (void *) mIndices.data(), bufferSize); // buffer feltöltése
|
||||||
|
|
||||||
|
// index buffer view beállítása
|
||||||
|
mIndexBufferView.Format = /*(sizeof(IT) == 2) ? DXGI_FORMAT_R16_UINT :*/ DXGI_FORMAT_R32_UINT;
|
||||||
|
mIndexBufferView.BufferLocation = mIndexBuffer->GetGPUVirtualAddress();
|
||||||
|
mIndexBufferView.SizeInBytes = bufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint32_t> Geometry::getIndices() const {
|
||||||
|
return mIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::setTopology(D3D_PRIMITIVE_TOPOLOGY topology) {
|
||||||
|
mTopology = topology;
|
||||||
|
}
|
||||||
|
|
||||||
|
D3D_PRIMITIVE_TOPOLOGY Geometry::getTopology() const {
|
||||||
|
return mTopology;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::setupInputAssembler(ComPtr<ID3D12GraphicsCommandList> commandList)
|
||||||
|
const {
|
||||||
|
commandList->IASetVertexBuffers(0, 1, &mVertexBufferView);
|
||||||
|
commandList->IASetIndexBuffer(&mIndexBufferView);
|
||||||
|
commandList->IASetPrimitiveTopology(mTopology);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::draw(ComPtr<ID3D12GraphicsCommandList> commandList) const {
|
||||||
|
setupInputAssembler(commandList); // input assembler beállítása
|
||||||
|
commandList->DrawIndexedInstanced(mIndices.size(), 1, 0, 0, 0); // rajzolás
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::merge(const Geometry &other) {
|
||||||
|
std::vector<Vertex> vertices = mVertices;
|
||||||
|
vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end());
|
||||||
|
|
||||||
|
uint32_t offset = mIndices.size();
|
||||||
|
std::vector<uint32_t> indices = mIndices;
|
||||||
|
indices.insert(indices.end(), other.mIndices.begin(), other.mIndices.end());
|
||||||
|
|
||||||
|
// indexek megváltozatása offsettel
|
||||||
|
for (uint32_t i = offset; i < indices.size(); i++) {
|
||||||
|
indices[i] += offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
// bufferek újrafoglalása a videokártyán
|
||||||
|
setVertices(vertices);
|
||||||
|
setIndices(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
Geometry &Geometry::operator+=(const Geometry &other) {
|
||||||
|
merge(other);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Geometry::loadBinarySTL(const std::string &fileName) {
|
||||||
|
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
|
||||||
|
LOG("Failed to open file: '" + fileName + "'! STL geometry loading failed!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// vertexek és indexek
|
||||||
|
std::vector<Vertex> vertices;
|
||||||
|
std::vector<uint32_t> indices;
|
||||||
|
|
||||||
|
uint32_t triN; // háromszögek száma
|
||||||
|
|
||||||
|
inFile.seekg(80, std::ios::beg); // fejléc átugrása
|
||||||
|
inFile.read((char *) &triN, sizeof(triN)); // háromszögek számának kiolvasása
|
||||||
|
|
||||||
|
LOG(std::to_string(sizeof(Vertex)));
|
||||||
|
|
||||||
|
// vertexek kiolvasása
|
||||||
|
for (uint32_t i = 0; i < triN; i++) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
inFile.close();
|
||||||
|
|
||||||
|
setVertices(vertices);
|
||||||
|
setIndices(indices);
|
||||||
|
|
||||||
|
LOG("Successful geometry loading from file '" + fileName + "' of " + std::to_string(triN) + " triangles.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
167
Geometry.h
167
Geometry.h
@ -37,162 +37,21 @@ namespace eg3d {
|
|||||||
D3D12_VERTEX_BUFFER_VIEW mVertexBufferView;
|
D3D12_VERTEX_BUFFER_VIEW mVertexBufferView;
|
||||||
D3D12_INDEX_BUFFER_VIEW mIndexBufferView;
|
D3D12_INDEX_BUFFER_VIEW mIndexBufferView;
|
||||||
public:
|
public:
|
||||||
explicit Geometry(ComPtr<ID3D12Device> device) : mDevice(device) {
|
explicit Geometry(ComPtr<ID3D12Device> device); // konstr.
|
||||||
// topológia beállítása
|
Geometry(const Geometry& other); // másolókonstruktor
|
||||||
setTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
void setVertices(const std::vector<Vertex> &vertices); // vertexek beállítása
|
||||||
}
|
std::vector<Vertex> getVertices() const; // vertexek lekérése
|
||||||
|
void setIndices(const std::vector<uint32_t> &indices); // indexek beállítása
|
||||||
// másolókonstruktor
|
std::vector<uint32_t> getIndices() const; // indexek lekérése
|
||||||
Geometry(const Geometry& other) : mDevice(other.mDevice) {
|
void setTopology(D3D_PRIMITIVE_TOPOLOGY topology); // topológia beállítása
|
||||||
setTopology(other.getTopology());
|
D3D_PRIMITIVE_TOPOLOGY getTopology() const; // topológia lekérése
|
||||||
setVertices(other.getVertices());
|
void setupInputAssembler(ComPtr<ID3D12GraphicsCommandList> commandList) const; // input assembler beállítása
|
||||||
setIndices(other.getIndices());
|
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
|
||||||
// vertexek beállítása
|
void loadBinarySTL(const std::string& fileName); // bináris STL fájl betöltése
|
||||||
void setVertices(const std::vector<Vertex> &vertices) {
|
|
||||||
mVertices = vertices;
|
|
||||||
|
|
||||||
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
|
|
||||||
bufferMapCopy(mVertexBuffer, (void *) mVertices.data(), bufferSize); // buffer feltöltése
|
|
||||||
|
|
||||||
// vertex buffer view beállítása
|
|
||||||
mVertexBufferView.BufferLocation = mVertexBuffer->GetGPUVirtualAddress();
|
|
||||||
mVertexBufferView.StrideInBytes = sizeof(Vertex);
|
|
||||||
mVertexBufferView.SizeInBytes = bufferSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
// vertexek lekérése
|
|
||||||
std::vector<Vertex> getVertices() const {
|
|
||||||
return mVertices;
|
|
||||||
}
|
|
||||||
|
|
||||||
// indexek beállítása
|
|
||||||
void setIndices(const std::vector<uint32_t> &indices) {
|
|
||||||
mIndices = indices;
|
|
||||||
|
|
||||||
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
|
|
||||||
bufferMapCopy(mIndexBuffer, (void *) mIndices.data(), bufferSize); // buffer feltöltése
|
|
||||||
|
|
||||||
// index buffer view beállítása
|
|
||||||
mIndexBufferView.Format = /*(sizeof(IT) == 2) ? DXGI_FORMAT_R16_UINT :*/ DXGI_FORMAT_R32_UINT;
|
|
||||||
mIndexBufferView.BufferLocation = mIndexBuffer->GetGPUVirtualAddress();
|
|
||||||
mIndexBufferView.SizeInBytes = bufferSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
// indexek lekérése
|
|
||||||
std::vector<uint32_t> getIndices() const {
|
|
||||||
return mIndices;
|
|
||||||
}
|
|
||||||
|
|
||||||
// topológia beállítása
|
|
||||||
void setTopology(D3D_PRIMITIVE_TOPOLOGY topology) {
|
|
||||||
mTopology = topology;
|
|
||||||
}
|
|
||||||
|
|
||||||
// topológia beállítása
|
|
||||||
D3D_PRIMITIVE_TOPOLOGY getTopology() const {
|
|
||||||
return mTopology;
|
|
||||||
}
|
|
||||||
|
|
||||||
void loadFromFile(const std::string& fileName) {
|
|
||||||
// TODO Epagrisnak: Assimpot le kell fordítani mindenkinek!
|
|
||||||
}
|
|
||||||
|
|
||||||
// input assembler beállítása
|
|
||||||
void setupInputAssembler(ComPtr<ID3D12GraphicsCommandList> commandList) const {
|
|
||||||
commandList->IASetVertexBuffers(0, 1, &mVertexBufferView);
|
|
||||||
commandList->IASetIndexBuffer(&mIndexBufferView);
|
|
||||||
commandList->IASetPrimitiveTopology(mTopology);
|
|
||||||
}
|
|
||||||
|
|
||||||
// kirajzolás
|
|
||||||
void draw(ComPtr<ID3D12GraphicsCommandList> commandList) const override {
|
|
||||||
setupInputAssembler(commandList); // input assembler beállítása
|
|
||||||
commandList->DrawIndexedInstanced(mIndices.size(), 1, 0, 0, 0); // rajzolás
|
|
||||||
}
|
|
||||||
|
|
||||||
// két geometria egyesítése
|
|
||||||
void merge(const Geometry& other)
|
|
||||||
{
|
|
||||||
std::vector<Vertex> vertices = mVertices;
|
|
||||||
vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end());
|
|
||||||
|
|
||||||
uint32_t offset = mIndices.size();
|
|
||||||
std::vector<uint32_t> indices = mIndices;
|
|
||||||
indices.insert(indices.end(), other.mIndices.begin(), other.mIndices.end());
|
|
||||||
|
|
||||||
// indexek megváltozatása offsettel
|
|
||||||
for (uint32_t i = offset; i < indices.size(); i++)
|
|
||||||
{
|
|
||||||
indices[i] += offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bufferek újrafoglalása a videokártyán
|
|
||||||
setVertices(vertices);
|
|
||||||
setIndices(indices);
|
|
||||||
}
|
|
||||||
|
|
||||||
// két geometria egyesítése
|
|
||||||
Geometry& operator+=(const Geometry& other)
|
|
||||||
{
|
|
||||||
merge(other);
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bináris STL fájl betöltése
|
|
||||||
void loadBinarySTL(const std::string& fileName) {
|
|
||||||
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
|
|
||||||
LOG("Failed to open file: '" + fileName + "'! STL geometry loading failed!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// vertexek és indexek
|
|
||||||
std::vector<Vertex> vertices;
|
|
||||||
std::vector<uint32_t> indices;
|
|
||||||
|
|
||||||
uint32_t triN; // háromszögek száma
|
|
||||||
|
|
||||||
inFile.seekg(80, std::ios::beg); // fejléc átugrása
|
|
||||||
inFile.read((char *)&triN, sizeof(triN)); // háromszögek számának kiolvasása
|
|
||||||
|
|
||||||
LOG(std::to_string(sizeof(Vertex)));
|
|
||||||
|
|
||||||
// vertexek kiolvasása
|
|
||||||
for (uint32_t i = 0; i < triN; i++) {
|
|
||||||
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);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
inFile.close();
|
|
||||||
|
|
||||||
setVertices(vertices);
|
|
||||||
setIndices(indices);
|
|
||||||
|
|
||||||
LOG("Successful geometry loading from file '" + fileName + "' of " + std::to_string(triN) + " triangles.");
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// geometriatároló típusa
|
// geometriatároló típusa
|
||||||
using GeometryPool = std::vector<std::shared_ptr<Geometry>>;
|
using GeometryPool = std::vector<std::shared_ptr<Geometry>>;
|
||||||
}
|
}
|
||||||
|
17
IHasDefault.h
Normal file
17
IHasDefault.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 19..
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef IHASDEFAULT
|
||||||
|
#define IHASDEFAULT
|
||||||
|
|
||||||
|
namespace eg3d {
|
||||||
|
|
||||||
|
class IHasDefault {
|
||||||
|
public:
|
||||||
|
virtual void loadDefaults() = 0; // alapértelmezett értékek betöltése
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //IHASDEFAULT
|
14
Window.cpp
14
Window.cpp
@ -1,4 +1,5 @@
|
|||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
|
#include <hidusage.h>
|
||||||
|
|
||||||
namespace eg3d {
|
namespace eg3d {
|
||||||
|
|
||||||
@ -10,6 +11,8 @@ namespace eg3d {
|
|||||||
{
|
{
|
||||||
initializeWndClass(this->pWindowFunction); // window class inicializálása
|
initializeWndClass(this->pWindowFunction); // window class inicializálása
|
||||||
create(); // ablak létrehozása
|
create(); // ablak létrehozása
|
||||||
|
registerRawInputDevice(); // RID-ok regisztrálása
|
||||||
|
|
||||||
this->show(show);
|
this->show(show);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,6 +140,17 @@ namespace eg3d {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::registerRawInputDevice()
|
||||||
|
{
|
||||||
|
RAWINPUTDEVICE device;
|
||||||
|
device.usUsagePage = HID_USAGE_PAGE_GENERIC;
|
||||||
|
device.usUsage = HID_USAGE_GENERIC_MOUSE;
|
||||||
|
device.dwFlags = RIDEV_INPUTSINK;
|
||||||
|
device.hwndTarget = mHWND;
|
||||||
|
|
||||||
|
RegisterRawInputDevices(&device, 1, sizeof(RAWINPUTDEVICE));
|
||||||
|
}
|
||||||
|
|
||||||
void Window::create()
|
void Window::create()
|
||||||
{
|
{
|
||||||
// ablak létrehozása
|
// ablak létrehozása
|
||||||
|
2
Window.h
2
Window.h
@ -21,6 +21,8 @@ namespace eg3d {
|
|||||||
// TODO Epagrisnak: ezt majd még átírom
|
// TODO Epagrisnak: ezt majd még átírom
|
||||||
EventHandler* pmEventHandler; // eseményfeldolgozó osztály pointere
|
EventHandler* pmEventHandler; // eseményfeldolgozó osztály pointere
|
||||||
|
|
||||||
|
void registerRawInputDevice(); // ...
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void create(); // ablak létrehozása
|
void create(); // ablak létrehozása
|
||||||
HWND mHWND; // ablak-handler
|
HWND mHWND; // ablak-handler
|
||||||
|
23
const_bufs.h
Normal file
23
const_bufs.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by Epagris on 2020. 03. 19..
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef CONST_BUFS
|
||||||
|
#define CONST_BUFS
|
||||||
|
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
|
// konstansbuffer struktúrája
|
||||||
|
// TODO refactor!
|
||||||
|
struct ConstantBuffer
|
||||||
|
{
|
||||||
|
DirectX::XMFLOAT4X4 transformMatrix;
|
||||||
|
DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix
|
||||||
|
DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CB_Entity {
|
||||||
|
DirectX::XMFLOAT4X4 worldMatrix; // világ-mátrix (modell-mátrix)
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //CONST_BUFS
|
4
main.cpp
4
main.cpp
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
|
// mátrixok transzponálása be
|
||||||
|
#define MT_ON
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
@ -54,7 +57,6 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
|
|||||||
auto pGeo2 = std::shared_ptr<Geometry>(new Geometry(win.getDevice()));
|
auto pGeo2 = std::shared_ptr<Geometry>(new Geometry(win.getDevice()));
|
||||||
pGeo2->loadBinarySTL("assets/utahteapot.stl");
|
pGeo2->loadBinarySTL("assets/utahteapot.stl");
|
||||||
|
|
||||||
//drawables.push_back(pGeo2);
|
|
||||||
(*pGeo) += (*pGeo2);
|
(*pGeo) += (*pGeo2);
|
||||||
|
|
||||||
DrawablePool drawables; // geometriák halmaza
|
DrawablePool drawables; // geometriák halmaza
|
||||||
|
10
utils.h
10
utils.h
@ -75,4 +75,12 @@ inline void bufferMapCopy(ComPtr<ID3D12Resource> bufferResource, void * data, si
|
|||||||
bufferResource->Map(0, nullptr, reinterpret_cast<void**>(&mappedData));
|
bufferResource->Map(0, nullptr, reinterpret_cast<void**>(&mappedData));
|
||||||
memcpy(mappedData, data, size);
|
memcpy(mappedData, data, size);
|
||||||
bufferResource->Unmap(0, nullptr);
|
bufferResource->Unmap(0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// feltételes transzponálás (ha definiálva van, akkor automatikusan transzponál minden mátrixot)
|
||||||
|
#ifdef MT_ON
|
||||||
|
#define MT(m) XMMatrixTranspose(m)
|
||||||
|
#endif
|
||||||
|
#ifndef MT_ON
|
||||||
|
#define MT(m) (m)
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user