Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
SoftwareFramebuffer.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12#include <algorithm>
13#include <cstddef>
14#include <cstdint>
15#include <span>
16#include <vector>
17
18namespace correlation::render3d {
19
25public:
30
37
43 void resize(uint32_t width, uint32_t height) {
44 width_ = width;
46 const std::size_t pixel_count = static_cast<std::size_t>(width_) * height_;
47 color_buffer_.resize(pixel_count * 4, 0);
48 depth_buffer_.resize(pixel_count, 1.0F);
49 }
50
55 void clearColor(RgbaColor color) noexcept {
56 const std::size_t pixel_count = static_cast<std::size_t>(width_) * height_;
57 for (std::size_t i = 0; i < pixel_count; ++i) {
58 const std::size_t offset = i * 4;
59 color_buffer_[offset] = color.r;
60 color_buffer_[offset + 1] = color.g;
61 color_buffer_[offset + 2] = color.b;
62 color_buffer_[offset + 3] = color.a;
63 }
64 }
65
70 void clearDepth(float depth = 1.0F) noexcept { std::ranges::fill(depth_buffer_, depth); }
71
77 void clear(RgbaColor color, float depth = 1.0F) noexcept {
78 clearColor(color);
79 clearDepth(depth);
80 }
81
90 bool testAndSetPixel(uint32_t pixel_x, uint32_t pixel_y, float depth, RgbaColor color) noexcept {
91 if (pixel_x >= width_ || pixel_y >= height_) {
92 return false;
93 }
94 const std::size_t idx = static_cast<std::size_t>(pixel_y) * width_ + pixel_x;
95 if (depth < depth_buffer_[idx]) {
96 depth_buffer_[idx] = depth;
97 const std::size_t color_idx = idx * 4;
98 color_buffer_[color_idx] = color.r;
99 color_buffer_[color_idx + 1] = color.g;
100 color_buffer_[color_idx + 2] = color.b;
101 color_buffer_[color_idx + 3] = color.a;
102 return true;
103 }
104 return false;
105 }
106
116 bool blendAdditivePixel(uint32_t pixel_x, uint32_t pixel_y, float depth,
117 RgbaColor color) noexcept {
118 if (pixel_x >= width_ || pixel_y >= height_ || color.a == 0) {
119 return false;
120 }
121 const std::size_t idx = static_cast<std::size_t>(pixel_y) * width_ + pixel_x;
122 if (depth < depth_buffer_[idx]) {
123 const std::size_t color_idx = idx * 4;
124 const auto alpha_factor = static_cast<float>(color.a) / 255.0F;
125 const auto add_r = static_cast<uint32_t>(static_cast<float>(color.r) * alpha_factor);
126 const auto add_g = static_cast<uint32_t>(static_cast<float>(color.g) * alpha_factor);
127 const auto add_b = static_cast<uint32_t>(static_cast<float>(color.b) * alpha_factor);
128
129 color_buffer_[color_idx] = static_cast<uint8_t>(
130 std::min(255U, static_cast<uint32_t>(color_buffer_[color_idx]) + add_r));
131 color_buffer_[color_idx + 1] = static_cast<uint8_t>(
132 std::min(255U, static_cast<uint32_t>(color_buffer_[color_idx + 1]) + add_g));
133 color_buffer_[color_idx + 2] = static_cast<uint8_t>(
134 std::min(255U, static_cast<uint32_t>(color_buffer_[color_idx + 2]) + add_b));
135 return true;
136 }
137 return false;
138 }
139
146 [[nodiscard]] float getDepth(uint32_t pixel_x, uint32_t pixel_y) const noexcept {
147 if (pixel_x >= width_ || pixel_y >= height_) {
148 return 1.0F;
149 }
150 return depth_buffer_[static_cast<std::size_t>(pixel_y) * width_ + pixel_x];
151 }
152
153 [[nodiscard]] uint32_t width() const noexcept { return width_; }
154 [[nodiscard]] uint32_t height() const noexcept { return height_; }
155
156 [[nodiscard]] std::span<const uint8_t> colorBytes() const noexcept { return color_buffer_; }
157
158 [[nodiscard]] const std::vector<uint8_t> &colorBuffer() const noexcept { return color_buffer_; }
159
160 [[nodiscard]] const std::vector<float> &depthBuffer() const noexcept { return depth_buffer_; }
161
162private:
163 uint32_t width_{0};
164 uint32_t height_{0};
165 std::vector<uint8_t> color_buffer_;
166 std::vector<float> depth_buffer_;
167};
168
169} // namespace correlation::render3d
CPK color palette and visualization radii for atomic 3D rendering.
uint32_t height_
Definition SoftwareFramebuffer.hpp:164
bool blendAdditivePixel(uint32_t pixel_x, uint32_t pixel_y, float depth, RgbaColor color) noexcept
Performs depth-tested additive alpha blending into the color buffer without writing depth.
Definition SoftwareFramebuffer.hpp:116
std::vector< uint8_t > color_buffer_
Definition SoftwareFramebuffer.hpp:165
const std::vector< float > & depthBuffer() const noexcept
Definition SoftwareFramebuffer.hpp:160
const std::vector< uint8_t > & colorBuffer() const noexcept
Definition SoftwareFramebuffer.hpp:158
void clearDepth(float depth=1.0F) noexcept
Clears depth buffer to specified value.
Definition SoftwareFramebuffer.hpp:70
std::vector< float > depth_buffer_
Definition SoftwareFramebuffer.hpp:166
std::span< const uint8_t > colorBytes() const noexcept
Definition SoftwareFramebuffer.hpp:156
uint32_t height() const noexcept
Definition SoftwareFramebuffer.hpp:154
uint32_t width() const noexcept
Definition SoftwareFramebuffer.hpp:153
float getDepth(uint32_t pixel_x, uint32_t pixel_y) const noexcept
Reads depth value at pixel.
Definition SoftwareFramebuffer.hpp:146
bool testAndSetPixel(uint32_t pixel_x, uint32_t pixel_y, float depth, RgbaColor color) noexcept
Tests depth and writes pixel if closer than current depth.
Definition SoftwareFramebuffer.hpp:90
uint32_t width_
Definition SoftwareFramebuffer.hpp:163
SoftwareFramebuffer(uint32_t width, uint32_t height)
Constructs a framebuffer with specific pixel dimensions.
Definition SoftwareFramebuffer.hpp:36
void clear(RgbaColor color, float depth=1.0F) noexcept
Clears both color and depth buffers.
Definition SoftwareFramebuffer.hpp:77
SoftwareFramebuffer()=default
Constructs an empty framebuffer.
void clearColor(RgbaColor color) noexcept
Clears color buffer to specified RGBA color.
Definition SoftwareFramebuffer.hpp:55
void resize(uint32_t width, uint32_t height)
Resizes color and depth buffers.
Definition SoftwareFramebuffer.hpp:43
Definition Camera3D.hpp:14
8-bit RGBA color representation for vertex colors and framebuffers.
Definition ElementColors.hpp:23