Skip to content

Commit

Permalink
Delete submodule output/: use a frame buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Duong (Zach) Nguyen committed Jan 15, 2023
1 parent e10f167 commit 85fd310
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 77 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ target_include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/include/math
${CMAKE_SOURCE_DIR}/include/3d
${CMAKE_SOURCE_DIR}/include/output
)

install(
Expand Down
15 changes: 11 additions & 4 deletions include/3d/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
#define RENDERER_H

#include "Camera.hpp"
#include "ImageOutput.hpp"
#include "Ray.hpp"
#include "Scene.hpp"
#include "Vec3.hpp"

class Renderer {
private:
ImageOutput &output;
int output_width;
int output_height;
int num_pixels;
size_t frame_buffer_size;
float *frame_buffer;

public:
Renderer(ImageOutput &output);
Renderer(int output_width, int output_height);

ImageOutput &getOutput() const;
int getOutputWidth();
int getOutputHeight();
float *getFrameBuffer();

void setOutputSize(int output_width, int output_height);

void render(const Scene &scene, const Camera &camera);

Expand Down
11 changes: 0 additions & 11 deletions include/output/ImageOutput.hpp

This file was deleted.

37 changes: 0 additions & 37 deletions include/output/PPMOutput.hpp

This file was deleted.

40 changes: 27 additions & 13 deletions src/3d/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@

#include "MathUtils.hpp"

Renderer::Renderer(ImageOutput &output) : output(output) {}
Renderer::Renderer(int output_width, int output_height) {
this->setOutputSize(output_width, output_height);
}

ImageOutput &Renderer::getOutput() const { return this->output; }
int Renderer::getOutputWidth() { return this->output_width; }
int Renderer::getOutputHeight() { return this->output_height; }
float *Renderer::getFrameBuffer() { return this->frame_buffer; }

void Renderer::render(const Scene &scene, const Camera &camera) {
const int WIDTH = this->output.getWidth();
const int HEIGHT = this->output.getHeight();
void Renderer::setOutputSize(int output_width, int output_height) {
this->output_width = output_width;
this->output_height = output_height;

this->num_pixels = output_width * output_height;
this->frame_buffer_size = 3 * num_pixels * sizeof(float);
this->frame_buffer = (float *)malloc(frame_buffer_size);
}

for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
auto u = static_cast<double>(i) / (WIDTH - 1);
auto v = static_cast<double>(j) / (HEIGHT - 1);
void Renderer::render(const Scene &scene, const Camera &camera) {
for (int i = 0; i < this->output_width; i++) {
for (int j = 0; j < this->output_height; j++) {
auto u = static_cast<double>(i) / (this->output_width - 1);
auto v = static_cast<double>(j) / (this->output_height - 1);

Ray ray = camera.getRay(u, v);
Color ray_color = this->getRayColor(ray, scene);

int r = static_cast<int>(256 * clamp(ray_color.getX(), 0, 0.999));
int g = static_cast<int>(256 * clamp(ray_color.getY(), 0, 0.999));
int b = static_cast<int>(256 * clamp(ray_color.getZ(), 0, 0.999));
// int r = static_cast<int>(256 * clamp(ray_color.getX(), 0, 0.999));
// int g = static_cast<int>(256 * clamp(ray_color.getY(), 0, 0.999));
// int b = static_cast<int>(256 * clamp(ray_color.getZ(), 0, 0.999));

int pixel_index = 3 * (i + j * this->output_width);

this->output.writeColor(r, g, b);
this->frame_buffer[pixel_index] = clamp(ray_color.getX(), 0, 0.999);
this->frame_buffer[pixel_index + 1] = clamp(ray_color.getY(), 0, 0.999);
this->frame_buffer[pixel_index + 2] = clamp(ray_color.getZ(), 0, 0.999);
}
}
}
Expand Down
36 changes: 25 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <fstream>
#include <memory>
#include <ostream>

#include "PPMOutput.hpp"
#include "Renderer.hpp"
#include "Scene.hpp"
#include "Sphere.hpp"
Expand All @@ -15,16 +13,8 @@ int main() {

const int IMAGE_WIDTH = 256;
const int IMAGE_HEIGHT = 256;
const int MAX_COLOR = 255;

std::ofstream f_out("image.ppm");
std::ostream &f_ostream(f_out);

f_ostream.rdbuf(f_out.rdbuf());

auto output = PPMOutput(IMAGE_WIDTH, IMAGE_HEIGHT, MAX_COLOR, f_ostream);

output.writeHeader();

// Camera

Expand All @@ -38,7 +28,31 @@ int main() {

scene.add(test_sphere);

Renderer renderer(output);
// Render

Renderer renderer(IMAGE_WIDTH, IMAGE_HEIGHT);

float *frame_buffer = renderer.getFrameBuffer();

renderer.render(scene, camera);

// Write to PPM

f_out << "P3\n" << IMAGE_WIDTH << ' ' << IMAGE_HEIGHT << '\n' << 255 << '\n';

for (int i = 0; i < IMAGE_WIDTH; i++) {
for (int j = 0; j < IMAGE_HEIGHT; j++) {
int pixel_index = 3 * (i + j * IMAGE_WIDTH);

float r = frame_buffer[pixel_index + 0];
float g = frame_buffer[pixel_index + 1];
float b = frame_buffer[pixel_index + 2];

int ir = int(255.99 * r);
int ig = int(255.99 * g);
int ib = int(255.99 * b);

f_out << ir << ' ' << ig << ' ' << ib << '\n';
}
}
}

0 comments on commit 85fd310

Please sign in to comment.