Partily working (before transparaent)

This commit is contained in:
2024-12-18 10:46:56 +01:00
parent 78a38ec5dc
commit 8a19a2afa1
8 changed files with 169038 additions and 41 deletions

View File

@@ -3,6 +3,9 @@ cmake_minimum_required(VERSION 3.27)
# Project Name
project(GP1_DirectX)
# Use C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

15
LinkTinyObjLoader.cmake Normal file
View File

@@ -0,0 +1,15 @@
include(FetchContent)
macro(linkTinyObjLoader TARGET ACCESS)
FetchContent_Declare(
tinyobjloader
GIT_REPOSITORY https://github.com/tinyobjloader/tinyobjloader.git
GIT_TAG release
)
FetchContent_MakeAvailable(tinyobjloader)
target_link_libraries(${TARGET} ${ACCESS} tinyobjloader)
target_include_directories(${TARGET} PUBLIC ${tinyobjloader_SOURCE_DIR})
endmacro()

View File

@@ -19,6 +19,9 @@ set(SOURCES
# Create the executable
add_executable(${PROJECT_NAME} ${SOURCES})
include(../LinkTinyObjLoader.cmake)
LinkTinyObjLoader(${PROJECT_NAME} PUBLIC)
# only needed if header files are not in same directory as source files
# target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

165658
project/resources/scene.obj Normal file

File diff suppressed because it is too large Load Diff

3242
project/resources/test.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -4,17 +4,15 @@
#include "Effect.h"
Mesh::Mesh(ID3D11Device* devicePtr, const std::vector<VertexIn>& verticesIn, const std::vector<Uint32>& indices, const Material& material) :
m_EffectPtr(new Effect{ devicePtr, L"resources/PosCol3D.fx" }),
Mesh::Mesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices, const Material &material) :
m_EffectPtr(new Effect{devicePtr, L"resources/PosCol3D.fx"}),
m_InputLayoutPtr(nullptr),
m_VertexBufferPtr(nullptr),
m_IndexBufferPtr(nullptr),
m_VerticesIn(verticesIn),
m_Indices(indices),
m_IndicesCount(static_cast<UINT>(m_Indices.size())),
m_Material(material)
{
m_Material(material) {
HRESULT result;
D3D11_BUFFER_DESC bufferDesc{};
D3D11_SUBRESOURCE_DATA subresourceData{};
@@ -22,7 +20,7 @@ Mesh::Mesh(ID3D11Device* devicePtr, const std::vector<VertexIn>& verticesIn, con
m_EffectPtr->SetMaterial(m_Material);
//Create vertex layout
static constexpr uint32_t vertexElementCount{ 3 };
static constexpr uint32_t vertexElementCount{3};
D3D11_INPUT_ELEMENT_DESC vertexDesc[vertexElementCount]{};
vertexDesc[0].SemanticName = "POSITION";
@@ -45,7 +43,7 @@ Mesh::Mesh(ID3D11Device* devicePtr, const std::vector<VertexIn>& verticesIn, con
//Create input layout
D3DX11_PASS_DESC passDesc{};
ID3DX11EffectTechnique* techniquePtr = m_EffectPtr->GetTechniquePtr();
ID3DX11EffectTechnique *techniquePtr = m_EffectPtr->GetTechniquePtr();
techniquePtr->GetPassByIndex(0)->GetDesc(&passDesc);
result = devicePtr->CreateInputLayout(
@@ -80,8 +78,7 @@ Mesh::Mesh(ID3D11Device* devicePtr, const std::vector<VertexIn>& verticesIn, con
assert(result == S_OK && "Creating index buffer failed");
}
Mesh::~Mesh()
{
Mesh::~Mesh() {
m_InputLayoutPtr->Release();
m_InputLayoutPtr = nullptr;
@@ -95,8 +92,7 @@ Mesh::~Mesh()
m_EffectPtr = nullptr;
}
void Mesh::Render(ID3D11DeviceContext* deviceContextPtr, const Matrix& worldViewProj) const
{
void Mesh::Render(ID3D11DeviceContext *deviceContextPtr, const Matrix &worldViewProj) const {
m_EffectPtr->SetWorldViewProjMatrix(worldViewProj);
//1. Set primitive topology
deviceContextPtr->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
@@ -115,8 +111,7 @@ void Mesh::Render(ID3D11DeviceContext* deviceContextPtr, const Matrix& worldView
//5. Draw
D3DX11_TECHNIQUE_DESC techniqueDesc{};
m_EffectPtr->GetTechniquePtr()->GetDesc(&techniqueDesc);
for (UINT p{}; p < techniqueDesc.Passes; p++)
{
for (UINT p{}; p < techniqueDesc.Passes; p++) {
m_EffectPtr->GetTechniquePtr()->GetPassByIndex(p)->Apply(0, deviceContextPtr);
deviceContextPtr->DrawIndexed(m_IndicesCount, 0, 0);
}

View File

@@ -21,28 +21,17 @@ namespace dae {
std::vector<VertexIn> vertices{};
std::vector<uint32_t> indices{};
if(Utils::ParseOBJ("resources/vehicle.obj", vertices, indices, false)){
if (Utils::ParseOBJNew("resources/scene.obj", vertices, indices, false)) {
std::cout << "Model Loaded" << std::endl;
} else {
std::cout << "Model failed to load" << std::endl;
assert(true && "Model failed to load");
}
std::vector<VertexIn> quad = {
{{-1.f, 1.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 0.f}},
{{1.f, 1.f, 0.f}, {0.f, 1.f, 0.f}, {1.f, 0.f}},
{{1.f, -1.f, 0.f}, {0.f, 0.f, 1.f}, {1.f, 1.f}},
{{-1.f, -1.f, 0.f}, {1.f, 1.f, 1.f}, {0.f, 1.f}}
};
std::vector<uint32_t> quadIndices = {
0, 1, 2,
0, 2, 3
};
std::cout << "Vertices: " << vertices.size() << " Indices: " << indices.size() << std::endl;
m_material.diffuseTexturePtr = Texture::LoadFromFile("resources/vehicle_diffuse.png", m_DevicePtr);
m_mesh = new Mesh(m_DevicePtr, quad, quadIndices, m_material);
m_material.diffuseTexturePtr = Texture::LoadFromFile("resources/uv_grid_2.png", m_DevicePtr);
m_mesh = new Mesh(m_DevicePtr, vertices, indices, m_material);
m_Camera = Camera(Vector3(0.f, 0.f, -5.f), 90.f);
m_Camera.Initialize(90.f, Vector3(0.f, 0.f, -5.f), 1.f);
@@ -57,8 +46,7 @@ namespace dae {
m_SwapChainPtr->Release();
if(m_DeviceContextPtr)
{
if (m_DeviceContextPtr) {
m_DeviceContextPtr->ClearState();
m_DeviceContextPtr->Flush();
m_DeviceContextPtr->Release();
@@ -81,7 +69,7 @@ namespace dae {
return;
//Clear back buffer
const float clearColor[] = { .39f, .59f, .93f, 1.f };
const float clearColor[] = {.39f, .59f, .93f, 1.f};
m_DeviceContextPtr->ClearRenderTargetView(m_RenderTargetViewPtr, clearColor);
m_DeviceContextPtr->ClearDepthStencilView(m_DepthStencilViewPtr, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
@@ -112,8 +100,8 @@ namespace dae {
return result;
//Create DXGI factory
IDXGIFactory1* DxgiFactoryPtr{};
result = CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&DxgiFactoryPtr));
IDXGIFactory1 *DxgiFactoryPtr{};
result = CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void **>(&DxgiFactoryPtr));
if (FAILED(result))
return result;
@@ -179,7 +167,7 @@ namespace dae {
//.4 Create RenderTarget (RT) & RenderTargetView (RTV)
//Resource
result = m_SwapChainPtr->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&m_RenderTargetBufferPtr));
result = m_SwapChainPtr->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&m_RenderTargetBufferPtr));
if (FAILED(result))
return result;

View File

@@ -1,17 +1,19 @@
#pragma once
#include <fstream>
#include <vector>
#include "Math.h"
#include "Mesh.h"
#include "tiny_obj_loader.h"
namespace dae
{
namespace Utils
{
//Just parses vertices and indices
namespace dae {
namespace Utils {
//Just parses vertices and indices
#pragma warning(push)
#pragma warning(disable : 4505) //Warning unreferenced local function
static bool ParseOBJ(const std::string& filename, std::vector<VertexIn>& vertices, std::vector<uint32_t>& indices, bool flipAxisAndWinding = true) {
static bool ParseOBJ(const std::string &filename, std::vector<VertexIn> &vertices, std::vector<uint32_t> &indices,
bool flipAxisAndWinding = true) {
std::ifstream file(filename);
if (!file)
return false;
@@ -140,6 +142,97 @@ namespace dae
return true;
}
#pragma warning(pop)
}
static bool ParseOBJNew(const std::string &fileName, std::vector<VertexIn> &vertices, std::vector<uint32_t> &indices,
bool flipAxisAndWinding = true) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn, err;
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, fileName.c_str())) {
if (!warn.empty()) {
std::cerr << "Warning: " << warn << std::endl;
}
if (!err.empty()) {
std::cerr << "Error: " << err << std::endl;
}
return false;
}
vertices.clear();
indices.clear();
for (const auto &shape: shapes) {
size_t index_offset = 0;
for (size_t f = 0; f < shape.mesh.num_face_vertices.size(); f++) {
size_t fv = size_t(shape.mesh.num_face_vertices[f]);
if (fv != 3) {
std::cerr << "Error: Only triangular faces are supported." << std::endl;
return false;
}
uint32_t tempIndices[3];
for (size_t v = 0; v < fv; v++) {
tinyobj::index_t idx = shape.mesh.indices[index_offset + v];
VertexIn vertex;
if (idx.vertex_index >= 0) {
vertex.position = Vector3(
attrib.vertices[3 * idx.vertex_index + 0],
attrib.vertices[3 * idx.vertex_index + 1],
attrib.vertices[3 * idx.vertex_index + 2]
);
if (flipAxisAndWinding) {
vertex.position.z *= -1.0f;
}
}
if (idx.texcoord_index >= 0) {
vertex.uv = Vector2(
attrib.texcoords[2 * idx.texcoord_index + 0],
1.0f - attrib.texcoords[2 * idx.texcoord_index + 1] // Flip V
);
}
if (idx.normal_index >= 0) {
// vertex.normal = Vector3(
// attrib.normals[3 * idx.normal_index + 0],
// attrib.normals[3 * idx.normal_index + 1],
// attrib.normals[3 * idx.normal_index + 2]
// );
// if (flipAxisAndWinding) {
// vertex.normal.z *= -1.0f;
// }
}
vertices.push_back(vertex);
tempIndices[v] = static_cast<uint32_t>(vertices.size() - 1);
}
indices.push_back(tempIndices[0]);
if (flipAxisAndWinding) {
indices.push_back(tempIndices[2]);
indices.push_back(tempIndices[1]);
} else {
indices.push_back(tempIndices[1]);
indices.push_back(tempIndices[2]);
}
index_offset += fv;
shape.mesh.material_ids[f];
}
}
return true;
}
}
}