Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ImpostorFrames.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17#include "../../core/Types.hpp"
18#include "../../math/Math.hpp"
19
20#include <algorithm>
21#include <cmath>
22
24
26inline glm::vec2 encode(glm::vec3 d) {
27 d.y = std::max(d.y, 0.0f);
28 const f32 s = std::abs(d.x) + std::abs(d.y) + std::abs(d.z);
29 if (s < 1e-12f) return glm::vec2(0.5f);
30 const f32 px = d.x / s, pz = d.z / s;
31 return glm::vec2((px + pz) * 0.5f + 0.5f, (px - pz) * 0.5f + 0.5f);
32}
33
35inline glm::vec3 decode(glm::vec2 uv) {
36 const f32 qx = uv.x * 2.0f - 1.0f, qy = uv.y * 2.0f - 1.0f;
37 const f32 px = (qx + qy) * 0.5f, pz = (qx - qy) * 0.5f;
38 const f32 y = 1.0f - std::abs(px) - std::abs(pz);
39 return glm::normalize(glm::vec3(px, std::max(y, 0.0f), pz));
40}
41
43inline glm::vec3 frameDirection(u32 i, u32 j, u32 frames) {
44 const f32 last = static_cast<f32>(std::max(frames, 2u) - 1u);
45 return decode(glm::vec2(static_cast<f32>(i) / last, static_cast<f32>(j) / last));
46}
47
49inline glm::uvec2 nearestFrame(const glm::vec3& d, u32 frames) {
50 const glm::vec2 uv = encode(d);
51 const f32 last = static_cast<f32>(std::max(frames, 2u) - 1u);
52 const u32 i = static_cast<u32>(std::clamp(std::lround(uv.x * last), 0L, static_cast<long>(last)));
53 const u32 j = static_cast<u32>(std::clamp(std::lround(uv.y * last), 0L, static_cast<long>(last)));
54 return {i, j};
55}
56
63inline glm::vec3 pictureUp(const glm::vec3& d) {
64 glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f) - d * d.y;
65 if (glm::dot(up, up) < 1e-8f) return glm::vec3(0.0f, 0.0f, -1.0f);
66 return glm::normalize(up);
67}
68
69} // namespace esengine::impostor
Math utilities and GLM configuration for ESEngine.
Core type definitions and utilities for ESEngine.
glm::uvec2 nearestFrame(const glm::vec3 &d, u32 frames)
The frame taken nearest d, a direction from the object toward the viewer in the object's own space.
glm::vec3 decode(glm::vec2 uv)
The direction a point of the unit square unfolds to.
glm::vec3 frameDirection(u32 i, u32 j, u32 frames)
The direction, from the object toward the camera, that frame (i, j) is taken from.
glm::vec3 pictureUp(const glm::vec3 &d)
The up a picture is taken with from d: world up laid flat across the view.
glm::vec2 encode(glm::vec3 d)
Where on the unit square a direction of the upper hemisphere folds to. Below the horizon is taken as ...
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59