Canvas implementation and add build of LodePNG
This commit is contained in:
@@ -3,14 +3,14 @@
|
||||
# First most is build as a library
|
||||
add_library(rayonnement STATIC)
|
||||
|
||||
set(RAY_HEADERS include/tuples.h include/math_helper.h include/colour.h)
|
||||
set(RAY_SOURCES tuples.cpp math_helper.cpp colour.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
57
source/canvas.cpp
Normal 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;
|
||||
}
|
||||
32
source/include/canvas.h
Normal file
32
source/include/canvas.h
Normal 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 */
|
||||
Reference in New Issue
Block a user