draw: draw_distance for 3d spaces, proper positioning of skybox according to it, scenery demo on circle rasters

This commit is contained in:
veclavtalica
2025-02-26 15:53:59 +03:00
parent 23fbd45564
commit 8c0f43ec34
9 changed files with 123 additions and 80 deletions

View File

@ -20,6 +20,7 @@ DeferredCommand *deferred_commands;
/* 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_far_z;
float camera_2d_rotation;
Vec2 camera_2d_position;
@ -36,7 +37,7 @@ static void reset_camera_2d(void) {
void render_clear(void) {
draw_camera((Vec3){0, 0, 0}, (Vec3){0, 0, 1}, (Vec3){0, 1, 0}, 1.57079632679f, 1);
draw_camera((Vec3){0, 0, 0}, (Vec3){0, 0, 1}, (Vec3){0, 1, 0}, 1.57079632679f, 1, 100);
reset_camera_2d();
text_cache_reset_arena(&ctx.text_cache);
@ -423,7 +424,7 @@ TWN_API void draw_camera_2d(Vec2 position,
}
/* TODO: check for NaNs and alike */
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom) {
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom, float draw_distance) {
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov);
@ -437,6 +438,7 @@ void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom)
(Vec2){ 1/-zoom, 1/zoom },
(Vec2){ 1/zoom, 1/-zoom }
},
.far_z = draw_distance
};
if (!orthographic)
@ -444,6 +446,8 @@ void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom)
else
camera_projection_matrix = camera_orthographic(&camera);
camera_far_z = draw_distance;
camera_look_at_matrix = camera_look_at(&camera);
}
@ -455,7 +459,8 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
float pitch,
float yaw,
float fov,
float zoom)
float zoom,
float draw_distance)
{
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
if (!orthographic && fov >= (float)(M_PI))
@ -476,7 +481,7 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
Vec3 const up = (Vec3){0, 1, 0};
draw_camera(position, direction, up, fov, zoom);
draw_camera(position, direction, up, fov, zoom, draw_distance);
return (DrawCameraFromPrincipalAxesResult) {
.direction = direction,

View File

@ -16,6 +16,7 @@
extern Matrix4 camera_projection_matrix;
extern Matrix4 camera_look_at_matrix;
extern float camera_far_z;
extern float camera_2d_rotation;
extern Vec2 camera_2d_position;

View File

@ -441,66 +441,68 @@ void finally_render_skybox(DeferredCommandDrawSkybox command) {
glEnable(GL_DEPTH_CLAMP);
#endif
float const d = camera_far_z / sqrtf(3);
glBegin(GL_QUADS); {
/* up */
glTexCoord3f(50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glTexCoord3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glTexCoord3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glTexCoord3f(d, d, d);
glVertex3f(d, d, d);
glTexCoord3f(-d, d, d);
glVertex3f(-d, d, d);
glTexCoord3f(-d, d, -d);
glVertex3f(-d, d, -d);
glTexCoord3f(d, d, -d);
glVertex3f(d, d, -d);
/* down */
glTexCoord3f(50.f, -50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glTexCoord3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glTexCoord3f(d, -d, d);
glVertex3f(d, -d, d);
glTexCoord3f(d, -d, -d);
glVertex3f(d, -d, -d);
glTexCoord3f(-d, -d, -d);
glVertex3f(-d, -d, -d);
glTexCoord3f(-d, -d, d);
glVertex3f(-d, -d, d);
/* east */
glTexCoord3f(50.f, -50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glTexCoord3f(50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glTexCoord3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glTexCoord3f(d, -d, d);
glVertex3f(d, -d, d);
glTexCoord3f(d, d, d);
glVertex3f(d, d, d);
glTexCoord3f(d, d, -d);
glVertex3f(d, d, -d);
glTexCoord3f(d, -d, -d);
glVertex3f(d, -d, -d);
/* west */
glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glTexCoord3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glTexCoord3f(-d, -d, d);
glVertex3f(-d, -d, d);
glTexCoord3f(-d, -d, -d);
glVertex3f(-d, -d, -d);
glTexCoord3f(-d, d, -d);
glVertex3f(-d, d, -d);
glTexCoord3f(-d, d, d);
glVertex3f(-d, d, d);
/* north */
glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glTexCoord3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glTexCoord3f(50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glTexCoord3f(50.f, -50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glTexCoord3f(-d, -d, d);
glVertex3f(-d, -d, d);
glTexCoord3f(-d, d, d);
glVertex3f(-d, d, d);
glTexCoord3f(d, d, d);
glVertex3f(d, d, d);
glTexCoord3f(d, -d, d);
glVertex3f(d, -d, d);
/* south */
glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glTexCoord3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glTexCoord3f(-d, -d, -d);
glVertex3f(-d, -d, -d);
glTexCoord3f(d, -d, -d);
glVertex3f(d, -d, -d);
glTexCoord3f(d, d, -d);
glVertex3f(d, d, -d);
glTexCoord3f(-d, d, -d);
glVertex3f(-d, d, -d);
} glEnd();
#ifndef __EMSCRIPTEN__

View File

@ -6,7 +6,7 @@
#define CAMERA_NEAR_Z 0.1f
#define CAMERA_FAR_Z 1000.0f
// #define CAMERA_FAR_Z 400.0f
Matrix4 camera_look_at(const Camera *const camera) {
@ -42,13 +42,13 @@ Matrix4 camera_perspective(const Camera *const camera) {
const float aspect = ((float)ctx.base_render_width / (float)ctx.base_render_height);
const float f = 1.0f / tanf(camera->fov * 0.5f);
const float fn = 1.0f / (CAMERA_NEAR_Z - CAMERA_FAR_Z);
const float fn = 1.0f / (CAMERA_NEAR_Z - camera->far_z);
result.row[0].x = f / aspect;
result.row[1].y = f;
result.row[2].z = (CAMERA_NEAR_Z + CAMERA_FAR_Z) * fn;
result.row[2].z = (CAMERA_NEAR_Z + camera->far_z) * fn;
result.row[2].w = -1.0f;
result.row[3].z = 2.0f * CAMERA_NEAR_Z * CAMERA_FAR_Z * fn;
result.row[3].z = 2.0f * CAMERA_NEAR_Z * camera->far_z * fn;
return result;
}
@ -61,14 +61,14 @@ Matrix4 camera_orthographic(const Camera *const camera) {
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);
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 = -(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].z = (camera->far_z + -camera->far_z) * fn;
result.row[3].w = 1.0f;
return result;

View File

@ -12,8 +12,9 @@ typedef struct Camera {
Vec3 pos; /* eye position */
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)) */
float fov; /* field of view, in radians */
float far_z;
} Camera;
Matrix4 camera_look_at(const Camera *camera);

View File

@ -631,7 +631,7 @@ static bool initialize(void) {
input_state_init(&ctx.input);
ctx.render_double_buffered = true;
ctx.render_double_buffered = false;
ctx.window_mouse_resident = true;
ctx.game.fog_color = (Color){ 255, 255, 255, 255 }; /* TODO: pick some grey? */