STL loading fixed

This commit is contained in:
Wiesner András 2020-03-15 18:17:11 +01:00
parent 41eafc716e
commit 0d63bbd662
9 changed files with 107 additions and 33 deletions

View File

@ -11,6 +11,7 @@ namespace eg3d {
initDrawObjects(); initDrawObjects();
rotAngle = 0.0f; rotAngle = 0.0f;
camPosition = 0.0f;
} }
@ -378,18 +379,20 @@ namespace eg3d {
// transzformáció frissítése // transzformáció frissítése
//DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixScaling(2.0, 1.0, 1.0)); //DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixScaling(2.0, 1.0, 1.0));
rotAngle = rotAngle - 0.01; rotAngle = rotAngle + 0.01f;
if (rotAngle > XM_2PI) if (rotAngle > XM_2PI)
{ {
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,
//XMMatrixScaling(0.00001, 0.00001, 0.00001) * XMMatrixTranslation(0.0f, 0.0f, -100000.0f) * DirectX::XMMatrixTranspose(XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 1000)));
DirectX::XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 10));
DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixRotationZ(rotAngle));
bufferMapCopy(cbResource, static_cast<void *>(&constantBuffer), sizeof(constantBuffer)); bufferMapCopy(cbResource, static_cast<void *>(&constantBuffer), sizeof(constantBuffer));
} }
@ -426,7 +429,7 @@ namespace eg3d {
psoD.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT); psoD.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
//psoD.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; //psoD.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
//psoD.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME; psoD.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME;
psoD.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT); psoD.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
psoD.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT); psoD.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
psoD.SampleMask = UINT_MAX; psoD.SampleMask = UINT_MAX;
@ -460,4 +463,9 @@ namespace eg3d {
} }
} }
void DXWindow::moveCam(float deltaX)
{
camPosition += deltaX;
}
} }

View File

@ -22,6 +22,7 @@
struct ConstantBuffer struct ConstantBuffer
{ {
DirectX::XMFLOAT4X4 transformMatrix; DirectX::XMFLOAT4X4 transformMatrix;
DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix
DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix
}; };
@ -89,6 +90,7 @@ namespace eg3d {
ConstantBuffer constantBuffer; // konstansbuffer ConstantBuffer constantBuffer; // konstansbuffer
// TODO TRANSZFORMÁCIÓS TULAJDONSÁGOK // TODO TRANSZFORMÁCIÓS TULAJDONSÁGOK
float rotAngle; // forgatás szöge float rotAngle; // forgatás szöge
float camPosition; // kamera pozíciója
void initDrawObjects(); // kirajzolandó objektumok inicializálása void initDrawObjects(); // kirajzolandó objektumok inicializálása
@ -127,6 +129,8 @@ namespace eg3d {
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
}; };
} }

View File

@ -3,15 +3,58 @@
// //
#include "EventHandler.h" #include "EventHandler.h"
#include "Logger.h"
eg3d::EventHandler::EventHandler() { eg3d::EventHandler::EventHandler() {
init();
} }
void eg3d::EventHandler::init()
{
mCamVelocity = 0.0f; // kamera sebességének inicializációja
}
float eg3d::EventHandler::getCamVelocity() const
{
return mCamVelocity;
}
// -----------------------------------
int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
// 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
// switch()..... switch (message)
{
case WM_KEYDOWN:
{
switch (wParam)
{
case 'A':
pThis_EH->mCamVelocity = -cCAM_VELOCITY;
break;
case 'D':
pThis_EH->mCamVelocity = cCAM_VELOCITY;
break;
}
}
break;
case WM_KEYUP:
{
if (wParam == 'A' || wParam == 'D')
{
pThis_EH->mCamVelocity = 0.0f;
}
}
break;
default:
return 1;
}
return 0; return 0;
} }

View File

@ -5,9 +5,16 @@
namespace eg3d { namespace eg3d {
class EventHandler { class EventHandler {
private:
float mCamVelocity; // kamera mozgási sebessége
void init(); // osztály inicializálása
public: public:
static constexpr float cCAM_VELOCITY = 0.05;
EventHandler(); // konstr. EventHandler(); // konstr.
int processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); // esemény feldolgozása (0 -> feldolgozva, 1 -> nincs feldolgozva) 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
}; };
} }

View File

@ -119,17 +119,17 @@ namespace eg3d {
} }
// két geometria egyesítése // két geometria egyesítése
Geometry& operator+=(const Geometry& other) void merge(const Geometry& other)
{ {
std::vector<Vertex> vertices = mVertices; std::vector<Vertex> vertices = mVertices;
vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end()); vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end());
uint32_t offset = mIndices.size() - 1; uint32_t offset = mIndices.size();
std::vector<uint32_t> indices = mIndices; std::vector<uint32_t> indices = mIndices;
indices.insert(indices.end(), other.mIndices.begin(), other.mIndices.end()); indices.insert(indices.end(), other.mIndices.begin(), other.mIndices.end());
// indexek megváltozatása offsettel // indexek megváltozatása offsettel
for (uint32_t i = offset + 1; i < indices.size(); i++) for (uint32_t i = offset; i < indices.size(); i++)
{ {
indices[i] += offset; indices[i] += offset;
} }
@ -137,6 +137,12 @@ namespace eg3d {
// bufferek újrafoglalása a videokártyán // bufferek újrafoglalása a videokártyán
setVertices(vertices); setVertices(vertices);
setIndices(indices); setIndices(indices);
}
// két geometria egyesítése
Geometry& operator+=(const Geometry& other)
{
merge(other);
return *this; return *this;
} }
@ -159,14 +165,16 @@ namespace eg3d {
inFile.seekg(80, std::ios::beg); // fejléc átugrása inFile.seekg(80, std::ios::beg); // fejléc átugrása
inFile.read((char *)&triN, sizeof(triN)); // háromszögek számának kiolvasása inFile.read((char *)&triN, sizeof(triN)); // háromszögek számának kiolvasása
LOG(std::to_string(sizeof(Vertex)));
// vertexek kiolvasása // vertexek kiolvasása
for (uint32_t i = 0; i < triN; i++) { for (uint32_t i = 0; i < triN; i++) {
STLTriangle triangle; STLTriangle triangle;
inFile.read((char *)&triangle, sizeof(triangle)); // egy háromszög betöltése inFile.read(reinterpret_cast<char *>(&triangle), 50); // egy háromszög betöltése
vertices.push_back(triangle.v1); vertices.emplace_back(triangle.v1);
vertices.push_back(triangle.v2); vertices.emplace_back(triangle.v2);
vertices.push_back(triangle.v3); vertices.emplace_back(triangle.v3);
// indexek vektorának feltöltése // indexek vektorának feltöltése
uint32_t firstIndex = 3 * i; uint32_t firstIndex = 3 * i;

Binary file not shown.

View File

@ -13,6 +13,7 @@ struct VSOutput // vertex shader kimeneti strukt
cbuffer CBuf : register(b0) cbuffer CBuf : register(b0)
{ {
float4x4 transformMatrix; float4x4 transformMatrix;
float4x4 viewMatrix;
float4x4 projMatrix; float4x4 projMatrix;
}; };
@ -20,7 +21,7 @@ VSOutput vs_main(VSInput input)
{ {
VSOutput output; // vertex shader kimenete VSOutput output; // vertex shader kimenete
output.position_H = mul(projMatrix, mul(transformMatrix, float4(input.position_D, 1.0))); output.position_H = mul(float4(input.position_D, 1.0), mul(mul(transformMatrix, viewMatrix), projMatrix));
output.color = float3(1.0, 1.0, 1.0); output.color = float3(1.0, 1.0, 1.0);
return output; return output;

View File

@ -126,9 +126,9 @@ namespace eg3d {
break; break;
default: // minden egyéb esemény kezelése default: // minden egyéb esemény kezelése
if (pWindow != nullptr && pWindow->getEventHandler() != nullptr) { // ha meg van adva eseménykezelő if (pWindow != nullptr && pWindow->getEventHandler() != nullptr) { // ha meg van adva eseménykezelő
/*if (pWindow->getEventHandler()->processEvent(hwnd, message, wParam, lParam) != 0) { // ha nem lett lekezelve az esemény if (pWindow->getEventHandler()->processEvent(hwnd, message, wParam, lParam) != 0) { // ha nem lett lekezelve az esemény
return DefWindowProc(hwnd, message, wParam, lParam); // ...akkor rábízzuk a rendszerre return DefWindowProc(hwnd, message, wParam, lParam); // ...akkor rábízzuk a rendszerre
}*/ }
} }
return DefWindowProc(hwnd, message, wParam, lParam); // ...akkor rábízzuk a rendszerre return DefWindowProc(hwnd, message, wParam, lParam); // ...akkor rábízzuk a rendszerre

View File

@ -10,7 +10,6 @@
#include "Timer.h" #include "Timer.h"
#include "Geometry.h" #include "Geometry.h"
#include "not_used/vertex_types.h"
// FPS-mérés // FPS-mérés
size_t gFrames = 0; size_t gFrames = 0;
@ -31,6 +30,8 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
win.setTitle("Keretszöveg"); // keretszöveg beállítása win.setTitle("Keretszöveg"); // keretszöveg beállítása
// TODO eseménykezelő létrehozása és ablakhoz rendelése // TODO eseménykezelő létrehozása és ablakhoz rendelése
EventHandler eventHandler;
win.setEventHandler(&eventHandler);
// FPS-mérő callback-függvény regisztrálása // FPS-mérő callback-függvény regisztrálása
TimerCallbackData timerCBData; TimerCallbackData timerCBData;
@ -44,20 +45,21 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
{ 0.0f, 0.98f, 1.0f }, { 0.0f, 0.98f, 1.0f },
}; };
std::vector<uint32_t> indices = { 0, 2, 1 }; // indexek std::vector<uint32_t> indices = { 0, 1, 2 }; // indexek
auto pGeo = std::shared_ptr<Geometry>(new Geometry(win.getDevice())); auto pGeo = std::shared_ptr<Geometry>(new Geometry(win.getDevice()));
pGeo->setVertices(vertices); pGeo->setVertices(vertices);
pGeo->setIndices(indices); pGeo->setIndices(indices);
auto pGeo2 = std::shared_ptr<Geometry>(new Geometry(win.getDevice())); auto pGeo2 = std::shared_ptr<Geometry>(new Geometry(win.getDevice()));
pGeo2->loadBinarySTL("assets/crate.stl"); pGeo2->loadBinarySTL("assets/utahteapot.stl");
//drawables.push_back(pGeo2); //drawables.push_back(pGeo2);
//(*pGeo) += (*pGeo2); (*pGeo) += (*pGeo2);
DrawablePool drawables; // geometriák halmaza DrawablePool drawables; // geometriák halmaza
drawables.push_back(pGeo); // pGeo geometria hozzáadása drawables.push_back(pGeo); // pGeo geometria hozzáadása
//drawables.push_back(pGeo);
// ------------------------------------ // ------------------------------------
@ -74,6 +76,7 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA
{ {
Sleep(15); Sleep(15);
win.moveCam(eventHandler.getCamVelocity()); // kamera elmozdítása
win.Draw(drawables); // kirajzolás win.Draw(drawables); // kirajzolás
gFrames++; // képkockaszám léptetése gFrames++; // képkockaszám léptetése