Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
subdivision.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
32#include "psyqo/vector.hh"
33
34namespace psyqo {
35
68namespace Subdivision {
69
79
89enum Edge : unsigned {
91 EdgeAB = 1 << 0,
92 EdgeBD = 1 << 1,
93 EdgeCD = 1 << 2,
94 EdgeAC = 1 << 3,
96};
97
101[[nodiscard]] constexpr TexturedVertex half(const TexturedVertex& a, const TexturedVertex& b) {
102 return TexturedVertex{
103 .position = (a.position + b.position) / 2,
104 .uv = {.u = static_cast<uint8_t>((static_cast<unsigned>(a.uv.u) + b.uv.u) / 2),
105 .v = static_cast<uint8_t>((static_cast<unsigned>(a.uv.v) + b.uv.v) / 2)},
106 };
107}
108
137template <typename Decide, typename EmitQuad, typename EmitFill>
139 const TexturedVertex& d, unsigned depth, unsigned edges, Decide&& decide, EmitQuad&& emitQuad,
140 EmitFill&& emitFill) {
141 if ((depth == 0) || !decide(a, b, c, d, depth)) {
142 emitQuad(a, b, c, d);
143 return;
144 }
145
146 const auto ab = half(a, b);
147 const auto cd = half(c, d);
148 const auto ac = half(a, c);
149 const auto bd = half(b, d);
150 const auto center = half(ab, cd);
151
152 // Every edge of this quad that borders something which may not be divided
153 // is about to gain a vertex the neighbour does not have. Cover each one.
154 if (edges & EdgeAB) emitFill(a, ab, b);
155 if (edges & EdgeBD) emitFill(b, bd, d);
156 if (edges & EdgeCD) emitFill(c, cd, d);
157 if (edges & EdgeAC) emitFill(a, ac, c);
158
159 const unsigned next = depth - 1;
160 divideQuad(a, ab, ac, center, next, edges & (EdgeAB | EdgeAC), decide, emitQuad, emitFill);
161 divideQuad(ab, b, center, bd, next, edges & (EdgeAB | EdgeBD), decide, emitQuad, emitFill);
162 divideQuad(ac, center, c, cd, next, edges & (EdgeAC | EdgeCD), decide, emitQuad, emitFill);
163 divideQuad(center, bd, cd, d, next, edges & (EdgeBD | EdgeCD), decide, emitQuad, emitFill);
164}
165
166} // namespace Subdivision
167
168} // namespace psyqo
uint8_t cd
Definition gte-depthcue.c:206
uint8_t b
Definition gte-depthcue.c:39
Edge
Which edges of a quad lie on an outer boundary.
Definition subdivision.hh:89
@ EdgeCD
Definition subdivision.hh:93
@ NoEdges
Definition subdivision.hh:90
@ AllEdges
Definition subdivision.hh:95
@ EdgeBD
Definition subdivision.hh:92
@ EdgeAC
Definition subdivision.hh:94
@ EdgeAB
Definition subdivision.hh:91
void divideQuad(const TexturedVertex &a, const TexturedVertex &b, const TexturedVertex &c, const TexturedVertex &d, unsigned depth, unsigned edges, Decide &&decide, EmitQuad &&emitQuad, EmitFill &&emitFill)
Recursively divide a quad, in three dimensions.
Definition subdivision.hh:138
constexpr TexturedVertex half(const TexturedVertex &a, const TexturedVertex &b)
Midpoint of two vertices, in position and texture coordinates at once.
Definition subdivision.hh:101
Definition lua.hh:38
A primitive's UV coordinates attribute.
Definition common.hh:284
A model-space vertex and its texture coordinates.
Definition subdivision.hh:75
PrimPieces::UVCoords uv
Definition subdivision.hh:77
Vec3 position
Definition subdivision.hh:76
static int c
Definition syscalls.h:122