From 43714bd116945573e7e4c854445462afa7f9b1b4 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 11 Apr 2022 22:13:57 +0800 Subject: Implement ray3, timer and a simple viewport scanner. Fix bitmap wrong pixel sequence. Remove default constructor of bitmap. Add pixel mixture method. --- bitmap.h | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'bitmap.h') diff --git a/bitmap.h b/bitmap.h index b94bb25..31b9053 100644 --- a/bitmap.h +++ b/bitmap.h @@ -10,13 +10,48 @@ #include #include +#define COLORMIX_OVERFLOW_CHECK + +//// T is some unsigned integer +//template +//T saturate_add(T a, T b) { +// T c = a + b; +// if (a > c || b > c) { +// c = (1ULL << (sizeof(T) * 8U)) - 1ULL; +// } +// return c; +//} // T is some unsigned integer, do not use float point types! template struct pixel { T r, g, b; + + /** + * Create a pixel with given depth, from normalized color values. + * For example: for 8bit pixel, with (1, 0.5, 0.25), we get: (255, 127, 63). + */ + static pixel from_normalized(double r, double g, double b) { + const auto mod = (1ULL << (sizeof(T) * 8U)) - 1ULL; + return pixel{.r = (T) (mod * r), .g = (T) (mod * g), .b = (T) (mod * b)}; + } }; +// Mix two colors a and b. Returns a*u + b*v +template +inline pixel mix(const pixel &a, const pixel &b, double u, double v) { + assert(u >= 0); + assert(v >= 0); + assert(u <= 1); + assert(v <= 1); + assert(u + v <= 1); + pixel c{0, 0, 0}; + c.r = (T) (u * a.r + v * b.r); + c.g = (T) (u * a.g + v * b.g); + c.b = (T) (u * a.b + v * b.b); + return c; +} + // 8 bit pixel using pixel8b = pixel; @@ -28,16 +63,18 @@ class bitmap { pixel &image(unsigned x, unsigned y) { assert(x < width); assert(y < height); - return content[x * width + y]; + return content[x + y * width]; } pixel &image(unsigned x, unsigned y) const { assert(x < width); assert(y < height); - return content[x * width + y]; + return content[x + y * width]; } public: + bitmap() = delete; + bitmap(unsigned int width, unsigned int height) : width(width), height(height) { content.resize(width * height, pixel{}); } -- cgit v1.2.3