Skip to content

Commit 1bc327f

Browse files
author
BuildTools
committed
Volume Ray Casting
1 parent 510afad commit 1bc327f

12 files changed

+127
-30
lines changed

SphWaterfall/Build.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cd build
2-
cmake .. -G "Visual Studio 15 2017 Win64"
2+
cmake -G "Visual Studio 15 2017 Win64"
33
PAUSE

SphWaterfall/SphWaterfall/src/cui/CUI.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,20 @@ namespace CUI {
8585

8686
void render()
8787
{
88-
init(Vector3(0, 0, -30), Vector3(0, 0, 1), 1920, 1080);
88+
init(Vector3(0, 0, -10), Vector3(0, 0, 1), 1920, 1080);
8989

90-
addCamera(Vector3(-30, 0, -30), normalizeVector(Vector3(1, 0, 1)), 1920, 1080);
90+
// addCamera(Vector3(-30, 0, -30), normalizeVector(Vector3(1, 0, 1)), 1920, 1080);
9191

92-
vector<FluidParticle> particles;
93-
FluidParticle particle = FluidParticle(Vector3(0, 0, 0), Vector3(0, 0, 0));
94-
FluidParticle particle2 = FluidParticle(Vector3(0, 5, 0), Vector3(0, 0, 0));
95-
FluidParticle particle3 = FluidParticle(Vector3(5, 5, 0), Vector3(0, 0, 0));
96-
FluidParticle particle4 = FluidParticle(Vector3(-5, -5, -5), Vector3(0, 0, 0));
92+
vector<FluidParticle> particles = generateDebugParticles(10000);
93+
//FluidParticle particle = FluidParticle(Vector3(0, 0, 10), Vector3(0, 0, 0));
94+
// FluidParticle particle2 = FluidParticle(Vector3(0, 5, 0), Vector3(0, 0, 0));
95+
// FluidParticle particle3 = FluidParticle(Vector3(5, 5, 0), Vector3(0, 0, 0));
96+
// FluidParticle particle4 = FluidParticle(Vector3(-5, -5, -5), Vector3(0, 0, 0));
9797

98-
particles.emplace_back(particle);
99-
particles.emplace_back(particle2);
100-
particles.emplace_back(particle3);
101-
particles.emplace_back(particle4);
98+
// particles.emplace_back(particle);
99+
// particles.emplace_back(particle2);
100+
// particles.emplace_back(particle3);
101+
// particles.emplace_back(particle4);
102102

103103
cout << "Rendering, please wait..." << endl;
104104

SphWaterfall/SphWaterfall/src/data/Vector3.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ Vector3 Vector3::roundDownward() const{
9595
}
9696

9797
Vector3 Vector3::distanceTo(const Vector3& v, const Vector3& v1) {
98-
abs(v.x - v1.x), abs(v.y - v1.y), abs(v.z - v1.z);
98+
return Vector3(abs(v.x - v1.x), abs(v.y - v1.y), abs(v.z - v1.z));
9999
}

SphWaterfall/SphWaterfall/src/particleGen/StaticParticleGenerator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ std::vector<SphParticle> StaticParticleGenerator::generateStaticParticles(Face f
4545

4646

4747
StaticParticleGenerator StaticParticleGenerator::detectDuplicate(SphParticle a, SphParticle b) {
48-
48+
return StaticParticleGenerator();
4949
}
5050

5151
StaticParticleGenerator StaticParticleGenerator::removeDuplicate(SphParticle a, SphParticle b) {
52-
52+
return StaticParticleGenerator();
5353
}

SphWaterfall/SphWaterfall/src/visualization/Camera.cpp

+78-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Camera.h"
33
#include "util.h"
44
#include "Ray.h"
5+
#include <math.h>
56

67
Camera::Camera(Vector3 location, Vector3 direction, unsigned int width, unsigned int height, unsigned int ID) {
78
this->width = width;
@@ -13,7 +14,7 @@ Camera::Camera(Vector3 location, Vector3 direction, unsigned int width, unsigned
1314
this->direction = direction;
1415
}
1516

16-
void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
17+
void Camera::volumeRenderFrame(std::vector<DebugObject> particles, int frameID) {
1718

1819
Frame frame = Frame(this->width, this->height, frameID);
1920
const double aspectRatio = (double)width / (double)height;
@@ -46,6 +47,46 @@ void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
4647
Ray ray = Ray(vec_s, this->location);
4748

4849

50+
frame.setPixel(x, y, castVolumeRay(ray, particles));
51+
}
52+
}
53+
54+
this->outputDebugFrame(frame, (("volume_output_cam_" + std::to_string(this->ID)) + ".bmp").c_str());
55+
}
56+
57+
void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
58+
59+
Frame frame = Frame(this->width, this->height, frameID);
60+
const double aspectRatio = (double)width / (double)height;
61+
// view plane parameters
62+
const double l = -1.f *aspectRatio; //left
63+
const double r = +1.f *aspectRatio; //right
64+
const double b = -1.f; // bottom
65+
const double t = +1.f; // top
66+
const double d = +2.f; // distance to camera
67+
68+
//////////////
69+
// TODO: 2.1.1
70+
// Cast a ray from 'cameraPos' through the center(!) of each pixel on the view plane.
71+
// Use the view plane parametrization given above (l, r, b, t and d).
72+
// cf. lecture slides 39-43
73+
74+
//Calculate these vectors
75+
Vector3 vec_u = normalizeVector(findSkalarVectorWithYZero(this->direction));
76+
Vector3 vec_v = normalizeVector(findUpVector(this->direction, vec_u));
77+
78+
for (int x = 0; x < this->width; x++) {
79+
for (int y = 0; y < this->height; y++) {
80+
double u = l + (r - l) * (x + 0.5f) / this->width;
81+
double v = t + (b - t) * (y + 0.5f) / this->height;
82+
83+
Vector3 vec_s = Vector3(vec_u.x * u + vec_v.x * v + this->direction.x * d,
84+
vec_u.y * u + vec_v.y * v + this->direction.y * d,
85+
vec_u.z * u + vec_v.z * v + this->direction.z * d);
86+
87+
Ray ray = Ray(vec_s.normalize(), this->location);
88+
89+
4990
frame.setPixel(x, y, castDebugRay(ray, particles));
5091
}
5192
}
@@ -55,6 +96,7 @@ void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
5596

5697
Pixel Camera::castDebugRay(Ray ray, std::vector<DebugObject> particles) {
5798
double bestDistance = std::numeric_limits<float>::max();
99+
double waterDepth = 0;
58100
DebugObject hitObject;
59101
DebugObject *hit = &hitObject;
60102
hit = nullptr;
@@ -63,7 +105,7 @@ Pixel Camera::castDebugRay(Ray ray, std::vector<DebugObject> particles) {
63105
DebugObject &obj = particles.at(i);
64106
double currDist = bestDistance;
65107

66-
if (obj.intersects(ray, currDist)) {
108+
if (obj.intersects(ray, currDist, waterDepth)) {
67109
if (currDist < bestDistance) {
68110
bestDistance = currDist;
69111
hit = &obj;
@@ -74,6 +116,40 @@ Pixel Camera::castDebugRay(Ray ray, std::vector<DebugObject> particles) {
74116
return hit == nullptr ? Pixel(0, 0, 0) : hitObject.getColor();
75117
}
76118

119+
Pixel Camera::castVolumeRay(Ray ray, std::vector<DebugObject> particles) {
120+
double bestDistance = std::numeric_limits<float>::max();
121+
double waterDepth = 0;
122+
DebugObject hitObject;
123+
DebugObject *hit = &hitObject;
124+
hit = nullptr;
125+
126+
Pixel initColor = Pixel(255,255,255); //Make the background brown
127+
128+
for (int i = 0; i < particles.size(); i++) {
129+
DebugObject &obj = particles.at(i);
130+
double currDist = bestDistance;
131+
132+
if (obj.intersects(ray, currDist, waterDepth)) {
133+
if (currDist < bestDistance) {
134+
bestDistance = currDist;
135+
hit = &obj;
136+
}
137+
}
138+
}
139+
140+
if (hit != nullptr) {
141+
double facRG = 1 - 0.8*exp(-0.3f * waterDepth);
142+
double facB = 1 - 1.2*exp(-0.1f * waterDepth);
143+
facB = facB < 0 ? 0 : facB;
144+
initColor.setRed(initColor.getRedValue() - 255 * facRG);
145+
initColor.setGreen(initColor.getGreenValue() - 255 * facRG);
146+
initColor.setBlue(initColor.getBlueValue() - 220 * facB > 30 ? initColor.getBlueValue() - 220 * facB : 30);
147+
}
148+
149+
150+
return initColor;
151+
}
152+
77153
void Camera::renderFrame(std::vector<SphParticle> particles) {
78154
//TODO: implement rendering algorithm
79155
}

SphWaterfall/SphWaterfall/src/visualization/Camera.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Camera {
1111
Camera(Vector3 location, Vector3 direction, unsigned int width, unsigned int height, unsigned int ID);
1212

1313
void debugRenderFrame(std::vector<DebugObject> particles, int frameID);
14+
void volumeRenderFrame(std::vector<DebugObject> particles, int frameID);
1415

1516
void renderFrame(std::vector<SphParticle> particles); //Vector is only a placeholder here as the data structure isnt decided yet
1617

@@ -23,6 +24,7 @@ class Camera {
2324
private:
2425

2526
Pixel castDebugRay(Ray ray, std::vector<DebugObject> particles);
27+
Pixel castVolumeRay(Ray ray, std::vector<DebugObject> particles);
2628

2729
Vector3 location;
2830
Vector3 direction;

SphWaterfall/SphWaterfall/src/visualization/DebugObject.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Pixel DebugObject::getColor() {
2424
return Pixel(0, 255, 0);
2525
}
2626

27-
bool DebugObject::intersects(Ray &ray, double &distance) {
27+
bool DebugObject::intersects(Ray &ray, double &distance, double &highestDist) {
2828
distance = std::numeric_limits<double>::max();
2929

3030
Vector3 temp = Vector3(ray.origin.x - this->location.x, ray.origin.y - this->location.y, ray.origin.z - this->location.z);
@@ -40,6 +40,10 @@ bool DebugObject::intersects(Ray &ray, double &distance) {
4040
float t1 = (-1 * b + sqrt((b*b) - 4 * a*c)) / (2 * a);
4141
float t2 = (-1 * b - sqrt((b*b) - 4 * a*c)) / (2 * a);
4242

43+
double highDist = t1 > t2 ? t1 : t2;
44+
45+
if (highDist > highestDist) highestDist = highDist;
46+
4347
distance = t1 < t2 ? t1 : t2;
4448
}
4549

SphWaterfall/SphWaterfall/src/visualization/DebugObject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DebugObject {
1414

1515
Pixel getColor();
1616

17-
bool intersects(Ray &ray, double &distance);
17+
bool intersects(Ray &ray, double &distance, double &highestDistance);
1818

1919
private:
2020
Vector3 location;

SphWaterfall/SphWaterfall/src/visualization/Pixel.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ Pixel::Pixel() {
88
}
99

1010
Pixel::Pixel(unsigned short red, unsigned short green, unsigned short blue) {
11-
this->red = red;
12-
this->green = green;
13-
this->blue = blue;
11+
this->red = red > 255 ? 255 : (red < 0 ? 0 : red);
12+
this->green = green > 255 ? 255 : (green < 0 ? 0 : green);
13+
this->blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
1414
}
1515

1616
void Pixel::setColor(unsigned short red, unsigned short green, unsigned short blue) {
17-
this->red = red;
18-
this->green = green;
19-
this->blue = blue;
17+
this->red = red > 255 ? 255 : (red < 0 ? 0 : red);
18+
this->green = green > 255 ? 255 : (green < 0 ? 0 : green);
19+
this->blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
2020
}
2121

2222
void Pixel::setRed(unsigned short red) {
23-
this->red = red;
23+
this->red = red > 255 ? 255 : (red < 0 ? 0 : red);
2424
}
2525

2626
void Pixel::setGreen(unsigned short green) {
27-
this->green = green;
27+
this->green = green > 255 ? 255 : (green < 0 ? 0 : green);
2828
}
2929

3030
void Pixel::setBlue(unsigned short blue) {
31-
this->blue = blue;
31+
this->blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
3232
}
3333

3434
unsigned short Pixel::getRedValue() {

SphWaterfall/SphWaterfall/src/visualization/VisualizationManager.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ static void addCamera(Vector3 cameraLocation, Vector3 cameraDirection, unsigned
1616

1717
static void debugRenderFrame(std::vector<FluidParticle> particles) {
1818
for (int i = 0; i < cameras.size(); i++) {
19-
cameras[i].debugRenderFrame(convertSphParticles(particles), 1);
19+
cameras[i].volumeRenderFrame(convertSphParticles(particles), 1);
2020
}
21+
}
22+
23+
static std::vector<FluidParticle> generateDebugParticles(int count) {
24+
std::vector<FluidParticle> particles;
25+
26+
for (int i = 0; i < count; i++) {
27+
double x = double(rand() % 1000 - 500) / 100;
28+
double y = double(rand() % 1000 - 500) / 100;
29+
double z = double(rand() % 1000 - 500) / 100;
30+
31+
FluidParticle f = FluidParticle(Vector3(x, y, z));
32+
particles.emplace_back(f);
33+
}
34+
return particles;
35+
2136
}

SphWaterfall/SphWaterfall/src/visualization/util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static Vector3 normalizeVector(Vector3 vec) {
5151
static std::vector<DebugObject> convertSphParticles(std::vector<FluidParticle> &particles) {
5252
std::vector<DebugObject> output;
5353
for (unsigned int i = 0; i < particles.size(); i++) {
54-
output.emplace_back(DebugObject(particles[i].position, 1));
54+
output.emplace_back(DebugObject(particles[i].position, 0.4f));
5555
}
5656

5757
return output;

SphWaterfall/volume_output_cam_0.bmp

5.93 MB
Binary file not shown.

0 commit comments

Comments
 (0)