opengl moment #1

Merged
veclavtalica merged 27 commits from opengl into main 2024-07-28 14:50:35 +00:00
3 changed files with 154 additions and 129 deletions
Showing only changes of commit ea4d12212c - Show all commits

View File

@ -5,6 +5,7 @@
#include "../textures.h" #include "../textures.h"
#include "../rendering.h" #include "../rendering.h"
#include "../context.h" #include "../context.h"
#include "../util.h"
#include "quad_element_buffer.h" #include "quad_element_buffer.h"
#include <stb_ds.h> #include <stb_ds.h>
@ -180,9 +181,10 @@ static void render_sprites(const struct primitive_2d primitives[],
} else { } else {
/* rotated case */ /* rotated case */
const t_fvec2 c = frect_center(sprite.rect); const t_fvec2 c = frect_center(sprite.rect);
const t_fvec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
const t_fvec2 d = { const t_fvec2 d = {
.x = (cosf(sprite.rotation + (float)M_PI_4) * sprite.rect.w) * (float)M_SQRT1_2, .x = t.x * sprite.rect.w * (float)M_SQRT1_2,
.y = (sinf(sprite.rotation + (float)M_PI_4) * sprite.rect.h) * (float)M_SQRT1_2, .y = t.y * sprite.rect.h * (float)M_SQRT1_2,
}; };
payload[i] = (struct sprite_primitive_payload) { payload[i] = (struct sprite_primitive_payload) {

View File

@ -154,5 +154,28 @@ void tick_ftimer(float *value);
/* returns true if value was cycled */ /* returns true if value was cycled */
bool repeat_ftimer(float *value, float at); bool repeat_ftimer(float *value, float at);
/* http://www.azillionmonkeys.com/qed/sqroot.html */
static inline float fast_sqrt(float x)
{
union {
float f;
uint32_t u;
} pun = {.f = x};
pun.u += 127 << 23;
pun.u >>= 1;
return pun.f;
}
static inline t_fvec2 fast_cossine(float a) {
const float s = sinf(a);
return (t_fvec2){
.x = fast_sqrt(1.0f - s * s) * (a >= (float)M_PI_2 && a < (float)(M_PI + M_PI_2) ? -1 : 1),
.y = s
};
}
#endif #endif