Skip to content

Commit b305f92

Browse files
author
BuildTools
committed
Added ParticleIO, cleaned up code
1 parent 1bc327f commit b305f92

15 files changed

+215
-103
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ SphWaterfall/build/
1010
.settings/
1111

1212
# CMake
13-
cmake-build-debug/
13+
cmake-build-debug/
14+
15+
#Frame Outputs
16+
SphWaterfall/output/

SphWaterfall/SphWaterfall/src/cui/CUI.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ namespace CUI {
8686
void render()
8787
{
8888
init(Vector3(0, 0, -10), Vector3(0, 0, 1), 1920, 1080);
89-
89+
9090
// addCamera(Vector3(-30, 0, -30), normalizeVector(Vector3(1, 0, 1)), 1920, 1080);
9191

92-
vector<FluidParticle> particles = generateDebugParticles(10000);
93-
//FluidParticle particle = FluidParticle(Vector3(0, 0, 10), Vector3(0, 0, 0));
92+
// vector<FluidParticle> particles = generateDebugParticles(10000);
93+
// FluidParticle particle = FluidParticle(Vector3(0, 0, 10), Vector3(0, 0, 0));
9494
// FluidParticle particle2 = FluidParticle(Vector3(0, 5, 0), Vector3(0, 0, 0));
9595
// FluidParticle particle3 = FluidParticle(Vector3(5, 5, 0), Vector3(0, 0, 0));
9696
// FluidParticle particle4 = FluidParticle(Vector3(-5, -5, -5), Vector3(0, 0, 0));
@@ -102,7 +102,8 @@ namespace CUI {
102102

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

105-
debugRenderFrame(particles);
105+
// debugRenderFrame(particles);
106+
renderFrames("test.particles");
106107

107108
cout << "Done!" << endl;
108109
}
@@ -162,6 +163,11 @@ namespace CUI {
162163
command_buffer[0] = 5;
163164
render();
164165
}
166+
else if (command == "camera")
167+
{
168+
command_buffer[0] = 5;
169+
render();
170+
}
165171
else if (command == "help" || command == "?") {
166172
showHelp();
167173
}

SphWaterfall/SphWaterfall/src/data/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ target_sources(
55
"${CMAKE_CURRENT_LIST_DIR}/FluidParticle.h"
66
"${CMAKE_CURRENT_LIST_DIR}/NullableWrapper.cpp"
77
"${CMAKE_CURRENT_LIST_DIR}/NullableWrapper.h"
8+
"${CMAKE_CURRENT_LIST_DIR}/ParticleIO.h"
89
"${CMAKE_CURRENT_LIST_DIR}/SphParticle.cpp"
910
"${CMAKE_CURRENT_LIST_DIR}/SphParticle.h"
1011
"${CMAKE_CURRENT_LIST_DIR}/StaticParticle.cpp"

SphWaterfall/SphWaterfall/src/visualization/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ target_sources(SphWaterfall
66
"${CMAKE_CURRENT_LIST_DIR}/DebugObject.h"
77
"${CMAKE_CURRENT_LIST_DIR}/Frame.cpp"
88
"${CMAKE_CURRENT_LIST_DIR}/Frame.h"
9+
"${CMAKE_CURRENT_LIST_DIR}/ParticleObject.cpp"
10+
"${CMAKE_CURRENT_LIST_DIR}/ParticleObject.h"
911
"${CMAKE_CURRENT_LIST_DIR}/Pixel.cpp"
1012
"${CMAKE_CURRENT_LIST_DIR}/Pixel.h"
1113
"${CMAKE_CURRENT_LIST_DIR}/Ray.cpp"
1214
"${CMAKE_CURRENT_LIST_DIR}/Ray.h"
1315
"${CMAKE_CURRENT_LIST_DIR}/util.h"
16+
"${CMAKE_CURRENT_LIST_DIR}/VideoConverter.h"
1417
"${CMAKE_CURRENT_LIST_DIR}/VisualizationManager.h"
1518
)

SphWaterfall/SphWaterfall/src/visualization/Camera.cpp

+41-66
Original file line numberDiff line numberDiff line change
@@ -14,75 +14,27 @@ Camera::Camera(Vector3 location, Vector3 direction, unsigned int width, unsigned
1414
this->direction = direction;
1515
}
1616

17-
void Camera::volumeRenderFrame(std::vector<DebugObject> particles, int frameID) {
18-
19-
Frame frame = Frame(this->width, this->height, frameID);
20-
const double aspectRatio = (double)width / (double)height;
21-
// view plane parameters
22-
const double l = -1.f *aspectRatio; //left
23-
const double r = +1.f *aspectRatio; //right
24-
const double b = -1.f; // bottom
25-
const double t = +1.f; // top
26-
const double d = +2.f; // distance to camera
27-
28-
//////////////
29-
// TODO: 2.1.1
30-
// Cast a ray from 'cameraPos' through the center(!) of each pixel on the view plane.
31-
// Use the view plane parametrization given above (l, r, b, t and d).
32-
// cf. lecture slides 39-43
33-
34-
//Calculate these vectors
35-
Vector3 vec_u = normalizeVector(findSkalarVectorWithYZero(this->direction));
36-
Vector3 vec_v = normalizeVector(findUpVector(this->direction, vec_u));
37-
38-
for (int x = 0; x < this->width; x++) {
39-
for (int y = 0; y < this->height; y++) {
40-
double u = l + (r - l) * (x + 0.5f) / this->width;
41-
double v = t + (b - t) * (y + 0.5f) / this->height;
42-
43-
Vector3 vec_s = Vector3(vec_u.x * u + vec_v.x * v + this->direction.x * d,
44-
vec_u.y * u + vec_v.y * v + this->direction.y * d,
45-
vec_u.z * u + vec_v.z * v + this->direction.z * d);
46-
47-
Ray ray = Ray(vec_s, this->location);
48-
49-
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-
5717
void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
5818

5919
Frame frame = Frame(this->width, this->height, frameID);
6020
const double aspectRatio = (double)width / (double)height;
6121
// view plane parameters
6222
const double l = -1.f *aspectRatio; //left
6323
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
24+
const double b = -1.f; // bottom
25+
const double t = +1.f; // top
26+
const double d = +2.f; // distance to camera
6727

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
7328

74-
//Calculate these vectors
75-
Vector3 vec_u = normalizeVector(findSkalarVectorWithYZero(this->direction));
76-
Vector3 vec_v = normalizeVector(findUpVector(this->direction, vec_u));
29+
Vector3 vec_u = findSkalarVectorWithYZero(this->direction).normalize();
30+
Vector3 vec_v = findUpVector(this->direction, vec_u).normalize();
7731

7832
for (int x = 0; x < this->width; x++) {
7933
for (int y = 0; y < this->height; y++) {
8034
double u = l + (r - l) * (x + 0.5f) / this->width;
8135
double v = t + (b - t) * (y + 0.5f) / this->height;
8236

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);
37+
Vector3 vec_s = vec_u * u + vec_v * v + this->direction * d;
8638

8739
Ray ray = Ray(vec_s.normalize(), this->location);
8840

@@ -113,20 +65,20 @@ Pixel Camera::castDebugRay(Ray ray, std::vector<DebugObject> particles) {
11365
}
11466
}
11567

116-
return hit == nullptr ? Pixel(0, 0, 0) : hitObject.getColor();
68+
return hit == nullptr ? Pixel(0, 0, 0) : Pixel(0, 255, 0);
11769
}
11870

119-
Pixel Camera::castVolumeRay(Ray ray, std::vector<DebugObject> particles) {
71+
Pixel Camera::castVolumeRay(Ray ray, std::vector<ParticleObject> particles) {
12072
double bestDistance = std::numeric_limits<float>::max();
12173
double waterDepth = 0;
122-
DebugObject hitObject;
123-
DebugObject *hit = &hitObject;
74+
ParticleObject hitObject;
75+
ParticleObject *hit = &hitObject;
12476
hit = nullptr;
12577

126-
Pixel initColor = Pixel(255,255,255); //Make the background brown
78+
Pixel initColor = Pixel(255,255,255); //Make the background white
12779

12880
for (int i = 0; i < particles.size(); i++) {
129-
DebugObject &obj = particles.at(i);
81+
ParticleObject &obj = particles.at(i);
13082
double currDist = bestDistance;
13183

13284
if (obj.intersects(ray, currDist, waterDepth)) {
@@ -138,24 +90,47 @@ Pixel Camera::castVolumeRay(Ray ray, std::vector<DebugObject> particles) {
13890
}
13991

14092
if (hit != nullptr) {
141-
double facRG = 1 - 0.8*exp(-0.3f * waterDepth);
93+
double facRG = 1 - 0.8*exp(-0.15f * waterDepth);
14294
double facB = 1 - 1.2*exp(-0.1f * waterDepth);
14395
facB = facB < 0 ? 0 : facB;
14496
initColor.setRed(initColor.getRedValue() - 255 * facRG);
14597
initColor.setGreen(initColor.getGreenValue() - 255 * facRG);
14698
initColor.setBlue(initColor.getBlueValue() - 220 * facB > 30 ? initColor.getBlueValue() - 220 * facB : 30);
99+
initColor.setShaderUsage(true);
147100
}
148101

149102

150103
return initColor;
151104
}
152105

153-
void Camera::renderFrame(std::vector<SphParticle> particles) {
154-
//TODO: implement rendering algorithm
155-
}
106+
Frame Camera::renderFrame(std::vector<ParticleObject> particles, int frameID) {
107+
Frame frame = Frame(this->width, this->height, frameID);
108+
const double aspectRatio = (double)width / (double)height;
109+
// view plane parameters
110+
const double l = -1.f *aspectRatio; //left
111+
const double r = +1.f *aspectRatio; //right
112+
const double b = -1.f; // bottom
113+
const double t = +1.f; // top
114+
const double d = +2.f; // distance to camera
115+
116+
117+
Vector3 vec_u = findSkalarVectorWithYZero(this->direction).normalize();
118+
Vector3 vec_v = findUpVector(this->direction, vec_u).normalize();
119+
120+
for (int x = 0; x < this->width; x++) {
121+
for (int y = 0; y < this->height; y++) {
122+
double u = l + (r - l) * (x + 0.5f) / this->width;
123+
double v = t + (b - t) * (y + 0.5f) / this->height;
124+
125+
Vector3 vec_s = vec_u * u + vec_v * v + this->direction * d;
126+
Ray ray = Ray(vec_s, this->location);
127+
128+
129+
frame.setPixel(x, y, castVolumeRay(ray, particles));
130+
}
131+
}
156132

157-
void Camera::mergeFramesAndFlushVideo(std::string file) {
158-
//TODO: merge all Frames to a video
133+
return frame;
159134
}
160135

161136
void Camera::outputDebugFrame(Frame f, const char* fileName) {

SphWaterfall/SphWaterfall/src/visualization/Camera.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "DebugObject.h"
44
#include "../data/SphParticle.h"
55
#include "../data/Vector3.h"
6+
#include "ParticleObject.h"
67
#include <vector>
78
#include <string>
89

@@ -11,11 +12,9 @@ class Camera {
1112
Camera(Vector3 location, Vector3 direction, unsigned int width, unsigned int height, unsigned int ID);
1213

1314
void debugRenderFrame(std::vector<DebugObject> particles, int frameID);
14-
void volumeRenderFrame(std::vector<DebugObject> particles, int frameID);
1515

16-
void renderFrame(std::vector<SphParticle> particles); //Vector is only a placeholder here as the data structure isnt decided yet
16+
Frame renderFrame(std::vector<ParticleObject> particles, int frameID); //Vector is only a placeholder here as the data structure isnt decided yet
1717

18-
void mergeFramesAndFlushVideo(std::string file);
1918
void outputDebugFrame(Frame f, const char* fileName);
2019

2120
Vector3 getLocation();
@@ -24,7 +23,7 @@ class Camera {
2423
private:
2524

2625
Pixel castDebugRay(Ray ray, std::vector<DebugObject> particles);
27-
Pixel castVolumeRay(Ray ray, std::vector<DebugObject> particles);
26+
Pixel castVolumeRay(Ray ray, std::vector<ParticleObject> particles);
2827

2928
Vector3 location;
3029
Vector3 direction;

SphWaterfall/SphWaterfall/src/visualization/DebugObject.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ double DebugObject::getRadius() {
2020
return this->radius;
2121
}
2222

23-
Pixel DebugObject::getColor() {
24-
return Pixel(0, 255, 0);
25-
}
26-
2723
bool DebugObject::intersects(Ray &ray, double &distance, double &highestDist) {
2824
distance = std::numeric_limits<double>::max();
2925

30-
Vector3 temp = Vector3(ray.origin.x - this->location.x, ray.origin.y - this->location.y, ray.origin.z - this->location.z);
26+
Vector3 temp = ray.origin - this->location;
3127

3228
double a = ray.direction.x * ray.direction.x + ray.direction.y * ray.direction.y + ray.direction.z * ray.direction.z;
3329
double b = 2 * (ray.direction.x * temp.x + ray.direction.y * temp.y + ray.direction.z * temp.z);

SphWaterfall/SphWaterfall/src/visualization/DebugObject.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
2-
#include <vector>
2+
33
#include "Ray.h"
44
#include "../data/Vector3.h"
5-
#include "Pixel.h"
65

76
class DebugObject {
87
public:
@@ -12,8 +11,6 @@ class DebugObject {
1211
Vector3 getLocation();
1312
double getRadius();
1413

15-
Pixel getColor();
16-
1714
bool intersects(Ray &ray, double &distance, double &highestDistance);
1815

1916
private:

SphWaterfall/SphWaterfall/src/visualization/Frame.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22
#include "Frame.h"
3-
#include "Pixel.h"
43

54
Frame::Frame() {
65
this->width = 0;
@@ -31,5 +30,13 @@ void Frame::setPixel(unsigned int x, unsigned int y, Pixel pixel) {
3130
this->pixels[y*width + x] = pixel;
3231
}
3332

33+
unsigned int Frame::getWidth() {
34+
return this->width;
35+
}
36+
37+
unsigned Frame::getHeight() {
38+
return this->height;
39+
}
40+
3441

3542

SphWaterfall/SphWaterfall/src/visualization/Frame.h

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Frame {
1010
long getFrameID();
1111
Pixel getPixel(unsigned int x, unsigned int y);
1212
void setPixel(unsigned int x, unsigned int y, Pixel pixel);
13+
14+
unsigned int getWidth();
15+
unsigned int getHeight();
1316

1417
private:
1518
unsigned int width;

SphWaterfall/SphWaterfall/src/visualization/Pixel.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ Pixel::Pixel() {
55
this->red = 0;
66
this->green = 0;
77
this->blue = 0;
8+
this->shaderUsage = false;
89
}
910

1011
Pixel::Pixel(unsigned short red, unsigned short green, unsigned short blue) {
1112
this->red = red > 255 ? 255 : (red < 0 ? 0 : red);
1213
this->green = green > 255 ? 255 : (green < 0 ? 0 : green);
1314
this->blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
15+
this->shaderUsage = false;
1416
}
1517

1618
void Pixel::setColor(unsigned short red, unsigned short green, unsigned short blue) {
@@ -42,3 +44,11 @@ unsigned short Pixel::getGreenValue() {
4244
unsigned short Pixel::getBlueValue() {
4345
return this->blue;
4446
}
47+
48+
bool Pixel::usesShader() {
49+
return this->shaderUsage;
50+
}
51+
52+
void Pixel::setShaderUsage(bool shaderUsage) {
53+
this->shaderUsage = shaderUsage;
54+
}

SphWaterfall/SphWaterfall/src/visualization/Pixel.h

+4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ class Pixel {
1414
void setGreen(unsigned short green);
1515
void setBlue(unsigned short blue);
1616

17+
bool usesShader();
18+
void setShaderUsage(bool shaderUsage);
19+
1720
private:
1821
unsigned short red;
1922
unsigned short green;
2023
unsigned short blue;
24+
bool shaderUsage;
2125
};

0 commit comments

Comments
 (0)