twn_draw.c: draw_camera_2d()!

This commit is contained in:
veclavtalica 2025-02-05 00:54:38 +03:00
parent d9d7072c86
commit d9df3f9b04
6 changed files with 63 additions and 3 deletions

View File

@ -18,6 +18,11 @@ static void ingame_tick(State *state) {
input_action("player_jump", "SPACE");
input_action("player_run", "LSHIFT");
draw_camera_2d((Vec2){ scn->player->rect.x + scn->player->rect.w / 2 - ctx.resolution.x / 2,
scn->player->rect.y + scn->player->rect.h / 2 - ctx.resolution.y / 2 },
0, 1
);
world_drawdef(scn->world);
player_calc(scn->player);
}

View File

@ -81,6 +81,10 @@ TWN_API void draw_billboard(const char *texture,
Color color, /* optional, default: all 255 */
bool cylindrical); /* optional, default: false */
TWN_API void draw_camera_2d(Vec2 position, /* optional, default: (0, 0) */
float rotation, /* optional, default: 0 */
float zoom); /* optional, default: 1 */
/* sets a perspective 3d camera to be used for all 3d commands */
/* fov = 0 corresponds to orthographic projection */
TWN_API void draw_camera(Vec3 position,

View File

@ -185,6 +185,17 @@
]
},
"draw_camera_2d": {
"module": "draw",
"symbol": "camera_2d",
"header": "twn_draw.h",
"params": [
{ "name": "position", "type": "Vec2", "default": { "x": 0, "y": 0 } },
{ "name": "rotation", "type": "float", "default": 0 },
{ "name": "zoom", "type": "float", "default": 1 }
]
},
"draw_camera": {
"module": "draw",
"symbol": "camera",

View File

@ -17,19 +17,30 @@
DeferredCommand *deferred_commands;
/* TODO: have a default initialized one */
/* TODO: with buffered render, don't we use camera of wrong frame right now ? */
Matrix4 camera_projection_matrix;
Matrix4 camera_look_at_matrix;
float camera_2d_rotation;
Vec2 camera_2d_position;
float camera_2d_zoom;
double depth_range_low, depth_range_high;
static void reset_camera_2d(void) {
camera_2d_position = (Vec2){0};
camera_2d_zoom = 1;
camera_2d_rotation = 0;
}
void render_clear(void) {
draw_camera((Vec3){0, 0, 0}, (Vec3){0, 0, 1}, (Vec3){0, 1, 0}, 1.57079632679f, 1);
reset_camera_2d();
text_cache_reset_arena(&ctx.text_cache);
/* since i don't intend to free the queues, */
/* it's faster and simpler to just "start over" */
/* and start overwriting the existing data */
@ -415,6 +426,22 @@ void render(void) {
}
/* TODO: check for NaNs and alike */
TWN_API void draw_camera_2d(Vec2 position,
float rotation,
float zoom)
{
if (zoom <= 0) {
log_warn("Invalid zoom value given to draw_camera_2d()");
zoom = 0.1f;
}
camera_2d_position = position;
camera_2d_rotation = rotation;
camera_2d_zoom = zoom;
}
/* TODO: check for NaNs and alike */
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 >= (float)(M_PI))
@ -441,6 +468,8 @@ void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom)
/* TODO: https://stackoverflow.com/questions/62493770/how-to-add-roll-in-camera-class */
/* TODO: check for NaNs and alike */
/* TODOL call draw_camera() instead, to reuse the code */
DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
float roll,
float pitch,

View File

@ -17,6 +17,10 @@
extern Matrix4 camera_projection_matrix;
extern Matrix4 camera_look_at_matrix;
extern float camera_2d_rotation;
extern Vec2 camera_2d_position;
extern float camera_2d_zoom;
extern double depth_range_low, depth_range_high;
#define QUAD_ELEMENT_BUFFER_LENGTH (65536 / 6)

View File

@ -161,8 +161,15 @@ static void finally_use_2d_pipeline(void) {
glOrtho(0, (double)ctx.base_render_width, (double)ctx.base_render_height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
/* TODO: 2d camera */
glLoadIdentity();
/* rotate against center of the viewport */
Vec2 const center_offset = { ctx.game.resolution.x / 2, ctx.game.resolution.y / 2 };
glTranslatef(center_offset.x, center_offset.y, 0);
glRotatef(camera_2d_rotation, 0, 0, 1);
glScalef(camera_2d_zoom, camera_2d_zoom, 1);
glTranslatef(-center_offset.x, -center_offset.y, 0);
/* apply the rest */
glTranslatef(-camera_2d_position.x, -camera_2d_position.y, 0);
texture_mode_last_used = -1;
pipeline_last_used = PIPELINE_2D;