1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <vector> #include <string>
class Vector { public: double x, y, z; Vector(const double x = 0, const double y = 0, const double z = 0) : x(x), y(y), z(z) {} Vector(const Vector&) = default; Vector& operator=(const Vector&) = default; Vector operator+(const Vector& b) const { return Vector(x + b.x, y + b.y, z + b.z); } Vector operator-(const Vector& b) const { return Vector(x - b.x, y - b.y, z - b.z); } Vector operator-() const { return Vector(-x, -y, -z); } Vector operator*(const double s) const { return Vector(x * s, y * s, z * s); } Vector operator/(const double s) const { return Vector(x / s, y / s, z / s); } bool isZero() const { return (x == 0.) && (y == 0.) && (z == 0.); } }; inline double LengthSquared(const Vector& v) { return v.x * v.x + v.y * v.y + v.z * v.z; } inline double Length(const Vector& v) { return std::sqrt(LengthSquared(v)); } inline Vector operator*(const double s, const Vector& v) { return v * s; } inline Vector Normalize(const Vector& v) { return v / Length(v); } inline Vector Multiply(const Vector& v1, const Vector& v2) { return Vector(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); } inline double Dot(const Vector& v1, const Vector& v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } inline const Vector Cross(const Vector& v1, const Vector& v2) { return Vector((v1.y * v2.z) - (v1.z * v2.y), (v1.z * v2.x) - (v1.x * v2.z), (v1.x * v2.y) - (v1.y * v2.x)); } using Color = Vector; constexpr double pi_2 = 1.5707963267948966192313216916398; constexpr int hemicube_res = 256;
double multiplier_front[hemicube_res][hemicube_res]; double multiplier_down[hemicube_res / 2][hemicube_res];
class Camera { public: Vector pos, dir, up; double fov, aspect_ratio; double width, height; Camera(Vector pos, Vector dir, Vector up, double fov, double aspect_ratio = 1.) : pos(pos), dir(dir), up(up), fov(fov), aspect_ratio(aspect_ratio) { height = 2 * std::tan(fov / 2); width = height * aspect_ratio; } Vector project(const Vector& v) const { Vector a = v - pos; double z = Dot(a, dir); double y = Dot(a, up); double x = Dot(a, Cross(up, dir)); return Vector(-x / z, y / z, z); } };
class Patch { public: Vector pos; Vector a, b; Color emission; Color reflectance; Color incident; Color excident; Patch(Vector pos, Vector a, Vector b, Color emission, Color reflectance) : pos(pos), a(a), b(b), emission(emission), reflectance(reflectance), incident(), excident(emission) {} };
bool check_triangle(double cx, double cy, Vector a, Vector b, Vector c, double& depth) { Vector ab = b - a, ac = c - a; double tmp = ab.x * ac.y - ab.y * ac.x; if (std::fabs(tmp) > 1e-6) { double alpha = (-ac.x * (cy - a.y) + ac.y * (cx - a.x)) / tmp; double beta = (ab.x * (cy - a.y) - ab.y * (cx - a.x)) / tmp; if (alpha >= 0 && beta >= 0 && alpha + beta <= 1) { depth = alpha * b.z + beta * c.z + (1 - alpha - beta) * a.z; return true; } } return false; }
bool inside_quadrilateral(double cx, double cy, Vector a, Vector b, Vector c, Vector d, double& depth) { return check_triangle(cx, cy, a, b, c, depth) || check_triangle(cx, cy, d, b, c, depth); }
std::vector<Color> render_view(const Camera& camera, const std::vector<Patch>& scene, const int width, const int height) { std::vector<Color> image(width * height); std::vector<double> zbuffer(width * height, 1000000); for (const auto& p : scene) { if (Length(p.pos + 0.5 * (p.a + p.b) - camera.pos) < 1e-6) continue; Vector a = camera.project(p.pos); Vector b = camera.project(p.pos + p.a); Vector c = camera.project(p.pos + p.b); Vector d = camera.project(p.pos + p.a + p.b); if (a.z < 1e-6 || b.z < 1e-6 || c.z < 1e-6 || d.z < 1e-6)continue; double px = camera.width / width; double py = camera.height / height; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { double cx = (j + 0.5) * px - camera.width * 0.5; double cy = (i + 0.5) * py - camera.height * 0.5; double depth = 1000000; if (inside_quadrilateral(cx, cy, a, b, c, d, depth)) { if (depth < zbuffer[i * width + j] && depth > 1e-6) { image[i * width + j] = p.excident; zbuffer[i * width + j] = depth; } } } } } return image; }
void save_bmp_file(const std::string& filename, const std::vector<Color>& image, const int width, const int height) { FILE* f = fopen(filename.c_str(), "wb"); int filesize = 54 + 3 * width * height; unsigned char* img = new unsigned char[3 * width * height]; memset(img, 0, 3 * width * height);
for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { double r = sqrt(image[i + j * width].x) * 255; double g = sqrt(image[i + j * width].y) * 255; double b = sqrt(image[i + j * width].z) * 255; if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255; img[(i + j * width) * 3 + 2] = (unsigned char)(r); img[(i + j * width) * 3 + 1] = (unsigned char)(g); img[(i + j * width) * 3 + 0] = (unsigned char)(b); } } unsigned char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 }; unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 }; unsigned char bmppad[3] = { 0,0,0 };
bmpfileheader[2] = (unsigned char)(filesize); bmpfileheader[3] = (unsigned char)(filesize >> 8); bmpfileheader[4] = (unsigned char)(filesize >> 16); bmpfileheader[5] = (unsigned char)(filesize >> 24);
bmpinfoheader[4] = (unsigned char)(width); bmpinfoheader[5] = (unsigned char)(width >> 8); bmpinfoheader[6] = (unsigned char)(width >> 16); bmpinfoheader[7] = (unsigned char)(width >> 24); bmpinfoheader[8] = (unsigned char)(height); bmpinfoheader[9] = (unsigned char)(height >> 8); bmpinfoheader[10] = (unsigned char)(height >> 16); bmpinfoheader[11] = (unsigned char)(height >> 24);
fwrite(bmpfileheader, 1, 14, f); fwrite(bmpinfoheader, 1, 40, f); for (int i = 0; i < height; ++i) { fwrite(img + (i * width * 3), 3, width, f); fwrite(bmppad, 1, (4 - (width * 3) % 4) % 4, f); } fclose(f); delete[] img; }
void load_scene(std::vector<Patch>& scene) { FILE* fi; int64_t n; fi = fopen("conf.data", "rb"); fread(&n, 1, 8, fi); for (int i = 0; i < n; i++) { Vector a[5]; fread(a, 1, 120, fi); scene.emplace_back(Patch(a[0], a[1], a[2], a[3], a[4])); } fclose(fi); }
void divide_patches(std::vector<Patch>& scene, double threshold) { std::vector<Patch> tmp = std::move(scene); for (const auto& p : tmp) { double len_a = Length(p.a); double len_b = Length(p.b); int a = static_cast<int>(len_a / threshold); int b = static_cast<int>(len_b / threshold); for (int i = 0; i <= a; ++i) { for (int j = 0; j <= b; ++j) { Vector pos = p.pos + i * threshold * Normalize(p.a) + j * threshold * Normalize(p.b); Vector pa = (i + 1) * threshold > len_a ? p.a - i * threshold * Normalize(p.a) : threshold * Normalize(p.a); Vector pb = (j + 1) * threshold > len_b ? p.b - j * threshold * Normalize(p.b) : threshold * Normalize(p.b); if (pa.isZero() || pb.isZero()) continue; scene.emplace_back(Patch(pos, pa, pb, p.emission, p.reflectance)); } } } }
void cal_multiplier_map() { constexpr double pw = 2. / hemicube_res;
double s=0; for (int i = 0; i < hemicube_res; ++i) { for (int j = 0; j < hemicube_res; ++j) { double cx = (j + 0.5) * pw - 1; double cy = (i + 0.5) * pw - 1; multiplier_front[i][j] = 1 / (cx * cx + cy * cy + 1)/ (cx * cx + cy * cy + 1); s = s + multiplier_front[i][j]; } } for (int i = 0; i < hemicube_res / 2; ++i) { for (int j = 0; j < hemicube_res; ++j) { double cx = (j + 0.5) * pw - 1; double cz = (i + 0.5) * pw; multiplier_down[i][j] = cz / (cx * cx + cz * cz + 1)/(cx * cx + cz * cz + 1); s = s + 4 * multiplier_down[i][j]; } } for (int i = 0; i < hemicube_res; ++i) { for (int j = 0; j < hemicube_res; ++j) { } } s = s / (hemicube_res * hemicube_res / 4) / 3.1416; printf("%f\n", s); }
void cal_incident_light(std::vector<Patch>& scene) {
for (int i = 0; i < scene.size(); i++) { auto& p = scene[i];
Vector cpos = p.pos + 0.5 * (p.a + p.b); std::vector<Color> front = render_view(Camera(cpos, Normalize(Cross(p.b, p.a)), Normalize(p.b), pi_2), scene, hemicube_res, hemicube_res); std::vector<Color> up = render_view(Camera(cpos, Normalize(p.b), -Normalize(Cross(p.b, p.a)), pi_2), scene, hemicube_res, hemicube_res); std::vector<Color> left = render_view(Camera(cpos, -Normalize(p.a), Normalize(p.b), pi_2), scene, hemicube_res, hemicube_res); std::vector<Color> right = render_view(Camera(cpos, Normalize(p.a), Normalize(p.b), pi_2), scene, hemicube_res, hemicube_res); std::vector<Color> down = render_view(Camera(cpos, -Normalize(p.b), Normalize(Cross(p.b, p.a)), pi_2), scene, hemicube_res, hemicube_res); Color total_light{}; for (int i = 0; i < hemicube_res; ++i) { for (int j = 0; j < hemicube_res; ++j) { total_light = total_light + front[i * hemicube_res + j] * multiplier_front[i][j]; if (i < hemicube_res / 2) total_light = total_light + up[i * hemicube_res + j] * multiplier_down[hemicube_res / 2 - 1 - i][j]; if (i >= hemicube_res / 2) total_light = total_light + down[i * hemicube_res + j] * multiplier_down[i - hemicube_res / 2][j]; if (j < hemicube_res / 2) total_light = total_light + right[i * hemicube_res + j] * multiplier_down[hemicube_res / 2 - 1 - j][i]; if (j >= hemicube_res / 2) total_light = total_light + left[i * hemicube_res + j] * multiplier_down[j - hemicube_res / 2][i]; } } p.incident = total_light / (hemicube_res * hemicube_res / 4) / 3.1416; } }
void cal_excident_light(std::vector<Patch>& scene) { for (auto& p : scene) { p.excident = Multiply(p.incident, p.reflectance) + p.emission; } }
int main() { const int width = 512, height = 512; Camera camera(Vector(278, 273, -800), Vector(0, 0, 1), Vector(0, 1, 0), 2 * std::atan2(0.0125, 0.035)); std::vector<Patch> scene;
std::cout << "init scene" << std::endl; load_scene(scene);
std::cout << "divide patches" << std::endl; divide_patches(scene, 15); std::cout << "total patch number: " << scene.size() << std::endl;
int iter = 0; std::cout << "render view " << iter << std::endl; std::vector<Color> image = render_view(camera, scene, width, height);
std::cout << "save image " << iter << std::endl; save_bmp_file("cornellbox" + std::to_string(iter) + ".bmp", image, width, height);
cal_multiplier_map();
const int max_iteration = 5; for (iter = 1; iter <= max_iteration; ++iter) { cal_incident_light(scene); cal_excident_light(scene); std::cout << "render view " << iter << std::endl; image = render_view(camera, scene, width, height);
std::cout << "save image " << iter << std::endl; save_bmp_file("cornellbox" + std::to_string(iter) + ".bmp", image, width, height); }
return 0; }
|