MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "mxvk/argz.hpp"
2#include "mxvk/mxvk.hpp"
4#if defined(MXVK_USE_EIGEN_MATH)
6#else
7#include "mxvk/mxvk_math.h"
8#endif
9#include "mxvk/mxvk_png.hpp"
10
11#include <SDL3/SDL.h>
12
13#include <algorithm>
14#include <array>
15#include <cstdint>
16#include <cstdlib>
17#include <filesystem>
18#include <format>
19#include <iostream>
20#include <memory>
21#include <string>
22#include <vector>
23
24namespace {
25 class SurfaceDeleter {
26 public:
27 void operator()(SDL_Surface *surface) const {
28 SDL_DestroySurface(surface);
29 }
30 };
31
32 using SurfacePtr = std::unique_ptr<SDL_Surface, SurfaceDeleter>;
33
34 SurfacePtr create_frame_surface(int width, int height) {
35 SurfacePtr surface(SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGBA32));
36 if (!surface) {
37 throw mxvk::Exception(std::format("Failed to create 3dmath_texture_array frame surface: {}", SDL_GetError()));
38 }
39 return surface;
40 }
41
42 struct Texture {
43 int width = 0;
44 int height = 0;
45 std::vector<mxvk::MXCOLOR> pixels;
46
47 [[nodiscard]] mxvk::MXCOLOR sample(float u, float v) const {
48 if (width <= 0 || height <= 0 || pixels.empty()) {
49 return mxvk::MXVK_RGB(255, 255, 255);
50 }
51
52 u = std::clamp(u, 0.0f, 1.0f);
53 v = std::clamp(v, 0.0f, 1.0f);
54 const int x = std::clamp(static_cast<int>(u * static_cast<float>(width - 1) + 0.5f), 0, width - 1);
55 const int y = std::clamp(static_cast<int>(v * static_cast<float>(height - 1) + 0.5f), 0, height - 1);
56 return pixels[static_cast<std::size_t>(y * width + x)];
57 }
58
59 [[nodiscard]] mxvk::MXCOLOR sample_nearest(float u, float v) const {
60 u = std::clamp(u, 0.0f, 1.0f);
61 v = std::clamp(v, 0.0f, 1.0f);
62 const int x = static_cast<int>(u * static_cast<float>(width - 1) + 0.5f);
63 const int y = static_cast<int>(v * static_cast<float>(height - 1) + 0.5f);
64 return pixels[static_cast<std::size_t>(y * width + x)];
65 }
66 };
67
68 struct TexVertex {
69 mxvk::vec4D position;
70 mxvk::vec2D uv;
71 float depth = 1.0f;
72 };
73
74 struct FaceDraw {
75 std::array<TexVertex, 4> vertices{};
76 float depth = 0.0f;
77 float intensity = 1.0f;
78 };
79
80 [[nodiscard]] std::string resolve_texture_path(const Arguments &args) {
81 std::string texture_path = !args.filename.empty() ? args.filename : args.texture;
82 if (texture_path.empty()) {
83 throw mxvk::Exception("3dmath_texture_array: pass a PNG with --filename <file.png> or --texture <file.png>");
84 }
85
86 namespace fs = std::filesystem;
87 fs::path requested(texture_path);
88 if (requested.is_absolute() || fs::exists(requested)) {
89 return requested.string();
90 }
91
92 if (!args.path.empty()) {
93 const fs::path from_asset_path = fs::path(args.path) / requested;
94 if (fs::exists(from_asset_path)) {
95 return from_asset_path.string();
96 }
97 }
98
99 return requested.string();
100 }
101
102 [[nodiscard]] Texture load_texture(const std::string &path) {
103 SurfacePtr loaded(mxvk::LoadPNG(path.c_str()));
104 if (!loaded) {
105 throw mxvk::Exception(std::format("3dmath_texture_array: failed to load PNG '{}'", path));
106 }
107
108 SurfacePtr rgba(SDL_ConvertSurface(loaded.get(), SDL_PIXELFORMAT_RGBA32));
109 if (!rgba) {
110 throw mxvk::Exception(std::format("3dmath_texture_array: failed to convert PNG '{}': {}", path, SDL_GetError()));
111 }
112
113 const SDL_PixelFormatDetails *format = SDL_GetPixelFormatDetails(rgba->format);
114 if (format == nullptr) {
115 throw mxvk::Exception(std::format("3dmath_texture_array: failed to query PNG format '{}': {}", path, SDL_GetError()));
116 }
117
118 Texture texture;
119 texture.width = rgba->w;
120 texture.height = rgba->h;
121 texture.pixels.resize(static_cast<std::size_t>(texture.width * texture.height));
122
123 for (int y = 0; y < texture.height; ++y) {
124 const auto *row = static_cast<const std::uint8_t *>(rgba->pixels) + (static_cast<std::size_t>(y) * static_cast<std::size_t>(rgba->pitch));
125 const auto *src = reinterpret_cast<const std::uint32_t *>(row);
126 for (int x = 0; x < texture.width; ++x) {
127 std::uint8_t r = 0;
128 std::uint8_t g = 0;
129 std::uint8_t b = 0;
130 std::uint8_t a = 0;
131 SDL_GetRGBA(src[x], format, nullptr, &r, &g, &b, &a);
132 texture.pixels[static_cast<std::size_t>(y * texture.width + x)] =
133 (static_cast<mxvk::MXCOLOR>(a) << 24U) |
134 (static_cast<mxvk::MXCOLOR>(r) << 16U) |
135 (static_cast<mxvk::MXCOLOR>(g) << 8U) |
136 static_cast<mxvk::MXCOLOR>(b);
137 }
138 }
139
140 return texture;
141 }
142} // namespace
143
144namespace example {
146 public:
147 Math3DTextureArrayWindow(const Arguments &args, const std::string &title)
148 : mxvk::VK_Window(title, args.width, args.height, args.fullscreen, MXVK_VALIDATION, args.enable_vsync),
149 texture(load_texture(resolve_texture_path(args))),
150 frame_width(args.framebuffer.width),
151 frame_height(args.framebuffer.height),
152 fallback_width(args.width),
153 fallback_height(args.height) {
154 setClearColor(0.012f, 0.015f, 0.022f, 1.0f);
156 }
157
158 void event(SDL_Event &e) override {
159 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE) {
160 exit();
161 }
162 if (e.type == SDL_EVENT_MOUSE_WHEEL) {
163 const float delta = (e.wheel.y != 0.0f) ? e.wheel.y : static_cast<float>(e.wheel.integer_y);
164 camera_distance = std::clamp(camera_distance - delta * CAMERA_ZOOM_STEP, MIN_CAMERA_DISTANCE, MAX_CAMERA_DISTANCE);
165 }
166 }
167
168 void proc() override {
169 const int output_width = swapchain_extent.width > 0U ? static_cast<int>(swapchain_extent.width) : fallback_width;
170 const int output_height = swapchain_extent.height > 0U ? static_cast<int>(swapchain_extent.height) : fallback_height;
171
172 ensure_framebuffer();
173 if (frame_sprite == nullptr || frame_surface == nullptr || frame_format == nullptr) {
174 return;
175 }
176
177 const float time = static_cast<float>(SDL_GetTicks()) * 0.001f;
178 clear_frame(mxvk::MXVK_RGB(3, 4, 8));
179
180 const std::array<mxvk::vec4D, 8> cube_vertices = {
181 mxvk::vec4D{-CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, 1.0f},
182 mxvk::vec4D{CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, 1.0f},
183 mxvk::vec4D{CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, 1.0f},
184 mxvk::vec4D{-CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, 1.0f},
185 mxvk::vec4D{-CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, 1.0f},
186 mxvk::vec4D{CUBE_HALF_EXTENT, -CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, 1.0f},
187 mxvk::vec4D{CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, 1.0f},
188 mxvk::vec4D{-CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, CUBE_HALF_EXTENT, 1.0f},
189 };
190
191 mxvk::Mat4D rotation;
192 rotation.BuildXYZ(time * 31.0f, time * 43.0f, time * 17.0f);
193
194 const std::array<std::array<int, 4>, 6> cube_faces = {{
195 {0, 3, 2, 1},
196 {4, 5, 6, 7},
197 {0, 4, 7, 3},
198 {1, 2, 6, 5},
199 {3, 7, 6, 2},
200 {0, 1, 5, 4},
201 }};
202
203 mxvk::vec3D light_dir(-0.35f, -0.55f, -1.0f);
204 light_dir.Normalize();
205
206 std::vector<FaceDraw> faces;
207 faces.reserve(GRID_CUBE_COUNT * cube_faces.size());
208
209 for (int grid_z = -GRID_RADIUS; grid_z <= GRID_RADIUS; ++grid_z) {
210 for (int grid_y = -GRID_RADIUS; grid_y <= GRID_RADIUS; ++grid_y) {
211 for (int grid_x = -GRID_RADIUS; grid_x <= GRID_RADIUS; ++grid_x) {
212 const mxvk::vec4D cube_center(
213 static_cast<float>(grid_x) * GRID_SPACING,
214 static_cast<float>(grid_y) * GRID_SPACING,
215 static_cast<float>(grid_z) * GRID_SPACING,
216 0.0f);
217
218 std::array<mxvk::vec4D, 8> camera_vertices{};
219 std::array<mxvk::vec4D, 8> projected{};
220 for (std::size_t i = 0; i < cube_vertices.size(); ++i) {
221 mxvk::vec4D point = rotation.MulVec(cube_vertices[i] + cube_center);
222 point.z += camera_distance;
223 camera_vertices[i] = point;
224 projected[i] = project_to_screen(point, frame_width, frame_height);
225 }
226
227 for (const auto &indices : cube_faces) {
228 const auto index0 = static_cast<std::size_t>(indices[0]);
229 const auto index1 = static_cast<std::size_t>(indices[1]);
230 const auto index2 = static_cast<std::size_t>(indices[2]);
231 const auto index3 = static_cast<std::size_t>(indices[3]);
232 const mxvk::vec4D &a = camera_vertices[index0];
233 const mxvk::vec4D &b = camera_vertices[index1];
234 const mxvk::vec4D &c = camera_vertices[index2];
235 mxvk::vec4D normal = mxvk::vec4D().Build(a, b).CrossProduct(mxvk::vec4D().Build(a, c));
236 normal.Normalize();
237
238 const mxvk::vec4D center = (a + b + c + camera_vertices[index3]) * 0.25f;
239 const mxvk::vec4D view_vector(-center.x, -center.y, -center.z, 1.0f);
240 if (normal.DotProduct(view_vector) <= 0.0f) {
241 continue;
242 }
243
244 const float diffuse = std::max(0.0f, normal.DotProduct(mxvk::vec4D(light_dir.x, light_dir.y, light_dir.z, 1.0f)));
245 FaceDraw face;
246 face.vertices = {{
247 {projected[index0], {0.0f, 1.0f}, camera_vertices[index0].z},
248 {projected[index1], {1.0f, 1.0f}, camera_vertices[index1].z},
249 {projected[index2], {1.0f, 0.0f}, camera_vertices[index2].z},
250 {projected[index3], {0.0f, 0.0f}, camera_vertices[index3].z},
251 }};
252 face.depth = center.z;
253 face.intensity = std::clamp(0.35f + diffuse * 0.65f, 0.0f, 1.0f);
254 faces.push_back(face);
255 }
256 }
257 }
258 }
259
260 std::ranges::sort(faces, [](const FaceDraw &left, const FaceDraw &right) {
261 return left.depth > right.depth;
262 });
263
264 for (const FaceDraw &face : faces) {
265 draw_textured_triangle(face.vertices[0], face.vertices[1], face.vertices[2], face.intensity);
266 draw_textured_triangle(face.vertices[0], face.vertices[2], face.vertices[3], face.intensity);
267 }
268
269 frame_sprite->updateTexture(frame_surface->pixels, frame_width, frame_height, frame_surface->pitch);
270 frame_sprite->drawSpriteRect(0, 0, output_width, output_height);
271 }
272
273 private:
274 Texture texture;
275 SurfacePtr frame_surface;
276 const SDL_PixelFormatDetails *frame_format = nullptr;
277 mxvk::VK_Sprite *frame_sprite = nullptr;
278 int frame_width = 1280;
279 int frame_height = 720;
280 int fallback_width = 1280;
281 int fallback_height = 720;
282 float camera_distance = 8.5f;
283 static constexpr int GRID_RADIUS = 1;
284 static constexpr std::size_t GRID_WIDTH = static_cast<std::size_t>((GRID_RADIUS * 2) + 1);
285 static constexpr std::size_t GRID_CUBE_COUNT = GRID_WIDTH * GRID_WIDTH * GRID_WIDTH;
286 static constexpr float CUBE_HALF_EXTENT = 0.52f;
287 static constexpr float GRID_SPACING = 1.45f;
288 static constexpr float MIN_CAMERA_DISTANCE = 5.0f;
289 static constexpr float MAX_CAMERA_DISTANCE = 18.0f;
290 static constexpr float CAMERA_ZOOM_STEP = 0.65f;
291
292 void ensure_framebuffer() {
293 if (frame_surface != nullptr) {
294 return;
295 }
296
297 frame_surface = create_frame_surface(frame_width, frame_height);
298 frame_format = SDL_GetPixelFormatDetails(frame_surface->format);
299 if (frame_format == nullptr) {
300 throw mxvk::Exception(std::format("Failed to query 3dmath_texture_array frame format: {}", SDL_GetError()));
301 }
302
303 clear_frame(mxvk::MXVK_RGB(3, 4, 8));
304 frame_sprite = createSprite(frame_surface.get());
305 frame_sprite->setTextureFilter(VK_FILTER_NEAREST);
306 }
307
308 [[nodiscard]] std::uint32_t map_color(mxvk::MXCOLOR color) const {
309 return SDL_MapRGBA(frame_format, nullptr, mxvk::color_r(color), mxvk::color_g(color), mxvk::color_b(color), mxvk::color_a(color));
310 }
311
312 void clear_frame(mxvk::MXCOLOR color) {
313 SDL_FillSurfaceRect(frame_surface.get(), nullptr, map_color(color));
314 }
315
316 void put_shaded_pixel_unchecked(int x, int y, mxvk::MXCOLOR color, std::uint16_t intensity) {
317 auto *row = static_cast<std::uint8_t *>(frame_surface->pixels) + (static_cast<std::size_t>(y) * static_cast<std::size_t>(frame_surface->pitch));
318 auto *pixel = row + (static_cast<std::size_t>(x) * 4U);
319 pixel[0] = static_cast<std::uint8_t>((static_cast<std::uint16_t>(mxvk::color_r(color)) * intensity) >> 8U);
320 pixel[1] = static_cast<std::uint8_t>((static_cast<std::uint16_t>(mxvk::color_g(color)) * intensity) >> 8U);
321 pixel[2] = static_cast<std::uint8_t>((static_cast<std::uint16_t>(mxvk::color_b(color)) * intensity) >> 8U);
322 pixel[3] = mxvk::color_a(color);
323 }
324
325 void draw_textured_triangle(const TexVertex &a, const TexVertex &b, const TexVertex &c, float intensity) {
326 if (texture.width <= 0 || texture.height <= 0 || texture.pixels.empty()) {
327 return;
328 }
329
330 const mxvk::vec2D p0(a.position.x, a.position.y);
331 const mxvk::vec2D p1(b.position.x, b.position.y);
332 const mxvk::vec2D p2(c.position.x, c.position.y);
333 const float area = mxvk::edge_function(p0, p1, p2);
334 if (std::fabs(area) <= mxvk::EPSILON) {
335 return;
336 }
337 const bool positive_area = area > 0.0f;
338
339 const int min_x = std::max(0, static_cast<int>(std::floor(std::min({p0.x, p1.x, p2.x}))));
340 const int max_x = std::min(frame_width - 1, static_cast<int>(std::ceil(std::max({p0.x, p1.x, p2.x}))));
341 const int min_y = std::max(0, static_cast<int>(std::floor(std::min({p0.y, p1.y, p2.y}))));
342 const int max_y = std::min(frame_height - 1, static_cast<int>(std::ceil(std::max({p0.y, p1.y, p2.y}))));
343
344 if (min_x > max_x || min_y > max_y) {
345 return;
346 }
347
348 const float inv_area = 1.0f / area;
349 const float inv_z0 = 1.0f / std::max(a.depth, 0.001f);
350 const float inv_z1 = 1.0f / std::max(b.depth, 0.001f);
351 const float inv_z2 = 1.0f / std::max(c.depth, 0.001f);
352 const float u_over_z0 = a.uv.x * inv_z0;
353 const float u_over_z1 = b.uv.x * inv_z1;
354 const float u_over_z2 = c.uv.x * inv_z2;
355 const float v_over_z0 = a.uv.y * inv_z0;
356 const float v_over_z1 = b.uv.y * inv_z1;
357 const float v_over_z2 = c.uv.y * inv_z2;
358 const std::uint16_t fixed_intensity = static_cast<std::uint16_t>(std::clamp(intensity, 0.0f, 1.0f) * 256.0f);
359
360 const float w0_dx = p2.y - p1.y;
361 const float w0_dy = -(p2.x - p1.x);
362 const float w1_dx = p0.y - p2.y;
363 const float w1_dy = -(p0.x - p2.x);
364 const float w2_dx = p1.y - p0.y;
365 const float w2_dy = -(p1.x - p0.x);
366
367 const mxvk::vec2D row_start(static_cast<float>(min_x) + 0.5f, static_cast<float>(min_y) + 0.5f);
368 float row_w0 = mxvk::edge_function(p1, p2, row_start);
369 float row_w1 = mxvk::edge_function(p2, p0, row_start);
370 float row_w2 = mxvk::edge_function(p0, p1, row_start);
371 float row_inv_z = ((row_w0 * inv_z0) + (row_w1 * inv_z1) + (row_w2 * inv_z2)) * inv_area;
372 float row_u_over_z = ((row_w0 * u_over_z0) + (row_w1 * u_over_z1) + (row_w2 * u_over_z2)) * inv_area;
373 float row_v_over_z = ((row_w0 * v_over_z0) + (row_w1 * v_over_z1) + (row_w2 * v_over_z2)) * inv_area;
374
375 const float inv_z_dx = ((w0_dx * inv_z0) + (w1_dx * inv_z1) + (w2_dx * inv_z2)) * inv_area;
376 const float inv_z_dy = ((w0_dy * inv_z0) + (w1_dy * inv_z1) + (w2_dy * inv_z2)) * inv_area;
377 const float u_over_z_dx = ((w0_dx * u_over_z0) + (w1_dx * u_over_z1) + (w2_dx * u_over_z2)) * inv_area;
378 const float u_over_z_dy = ((w0_dy * u_over_z0) + (w1_dy * u_over_z1) + (w2_dy * u_over_z2)) * inv_area;
379 const float v_over_z_dx = ((w0_dx * v_over_z0) + (w1_dx * v_over_z1) + (w2_dx * v_over_z2)) * inv_area;
380 const float v_over_z_dy = ((w0_dy * v_over_z0) + (w1_dy * v_over_z1) + (w2_dy * v_over_z2)) * inv_area;
381
382 for (int y = min_y; y <= max_y; ++y) {
383 float w0 = row_w0;
384 float w1 = row_w1;
385 float w2 = row_w2;
386 float inv_z = row_inv_z;
387 float u_over_z = row_u_over_z;
388 float v_over_z = row_v_over_z;
389
390 for (int x = min_x; x <= max_x; ++x) {
391 if ((positive_area && w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f) ||
392 (!positive_area && w0 <= 0.0f && w1 <= 0.0f && w2 <= 0.0f)) {
393 if (std::fabs(inv_z) > mxvk::EPSILON) {
394 const float reciprocal_z = 1.0f / inv_z;
395 const float u = u_over_z * reciprocal_z;
396 const float v = v_over_z * reciprocal_z;
397 put_shaded_pixel_unchecked(x, y, texture.sample_nearest(u, v), fixed_intensity);
398 }
399 }
400
401 w0 += w0_dx;
402 w1 += w1_dx;
403 w2 += w2_dx;
404 inv_z += inv_z_dx;
405 u_over_z += u_over_z_dx;
406 v_over_z += v_over_z_dx;
407 }
408
409 row_w0 += w0_dy;
410 row_w1 += w1_dy;
411 row_w2 += w2_dy;
412 row_inv_z += inv_z_dy;
413 row_u_over_z += u_over_z_dy;
414 row_v_over_z += v_over_z_dy;
415 }
416 }
417
418 static mxvk::vec4D project_to_screen(const mxvk::vec4D &point, int width, int height) {
419 const float scale = static_cast<float>(std::min(width, height)) * 0.52f;
420 const float center_x = static_cast<float>(width) * 0.5f;
421 const float center_y = static_cast<float>(height) * 0.5f;
422 const float z = std::max(point.z, 0.001f);
423 return {center_x + (point.x / z) * scale, center_y - (point.y / z) * scale, point.z, 1.0f};
424 }
425 };
426} // namespace example
427
428int main(int argc, char **argv) {
429 try {
430 Arguments args = proc_args(argc, argv);
431 example::Math3DTextureArrayWindow window(args, "MXVK 3D Math Texture Array");
432 window.loop();
433 } catch (mxvk::Exception &e) {
434 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
435 return EXIT_FAILURE;
436 } catch (ArgException<std::string> &e) {
437 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
438 return EXIT_FAILURE;
439 }
440 return EXIT_SUCCESS;
441}
constexpr int GRID_WIDTH
Definition acid.drop.cpp:25
Lightweight, header-only, template command-line argument parser.
Arguments proc_args(int &argc, char **argv)
Parse standard libmx2 command-line options from main()'s argv.
Definition argz.hpp:872
Exception thrown by Argz::proc() on unrecognised or malformed options.
Definition argz.hpp:178
void operator()(SDL_Surface *surface) const
Definition main.cpp:27
void event(SDL_Event &e) override
Handle one SDL event.
Definition main.cpp:158
void proc() override
Execute one processing/update step.
Definition main.cpp:168
Math3DTextureArrayWindow(const Arguments &args, const std::string &title)
Definition main.cpp:147
std::string text() const
Four-by-four homogeneous transform matrix.
Definition mxvk_math.h:812
void BuildXYZ(float theta_x, float theta_y, float theta_z)
Build an XYZ Euler rotation matrix from angles in degrees.
Definition mxvk_math.h:973
vec4D MulVec(const vec4D &in) const
Transform a homogeneous 4D vector by this matrix.
Definition mxvk_math.h:881
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:37
void loop()
Run the main event/render loop.
Definition mxvk.cpp:600
VK_Sprite * createSprite(const std::string &pngPath, const std::string &vertexShaderPath="", const std::string &fragmentShaderPath="")
Create a sprite from a PNG file and register it with this window.
Definition mxvk.cpp:3477
VkExtent2D swapchain_extent
Definition mxvk.hpp:495
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:593
void exit()
Request loop termination.
Definition mxvk.cpp:1126
VK_Window()=default
Construct an empty window object.
Two-dimensional float vector with common arithmetic helpers.
Definition mxvk_math.h:177
Three-dimensional float vector with arithmetic, dot, and cross-product helpers.
Definition mxvk_math.h:291
float z
Z coordinate.
Definition mxvk_math.h:300
float x
X coordinate.
Definition mxvk_math.h:294
void Normalize()
Normalize this vector in place, or reset it to zero if it is too short.
Definition mxvk_math.h:376
float y
Y coordinate.
Definition mxvk_math.h:297
Four-dimensional float vector used for homogeneous 3D coordinates.
Definition mxvk_math.h:416
float y
Y coordinate.
Definition mxvk_math.h:422
float x
X coordinate.
Definition mxvk_math.h:419
void Normalize()
Normalize the 3D components in place and reset W to 1.
Definition mxvk_math.h:518
constexpr float DotProduct(const vec4D &v) const
Compute the 3D dot product, ignoring the W component.
Definition mxvk_math.h:503
float z
Z coordinate.
Definition mxvk_math.h:425
void Build(const vec4D &to)
Replace this vector with the direction from this point to to.
Definition mxvk_math.h:544
int main(void)
Definition main.cpp:7
#define MXVK_VALIDATION
Definition mxvk.hpp:27
Math, geometry, rasterization, and simple software 3D pipeline helpers for MXVK examples.
PNG image loading and saving utilities via SDL3.
std::unique_ptr< SDL_Surface, SurfaceDeleter > SurfacePtr
Definition main.cpp:29
std::string resolve_texture_path(const Arguments &args)
Definition main.cpp:80
std::filesystem::path texture_path(const std::string &filename, const std::string &asset_path)
Definition main.cpp:147
Texture load_texture(const std::string &filename, const std::string &asset_path, bool generate_mipmaps)
Definition main.cpp:160
SurfacePtr create_frame_surface(int width, int height)
Definition main.cpp:31
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
constexpr std::uint8_t color_r(MXCOLOR color)
Extract the red component from a packed ARGB color.
Definition mxvk_math.h:54
std::uint32_t MXCOLOR
Packed 32-bit color in ARGB byte order.
Definition mxvk_math.h:40
void BuildTables()
Rebuild the sine and cosine lookup tables.
Definition mxvk_math.h:113
SDL_Surface * LoadPNG(const char *file)
Load a PNG file into an SDL_Surface.
Definition mxvk_png.cpp:103
constexpr std::uint8_t color_g(MXCOLOR color)
Extract the green component from a packed ARGB color.
Definition mxvk_math.h:59
constexpr MXCOLOR MXVK_RGB(int r, int g, int b)
Build an opaque ARGB color from red, green, and blue components.
Definition mxvk_math.h:49
constexpr std::uint8_t color_a(MXCOLOR color)
Extract the alpha component from a packed ARGB color.
Definition mxvk_math.h:69
float edge_function(const vec2D &a, const vec2D &b, const vec2D &p)
Compute the signed edge function value for point p relative to edge a-b.
Definition mxvk_math.h:2127
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37
constexpr std::uint8_t color_b(MXCOLOR color)
Extract the blue component from a packed ARGB color.
Definition mxvk_math.h:64
Plain data structure returned by proc_args() with all common libmx2 CLI options.
Definition argz.hpp:730
std::string texture
Optional texture file path (--texture).
Definition argz.hpp:764
std::string filename
Optional input filename (--filename).
Definition argz.hpp:738
std::string path
Asset root; proc_args() defaults it to the executable directory.
Definition argz.hpp:735
std::array< TexVertex, 4 > vertices
Definition main.cpp:75
mxvk::MXCOLOR sample(float u, float v) const
Definition main.cpp:47
std::vector< mxvk::MXCOLOR > pixels
Definition main.cpp:199
mxvk::MXCOLOR sample_nearest(float u, float v) const
Definition main.cpp:59