Assignment2 added.

This commit is contained in:
hal8174 2024-05-14 11:47:15 +02:00
parent 025341336d
commit 1948277dbd
19 changed files with 111151 additions and 11 deletions

View file

@ -101,8 +101,10 @@ OPTION(EMBREE_TUTORIALS "Enable to build Embree tutorials" ON)
# Embree configuration
##############################################################
SET(EMBREE_STATIC_LIB ON CACHE BOOL "Build Embree as a static library." FORCE)
MARK_AS_ADVANCED(EMBREE_STATIC_LIB)
IF (EMBREE_STATIC_LIB)
SET(EMBREE_LIB_TYPE STATIC)
ADD_DEFINITIONS(-DEMBREE_STATIC_LIB)

View file

@ -8,7 +8,7 @@
#include <lexers/parsestream.h>
#include <algorithms/parallel_for.h>
#include <scenegraph/scenegraph.h>
#include <scenegraph/grid.h>
// #include <lights/light.h>
#include "scene.hpp"
@ -25,6 +25,8 @@ struct Sample {
struct Data {
RenderScene* scene;
Grid* densityGrid;
Grid* tempGrid;
int spp;
int max_path_length;

View file

@ -11,6 +11,8 @@
namespace embree {
struct Light;
enum Type { TRIANGLE_MESH, SUBDIV_MESH, CURVES, INSTANCE, INSTANCE_ARRAY, GROUP, QUAD_MESH, GRID_MESH , POINTS };
}
using namespace embree;
@ -71,8 +73,6 @@ struct Scene {
std::vector<embree::Ref<embree::SceneGraph::LightNode> > lights; //!< list of lights
};
enum Type { TRIANGLE_MESH, SUBDIV_MESH, CURVES, INSTANCE, INSTANCE_ARRAY, GROUP, QUAD_MESH, GRID_MESH /*, POINTS */}; // fast compilation fix!
struct Geometry {
Geometry(Type type) : type(type), geometry(nullptr), materialID(-1), lightID(-1), visited(false) {
}
@ -187,9 +187,6 @@ struct RenderScene {
}
auto test = (OBJMaterial*) materials[0];
}
~RenderScene() {
@ -213,7 +210,7 @@ struct RenderScene {
return (Geometry *) in->geometry;
else if (Ref<SceneGraph::TriangleMeshNode> mesh = in.dynamicCast<SceneGraph::TriangleMeshNode>())
geom = (Geometry *) new TriangleMesh(device, scene, mesh);
else
THROW_RUNTIME_ERROR("unknown geometry type");

138
Framework/scenegraph/grid.h Normal file
View file

@ -0,0 +1,138 @@
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "scenegraph.h"
namespace embree
{
class Grid {
private:
std::vector<float> data;
Vec3ia res;
Vec3fa worldPos, scale;
public:
Grid(const std::string& filePath, const Vec3ia res, const Vec3fa& worldPos, const Vec3fa& scale)
: res(res), worldPos(worldPos), scale(scale) {
loadVolume(filePath);
}
// Check if the point is inside the box
bool isInside(Vec3fa point) {
Vec3fa maxExtent = worldPos + scale;
return (point.x >= worldPos.x && point.x <= maxExtent.x &&
point.y >= worldPos.y && point.y <= maxExtent.y &&
point.z >= worldPos.z && point.z <= maxExtent.z);
}
bool intersect(Vec3fa rayOrigin, Vec3fa rayDir, Vec2fa& resT) {
Vec3fa boxMin = worldPos;
Vec3fa boxMax = worldPos + scale;
Vec3fa tMin = (boxMin - rayOrigin) / rayDir;
Vec3fa tMax = (boxMax - rayOrigin) / rayDir;
Vec3fa t1 = min(tMin, tMax);
Vec3fa t2 = max(tMin, tMax);
float tNear = max(max(t1.x, t1.y), t1.z);
float tFar = min(min(t2.x, t2.y), t2.z);
resT = Vec2fa(tNear, tFar);
return resT.y > resT.x;
};
void loadVolume(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
throw std::runtime_error("Failed to open the file.");
}
data.resize(res.x * res.y * res.z);
file.read(reinterpret_cast<char*>(data.data()), data.size() * sizeof(float));
file.close();
}
// return -1.0f if the point is out of the bounding box
float sampleW(const Vec3fa& worldPos) const {
// Convert world position to local position
Vec3fa localPos = (worldPos - this->worldPos) / scale;
if (localPos.x < 0.0 || localPos.y < 0.0 || localPos.z < 0.0 || localPos.x > 1.0 || localPos.z > 1.0 || localPos.y > 1.0) {
return -1.0f;
}
return sample(localPos);
}
private:
float get(int index) const {
assert(index > 0 && index < res.x * res.y * res.z);
return data[index];
}
float get(const Vec3ia& p) const {
// Clamp the coordinates to the nearest valid value within the grid bounds
int clampedX = std::max(0, std::min(p.x, res.x - 1));
int clampedY = std::max(0, std::min(p.y, res.y - 1));
int clampedZ = std::max(0, std::min(p.z, res.z - 1));
return data[clampedX + res.x * clampedY + res.x*res.y * clampedZ];
}
float sample(Vec3fa pos) const {
pos.x *= res.x-1;
pos.y *= res.y - 1;
pos.z *= res.z - 1;
int ix = static_cast<int>(std::floor(pos.x));
int iy = static_cast<int>(std::floor(pos.y));
int iz = static_cast<int>(std::floor(pos.z));
float fx = pos.x - ix;
float fy = pos.y - iy;
float fz = pos.z - iz;
// Fetch values from the eight surrounding corners
Vec3ia p000(ix, iy, iz);
Vec3ia p001(ix, iy, iz + 1);
Vec3ia p010(ix, iy + 1, iz);
Vec3ia p011(ix, iy + 1, iz + 1);
Vec3ia p100(ix + 1, iy, iz);
Vec3ia p101(ix + 1, iy, iz + 1);
Vec3ia p110(ix + 1, iy + 1, iz);
Vec3ia p111(ix + 1, iy + 1, iz + 1);
// Trilinear interpolation
float c000 = get(p000);
float c001 = get(p001);
float c010 = get(p010);
float c011 = get(p011);
float c100 = get(p100);
float c101 = get(p101);
float c110 = get(p110);
float c111 = get(p111);
float c00 = c000 * (1 - fz) + c001 * fz;
float c01 = c010 * (1 - fz) + c011 * fz;
float c10 = c100 * (1 - fz) + c101 * fz;
float c11 = c110 * (1 - fz) + c111 * fz;
float c0 = c00 * (1 - fy) + c01 * fy;
float c1 = c10 * (1 - fy) + c11 * fy;
float c = c0 * (1 - fx) + c1 * fx;
return c;
}
};
}

View file

@ -547,7 +547,7 @@ namespace embree {
Ref<MaterialNode> material,
Ref<LightNode> lightnode = 0
)
: Node(true), time_range(0.0f, 1.0f), texcoords(texcoords), triangles(triangles), material(material), light(lightnode) {
: Node(true), time_range(0.0f, 1.0f), texcoords(texcoords), triangles(triangles), material(material), light(lightnode){
positions.push_back(positions_in);
normals.push_back(normals_in);
}
@ -632,7 +632,6 @@ namespace embree {
std::vector<Triangle> triangles;
Ref<MaterialNode> material;
Ref<LightNode> light;
};
template<typename Light>

43
Framework/scenes/box.obj Normal file
View file

@ -0,0 +1,43 @@
o box
# left side
v 0.5 -0.5 -0.5
v 0.5 -0.5 0.5
v 0.5 0.5 0.5
v 0.5 0.5 -0.5
f -4 -3 -2 -1
# right side
v -0.5 -0.5 -0.5
v -0.5 -0.5 0.5
v -0.5 0.5 0.5
v -0.5 0.5 -0.5
f -4 -3 -2 -1
# bottom side
v -0.5 -0.5 -0.5
v -0.5 -0.5 0.5
v 0.5 -0.5 0.5
v 0.5 -0.5 -0.5
f -4 -3 -2 -1
# top side
v -0.5 0.5 -0.5
v -0.5 0.5 0.5
v 0.5 0.5 0.5
v 0.5 0.5 -0.5
f -4 -3 -2 -1
# front side
v -0.5 -0.5 -0.5
v -0.5 0.5 -0.5
v 0.5 0.5 -0.5
v 0.5 -0.5 -0.5
f -4 -3 -2 -1
# back side
v -0.5 -0.5 0.5
v -0.5 0.5 0.5
v 0.5 0.5 0.5
v 0.5 -0.5 0.5
f -4 -3 -2 -1

View file

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,14 @@
newmtl white
Ka 0 0 0
Kd 1 1 1
Ks 0 0 0
newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0
newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff