2
2
#include " Camera.h"
3
3
#include " util.h"
4
4
#include " Ray.h"
5
+ #include < math.h>
5
6
6
7
Camera::Camera (Vector3 location, Vector3 direction, unsigned int width, unsigned int height, unsigned int ID) {
7
8
this ->width = width;
@@ -13,7 +14,7 @@ Camera::Camera(Vector3 location, Vector3 direction, unsigned int width, unsigned
13
14
this ->direction = direction;
14
15
}
15
16
16
- void Camera::debugRenderFrame (std::vector<DebugObject> particles, int frameID) {
17
+ void Camera::volumeRenderFrame (std::vector<DebugObject> particles, int frameID) {
17
18
18
19
Frame frame = Frame (this ->width , this ->height , frameID);
19
20
const double aspectRatio = (double )width / (double )height;
@@ -46,6 +47,46 @@ void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
46
47
Ray ray = Ray (vec_s, this ->location );
47
48
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
+
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
+
49
90
frame.setPixel (x, y, castDebugRay (ray, particles));
50
91
}
51
92
}
@@ -55,6 +96,7 @@ void Camera::debugRenderFrame(std::vector<DebugObject> particles, int frameID) {
55
96
56
97
Pixel Camera::castDebugRay (Ray ray, std::vector<DebugObject> particles) {
57
98
double bestDistance = std::numeric_limits<float >::max ();
99
+ double waterDepth = 0 ;
58
100
DebugObject hitObject;
59
101
DebugObject *hit = &hitObject;
60
102
hit = nullptr ;
@@ -63,7 +105,7 @@ Pixel Camera::castDebugRay(Ray ray, std::vector<DebugObject> particles) {
63
105
DebugObject &obj = particles.at (i);
64
106
double currDist = bestDistance;
65
107
66
- if (obj.intersects (ray, currDist)) {
108
+ if (obj.intersects (ray, currDist, waterDepth )) {
67
109
if (currDist < bestDistance) {
68
110
bestDistance = currDist;
69
111
hit = &obj;
@@ -74,6 +116,40 @@ Pixel Camera::castDebugRay(Ray ray, std::vector<DebugObject> particles) {
74
116
return hit == nullptr ? Pixel (0 , 0 , 0 ) : hitObject.getColor ();
75
117
}
76
118
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
+
77
153
void Camera::renderFrame (std::vector<SphParticle> particles) {
78
154
// TODO: implement rendering algorithm
79
155
}
0 commit comments