Compare commits

...

5 Commits

Author SHA1 Message Date
veclavtalica
f625dde8d1 some TODOs to twn_draw.h 2025-01-27 04:16:00 +03:00
veclavtalica
166ae43981 make face culling optional 2025-01-27 04:15:46 +03:00
veclavtalica
6ef3cf1a3a change near z for orthographic proj 2025-01-27 04:09:59 +03:00
veclavtalica
d84e5f8610 normalize camera direction 2025-01-27 03:46:29 +03:00
veclavtalica
6a87119c70 add zoom option to camera to work around the orthographic camera 2025-01-27 03:25:14 +03:00
8 changed files with 43 additions and 15 deletions

View File

@ -47,6 +47,7 @@ TWN_API void draw_line(Vec2 start,
float thickness, /* optional, default: 1 */
Color color); /* optional, default: all 255 */
/* TODO: combine with draw_rectangle()? */
TWN_API void draw_box(Rect rect,
float thickness, /* optional, default: 1 */
Color color); /* optional, default: all 255 */
@ -64,6 +65,7 @@ TWN_API void draw_triangle(char const *texture,
Color c1, /* optional, default: all 255 */
Color c2); /* optional, default: all 255 */
/* TODO: double sided option */
TWN_API void draw_quad(char const *texture,
Vec3 v0, /* upper-left */
Vec3 v1, /* bottom-left */
@ -83,7 +85,8 @@ TWN_API void draw_billboard(const char *texture,
TWN_API void draw_camera(Vec3 position,
Vec3 direction, /* optional, default: (0, 0, -1) */
Vec3 up, /* optional, default: (0, 1, 0) */
float fov); /* optional, default: PI / 6 * 3 (90 degrees) */
float fov, /* optional, default: PI / 6 * 3 (90 degrees) */
float zoom); /* optional, default: 1 */
/* same as draw_camera(), but with first person controller in mind */
/* direction and up vectors are inferred from roll, pitch and yaw parameters (in radians) */
@ -97,7 +100,8 @@ draw_camera_from_principal_axes(Vec3 position,
float roll, /* optional, default: 0 */
float pitch, /* optional, default: 0 */
float yaw, /* optional, default: 0 */
float fov); /* optional, default: PI / 6 * 3 (90 degrees) */
float fov, /* optional, default: PI / 6 * 3 (90 degrees) */
float zoom); /* optional, default: 1 */
/* expects '*' masks that will be expanded to 6 names: 'up', 'down', 'east', 'west', 'north' and 'south' */
TWN_API void draw_skybox(const char *textures);

View File

@ -181,7 +181,8 @@
{ "name": "position", "type": "Vec3" },
{ "name": "direction", "type": "Vec3", "default": { "x": 0, "y": 0, "z": -1 } },
{ "name": "up", "type": "Vec3", "default": { "x": 0, "y": 1, "z": 0 } },
{ "name": "fov", "type": "float", "default": 1.57079632679 }
{ "name": "fov", "type": "float", "default": 1.57079632679 },
{ "name": "zoom", "type": "float", "default": 1 }
]
},
@ -194,7 +195,8 @@
{ "name": "roll", "type": "float", "default": 0 },
{ "name": "pitch", "type": "float", "default": 0 },
{ "name": "yaw", "type": "float", "default": 0 },
{ "name": "fov", "type": "float", "default": 1.57079632679 }
{ "name": "fov", "type": "float", "default": 1.57079632679 },
{ "name": "zoom", "type": "float", "default": 1 }
],
"return": {
"fields": [

View File

@ -413,7 +413,7 @@ void render(void) {
}
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov) {
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom) {
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
if (!orthographic && fov >= M_PIf)
log_warn("Invalid fov given (%f)", (double)fov);
@ -421,8 +421,12 @@ void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov) {
Camera const camera = {
.fov = fov,
.pos = position,
.target = direction,
.target = vec3_norm(direction),
.up = up,
.viewbox = {
(Vec2){ 1/-zoom, 1/zoom },
(Vec2){ 1/zoom, 1/-zoom }
},
};
if (!orthographic)
@ -439,7 +443,8 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
float roll,
float pitch,
float yaw,
float fov)
float fov,
float zoom)
{
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
if (!orthographic && fov >= M_PIf)
@ -454,12 +459,16 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
Camera const camera = {
.fov = fov,
.pos = position,
.target = m_vec_norm(((Vec3){
.target = vec3_norm(((Vec3){
yawc * pitchc,
pitchs,
yaws * pitchc,
})),
.up = (Vec3){0, 1, 0},
.viewbox = {
(Vec2){ 1/-zoom, 1/zoom },
(Vec2){ 1/zoom, 1/-zoom }
},
};
if (!orthographic)

View File

@ -96,7 +96,11 @@ static void finally_use_space_pipeline(void) {
if (GLAD_GL_ARB_depth_clamp)
glDisable(GL_DEPTH_CLAMP);
glEnable(GL_CULL_FACE);
if (ctx.cull_faces)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_DEPTH_TEST);

View File

@ -59,16 +59,16 @@ Matrix4 camera_orthographic(const Camera *const camera) {
Matrix4 result = {0};
const float rl = 1.0f / (float)1;
const float tb = 1.0f / (float)1;
const float fn = -1.0f / (CAMERA_FAR_Z - CAMERA_NEAR_Z);
const float rl = 1.0f / (camera->viewbox[0].y - camera->viewbox[0].x);
const float tb = 1.0f / (camera->viewbox[1].x - camera->viewbox[1].y);
const float fn = -1.0f / (CAMERA_FAR_Z - -CAMERA_FAR_Z);
result.row[0].x = 2.0f * rl;
result.row[1].y = 2.0f * tb;
result.row[2].z = 2.0f * fn;
result.row[3].x = -(float)1 * rl;
result.row[3].y = -(float)1 * tb;
result.row[3].z = (CAMERA_FAR_Z + CAMERA_NEAR_Z) * fn;
result.row[3].x = -(camera->viewbox[0].y + camera->viewbox[0].x) * rl;
result.row[3].y = -(camera->viewbox[1].x + camera->viewbox[1].y) * tb;
result.row[3].z = (CAMERA_FAR_Z + -CAMERA_FAR_Z) * fn;
result.row[3].w = 1.0f;
return result;

View File

@ -13,6 +13,7 @@ typedef struct Camera {
Vec3 target; /* normalized target vector */
Vec3 up; /* normalized up vector */
float fov; /* field of view, in radians */
Vec2 viewbox[2]; /* othrographic aabb, ((left, right), (top, bottom)) */
} Camera;
Matrix4 camera_look_at(const Camera *camera);

View File

@ -81,6 +81,7 @@ typedef struct EngineContext {
/* signals mouse focus, used to disable mouse capture */
bool window_mouse_resident;
bool audio_initialized;
bool cull_faces;
} EngineContext;
/* TODO: does it need to be marked with TWN_API? */

View File

@ -596,6 +596,13 @@ static bool initialize(void) {
}
}
SDL_free(datum_font_filtering.u.s);
toml_datum_t datum_cull_faces = toml_bool_in(engine, "cull_faces");
if (!datum_cull_faces.ok) {
ctx.cull_faces = true;
} else {
ctx.cull_faces = datum_cull_faces.u.b;
}
}
/* these are dynamic arrays and will be allocated lazily by stb_ds */