scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
rasterizer.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <scimesh/types.h>
12#include <scimesh/image.h>
13#include <vector>
14
15namespace scimesh {
16
45struct Rasterizer {
48
50 int width = 0;
51
53 int height = 0;
54
56
59
64 std::vector<float> z_buffer;
65
69 std::vector<Vec3> normal_buffer;
70
72
75
81 bool blend_mode = false;
82
84
87
91 Color specular_color = Color(0.0f, 0.0f, 0.0f, 0.0f);
92
96 float shininess = 0.0f;
97
99
102
107 std::vector<Light> lights;
108
112 float ambient = 0.3f;
113
115
118
120 float contrast = 1.0f;
121
123
126
128 bool fog_enabled = false;
129
133 float fog_start = 0.0f;
134
138 float fog_end = 1.0f;
139
145
149 float z_near = 0.1f;
150
154 float z_far = 10000.0f;
155
158 bool orthographic = false;
159
162
164
167
169 bool ssao_enabled = false;
170
172 float ssao_radius = 16.0f;
173
175 float ssao_intensity = 0.8f;
176
178
185 Rasterizer(int w, int h);
186
192 void clear(float clear_depth = 1.0f);
193
212 float fog_depth_from_ndc(float z_ndc) const;
213
219
249 const Vec3 &screen_v0, const Color &color0, const Vec3 &normal0, const Vec2 &uv0,
250 const Vec3 &screen_v1, const Color &color1, const Vec3 &normal1, const Vec2 &uv1,
251 const Vec3 &screen_v2, const Color &color2, const Vec3 &normal2, const Vec2 &uv2,
252 bool backface_culling,
253 bool smooth_shading,
254 const Vec3 &light_direction,
255 bool wireframe,
256 const Color &wireframe_color,
257 Image &output);
258
271 void rasterize_point(float screen_x, float screen_y, float depth,
272 float radius, const Color &color,
273 const Vec3 &normal, const Vec3 &light_direction,
274 Image &output);
275
297 void rasterize_line(const Vec3 &screen_v0, const Color &color0,
298 const Vec3 &screen_v1, const Color &color1,
299 float width, bool lit, const Vec3 &normal,
301
324 void apply_ssao(Image &output, float z_near, float z_far);
325
332
333private:
339 void shade_and_write(int x, int y, float depth,
340 const Color &color, const Vec3 &normal,
342 bool lit = true);
343};
344
345} // namespace scimesh
The Image — an RGBA pixel buffer with compositing and I/O operations.
glm::vec2 Vec2
2-component floating-point vector (xy).
Definition types.h:32
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
FogSpace
Coordinate space (and hence the unit) of the fog distances RenderOptions::fog_start and RenderOptions...
Definition types.h:297
@ WORLD
World units: distance from the camera, measured along the camera's viewing direction.
constexpr Color TRANSPARENT_BLACK
A fully transparent black color (0, 0, 0, 0).
Definition types.h:150
Vec3 normal2
Definition renderer.cpp:24
Vec3 screen_v2
Definition renderer.cpp:22
Vec3 normal1
Definition renderer.cpp:24
Vec3 normal0
Definition renderer.cpp:24
Vec3 screen_v1
Definition renderer.cpp:22
Vec2 uv1
Definition renderer.cpp:25
Color color0
Definition renderer.cpp:23
bool lit
Definition renderer.cpp:37
Vec2 uv0
Definition renderer.cpp:25
Color color1
Definition renderer.cpp:23
Vec3 screen_v0
Definition renderer.cpp:22
Vec2 uv2
Definition renderer.cpp:25
Color color2
Definition renderer.cpp:23
An RGBA color with floating-point components.
Definition types.h:88
A 2D RGBA image buffer.
Definition image.h:92
Low-level triangle rasterizer with depth buffering and lighting.
Definition rasterizer.h:45
void rasterize_line(const Vec3 &screen_v0, const Color &color0, const Vec3 &screen_v1, const Color &color1, float width, bool lit, const Vec3 &normal, const Vec3 &light_direction, Image &output)
Rasterize a line segment with a screen-space width.
float ssao_radius
SSAO sample radius in pixels (default: 16).
Definition rasterizer.h:172
void clear(float clear_depth=1.0f)
Clear the depth and normal buffers.
void rasterize_point(float screen_x, float screen_y, float depth, float radius, const Color &color, const Vec3 &normal, const Vec3 &light_direction, Image &output)
Rasterize a single point (filled circle) into the output image.
float fog_end
Distance where fog is fully opaque (default: 1).
Definition rasterizer.h:138
Color fog_color
The fog color (what distant objects blend into).
Definition rasterizer.h:161
bool fog_enabled
Enable depth fog (default: false).
Definition rasterizer.h:128
FogSpace fog_space
The space (and unit) of fog_start / fog_end (default: FogSpace::WORLD).
Definition rasterizer.h:144
float z_far
Far plane distance of the current projection (default: 10000.0).
Definition rasterizer.h:154
float fog_depth_from_ndc(float z_ndc) const
Convert a depth-buffer value to a distance in world units.
float fog_start
Distance where fog begins (default: 0).
Definition rasterizer.h:133
bool orthographic
Whether the current projection is orthographic (default: false).
Definition rasterizer.h:158
void apply_ssao(Image &output, float z_near, float z_far)
Apply screen-space ambient occlusion to the output image.
int height
Output image height in pixels.
Definition rasterizer.h:53
Image * active_texture
Optional texture image for textured meshes.
Definition rasterizer.h:331
float ssao_intensity
SSAO darkening intensity (0.0–1.0, default: 0.8).
Definition rasterizer.h:175
float ambient
Ambient light level (0.0–1.0, default: 0.3).
Definition rasterizer.h:112
int width
Output image width in pixels.
Definition rasterizer.h:50
void set_blend_mode(bool enabled)
Enable or disable alpha blending.
Definition rasterizer.h:218
Color specular_color
Specular highlight color.
Definition rasterizer.h:91
std::vector< Light > lights
Light sources for Blinn-Phong shading.
Definition rasterizer.h:107
float shininess
Shininess exponent (Phong model).
Definition rasterizer.h:96
void rasterize_triangle(const Vec3 &screen_v0, const Color &color0, const Vec3 &normal0, const Vec2 &uv0, const Vec3 &screen_v1, const Color &color1, const Vec3 &normal1, const Vec2 &uv1, const Vec3 &screen_v2, const Color &color2, const Vec3 &normal2, const Vec2 &uv2, bool backface_culling, bool smooth_shading, const Vec3 &light_direction, bool wireframe, const Color &wireframe_color, Image &output)
Rasterize a single triangle into the output image.
bool ssao_enabled
Enable SSAO (default: false).
Definition rasterizer.h:169
float z_near
Near plane distance of the current projection (default: 0.1).
Definition rasterizer.h:149
std::vector< float > z_buffer
Z-buffer (depth buffer), one float per pixel.
Definition rasterizer.h:64
bool blend_mode
Enable alpha blending for transparent triangles (default: false).
Definition rasterizer.h:81
float contrast
Contrast adjustment (1.0 = no change).
Definition rasterizer.h:120
std::vector< Vec3 > normal_buffer
Normal buffer, one Vec3 per pixel.
Definition rasterizer.h:69
int y
Top edge of the bitmap, in image pixels.
Definition text.cpp:224
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.