Skip to content

Commit

Permalink
Moving closest intersection logic to scene.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ichko committed Mar 5, 2017
1 parent d26e35d commit d8248a3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
20 changes: 5 additions & 15 deletions pantaray/cpp/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,12 @@ namespace PantaRay {
}

Color Renderer::Trace(Ray& ray, Scene& scene) {
auto closest_intersection = Intersection();
closest_intersection.distance = Constants::inf;
Mesh* closest_mesh = nullptr;

for (auto& object : scene.GetObjects()) {
auto intersection = Intersection();
if (object.geometry->Intersect(ray, intersection) &&
intersection.distance < closest_intersection.distance) {
closest_intersection = intersection;
closest_mesh = &object;
}
}
Intersection intersection;
scene.Intersect(ray, intersection);

if (closest_mesh != nullptr) {
ShadingContext context(ray, closest_intersection, closest_mesh->texture, scene);
return closest_mesh->shader->Shade(context);
if (intersection.mesh != nullptr) {
ShadingContext context(ray, intersection, intersection.mesh->texture, scene);
return intersection.mesh->shader->Shade(context);
}

return Color();
Expand Down
26 changes: 26 additions & 0 deletions pantaray/cpp/scene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "../hpp/scene.hpp"


namespace PantaRay {

bool Scene::Intersect(const Ray& ray, Intersection& intersection) {
auto closest_intersection = Intersection();
closest_intersection.distance = Constants::inf;
Mesh* closest_mesh = nullptr;

for (auto& object : GetObjects()) {
auto intersection = Intersection();
if (object.geometry->Intersect(ray, intersection) &&
intersection.distance < closest_intersection.distance) {
closest_intersection = intersection;
closest_mesh = &object;
}
}

intersection = closest_intersection;
intersection.mesh = closest_mesh;

return true;
}

}
3 changes: 3 additions & 0 deletions pantaray/hpp/intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

namespace PantaRay {

struct Mesh;

struct Intersection {

Vector position;
Vector normal;
Mesh* mesh;
float distance;
float u, v;

Expand Down
1 change: 1 addition & 0 deletions pantaray/hpp/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace PantaRay {

struct IShader;
struct ITexture;

struct Mesh {

Expand Down
4 changes: 3 additions & 1 deletion pantaray/hpp/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace PantaRay {

class Scene {
class Scene : IGeometry {

std::vector<Mesh> objects;
std::vector<ILight*> lights;
Expand All @@ -24,6 +24,8 @@ namespace PantaRay {
return *this;
}

bool Intersect(const Ray& ray, Intersection& intersection);

std::vector<Mesh>& GetObjects() {
return objects;
}
Expand Down
1 change: 1 addition & 0 deletions pantaray/pantaray.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<ClCompile Include="cpp\light.cpp" />
<ClCompile Include="cpp\matrix.cpp" />
<ClCompile Include="cpp\renderer.cpp" />
<ClCompile Include="cpp\scene.cpp" />
<ClCompile Include="cpp\shading.cpp" />
<ClCompile Include="cpp\texture.cpp" />
<ClCompile Include="main.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions pantaray/pantaray.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ClCompile Include="cpp\light.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="cpp\scene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="hpp\camera.hpp">
Expand Down

0 comments on commit d8248a3

Please sign in to comment.