3 Commits

Author SHA1 Message Date
Godzil
fac2212661 Canvas implementation and add build of LodePNG 2020-02-14 16:04:28 +00:00
Godzil
a9321b5051 Update an header 2020-02-14 15:08:50 +00:00
Godzil
f3678992c5 Add Colour implementation 2020-02-14 15:08:40 +00:00
10 changed files with 248 additions and 7 deletions

View File

@@ -17,6 +17,12 @@ ExternalProject_Add(googletest
TEST_COMMAND ""
)
# LodePNG don't make a .a or .so, so let's build a library here
add_library(LodePNG STATIC)
set(LODEPNG_INCLUDE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/external/lodepng)
target_sources(LodePNG PRIVATE external/lodepng/lodepng.cpp external/lodepng/lodepng.h)
# Main app
add_subdirectory(source)
# Unit Tests

View File

@@ -1,16 +1,16 @@
# To simplify testing, the app is build in two passes,
# First most is build as a library
add_library(rayonnement STATIC math_helper.cpp)
add_library(rayonnement STATIC)
set(RAY_HEADERS include/tuples.h include/math_helper.h)
set(RAY_SOURCES tuples.cpp)
set(RAY_HEADERS include/tuples.h include/math_helper.h include/colour.h include/canvas.h)
set(RAY_SOURCES tuples.cpp math_helper.cpp colour.cpp canvas.cpp)
target_include_directories(rayonnement PUBLIC include)
target_sources(rayonnement PRIVATE ${RAY_HEADERS} ${RAY_SOURCES})
target_link_libraries(rayonnement LodePNG)
# Second we build the main executable
add_executable(dorayme main.cpp)
target_include_directories(rayonnement PUBLIC include)
target_include_directories(rayonnement PUBLIC include ${LODEPNG_INCLUDE_FOLDER})
target_link_libraries(dorayme rayonnement)

57
source/canvas.cpp Normal file
View File

@@ -0,0 +1,57 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Canvas implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <canvas.h>
#include <lodepng.h>
#define BPP (24)
#define BytePP (BPP / 8)
#define MIN(_a, _b) ((_a)<(_b)?(_a):(_b))
#define MAX(_a, _b) ((_a)>(_b)?(_a):(_b))
Canvas::Canvas(uint32_t width, uint32_t height) : width(width), height(height)
{
this->bitmap = (uint8_t *)calloc(4, width * height);
this->stride = BytePP * width;
}
Canvas::~Canvas()
{
if (this->bitmap != nullptr)
{
free(this->bitmap);
}
}
void Canvas::put_pixel(uint32_t x, uint32_t y, Colour c)
{
uint32_t offset = y * this->stride + x * BytePP;
this->bitmap[offset + 0] = MAX(MIN(c.red() * 255, 255), 0);
this->bitmap[offset + 1] = MAX(MIN(c.green() * 255, 255), 0);
this->bitmap[offset + 2] = MAX(MIN(c.blue() * 255, 255), 0);
}
Colour Canvas::get_pixel(uint32_t x, uint32_t y)
{
uint32_t offset = y * this->stride + x * BytePP;
return Colour(this->bitmap[offset + 0] / 255, this->bitmap[offset + 1] / 255, this->bitmap[offset + 2] / 255);
}
bool Canvas::SaveAsPNG(const char *filename)
{
uint32_t ret = lodepng_encode24_file(filename, this->bitmap, this->width, this->height);
if (ret > 0)
{
printf("lodepng_encode_file returned %d!\n", ret);
}
return ret == 0;
}

10
source/colour.cpp Normal file
View File

@@ -0,0 +1,10 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Colour implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include "colour.h"

32
source/include/canvas.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Canvas header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_CANVAS_H
#define DORAYME_CANVAS_H
#include <stdint.h>
#include <colour.h>
class Canvas
{
private:
uint8_t *bitmap;
uint32_t stride;
public:
uint32_t width, height;
Canvas(uint32_t width, uint32_t height);
~Canvas();
void put_pixel(uint32_t x, uint32_t y, Colour c);
Colour get_pixel(uint32_t x, uint32_t y);
bool SaveAsPNG(const char *filename);
};
#endif /* DORAYME_CANVAS_H */

32
source/include/colour.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Colour header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_COLOUR_H
#define DORAYME_COLOUR_H
#include <tuples.h>
class Colour : public Tuple
{
public:
Colour(double red, double green, double blue) : Tuple(red, green, blue, 0) {};
double red() { return this->x; };
double green() { return this->y; };
double blue() { return this->z; };
double red(double v) { this->x = v; return v; };
double green(double v) { this->y = v; return v; };
double blue(double v) { this->z = v; return v; };
using Tuple::operator*;
Colour operator*(const Colour &b) const { return Colour(this->x * b.x,
this->y * b.y,
this->z * b.z); };
};
#endif /* DORAYME_COLOUR_H */

View File

@@ -3,7 +3,7 @@ project(DoRayTested)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(TESTS_SRC tuples_test.cpp)
set(TESTS_SRC tuples_test.cpp colour_test.cpp canvas_test.cpp)
add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

53
tests/canvas_test.cpp Normal file
View File

@@ -0,0 +1,53 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Canvas unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <colour.h>
#include <canvas.h>
#include <math.h>
#include <gtest/gtest.h>
TEST(CanvasTest, Creating_a_canvas)
{
Canvas c = Canvas(10, 20);
int x, y;
ASSERT_EQ(c.width, 10);
ASSERT_EQ(c.height, 20);
for(y = 0; y < 20; y++)
{
for(x = 0; x < 10; x++)
{
ASSERT_EQ(c.get_pixel(x, y), Colour(0, 0, 0));
}
}
}
TEST(CanvasTest, Test_Writing_pixels_to_a_canvas_Test)
{
Canvas c = Canvas(10, 20);
Colour red = Colour(1, 0, 0);
c.put_pixel(2, 3, red);
ASSERT_EQ(c.get_pixel(2, 3), red);
}
TEST(CanvasTest, Save_a_PNG_file)
{
Canvas c = Canvas(5, 3);
Colour c1 = Colour(1.5, 0, 0);
Colour c2 = Colour(0, 0.5, 0);
Colour c3 = Colour(-0.5, 0, 1);
c.put_pixel(0, 0, c1);
c.put_pixel(2, 1, c2);
c.put_pixel(4, 2, c3);
ASSERT_TRUE(c.SaveAsPNG("Save_a_PNG_file.png"));
}

51
tests/colour_test.cpp Normal file
View File

@@ -0,0 +1,51 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Colour unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <colour.h>
#include <math.h>
#include <gtest/gtest.h>
TEST(ColourTest, Color_is_tuple)
{
Colour c = Colour(-0.5, 0.4, 1.7);
ASSERT_EQ(c.red(), -0.5);
ASSERT_EQ(c.green(), 0.4);
ASSERT_EQ(c.blue(), 1.7);
}
TEST(ColourTest, Adding_colours)
{
Colour c1 = Colour(0.9, 0.6, 0.75);
Colour c2 = Colour(0.7, 0.1, 0.25);
ASSERT_EQ(c1 + c2, Colour(1.6, 0.7, 1.0));
}
TEST(ColourTest, Substracting_colours)
{
Colour c1 = Colour(0.9, 0.6, 0.75);
Colour c2 = Colour(0.7, 0.1, 0.25);
ASSERT_EQ(c1 - c2, Colour(0.2, 0.5, 0.5));
}
TEST(ColourTest, Multiplying_colour_by_a_scalar)
{
Colour c = Colour(0.2, 0.3, 0.4);
ASSERT_EQ(c * 2, Colour(0.4, 0.6, 0.8));
}
TEST(ColourTest, Multiplying_colours)
{
Colour c1 = Colour(1, 0.2, 0.4);
Colour c2 = Colour(0.9, 1, 0.1);
ASSERT_EQ(c1 * c2, Colour(0.9, 0.2, 0.04));
}

View File

@@ -1,6 +1,6 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Tuples tests
* Tuples unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.