105 throw mxvk::Exception(std::format(
"3dmath_texture: failed to load PNG '{}'", path));
108 SurfacePtr rgba(SDL_ConvertSurface(loaded.get(), SDL_PIXELFORMAT_RGBA32));
110 throw mxvk::Exception(std::format(
"3dmath_texture: failed to convert PNG '{}': {}", path, SDL_GetError()));
113 const SDL_PixelFormatDetails *format = SDL_GetPixelFormatDetails(rgba->format);
114 if (format ==
nullptr) {
115 throw mxvk::Exception(std::format(
"3dmath_texture: failed to query PNG format '{}': {}", path, SDL_GetError()));
119 texture.width = rgba->w;
120 texture.height = rgba->h;
121 texture.pixels.resize(
static_cast<std::size_t
>(texture.width * texture.height));
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) {
131 SDL_GetRGBA(src[x], format,
nullptr, &r, &g, &b, &a);
132 texture.pixels[
static_cast<std::size_t
>(y * texture.width + x)] =
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) {
159 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE) {
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);
172 ensure_framebuffer();
173 if (frame_sprite ==
nullptr || frame_surface ==
nullptr || frame_format ==
nullptr) {
177 const float time =
static_cast<float>(SDL_GetTicks()) * 0.001f;
180 const std::array<mxvk::vec4D, 8> cube_vertices = {
192 rotation.
BuildXYZ(time * 31.0f, time * 43.0f, time * 17.0f);
194 std::array<mxvk::vec4D, 8> camera_vertices{};
195 std::array<mxvk::vec4D, 8> projected{};
196 for (std::size_t i = 0; i < cube_vertices.size(); ++i) {
198 point.
z += camera_distance;
199 camera_vertices[i] = point;
200 projected[i] = project_to_screen(point, frame_width, frame_height);
203 const std::array<std::array<int, 4>, 6> cube_faces = {{
215 std::vector<FaceDraw> faces;
216 faces.reserve(cube_faces.size());
217 for (
const auto &indices : cube_faces) {
218 const mxvk::vec4D &a = camera_vertices[
static_cast<std::size_t
>(indices[0])];
219 const mxvk::vec4D &b = camera_vertices[
static_cast<std::size_t
>(indices[1])];
220 const mxvk::vec4D &c = camera_vertices[
static_cast<std::size_t
>(indices[2])];
224 const mxvk::vec4D center = (a + b + c + camera_vertices[
static_cast<std::size_t
>(indices[3])]) * 0.25f;
225 const mxvk::vec4D view_vector(-center.
x, -center.
y, -center.
z, 1.0f);
226 if (normal.DotProduct(view_vector) <= 0.0f) {
230 const float diffuse = std::max(0.0f, normal.DotProduct(
mxvk::vec4D(light_dir.
x, light_dir.
y, light_dir.
z, 1.0f)));
231 const float intensity = std::clamp(0.35f + diffuse * 0.65f, 0.0f, 1.0f);
232 faces.push_back({indices, center.
z, intensity});
235 std::ranges::sort(faces, [](
const FaceDraw &left,
const FaceDraw &right) {
236 return left.depth > right.depth;
239 for (
const FaceDraw &face : faces) {
240 const auto index0 =
static_cast<std::size_t
>(face.indices[0]);
241 const auto index1 =
static_cast<std::size_t
>(face.indices[1]);
242 const auto index2 =
static_cast<std::size_t
>(face.indices[2]);
243 const auto index3 =
static_cast<std::size_t
>(face.indices[3]);
244 const TexVertex a{projected[index0], {0.0f, 1.0f}, camera_vertices[index0].z};
245 const TexVertex b{projected[index1], {1.0f, 1.0f}, camera_vertices[index1].z};
246 const TexVertex c{projected[index2], {1.0f, 0.0f}, camera_vertices[index2].z};
247 const TexVertex d{projected[index3], {0.0f, 0.0f}, camera_vertices[index3].z};
248 draw_textured_triangle(a, b, c, face.intensity);
249 draw_textured_triangle(a, c, d, face.intensity);
252 frame_sprite->updateTexture(frame_surface->pixels, frame_width, frame_height, frame_surface->pitch);
253 frame_sprite->drawSpriteRect(0, 0, output_width, output_height);
258 SurfacePtr frame_surface;
259 const SDL_PixelFormatDetails *frame_format =
nullptr;
261 int frame_width = 1280;
262 int frame_height = 720;
263 int fallback_width = 1280;
264 int fallback_height = 720;
265 float camera_distance = 4.25f;
266 static constexpr float MIN_CAMERA_DISTANCE = 2.2f;
267 static constexpr float MAX_CAMERA_DISTANCE = 10.0f;
268 static constexpr float CAMERA_ZOOM_STEP = 0.45f;
270 void ensure_framebuffer() {
271 if (frame_surface !=
nullptr) {
275 frame_surface = create_frame_surface(frame_width, frame_height);
276 frame_format = SDL_GetPixelFormatDetails(frame_surface->format);
277 if (frame_format ==
nullptr) {
278 throw mxvk::Exception(std::format(
"Failed to query 3dmath_texture frame format: {}", SDL_GetError()));
283 frame_sprite->setTextureFilter(VK_FILTER_NEAREST);
286 [[nodiscard]] std::uint32_t map_color(
mxvk::MXCOLOR color)
const {
291 SDL_FillSurfaceRect(frame_surface.get(),
nullptr, map_color(color));
294 void put_shaded_pixel_unchecked(
int x,
int y,
mxvk::MXCOLOR color, std::uint16_t intensity) {
295 auto *row =
static_cast<std::uint8_t *
>(frame_surface->pixels) + (
static_cast<std::size_t
>(y) *
static_cast<std::size_t
>(frame_surface->pitch));
296 auto *pixel = row + (
static_cast<std::size_t
>(x) * 4U);
297 pixel[0] =
static_cast<std::uint8_t
>((
static_cast<std::uint16_t
>(
mxvk::color_r(color)) * intensity) >> 8U);
298 pixel[1] =
static_cast<std::uint8_t
>((
static_cast<std::uint16_t
>(
mxvk::color_g(color)) * intensity) >> 8U);
299 pixel[2] =
static_cast<std::uint8_t
>((
static_cast<std::uint16_t
>(
mxvk::color_b(color)) * intensity) >> 8U);
303 void draw_textured_triangle(
const TexVertex &a,
const TexVertex &b,
const TexVertex &c,
float intensity) {
304 if (texture.width <= 0 || texture.height <= 0 || texture.pixels.empty()) {
308 const mxvk::vec2D p0(a.position.x, a.position.y);
309 const mxvk::vec2D p1(b.position.x, b.position.y);
310 const mxvk::vec2D p2(c.position.x, c.position.y);
315 const bool positive_area = area > 0.0f;
317 const int min_x = std::max(0,
static_cast<int>(std::floor(std::min({p0.x, p1.x, p2.x}))));
318 const int max_x = std::min(frame_width - 1,
static_cast<int>(std::ceil(std::max({p0.x, p1.x, p2.x}))));
319 const int min_y = std::max(0,
static_cast<int>(std::floor(std::min({p0.y, p1.y, p2.y}))));
320 const int max_y = std::min(frame_height - 1,
static_cast<int>(std::ceil(std::max({p0.y, p1.y, p2.y}))));
322 if (min_x > max_x || min_y > max_y) {
326 const float inv_area = 1.0f / area;
327 const float inv_z0 = 1.0f / std::max(a.depth, 0.001f);
328 const float inv_z1 = 1.0f / std::max(b.depth, 0.001f);
329 const float inv_z2 = 1.0f / std::max(c.depth, 0.001f);
330 const float u_over_z0 = a.uv.x * inv_z0;
331 const float u_over_z1 = b.uv.x * inv_z1;
332 const float u_over_z2 = c.uv.x * inv_z2;
333 const float v_over_z0 = a.uv.y * inv_z0;
334 const float v_over_z1 = b.uv.y * inv_z1;
335 const float v_over_z2 = c.uv.y * inv_z2;
336 const std::uint16_t fixed_intensity =
static_cast<std::uint16_t
>(std::clamp(intensity, 0.0f, 1.0f) * 256.0f);
338 const float w0_dx = p2.y - p1.y;
339 const float w0_dy = -(p2.x - p1.x);
340 const float w1_dx = p0.y - p2.y;
341 const float w1_dy = -(p0.x - p2.x);
342 const float w2_dx = p1.y - p0.y;
343 const float w2_dy = -(p1.x - p0.x);
345 const mxvk::vec2D row_start(
static_cast<float>(min_x) + 0.5f,
static_cast<float>(min_y) + 0.5f);
349 float row_inv_z = ((row_w0 * inv_z0) + (row_w1 * inv_z1) + (row_w2 * inv_z2)) * inv_area;
350 float row_u_over_z = ((row_w0 * u_over_z0) + (row_w1 * u_over_z1) + (row_w2 * u_over_z2)) * inv_area;
351 float row_v_over_z = ((row_w0 * v_over_z0) + (row_w1 * v_over_z1) + (row_w2 * v_over_z2)) * inv_area;
353 const float inv_z_dx = ((w0_dx * inv_z0) + (w1_dx * inv_z1) + (w2_dx * inv_z2)) * inv_area;
354 const float inv_z_dy = ((w0_dy * inv_z0) + (w1_dy * inv_z1) + (w2_dy * inv_z2)) * inv_area;
355 const float u_over_z_dx = ((w0_dx * u_over_z0) + (w1_dx * u_over_z1) + (w2_dx * u_over_z2)) * inv_area;
356 const float u_over_z_dy = ((w0_dy * u_over_z0) + (w1_dy * u_over_z1) + (w2_dy * u_over_z2)) * inv_area;
357 const float v_over_z_dx = ((w0_dx * v_over_z0) + (w1_dx * v_over_z1) + (w2_dx * v_over_z2)) * inv_area;
358 const float v_over_z_dy = ((w0_dy * v_over_z0) + (w1_dy * v_over_z1) + (w2_dy * v_over_z2)) * inv_area;
360 for (
int y = min_y; y <= max_y; ++y) {
364 float inv_z = row_inv_z;
365 float u_over_z = row_u_over_z;
366 float v_over_z = row_v_over_z;
368 for (
int x = min_x; x <= max_x; ++x) {
369 if ((positive_area && w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f) ||
370 (!positive_area && w0 <= 0.0f && w1 <= 0.0f && w2 <= 0.0f)) {
372 const float reciprocal_z = 1.0f / inv_z;
373 const float u = u_over_z * reciprocal_z;
374 const float v = v_over_z * reciprocal_z;
375 put_shaded_pixel_unchecked(x, y, texture.sample_nearest(u, v), fixed_intensity);
383 u_over_z += u_over_z_dx;
384 v_over_z += v_over_z_dx;
390 row_inv_z += inv_z_dy;
391 row_u_over_z += u_over_z_dy;
392 row_v_over_z += v_over_z_dy;
396 static mxvk::vec4D project_to_screen(
const mxvk::vec4D &point,
int width,
int height) {
397 const float scale =
static_cast<float>(std::min(width, height)) * 0.52f;
398 const float center_x =
static_cast<float>(width) * 0.5f;
399 const float center_y =
static_cast<float>(height) * 0.5f;
400 const float z = std::max(point.
z, 0.001f);
401 return {center_x + (point.
x / z) * scale, center_y - (point.
y / z) * scale, point.
z, 1.0f};
void event(SDL_Event &e) override
Handle one SDL event.
Math3DTextureWindow(const Arguments &args, const std::string &title)
void proc() override
Execute one processing/update step.
std::unique_ptr< SDL_Surface, SurfaceDeleter > SurfacePtr
std::filesystem::path texture_path(const std::string &filename, const std::string &asset_path)
Texture load_texture(const std::string &filename, const std::string &asset_path, bool generate_mipmaps)