1#ifndef NNGN_RENDER_GEN_H
2#define NNGN_RENDER_GEN_H
4#include "collision/colliders.h"
7#include "font/textbox.h"
8#include "graphics/graphics.h"
32 u32 tex,
const std::array<vec4, 6> &uv);
50 const Font &
font,
const Text &txt,
bool mono,
float left,
51 vec2 *pos_p,
float *color_p,
u64 *i_p,
u64 *n_visible_p);
61 static constexpr std::array<vec2, 2>
62 CIRCLE_UV_32 = {{{ 32/512.0f, 1}, { 64/512.0f, 1 - 32/512.0f}}},
63 CIRCLE_UV_64 = {{{128/512.0f, 1}, {256/512.0f, 1 - 128/512.0f}}};
67 constexpr std::size_t n_verts = 4, log2 = std::countr_zero(n_verts);
68 auto i =
static_cast<u32>(i_64) << log2;
69 for(
const auto e =
i + (
n << log2);
i !=
e;
i += n_verts) {
70 *
p++ =
i ; *(
p++) =
i + 1; *(
p++) =
i + 2;
71 *
p++ =
i + 2; *(
p++) =
i + 1; *(
p++) =
i + 3;
79 *
p++ = {{
bl,
z}, norm, color};
80 *
p++ = {{
tr.
x,
bl.
y,
z}, norm, color};
81 *
p++ = {{
bl.
x,
tr.
y,
z}, norm, color};
82 *
p++ = {{
tr,
z}, norm, color};
90 const auto tex_f =
static_cast<float>(tex);
92 *
p++ = {{
bl,
z}, norm, {uv0.x, uv0.y, tex_f}};
93 *
p++ = {{
tr.
x,
bl.
y,
z}, norm, {uv1.x, uv0.y, tex_f}};
94 *
p++ = {{
bl.
x,
tr.
y,
z}, norm, {uv0.x, uv1.y, tex_f}};
95 *
p++ = {{
tr,
z}, norm, {uv1.x, uv1.y, tex_f}};
103 const auto tex_f =
static_cast<float>(tex);
105 *
p++ = {{
bl.
x, y,
bl.
y}, norm, {uv0.x, uv0.y, tex_f}};
106 *
p++ = {{
tr.
x, y,
bl.
y}, norm, {uv1.x, uv0.y, tex_f}};
107 *
p++ = {{
bl.
x, y,
tr.
y}, norm, {uv0.x, uv1.y, tex_f}};
108 *
p++ = {{
tr.
x, y,
tr.
y}, norm, {uv1.x, uv1.y, tex_f}};
116 const auto tex_f =
static_cast<float>(tex);
118 *
p++ = {{
bl.
x,
bl.
y,
z}, norm, {uv0.x, uv0.y, tex_f}};
119 *
p++ = {{
tr.
x,
bl.
y,
z}, norm, {uv1.x, uv0.y, tex_f}};
120 *
p++ = {{
bl.
x,
tr.
y,
z +
tr.
y -
bl.
y}, norm, {uv0.x, uv1.y, tex_f}};
121 *
p++ = {{
tr.
x,
tr.
y,
z +
tr.
y -
bl.
y}, norm, {uv1.x, uv1.y, tex_f}};
126 const auto s =
size / 2.0f;
129 *
p++ = {
bl , { 0, 0, -1}, color};
130 *
p++ = {{
bl.
x,
tr.
y,
bl.z}, { 0, 0, -1}, color};
131 *
p++ = {{
tr.
x,
bl.
y,
bl.z}, { 0, 0, -1}, color};
132 *
p++ = {{
tr.
x,
tr.
y,
bl.z}, { 0, 0, -1}, color};
133 *
p++ = {{
bl.
x,
bl.
y,
tr.z}, { 0, 0, 1}, color};
134 *
p++ = {{
tr.
x,
bl.
y,
tr.z}, { 0, 0, 1}, color};
135 *
p++ = {{
bl.
x,
tr.
y,
tr.z}, { 0, 0, 1}, color};
136 *
p++ = {
tr , { 0, 0, 1}, color};
137 *
p++ = {
bl , {-1, 0, 0}, color};
138 *
p++ = {{
bl.
x,
bl.
y,
tr.z}, {-1, 0, 0}, color};
139 *
p++ = {{
bl.
x,
tr.
y,
bl.z}, {-1, 0, 0}, color};
140 *
p++ = {{
bl.
x,
tr.
y,
tr.z}, {-1, 0, 0}, color};
141 *
p++ = {{
tr.
x,
bl.
y,
bl.z}, { 1, 0, 0}, color};
142 *
p++ = {{
tr.
x,
tr.
y,
bl.z}, { 1, 0, 0}, color};
143 *
p++ = {{
tr.
x,
bl.
y,
tr.z}, { 1, 0, 0}, color};
144 *
p++ = {
tr , { 1, 0, 0}, color};
145 *
p++ = {
bl , { 0, -1, 0}, color};
146 *
p++ = {{
tr.
x,
bl.
y,
bl.z}, { 0, -1, 0}, color};
147 *
p++ = {{
bl.
x,
bl.
y,
tr.z}, { 0, -1, 0}, color};
148 *
p++ = {{
tr.
x,
bl.
y,
tr.z}, { 0, -1, 0}, color};
149 *
p++ = {{
bl.
x,
tr.
y,
bl.z}, { 0, 1, 0}, color};
150 *
p++ = {{
bl.
x,
tr.
y,
tr.z}, { 0, 1, 0}, color};
151 *
p++ = {{
tr.
x,
tr.
y,
bl.z}, { 0, 1, 0}, color};
152 *
p++ = {
tr , { 0, 1, 0}, color};
158 u32 tex,
const std::array<vec4, 6> &uv
160 const auto ftex =
static_cast<float>(tex);
161 const auto s =
size / 2.0f;
164 *((*p)++) = {
bl , { 0, 0, -1}, {uv_.
xy(), ftex}};
165 *((*p)++) = {{
bl.
x,
tr.
y,
bl.z}, { 0, 0, -1}, {uv_.xw(), ftex}};
166 *((*p)++) = {{
tr.
x,
bl.
y,
bl.z}, { 0, 0, -1}, {uv_.zy(), ftex}};
167 *((*p)++) = {{
tr.
x,
tr.
y,
bl.z}, { 0, 0, -1}, {uv_.zw(), ftex}};
169 *((*p)++) = {{
bl.
x,
bl.
y,
tr.z}, { 0, 0, 1}, {uv_.
xy(), ftex}};
170 *((*p)++) = {{
tr.
x,
bl.
y,
tr.z}, { 0, 0, 1}, {uv_.zy(), ftex}};
171 *((*p)++) = {{
bl.
x,
tr.
y,
tr.z}, { 0, 0, 1}, {uv_.xw(), ftex}};
172 *((*p)++) = {
tr , { 0, 0, 1}, {uv_.zw(), ftex}};
174 *((*p)++) = {
bl , {-1, 0, 0}, {uv_.
xy(), ftex}};
175 *((*p)++) = {{
bl.
x,
bl.
y,
tr.z}, {-1, 0, 0}, {uv_.xw(), ftex}};
176 *((*p)++) = {{
bl.
x,
tr.
y,
bl.z}, {-1, 0, 0}, {uv_.zy(), ftex}};
177 *((*p)++) = {{
bl.
x,
tr.
y,
tr.z}, {-1, 0, 0}, {uv_.zw(), ftex}};
179 *((*p)++) = {{
tr.
x,
bl.
y,
bl.z}, { 1, 0, 0}, {uv_.
xy(), ftex}};
180 *((*p)++) = {{
tr.
x,
tr.
y,
bl.z}, { 1, 0, 0}, {uv_.zy(), ftex}};
181 *((*p)++) = {{
tr.
x,
bl.
y,
tr.z}, { 1, 0, 0}, {uv_.xw(), ftex}};
182 *((*p)++) = {
tr , { 1, 0, 0}, {uv_.zw(), ftex}};
184 *((*p)++) = {
bl , { 0, -1, 0}, {uv_.
xy(), ftex}};
185 *((*p)++) = {{
tr.
x,
bl.
y,
bl.z}, { 0, -1, 0}, {uv_.zy(), ftex}};
186 *((*p)++) = {{
bl.
x,
bl.
y,
tr.z}, { 0, -1, 0}, {uv_.xw(), ftex}};
187 *((*p)++) = {{
tr.
x,
bl.
y,
tr.z}, { 0, -1, 0}, {uv_.zw(), ftex}};
189 *((*p)++) = {{
bl.
x,
tr.
y,
bl.z}, { 0, 1, 0}, {uv_.
xy(), ftex}};
190 *((*p)++) = {{
bl.
x,
tr.
y,
tr.z}, { 0, 1, 0}, {uv_.xw(), ftex}};
191 *((*p)++) = {{
tr.
x,
tr.
y,
bl.z}, { 0, 1, 0}, {uv_.zy(), ftex}};
192 *((*p)++) = {
tr , { 0, 1, 0}, {uv_.zw(), ftex}};
197 | (
static_cast<u32>(
g) << 8)
198 |
static_cast<u32>(b));
203 static_assert(
sizeof(ret) ==
sizeof(
c));
204 std::memcpy(&ret, &
c,
sizeof(ret));
209 x->flags.clear(Renderer::Flag::UPDATED);
210 const auto pos = x->pos.xy();
211 const auto s = x->size / 2.0f;
214 {0, 0, 1}, x->tex, x->uv[0], x->uv[1]);
218 constexpr auto nl = Math::sq2_2<float>();
219 x->flags.clear(Renderer::Flag::UPDATED);
220 const auto pos = x->pos.xy();
221 const auto s = x->size / 2.0f;
224 {0, -nl, nl}, x->tex, x->uv[0], x->uv[1]);
228 x->flags.clear(Renderer::Flag::UPDATED);
229 const auto s = x->size / 2.0f;
230 const vec2 bl = {x->pos.
x -
s.x, -
s.y - x->z_off};
232 p,
bl, {x->pos.
x +
s.x,
bl.
y + x->size.y},
233 x->pos.y + x->z_off, {0, -1, 0}, x->tex, x->uv[0], x->uv[1]);
237 x->flags.clear(Renderer::Flag::UPDATED);
238 const auto pos = x->pos.xy();
239 const auto s = x->size / 2.0f;
242 {0, 0, 1}, x->tex, x->uv[0], x->uv[1]);
246 x->flags.clear(Renderer::Flag::UPDATED);
248 p, {x->pos.x, x->pos.y, x->pos.z - x->pos.y},
249 vec3{x->size}, x->color);
253 x->flags.clear(Renderer::Flag::UPDATED);
258 x->flags.clear(Renderer::Flag::UPDATED);
260 p, {x->pos.x, x->pos.y, x->pos.z - x->pos.y},
261 x->size, x->tex, x->uv);
265 x->flags.clear(Renderer::Flag::UPDATED);
270 constexpr vec3 norm = {0, 0, 1};
271 const auto pos = x->pos.
xy();
272 auto s = x->size / 2.0f;
279 bl.
y = x->pos.y + x->z_off -
s.y;
280 tr.
y = x->pos.y + x->z_off +
s.y;
294 const Font &
font,
const Text &txt,
bool mono,
float left,
295 vec2 *pos_p,
float *color_p,
u64 *i_p,
u64 *n_visible_p
298 auto color = *color_p;
299 auto i =
static_cast<std::size_t
>(*i_p);
300 auto n_visible = *n_visible_p;
301 const auto font_size =
static_cast<float>(
font.size);
303 using C = Textbox::Command;
304 const auto c =
static_cast<unsigned char>(txt.str[
i++]);
306 case '\n':
pos = {left,
pos.y - font_size - txt.spacing};
continue;
312 const auto fc =
font.chars[
static_cast<std::size_t
>(
c)];
313 const auto size =
static_cast<vec2>(fc.size);
314 const auto cpos =
pos
315 +
vec2(font_size / 2, txt.size.y - font_size / 2)
316 +
static_cast<vec2>(fc.bearing);
318 p, cpos, cpos + size, color,
319 {0, 0, 1},
static_cast<u32>(
c),
320 {0,
size.y / font_size}, {
size.x / font_size, 0});
321 pos.x += mono ? font_size : fc.advance;
326 *i_p =
static_cast<u64>(
i);
327 *n_visible_p = n_visible;
331 constexpr vec3 norm = {0, 0, 1};
332 constexpr vec3 title_color = {0, 0, 1};
333 constexpr vec3 box_color = {1, 1, 1};
339 const auto pos = x.pos.xy();
340 const auto s = x.size / 2.0f;
344inline void Gen::aabb(Vertex **
p,
const AABBCollider &x,
vec3 color) {
351 p, x.center - x.radius, x.center + x.radius, 0, {0, 0, 1}, 1,
355inline void Gen::bb(Vertex **pp,
const BBCollider &x,
vec3 color) {
356 constexpr vec3 norm = {0, 0, 1};
357 std::array<vec2, 4> rot = {{
363 const auto pos = x.bl + (x.tr - x.bl) / 2.0f;
367 r.x * x.cos -
r.y * x.sin,
368 r.x * x.sin +
r.y * x.cos,
373 *
p++ = {{rot[0], 0}, norm, color};
374 *
p++ = {{rot[1], 0}, norm, color};
375 *
p++ = {{rot[2], 0}, norm, color};
376 *
p++ = {{rot[3], 0}, norm, color};
382 const auto pos = x.pos.xy();
for i
Definition: font.lua:5
c
Definition: gamma.lua:11
font
Definition: repl.lua:1
pos
Definition: lua_audio.cpp:17
n
Definition: dump_lights.lua:5
std::chrono::seconds s
Definition: timing.cpp:6
static constexpr nngn::vec2 tr
Definition: collision.cpp:8
static constexpr nngn::vec2 bl
Definition: collision.cpp:8
auto size(const Colliders &c)
Definition: lua_collision.cpp:29
std::uint32_t u32
Definition: def.h:14
vec2_base< float > vec2
Definition: vec2.h:29
std::uint64_t u64
Definition: def.h:15
vec3_base< float > vec3
Definition: vec3.h:51
std::uint8_t u8
Definition: def.h:12
Definition: colliders.h:34
Definition: colliders.h:44
Definition: renderers.h:44
static void quad_vertices(Vertex **p, vec2 bl, vec2 tr, float z, vec3 norm, vec3 color)
static void quad_vertices_zsprite(Vertex **p, vec2 bl, vec2 tr, float z, vec3 norm, u32 tex, vec2 uv0, vec2 uv1)
Definition: gen.h:112
static void light(Vertex **p, const Light &x, vec3 pos)
static void voxel_ortho(Vertex **p, VoxelRenderer *x)
static void quad_vertices_persp(Vertex **p, vec2 bl, vec2 tr, float y, vec3 norm, u32 tex, vec2 uv0, vec2 uv1)
Definition: gen.h:99
static void sprite_debug(Vertex **p, SpriteRenderer *x)
static void light(Vertex **p, const Light &x, vec3 pos)
Definition: gen.h:386
static void voxel_persp(Vertex **p, VoxelRenderer *x)
static void sprite_ortho(Vertex **p, SpriteRenderer *x)
static void cube_persp(Vertex **p, CubeRenderer *x)
Definition: gen.h:252
static void aabb(Vertex **p, const AABBCollider &x, vec3 color)
static void quad_vertices(Vertex **p, vec2 bl, vec2 tr, float z, vec3 norm, u32 tex, vec2 uv0, vec2 uv1)
Definition: gen.h:86
static void textbox(Vertex **p, const Textbox &x)
static void text(Vertex **p, u64 n, const Font &font, const Text &txt, bool mono, float left, vec2 *pos_p, float *color_p, u64 *i_p, u64 *n_visible_p)
Definition: gen.h:292
static void cube_ortho(Vertex **p, CubeRenderer *x)
static void voxel_debug(Vertex **p, VoxelRenderer *x)
static void sprite_persp(Vertex **p, SpriteRenderer *x)
Definition: gen.h:227
static void voxel_debug(Vertex **p, VoxelRenderer *x)
Definition: gen.h:288
static void screen_sprite(Vertex **p, SpriteRenderer *x)
Definition: gen.h:236
static void quad_vertices_zsprite(Vertex **p, vec2 bl, vec2 tr, float z, vec3 norm, u32 tex, vec2 uv0, vec2 uv1)
static void screen_sprite(Vertex **p, SpriteRenderer *x)
static void textbox(Vertex **p, const Textbox &x)
Definition: gen.h:330
static void sprite_orthoz(Vertex **p, SpriteRenderer *x)
Definition: gen.h:217
static void cube_debug(Vertex **p, CubeRenderer *x)
static void cube_vertices(Vertex **p, vec3 pos, vec3 size, vec3 color)
Definition: gen.h:125
static void sprite_orthoz(Vertex **p, SpriteRenderer *x)
static void light_range(Vertex **p, const Light &x)
static void cube_ortho(Vertex **p, CubeRenderer *x)
Definition: gen.h:245
static constexpr std::array< vec2, 2 > CIRCLE_UV_64
Definition: gen.h:63
static void voxel_ortho(Vertex **p, VoxelRenderer *x)
Definition: gen.h:257
static void aabb_circle(Vertex **p, const AABBCollider &x)
static void sprite_persp(Vertex **p, SpriteRenderer *x)
static float text_color(u8 r, u8 g, u8 b)
Definition: gen.h:195
static void bb(Vertex **p, const BBCollider &x, vec3 color)
static float text_color(u8 r, u8 g, u8 b)
static void coll_sphere(Vertex **p, const SphereCollider &x)
static void quad_vertices_persp(Vertex **p, vec2 bl, vec2 tr, float y, vec3 norm, u32 tex, vec2 uv0, vec2 uv1)
static void quad_vertices(Vertex **p, vec2 bl, vec2 tr, float z, vec3 norm, u32 tex, vec2 uv0, vec2 uv1)
static void coll_sphere(Vertex **p, const SphereCollider &x)
Definition: gen.h:380
static void text(Vertex **p, u64 n, const Font &font, const Text &txt, bool mono, float left, vec2 *pos_p, float *color_p, u64 *i_p, u64 *n_visible_p)
static void sprite_debug(Vertex **p, SpriteRenderer *x)
Definition: gen.h:269
static void voxel_persp(Vertex **p, VoxelRenderer *x)
Definition: gen.h:264
static void cube_vertices(Vertex **p, vec3 pos, vec3 size, vec3 color)
static void cube_vertices(Vertex **p, vec3 pos, vec3 size, u32 tex, const std::array< vec4, 6 > &uv)
static void aabb(Vertex **p, const AABBCollider &x, vec3 color)
Definition: gen.h:344
static void cube_persp(Vertex **p, CubeRenderer *x)
static float text_color(u32 c)
static void quad_indices(u64 i, u64 n, u32 *p)
Definition: gen.h:66
static void quad_indices(u64 i, u64 n, u32 *p)
static constexpr std::array< vec2, 2 > CIRCLE_UV_32
Definition: gen.h:62
static void sprite_ortho(Vertex **p, SpriteRenderer *x)
Definition: gen.h:208
static void aabb_circle(Vertex **p, const AABBCollider &x)
Definition: gen.h:348
static void selection(Vertex **p, const SpriteRenderer &x)
Definition: gen.h:338
static void bb(Vertex **p, const BBCollider &x, vec3 color)
Definition: gen.h:355
static void light_range(Vertex **p, const Light &x)
Definition: gen.h:390
static void selection(Vertex **p, const SpriteRenderer &x)
static void cube_debug(Vertex **p, CubeRenderer *x)
Definition: gen.h:284
Definition: colliders.h:53
Definition: renderers.h:33
Definition: graphics.h:136
Definition: renderers.h:50
constexpr vec2_base< T > xy() const
Definition: vec2.h:18
constexpr vec2_base< T > xy() const
Definition: vec3.h:26
constexpr vec3_base< T > xyz() const
Definition: vec3.h:34