adding brdf, but virtual is probably terrible for performance

This commit is contained in:
Dynamitos
2025-01-26 15:27:43 +01:00
parent 8fdaff1e4c
commit 98020d4455
9 changed files with 104 additions and 24 deletions
+2 -1
View File
@@ -93,7 +93,8 @@ void Renderer::render(Camera camera, RenderParameter params)
if (intersection.has_value()) if (intersection.has_value())
{ {
accumulator[w + h * params.width] = intersection->albedo;
accumulator[w + h * params.width] = intersection->shadingInfo.albedo;
} }
} }
co_return; co_return;
+32 -11
View File
@@ -80,9 +80,9 @@ std::optional<IntersectionInfo> Scene::traceRay(Ray ray) const
IntersectionInfo info; IntersectionInfo info;
for (uint32_t i = 0; i < results.size(); ++i) for (uint32_t i = 0; i < results.size(); ++i)
{ {
if (results[i].t < closestT) if (results[i].hitInfo.t < closestT)
{ {
closestT = results[i].t; closestT = results[i].hitInfo.t;
info = results[i]; info = results[i];
} }
} }
@@ -128,14 +128,22 @@ std::optional<IntersectionInfo> Scene::intersectModel(const ModelReference& refe
for (size_t posIndex = 0, edgeIndex = 0, normalIndex = 0; posIndex < reference.numIndices; posIndex++, edgeIndex += 2, normalIndex++) for (size_t posIndex = 0, edgeIndex = 0, normalIndex = 0; posIndex < reference.numIndices; posIndex++, edgeIndex += 2, normalIndex++)
{ {
const auto p0 = positionPool[reference.positionOffset + indicesPool[reference.indicesOffset + posIndex].x]; const auto i0 = indicesPool[reference.indicesOffset + posIndex].x;
const auto p1 = positionPool[reference.positionOffset + indicesPool[reference.indicesOffset + posIndex].y]; const auto i1 = indicesPool[reference.indicesOffset + posIndex].y;
const auto p2 = positionPool[reference.positionOffset + indicesPool[reference.indicesOffset + posIndex].z]; const auto i2 = indicesPool[reference.indicesOffset + posIndex].z;
const auto e0 = edgesPool[reference.indicesOffset + edgeIndex]; const auto& p0 = positionPool[reference.positionOffset + i0];
const auto e1 = edgesPool[reference.indicesOffset + edgeIndex + 1]; const auto& p1 = positionPool[reference.positionOffset + i1];
const auto& p2 = positionPool[reference.positionOffset + i2];
const auto n = faceNormalsPool[reference.indicesOffset + normalIndex]; const auto& t0 = texCoordsPool[reference.positionOffset + i0];
const auto& t1 = texCoordsPool[reference.positionOffset + i1];
const auto& t2 = texCoordsPool[reference.positionOffset + i2];
const auto& e0 = edgesPool[reference.indicesOffset + edgeIndex];
const auto& e1 = edgesPool[reference.indicesOffset + edgeIndex + 1];
const auto& n = faceNormalsPool[reference.indicesOffset + normalIndex];
const auto s = ray.origin - p0; const auto s = ray.origin - p0;
const auto s1 = glm::cross(ray.direction, e1); const auto s1 = glm::cross(ray.direction, e1);
@@ -146,6 +154,8 @@ std::optional<IntersectionInfo> Scene::intersectModel(const ModelReference& refe
const float b3 = 1.0f - resultVector.y - resultVector.z; const float b3 = 1.0f - resultVector.y - resultVector.z;
const auto texCoords = t0 * resultVector.y + t1 * resultVector.z + t2 * b3;
if (b3 < 0 || b3 > 1) if (b3 < 0 || b3 > 1)
continue; continue;
if (resultVector.y < 0 || resultVector.y > 1) if (resultVector.y < 0 || resultVector.y > 1)
@@ -158,10 +168,21 @@ std::optional<IntersectionInfo> Scene::intersectModel(const ModelReference& refe
if (!intersection.has_value() || resultVector.x < distance) if (!intersection.has_value() || resultVector.x < distance)
{ {
intersection = IntersectionInfo{.position = ray.origin + ray.direction * resultVector.x, intersection = IntersectionInfo{
.hitInfo =
{
.t = resultVector.x,
.position = ray.origin + ray.direction * resultVector.x,
.normal = n, .normal = n,
.albedo = glm::vec3(0.7f, 0.7f, 0.7f), .texCoords = texCoords,
.emissive = glm::vec3(0.0f, 0.0f, 0.0f),}; },
.shadingInfo =
{
.albedo = glm::vec3(texCoords, 0.0f),
.emissive = glm::vec3(0.0f, 0.0f, 0.0f),
.type = MaterialType::DIFFUSE,
},
};
distance = resultVector.x; distance = resultVector.x;
} }
} }
+11
View File
@@ -0,0 +1,11 @@
#include "BRDF.h"
glm::vec3 BlinnPhong::evaluate(HitInfo hit, glm::vec3 viewDir, glm::vec3 lightDir, glm::vec3 lightColor)
{
glm::vec3 normal = hit.normal;
float diffuse = std::max(glm::dot(normal, lightDir), 0.0f);
glm::vec3 h = glm::normalize(lightDir + viewDir);
float specular = pow(std::min(std::max(glm::dot(normal, h), 0.0f), 1.0f), shininess);
return (albedo * diffuse * lightColor) + (specularColor * specular);
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include "Material.h"
class BRDF
{
virtual glm::vec3 evaluate(HitInfo hit, glm::vec3 viewDir, glm::vec3 lightDir, glm::vec3 lightColor) = 0;
};
class BlinnPhong : public BRDF
{
glm::vec3 albedo = glm::vec3(1, 1, 1);
float alpha = 1;
glm::vec3 specularColor = glm::vec3(1, 1, 1);
float shininess = 0;
glm::vec3 emissive = glm::vec3(0, 0, 0);
virtual glm::vec3 evaluate(HitInfo hit, glm::vec3 viewDir, glm::vec3 lightDir, glm::vec3 lightColor);
};
+2
View File
@@ -1,5 +1,7 @@
target_sources(RayTracer target_sources(RayTracer
PRIVATE PRIVATE
BRDF.h
BRDF.cpp
Camera.h Camera.h
Material.h Material.h
Material.cpp Material.cpp
+5
View File
@@ -1 +1,6 @@
#include "Material.h" #include "Material.h"
std::unique_ptr<BRDF> Material::evaluate(HitInfo hitInfo)
{
return std::make_unique<BlinnPhong>();
}
+14 -1
View File
@@ -1,7 +1,20 @@
#pragma once #pragma once
#include "Minimal.h"
#include "Texture.h"
#include "Ray.h"
#include "BRDF.h"
enum class MaterialType
{
DIFFUSE,
SPECULAR,
REFRACTIVE,
};
class Material class Material
{ {
public: public:
private: std::unique_ptr<BRDF> evaluate(HitInfo hitInfo);
PTexture albedoTexture;
PTexture emissiveTexture;
}; };
+3 -9
View File
@@ -1,20 +1,14 @@
#pragma once #pragma once
#include "Minimal.h" #include "Minimal.h"
#include "Material.h"
#include "scene/AABB.h" #include "scene/AABB.h"
#include <optional> #include <optional>
#include <vector> #include <vector>
// material infos
// shading parameter
// hit data
struct IntersectionInfo struct IntersectionInfo
{ {
float t; HitInfo hitInfo;
glm::vec3 position; MaterialInfo shadingInfo;
glm::vec3 normal;
glm::vec3 albedo;
glm::vec3 emissive;
//....
}; };
class Model class Model
{ {
+16
View File
@@ -1,8 +1,24 @@
#pragma once #pragma once
#include <glm/glm.hpp> #include <glm/glm.hpp>
struct Payload
{
glm::vec3 accumulatedRadiance = glm::vec3(0);
glm::vec3 accumulatedMaterial = glm::vec3(0);
};
struct Ray struct Ray
{ {
glm::vec3 origin; glm::vec3 origin;
glm::vec3 direction; glm::vec3 direction;
uint32_t depth = 0;
Payload payload;
};
struct HitInfo
{
float t;
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoords;
}; };