Fix
This commit is contained in:
@@ -152,16 +152,6 @@ namespace dae {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Matrix Matrix::CreatePerspectiveFovLH(float fovy, float aspect, float zn, float zf) {
|
Matrix Matrix::CreatePerspectiveFovLH(float fovy, float aspect, float zn, float zf) {
|
||||||
// const float yScale = 1.f / tanf(fovy / 2.f);
|
|
||||||
// const float xScale = yScale / aspect;
|
|
||||||
//
|
|
||||||
// return {
|
|
||||||
// {xScale, 0, 0, 0},
|
|
||||||
// {0, yScale, 0, 0},
|
|
||||||
// {0, 0, zf / (zf - zn), 1},
|
|
||||||
// {0, 0, -zn * zf / (zf - zn), 0}
|
|
||||||
// };
|
|
||||||
|
|
||||||
return Matrix(
|
return Matrix(
|
||||||
{ 1.f / (aspect * fovy), 0, 0, 0 },
|
{ 1.f / (aspect * fovy), 0, 0, 0 },
|
||||||
{ 0, 1.f / fovy, 0, 0 },
|
{ 0, 1.f / fovy, 0, 0 },
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ void Renderer::Render() {
|
|||||||
ColorRGB finalColor{};
|
ColorRGB finalColor{};
|
||||||
constexpr int numVerticies = 3;
|
constexpr int numVerticies = 3;
|
||||||
|
|
||||||
std::vector<Vertex> verticiesScreenSpace{};
|
verticiesScreenSpace.clear();
|
||||||
|
|
||||||
for (const Mesh ¤tMesh: m_worldMeshes) {
|
for (const Mesh ¤tMesh: m_worldMeshes) {
|
||||||
|
|
||||||
@@ -184,6 +184,24 @@ void Renderer::Render() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float min{std::numeric_limits<float>::max()};
|
||||||
|
float max{std::numeric_limits<float>::min()};
|
||||||
|
|
||||||
|
for (int i{}; i < m_Width * m_Height; ++i) {
|
||||||
|
if (m_pDepthBufferPixels[i] == std::numeric_limits<float>::max()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
min = std::min(min, m_pDepthBufferPixels[i]);
|
||||||
|
max = std::max(max, m_pDepthBufferPixels[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i{}; i < m_Width * m_Height; ++i) {
|
||||||
|
if (m_pDepthBufferPixels[i] == std::numeric_limits<float>::max()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
m_pDepthBufferPixels[i] = (m_pDepthBufferPixels[i] - min) / (max - min);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//RENDER LOGIC
|
//RENDER LOGIC
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ namespace dae {
|
|||||||
|
|
||||||
|
|
||||||
std::vector<Mesh> m_worldMeshes{};
|
std::vector<Mesh> m_worldMeshes{};
|
||||||
|
std::vector<Vertex> verticiesScreenSpace{};
|
||||||
|
|
||||||
float* m_pDepthBufferPixels{};
|
float* m_pDepthBufferPixels{};
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "Maths.h"
|
#include "Maths.h"
|
||||||
@@ -6,23 +7,14 @@
|
|||||||
|
|
||||||
//#define DISABLE_OBJ
|
//#define DISABLE_OBJ
|
||||||
|
|
||||||
namespace dae
|
namespace dae {
|
||||||
{
|
namespace Utils {
|
||||||
namespace Utils
|
|
||||||
{
|
|
||||||
//Just parses vertices and indices
|
//Just parses vertices and indices
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
#pragma warning(disable : 4505) //Warning unreferenced local function
|
#pragma warning(disable : 4505) //Warning unreferenced local function
|
||||||
static bool ParseOBJ(const std::string& filename, std::vector<Vertex>& vertices, std::vector<uint32_t>& indices, bool flipAxisAndWinding = true)
|
|
||||||
{
|
|
||||||
#ifdef DISABLE_OBJ
|
|
||||||
|
|
||||||
//TODO: Enable the code below after uncommenting all the vertex attributes of DataTypes::Vertex
|
|
||||||
// >> Comment/Remove '#define DISABLE_OBJ'
|
|
||||||
assert(false && "OBJ PARSER not enabled! Check the comments in Utils::ParseOBJ");
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
|
static bool ParseOBJ(const std::string &filename, std::vector<Vertex> &vertices, std::vector<uint32_t> &indices,
|
||||||
|
bool flipAxisAndWinding = true) {
|
||||||
std::ifstream file(filename);
|
std::ifstream file(filename);
|
||||||
if (!file)
|
if (!file)
|
||||||
return false;
|
return false;
|
||||||
@@ -36,40 +28,30 @@ namespace dae
|
|||||||
|
|
||||||
std::string sCommand;
|
std::string sCommand;
|
||||||
// start a while iteration ending when the end of file is reached (ios::eof)
|
// start a while iteration ending when the end of file is reached (ios::eof)
|
||||||
while (!file.eof())
|
while (!file.eof()) {
|
||||||
{
|
|
||||||
//read the first word of the string, use the >> operator (istream::operator>>)
|
//read the first word of the string, use the >> operator (istream::operator>>)
|
||||||
file >> sCommand;
|
file >> sCommand;
|
||||||
//use conditional statements to process the different commands
|
//use conditional statements to process the different commands
|
||||||
if (sCommand == "#")
|
if (sCommand == "#") {
|
||||||
{
|
|
||||||
// Ignore Comment
|
// Ignore Comment
|
||||||
}
|
} else if (sCommand == "v") {
|
||||||
else if (sCommand == "v")
|
|
||||||
{
|
|
||||||
//Vertex
|
//Vertex
|
||||||
float x, y, z;
|
float x, y, z;
|
||||||
file >> x >> y >> z;
|
file >> x >> y >> z;
|
||||||
|
|
||||||
positions.emplace_back(x, y, z);
|
positions.emplace_back(x, y, z);
|
||||||
}
|
} else if (sCommand == "vt") {
|
||||||
else if (sCommand == "vt")
|
|
||||||
{
|
|
||||||
// Vertex TexCoord
|
// Vertex TexCoord
|
||||||
float u, v;
|
float u, v;
|
||||||
file >> u >> v;
|
file >> u >> v;
|
||||||
UVs.emplace_back(u, 1 - v);
|
UVs.emplace_back(u, 1 - v);
|
||||||
}
|
} else if (sCommand == "vn") {
|
||||||
else if (sCommand == "vn")
|
|
||||||
{
|
|
||||||
// Vertex Normal
|
// Vertex Normal
|
||||||
float x, y, z;
|
float x, y, z;
|
||||||
file >> x >> y >> z;
|
file >> x >> y >> z;
|
||||||
|
|
||||||
normals.emplace_back(x, y, z);
|
normals.emplace_back(x, y, z);
|
||||||
}
|
} else if (sCommand == "f") {
|
||||||
else if (sCommand == "f")
|
|
||||||
{
|
|
||||||
//if a face is read:
|
//if a face is read:
|
||||||
//construct the 3 vertices, add them to the vertex array
|
//construct the 3 vertices, add them to the vertex array
|
||||||
//add three indices to the index array
|
//add three indices to the index array
|
||||||
@@ -80,8 +62,7 @@ namespace dae
|
|||||||
size_t iPosition, iTexCoord, iNormal;
|
size_t iPosition, iTexCoord, iNormal;
|
||||||
|
|
||||||
uint32_t tempIndices[3];
|
uint32_t tempIndices[3];
|
||||||
for (size_t iFace = 0; iFace < 3; iFace++)
|
for (size_t iFace = 0; iFace < 3; iFace++) {
|
||||||
{
|
|
||||||
// OBJ format uses 1-based arrays
|
// OBJ format uses 1-based arrays
|
||||||
file >> iPosition;
|
file >> iPosition;
|
||||||
vertex.position = positions[iPosition - 1].ToVector4();
|
vertex.position = positions[iPosition - 1].ToVector4();
|
||||||
@@ -90,15 +71,13 @@ namespace dae
|
|||||||
{
|
{
|
||||||
file.ignore();//read and ignore one element ('/')
|
file.ignore();//read and ignore one element ('/')
|
||||||
|
|
||||||
if ('/' != file.peek())
|
if ('/' != file.peek()) {
|
||||||
{
|
|
||||||
// Optional texture coordinate
|
// Optional texture coordinate
|
||||||
file >> iTexCoord;
|
file >> iTexCoord;
|
||||||
vertex.uv = UVs[iTexCoord - 1];
|
vertex.uv = UVs[iTexCoord - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('/' == file.peek())
|
if ('/' == file.peek()) {
|
||||||
{
|
|
||||||
file.ignore();
|
file.ignore();
|
||||||
|
|
||||||
// Optional vertex normal
|
// Optional vertex normal
|
||||||
@@ -113,13 +92,10 @@ namespace dae
|
|||||||
}
|
}
|
||||||
|
|
||||||
indices.push_back(tempIndices[0]);
|
indices.push_back(tempIndices[0]);
|
||||||
if (flipAxisAndWinding)
|
if (flipAxisAndWinding) {
|
||||||
{
|
|
||||||
indices.push_back(tempIndices[2]);
|
indices.push_back(tempIndices[2]);
|
||||||
indices.push_back(tempIndices[1]);
|
indices.push_back(tempIndices[1]);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
indices.push_back(tempIndices[1]);
|
indices.push_back(tempIndices[1]);
|
||||||
indices.push_back(tempIndices[2]);
|
indices.push_back(tempIndices[2]);
|
||||||
}
|
}
|
||||||
@@ -129,8 +105,7 @@ namespace dae
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Cheap Tangent Calculations
|
//Cheap Tangent Calculations
|
||||||
for (uint32_t i = 0; i < indices.size(); i += 3)
|
for (uint32_t i = 0; i < indices.size(); i += 3) {
|
||||||
{
|
|
||||||
uint32_t index0 = indices[i];
|
uint32_t index0 = indices[i];
|
||||||
uint32_t index1 = indices[size_t(i) + 1];
|
uint32_t index1 = indices[size_t(i) + 1];
|
||||||
uint32_t index2 = indices[size_t(i) + 2];
|
uint32_t index2 = indices[size_t(i) + 2];
|
||||||
@@ -155,12 +130,10 @@ namespace dae
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Fix the tangents per vertex now because we accumulated
|
//Fix the tangents per vertex now because we accumulated
|
||||||
for (auto& v : vertices)
|
for (auto &v: vertices) {
|
||||||
{
|
|
||||||
v.tangent = Vector3::Reject(v.tangent, v.normal).Normalized();
|
v.tangent = Vector3::Reject(v.tangent, v.normal).Normalized();
|
||||||
|
|
||||||
if(flipAxisAndWinding)
|
if (flipAxisAndWinding) {
|
||||||
{
|
|
||||||
v.position.z *= -1.f;
|
v.position.z *= -1.f;
|
||||||
v.normal.z *= -1.f;
|
v.normal.z *= -1.f;
|
||||||
v.tangent.z *= -1.f;
|
v.tangent.z *= -1.f;
|
||||||
@@ -169,7 +142,6 @@ namespace dae
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user