Skip to content

Commit 3dfbba6

Browse files
author
BuildTools
committed
Fixed white gate and particles rendering in front of terrain that were supposed to be behind
1 parent eeeb9e6 commit 3dfbba6

File tree

7 files changed

+70
-40
lines changed

7 files changed

+70
-40
lines changed

SphWaterfall/SphWaterfall/src/visualization/Camera.cpp

+30-17
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Pixel Camera::castDebugRay(Ray& ray, std::vector<DebugObject> particles) {
8484

8585
Pixel Camera::castVolumeRay(Ray& ray, std::vector<ParticleObject> particles, Pixel basePixel) {
8686
double bestDistance = std::numeric_limits<float>::max();
87-
double waterDepth = 0;
87+
double waterDepth = -0.2;
8888
ParticleObject hitObject;
8989
ParticleObject *hit = &hitObject;
9090
hit = nullptr;
@@ -105,17 +105,19 @@ Pixel Camera::castVolumeRay(Ray& ray, std::vector<ParticleObject> particles, Pix
105105
waterDepth = waterDepth > 15 ? 15 : waterDepth; //Cap waterDepth at 15
106106

107107
//Copy the pixel from the base Frame
108-
Pixel pixel = Pixel(basePixel.getRedValue(), basePixel.getGreenValue(), basePixel.getRedValue());
108+
Pixel pixel = Pixel(basePixel.getRedValue(), basePixel.getGreenValue(), basePixel.getBlueValue());
109109

110110
if (waterDepth <= 0) {
111111
return pixel;
112112
}
113113

114114
Pixel waterColor = Pixel(30, 30, 170); //Color of "pure water"
115115
pixel.setShaderUsage(true);
116+
116117
//Shade the pixel depending on water depth
117118
if (hit != nullptr) {
118119
//New Shading, should work smiliar to alpha
120+
119121
int diffR = waterColor.getRedValue() - pixel.getRedValue();
120122
int diffG = waterColor.getGreenValue() - pixel.getGreenValue();
121123
int diffB = waterColor.getBlueValue() - pixel.getBlueValue();
@@ -129,6 +131,7 @@ Pixel Camera::castVolumeRay(Ray& ray, std::vector<ParticleObject> particles, Pix
129131
pixel.setRed((unsigned short)newR);
130132
pixel.setGreen((unsigned short)newG);
131133
pixel.setBlue((unsigned short)newB);
134+
132135
}
133136

134137
return pixel;
@@ -157,15 +160,14 @@ Frame Camera::renderFrame(std::vector<ParticleObject> particles, int frameID) {
157160

158161
//Ray Direction Vector
159162
Vector3 vec_s = vec_u * u + vec_v * v + this->direction * d;
160-
Ray ray = Ray(vec_s, this->location);
163+
Ray ray = Ray(vec_s.normalize(), this->location);
161164

162165
//Set pixel in Frame
163-
frame.setPixel(x, y, castVolumeRay(ray, particles, getCurrentlyUsedBaseFrame(frameID).getPixel(x, y)));
164-
166+
frame.setPixel(x, y, castVolumeRay(ray, particles, getCurrentlyUsedBaseFrame(frameID).getPixel(x, y)));
165167
}
166168
}
167169

168-
return Shader::applyGaussianSmoothing(frame, 5, 8);
170+
return frame;// Shader::applyGaussianSmoothing(frame, 5, 8);
169171
}
170172

171173
void Camera::renderGeometryFrames(Terrain terrain, Terrain gate) {
@@ -188,7 +190,7 @@ void Camera::renderGeometryFrames(Terrain terrain, Terrain gate) {
188190
double v = t + (b - t) * (y + 0.5f) / this->height;
189191

190192
Vector3 vec_s = vec_u * u + vec_v * v + this->direction * d;
191-
Ray ray = Ray(vec_s, this->location);
193+
Ray ray = Ray(vec_s.normalize(), this->location);
192194

193195
baseFrameOpen.setPixel(x, y, castTerrainRay(ray, terrain));
194196
}
@@ -202,7 +204,7 @@ void Camera::renderGeometryFrames(Terrain terrain, Terrain gate) {
202204
double v = t + (b - t) * (y + 0.5f) / this->height;
203205

204206
Vector3 vec_s = vec_u * u + vec_v * v + this->direction * d;
205-
Ray ray = Ray(vec_s, this->location);
207+
Ray ray = Ray(vec_s.normalize(), this->location);
206208

207209
baseFrameClosed.setPixel(x, y, castTerrainGateRay(ray, terrain, gate));
208210
}
@@ -251,10 +253,11 @@ Pixel Camera::castTerrainRay(Ray& ray, Terrain& terrain) {
251253
rad = rad > M_PI ? rad - M_PI : rad;
252254

253255
//Get a factor between 1 and 0; 90° = 0 ; 0° = 180° = 1
254-
double factor = 1 - (rad / (M_PI / 2));
256+
double factor = 1 - std::fmax(std::fmin(rad / (M_PI / 2), 1), 0);
255257

256258
//Darken the pixel based on the factor
257259
Pixel p = Pixel(127 * factor, 67 * factor, 67 * factor);
260+
p.setBaseDepth(bestDistance);
258261

259262
return p;
260263
}
@@ -268,7 +271,7 @@ Pixel Camera::castTerrainGateRay(Ray& ray, Terrain& terrain, Terrain& gate) {
268271

269272
Pixel initColor = Pixel(200, 200, 200); //Make the background gray
270273

271-
for (int i = 0; i < terrain.getFaceCount() + gate.getFaceCount(); i++) {
274+
for (int i = 0; i <= terrain.getFaceCount() + gate.getFaceCount(); i++) {
272275
double currDist = bestDistance;
273276

274277
if (i >= terrain.getFaceCount()) {
@@ -293,9 +296,17 @@ Pixel Camera::castTerrainGateRay(Ray& ray, Terrain& terrain, Terrain& gate) {
293296

294297
//Calculate Light to not have just brown everywhere
295298
if (hitIndex >= 0) {
299+
Vector3 planar1;
300+
Vector3 planar2;
296301

297-
Vector3 planar1 = terrain.getFace(hitIndex).a - terrain.getFace(hitIndex).b;
298-
Vector3 planar2 = terrain.getFace(hitIndex).a - terrain.getFace(hitIndex).c;
302+
if (hitIndex >= terrain.getFaceCount()) {
303+
planar1 = gate.getFace(hitIndex - terrain.getFaceCount()).a - gate.getFace(hitIndex - terrain.getFaceCount()).b;
304+
planar2 = gate.getFace(hitIndex - terrain.getFaceCount()).a - gate.getFace(hitIndex - terrain.getFaceCount()).c;
305+
}
306+
else {
307+
planar1 = terrain.getFace(hitIndex).a - terrain.getFace(hitIndex).b;
308+
planar2 = terrain.getFace(hitIndex).a - terrain.getFace(hitIndex).c;
309+
}
299310

300311
//Calculate norm vector
301312
Vector3 n = planar1.cross(planar2).normalize();
@@ -307,13 +318,14 @@ Pixel Camera::castTerrainGateRay(Ray& ray, Terrain& terrain, Terrain& gate) {
307318
double rad = acos(n.dot(lightToHit) / (n.length() * lightToHit.length()));
308319

309320
//Check if the angle is more than 180°, if so substract 180°
310-
rad = rad > M_PI ? rad - M_PI : rad;
321+
rad = rad >= M_PI ? rad - M_PI : rad;
311322

312323
//Get a factor between 1 and 0; 90° = 0 ; 0° = 180° = 1
313-
double factor = 1 - (rad / (M_PI / 2));
324+
double factor = 1 - std::fmax(std::fmin(rad / (M_PI / 2), 1), 0);
314325

315326
//Darken the pixel based on the factor
316327
Pixel p = Pixel(127 * factor, 67 * factor, 67 * factor);
328+
p.setBaseDepth(bestDistance);
317329

318330
return p;
319331
}
@@ -336,14 +348,15 @@ void Camera::shareBaseFrame(int rank)
336348
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
337349

338350
for (int i = 1; i < world_size; i++) {
339-
Frame::MpiSendFrame(baseFrameOpen, i);
340-
Frame::MpiSendFrame(baseFrameClosed, i);
351+
Frame::MpiSendFrame(this->baseFrameOpen, i);
352+
Frame::MpiSendFrame(this->baseFrameClosed, i);
341353
}
342354
}
343355
else {
344356
this->baseFrameOpen = Frame::MpiReceiveFrame(0);
345357
this->baseFrameClosed = Frame::MpiReceiveFrame(0);
346-
std::cout << "Base Frames passed to processor" << rank << std::endl;
358+
cout << this->baseFrameOpen.getPixel(400, 300).getBaseDepth() << endl;
359+
std::cout << "Base Frames passed to processor " << rank << std::endl;
347360
}
348361
}
349362

SphWaterfall/SphWaterfall/src/visualization/Frame.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "Frame.h"
2+
#include <iostream>
3+
24

35
Frame::Frame() {
46
this->width = 0;
@@ -13,7 +15,7 @@ Frame::Frame(unsigned int width, unsigned int height) {
1315
}
1416
}
1517

16-
Pixel Frame::getPixel(unsigned int x, unsigned int y) {
18+
Pixel& Frame::getPixel(unsigned int x, unsigned int y) {
1719
if (x >= this->width || y >= this->height) return Pixel(0, 0, 0);
1820
return pixels.at(y*width + x);
1921
}
@@ -43,10 +45,10 @@ void Frame::MpiSendFrame(Frame frame, int dest)
4345
unsigned short pixel[3] = {
4446
frame.getPixel(x, y).getRedValue(), //R
4547
frame.getPixel(x, y).getGreenValue(), //G
46-
frame.getPixel(x, y).getBlueValue() //B
48+
frame.getPixel(x, y).getBlueValue(), //B
4749
};
4850
MPI_Send(pixel, 3, MPI_UNSIGNED_SHORT, dest, 0, MPI_COMM_WORLD);
49-
51+
5052
double pixel_depth[1] = { frame.getPixel(x, y).getBaseDepth() };
5153
MPI_Send(pixel_depth, 1, MPI_DOUBLE, dest, 0, MPI_COMM_WORLD);
5254
}
@@ -67,6 +69,7 @@ Frame Frame::MpiReceiveFrame(int source)
6769
MPI_Recv(pixel, 3, MPI_UNSIGNED_SHORT, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
6870
frame.setPixel(x, y, Pixel(pixel[0], pixel[1], pixel[2]));
6971

72+
7073
double pixel_depth[1];
7174
MPI_Recv(pixel_depth, 1, MPI_DOUBLE, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
7275
frame.getPixel(x, y).setBaseDepth(pixel_depth[0]);

SphWaterfall/SphWaterfall/src/visualization/Frame.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Frame {
88
Frame(); //To genereate placeholder
99
Frame(unsigned int width, unsigned int height);
1010

11-
Pixel getPixel(unsigned int x, unsigned int y);
11+
Pixel& getPixel(unsigned int x, unsigned int y);
1212
void setPixel(unsigned int x, unsigned int y, Pixel pixel);
1313

1414
unsigned int getWidth();

SphWaterfall/SphWaterfall/src/visualization/ParticleObject.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,25 @@ bool ParticleObject::intersects(Ray &ray, double &distance, double &waterDepth,
3434
float t1 = (-1 * b + sqrt((b*b) - 4 * a*c)) / (2 * a);
3535
float t2 = (-1 * b - sqrt((b*b) - 4 * a*c)) / (2 * a);
3636

37-
waterDepth += t1 > t2 ? (t1 > maxDepth ? maxDepth : t1) - (t2 > maxDepth ? maxDepth : t2) : (t2 > maxDepth ? maxDepth : t2) - (t1 > maxDepth ? maxDepth : t1);
37+
if (t1 > t2) {
38+
if (t1 > maxDepth && t2 < maxDepth) waterDepth += maxDepth - t2;
39+
if (t1 < maxDepth) waterDepth += t1 - t2;
40+
}
41+
else {
42+
if (t2 > maxDepth && t1 < maxDepth) waterDepth += maxDepth - t1;
43+
if (t2 < maxDepth) waterDepth += t2 - t1;
44+
}
45+
46+
//waterDepth += t1 > t2 ? (t1 > maxDepth ? maxDepth : t1) - (t2 > maxDepth ? maxDepth : t2) : (t2 > maxDepth ? maxDepth : t2) - (t1 > maxDepth ? maxDepth : t1);
3847

3948
distance = t1 < t2 ? t1 : t2;
49+
50+
//Vector3 po = ray.origin + ray.direction * distance;
51+
if (maxDepth < 20000) {
52+
//std::cout << "t1 " << t1 << std::endl << "t2 " << t2 << std::endl << "mD " << maxDepth << std::endl << "Distance: " << distance << std::endl;
53+
}
54+
55+
4056
}
4157

4258
return ((b*b) - 4 * a*c) < 0 ? false : true;

SphWaterfall/SphWaterfall/src/visualization/Pixel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ Pixel::Pixel() {
55
this->green = 0;
66
this->blue = 0;
77
this->shaderUsage = false;
8-
this->baseDepth = std::numeric_limits<unsigned int>::max();
8+
this->baseDepth = std::numeric_limits<float>::max();
99
}
1010

1111
Pixel::Pixel(unsigned short red, unsigned short green, unsigned short blue) {
1212
this->red = red > 255 ? 255 : (red < 0 ? 0 : red);
1313
this->green = green > 255 ? 255 : (green < 0 ? 0 : green);
1414
this->blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
1515
this->shaderUsage = false;
16-
this->baseDepth = std::numeric_limits<unsigned int>::max();
16+
this->baseDepth = std::numeric_limits<float>::max();
1717
}
1818

1919
void Pixel::setColor(unsigned short red, unsigned short green, unsigned short blue) {

SphWaterfall/SphWaterfall/src/visualization/VisualizationManager.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ void VisualizationManager::renderFramesDistributed(string inputFileName, int ran
103103

104104
int counter = 0;
105105

106-
//Here it stops working for some reason..
107-
108106
for (int i = 0; i < frameCount; i++) {
109107
if ((i % (world_size - 1)) + 1 == rank) {
110108
if (counter >= frameParticles.size()) continue;

TestConfig.cfg

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# example config file
22

3-
print loadMesh -p "D:\Users\Magnus\Documents\Studium\StuPro\Project\StuProWaterfallFinal.obj"
4-
# loadMesh -p "D:\Users\Magnus\Documents\Studium\StuPro\Project\StuProWaterfallFinal.obj"
5-
loadMesh -p "D:\Users\Magnus\Documents\Studium\StuPro\Project\StuProCubeTri.obj"
3+
print loadMesh -p "StuProWaterfallFinal.obj"
4+
# loadMesh -p "StuProWaterfallFinal.obj"
5+
loadMesh -p "StuProWaterfallFinal.obj"
66

7-
print loadShutter -p "D:\Users\Magnus\Documents\Studium\StuPro\Project\StuProWaterfallShutter.obj"
8-
loadShutter -p "D:\Users\Magnus\Documents\Studium\StuPro\Project\StuProWaterfallShutter.obj"
7+
print loadShutter -p "StuProWaterfallShutter.obj"
8+
loadShutter -p "StuProWaterfallShutter.obj"
99

1010
# 0.0 5.5 10.3
11-
print addSource -v 0.0 51.5 11
12-
addSource -v 0.0 30.0 0.5
13-
# addSource -v 0.0 3.0 0.0
11+
#print addSource -v 0.0 51.5 11
12+
#addSource -v 0.0 30.0 0.5
13+
#addSource -v 0.0 3.0 0.0
1414

15-
print addSink -h 0.0
16-
addSink -h 0.0
15+
#print addSink -h 0.0
16+
#addSink -h 0.0
1717

18-
print particleGen
19-
particleGen
18+
#print particleGen
19+
#particleGen
2020

2121
print moveShutter -t 1
2222
moveShutter -t 1
2323

24-
print simulate -t 2
25-
simulate -t 2
24+
#print simulate -t 2
25+
#simulate -t 2
2626

2727
# render -v 10 50 -60
2828
print render -v 10 50 60

0 commit comments

Comments
 (0)