Compare commits
2 Commits
98a6c551c3
...
d2ce61e208
Author | SHA1 | Date | |
---|---|---|---|
|
d2ce61e208 | ||
|
415a2122b4 |
4
Makefile
4
Makefile
@ -1,6 +1,6 @@
|
||||
CXX = g++
|
||||
|
||||
CXXFLAGS = -std=c++14 -Wall -Wextra -Wno-unused-parameter -march=native -Ofast -fopenmp -flto
|
||||
CXXFLAGS = -std=c++20 -Wall -Wextra -Wno-unused-parameter -march=native -Ofast -flto -fopenmp
|
||||
|
||||
DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h material.h lodepng.h
|
||||
OBJ = wtracer.o material.o vec3.o lodepng.o
|
||||
@ -20,5 +20,5 @@ $(TARGET): $(OBJ)
|
||||
$(CXX) $(CXXFLAGS) -o $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) $(TARGET) $(OBJ) image.ppm
|
||||
$(RM) $(TARGET) $(OBJ) image.png
|
||||
|
||||
|
124
wtracer.cpp
124
wtracer.cpp
@ -1,10 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
|
||||
#define LODEPNG_NO_COMPILE_DECODER
|
||||
#include "lodepng.h"
|
||||
|
||||
#include <boost/lockfree/queue.hpp>
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
#include "color.h"
|
||||
#include "vec3.h"
|
||||
#include "ray.h"
|
||||
@ -31,11 +39,9 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) {
|
||||
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);
|
||||
}
|
||||
|
||||
Hittable_list setup_random_scene() {
|
||||
Hittable_list setup_random_scene(const int sph_i) {
|
||||
Hittable_list world;
|
||||
|
||||
const int sph_i = 3;
|
||||
|
||||
auto ground_material = std::make_shared<Lambertian>(Color(0.5, 0.5, 0.5));
|
||||
world.add(std::make_shared<Sphere>(Point3(0, -1000, 0), 1000, ground_material));
|
||||
for (int a = -sph_i; a<sph_i; a++) {
|
||||
@ -45,7 +51,7 @@ Hittable_list setup_random_scene() {
|
||||
|
||||
if ((center - Point3(4, 0.2, 0)).length() > 0.9) {
|
||||
std::shared_ptr<Material> sphere_material;
|
||||
if (choose_mat < 0.8) {
|
||||
if (choose_mat < 0.7) {
|
||||
// diffuse
|
||||
auto albedo = Color::random() * Color::random();
|
||||
sphere_material = std::make_shared<Lambertian>(albedo);
|
||||
@ -78,17 +84,78 @@ Hittable_list setup_random_scene() {
|
||||
return world;
|
||||
}
|
||||
|
||||
struct render_params {
|
||||
int image_width;
|
||||
int image_height;
|
||||
int samples_per_pixel;
|
||||
int max_depth;
|
||||
Hittable_list& world;
|
||||
Camera& cam;
|
||||
};
|
||||
|
||||
struct render_tile {
|
||||
int start_x;
|
||||
int start_y;
|
||||
int end_x;
|
||||
int end_y;
|
||||
};
|
||||
|
||||
void split_line(const int& line_length, int& start, int& end) {
|
||||
#ifdef _OPENMP
|
||||
const int line_length_thread = static_cast<int>(line_length/omp_get_num_threads());
|
||||
start = omp_get_thread_num() * line_length_thread;
|
||||
end = std::min((omp_get_thread_num()+1) * line_length_thread, line_length);
|
||||
#else
|
||||
start = 0;
|
||||
end = line_length;
|
||||
#endif
|
||||
}
|
||||
|
||||
void render(boost::lockfree::queue<render_tile>& queue,
|
||||
std::atomic<int>& queue_counter,
|
||||
const render_params& params, std::vector<Color>& image) {
|
||||
render_tile rt;
|
||||
while (queue.pop(rt)) {
|
||||
--queue_counter;
|
||||
int qc = queue_counter;
|
||||
printf("\rTiles remaining: %4d", qc);
|
||||
std::cout << std::flush;
|
||||
for (int i = rt.start_x; i<rt.end_x; ++i) {
|
||||
for (int j = rt.start_y; j<rt.end_y; ++j) {
|
||||
Color pixel_color(0, 0, 0);
|
||||
for (int s = 0; s<params.samples_per_pixel; ++s) {
|
||||
auto u = double(j + random_double(-0.5, 0.5)) / (params.image_width - 1);
|
||||
auto v = double(i + random_double(-0.5, 0.5)) / (params.image_height - 1);
|
||||
Ray r = params.cam.get_ray(u, v);
|
||||
pixel_color += ray_color(r, params.world, params.max_depth);
|
||||
}
|
||||
image[i*params.image_width+j] = pixel_color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const auto aspect_ratio = 16.0 / 9.0;
|
||||
//const int image_width = 1920;
|
||||
//const int image_width = 1280;
|
||||
const int image_width = 768;
|
||||
//const int image_width = 384;
|
||||
//const int image_width = 768;
|
||||
const int image_width = 384;
|
||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
||||
//const int samples_per_pixel = 2000;
|
||||
//const int samples_per_pixel = 1000;
|
||||
const int samples_per_pixel = 400;
|
||||
const int max_depth = 50;
|
||||
|
||||
auto world = setup_random_scene();
|
||||
//const int tile_size = 128;
|
||||
const int tile_size = 64;
|
||||
|
||||
const int sph_i = 3;
|
||||
//const int sph_i = 5;
|
||||
//const int sph_i = 7;
|
||||
//const int sph_i = 11;
|
||||
|
||||
auto world = setup_random_scene(sph_i);
|
||||
|
||||
Point3 lookfrom(13, 2, 3);
|
||||
Point3 lookat(0, 0, 0);
|
||||
@ -98,21 +165,36 @@ int main() {
|
||||
|
||||
Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
|
||||
|
||||
render_params params = {
|
||||
image_width,
|
||||
image_height,
|
||||
samples_per_pixel,
|
||||
max_depth,
|
||||
world,
|
||||
cam,
|
||||
};
|
||||
|
||||
std::atomic<int> queue_counter;
|
||||
boost::lockfree::queue<render_tile> queue(0);
|
||||
|
||||
for (int i = 0; i<std::ceil(double(image_height)/double(tile_size)); i++) {
|
||||
for (int j = 0; j<std::ceil(double(image_width)/double(tile_size)); j++) {
|
||||
render_tile rt = {
|
||||
i*tile_size,
|
||||
j*tile_size,
|
||||
std::min((i+1)*tile_size, image_height),
|
||||
std::min((j+1)*tile_size, image_width)
|
||||
};
|
||||
queue.push(rt);
|
||||
++queue_counter;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Color> image(image_width * image_height);
|
||||
|
||||
for (int i = image_height-1; i>=0; --i) {
|
||||
std::cerr << "\rScanline remaining: " << std::setw(4) << i << std::flush;
|
||||
#pragma omp parallel for
|
||||
for (int j = 0; j<image_width; ++j) {
|
||||
Color pixel_color(0, 0, 0);
|
||||
for (int s = 0; s<samples_per_pixel; ++s) {
|
||||
auto u = double(j + random_double(-0.5, 0.5)) / (image_width - 1);
|
||||
auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1);
|
||||
Ray r = cam.get_ray(u, v);
|
||||
pixel_color += ray_color(r, world, max_depth);
|
||||
}
|
||||
image.at(i*image_width+j) = pixel_color;
|
||||
}
|
||||
#pragma omp parallel
|
||||
{
|
||||
render(queue, queue_counter, params, image);
|
||||
}
|
||||
|
||||
std::cerr << "\nAssembling image.\n";
|
||||
@ -120,7 +202,7 @@ int main() {
|
||||
std::vector<unsigned char> img_lode(image_width * image_height * 3);
|
||||
for (int i = 0; i<image_height; ++i) {
|
||||
for (int j = 0; j<image_width; ++j) {
|
||||
write_color_vec(img_lode, i*image_width*3+j*3, image.at((image_height-1-i)*image_width+j), samples_per_pixel);
|
||||
write_color_vec(img_lode, i*image_width*3+j*3, image[(image_height-1-i)*image_width+j], samples_per_pixel);
|
||||
}
|
||||
}
|
||||
std::cerr << "Writing file \"image.png\".\n";
|
||||
|
Loading…
Reference in New Issue
Block a user