Skip to content

Commit

Permalink
WIP: shadow mapping
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 31, 2024
1 parent 5deae4e commit 0ddfe3c
Show file tree
Hide file tree
Showing 27 changed files with 616 additions and 257 deletions.
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ set(ENGINE_LIB libonehandclap)
set(ENGINE_SRC ${SERVER_SRC} )
set(ENGINE_SRC
object.c ref.c util.c logger.c graphics.c input.c messagebus.c
matrix.c model.c shader.c librarian.c json.c clap.c
matrix.c model.c shader.c librarian.c json.c clap.c view.c light.c
terrain.c ui.c scene.c font.c sound.c networking.c pngloader.c
physics.c ui-animations.c input-fuzzer.c character.c settings.c
gltf.c input-joystick.c render-gl.c mesh.c pipeline.c input-keyboard.c
Expand Down
75 changes: 0 additions & 75 deletions core/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,6 @@
#include "character.h"
#include "ui-debug.h"

void camera_calc_frustum(struct camera *c, mat4x4 projmx)
{
mat4x4 mvp, trans, invmvp;
vec4 corners[] = {
{ -1, -1, -1, 1 }, { 1, -1, -1, 1 },
{ 1, 1, -1, 1 }, { -1, 1, -1, 1 },
{ -1, -1, 1, 1 }, { 1, -1, 1, 1 },
{ 1, 1, 1, 1 }, { -1, 1, 1, 1 }
};
int i;

mat4x4_mul(mvp, projmx, c->view_mx->m);
mat4x4_transpose(trans, mvp);
mat4x4_invert(invmvp, mvp);

/* frustum planes */
vec4_add(c->frustum_planes[0], trans[3], trans[0]);
vec4_sub(c->frustum_planes[1], trans[3], trans[0]);
vec4_add(c->frustum_planes[2], trans[3], trans[1]);
vec4_sub(c->frustum_planes[3], trans[3], trans[1]);
vec4_add(c->frustum_planes[4], trans[3], trans[2]);
vec4_sub(c->frustum_planes[5], trans[3], trans[2]);

/* frustum corners */
for (i = 0; i < 8; i++) {
vec4 q;

mat4x4_mul_vec4(q, invmvp, corners[i]);
vec4_scale(c->frustum_corners[i], q, 1.f / q[3]);
}
}

bool camera_entity_in_frustum(struct camera *c, struct entity3d *e)
{
vec3 min, max;
int i;

entity3d_aabb_min(e, min);
entity3d_aabb_max(e, max);

for (i = 0; i < 6; i++) {
int r = 0;
vec4 v;

vec4_setup(v, min[0], min[1], min[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, max[0], min[1], min[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, min[0], max[1], min[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, max[0], max[1], min[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, min[0], min[1], max[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, max[0], min[1], max[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, min[0], max[1], max[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
vec4_setup(v, max[0], max[1], max[2], 1.0);
r += (vec4_mul_inner(c->frustum_planes[i], v) < 0.0) ? 1 : 0;
if (r == 8)
return false;
}

int r = 0;
for (r = 0, i = 0; i < 8; i++) r += c->frustum_corners[i][0] > max[0] ? 1 : 0; if (r == 8) return false;
for (r = 0, i = 0; i < 8; i++) r += c->frustum_corners[i][0] < min[0] ? 1 : 0; if (r == 8) return false;
for (r = 0, i = 0; i < 8; i++) r += c->frustum_corners[i][1] > max[1] ? 1 : 0; if (r == 8) return false;
for (r = 0, i = 0; i < 8; i++) r += c->frustum_corners[i][1] < min[1] ? 1 : 0; if (r == 8) return false;
for (r = 0, i = 0; i < 8; i++) r += c->frustum_corners[i][2] > max[2] ? 1 : 0; if (r == 8) return false;
for (r = 0, i = 0; i < 8; i++) r += c->frustum_corners[i][2] < min[2] ? 1 : 0; if (r == 8) return false;

return true;
}

void camera_setup(struct camera *c)
{
// starting yaw values
Expand Down
8 changes: 2 additions & 6 deletions core/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
//#include "matrix.h"
//#include "model.h"
#include "model.h"
#include "view.h"
//#include "physics.h"

#define NUMBER_OF_DEBUG_LINES 4

struct camera {
struct character *ch;
struct view view;
/* GLfloat pitch; /\* left/right *\/ */
/* GLfloat yaw; /\* sideways *\/ */
/* GLfloat roll; /\* up/down *\/ */
Expand All @@ -26,19 +28,13 @@ struct camera {
float dist;
float yaw_delta;
float pitch_delta;
struct matrix4f *view_mx;
struct matrix4f *inv_view_mx;
vec4 frustum_planes[6];
vec4 frustum_corners[8];
float tmp_debug_line_start[3];
float tmp_debug_line_end[3 * NUMBER_OF_DEBUG_LINES];
float debug_line_start[3];
float debug_line_end[3 * NUMBER_OF_DEBUG_LINES];
};

void camera_setup(struct camera *c);
void camera_calc_frustum(struct camera *c, mat4x4 projmx);
bool camera_entity_in_frustum(struct camera *c, struct entity3d *e);
void camera_move(struct camera *c, unsigned long fps);
void camera_position(struct camera *c, float x, float y, float z, GLfloat *pos);
void camera_reset_movement(struct camera *c);
Expand Down
33 changes: 33 additions & 0 deletions core/light.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "light.h"

void light_update(struct light *light, int idx)
{
// view_update(&light->view[idx], &light->pos[idx * 3], -90, 0, 0);
// mat4x4_look_at(light->view->view_mx.m, )
}

void light_set_pos(struct light *light, int idx, float pos[3])
{
int i;

for (i = 0; i < 3; i++)
light->pos[idx * 3 + i] = pos[i];

light_update(light, idx);
}

void light_set_color(struct light *light, int idx, float color[3])
{
int i;

for (i = 0; i < 3; i++)
light->color[idx * 3 + i] = color[i];
}

void light_set_attenuation(struct light *light, int idx, float attenuation[3])
{
int i;

for (i = 0; i < 3; i++)
light->attenuation[idx * 3 + i] = attenuation[i];
}
26 changes: 26 additions & 0 deletions core/light.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* SPDX-License-Identifier: Apache-2.0 */
#ifndef __CLAP_LIGHT_H__
#define __CLAP_LIGHT_H__

#include "common.h"
#include "render.h"
#include "display.h"
#include "view.h"

#define LIGHTS_MAX 4

struct light {
GLfloat pos[3 * LIGHTS_MAX];
GLfloat color[3 * LIGHTS_MAX];
GLfloat attenuation[3 * LIGHTS_MAX];
GLfloat dir[3 * LIGHTS_MAX];
struct view view[LIGHTS_MAX];
texture_t *shadow[LIGHTS_MAX];
};

void light_update(struct light *light, int idx);
void light_set_pos(struct light *light, int idx, float pos[3]);
void light_set_color(struct light *light, int idx, float color[3]);
void light_set_attenuation(struct light *light, int idx, float attenuation[3]);

#endif /* __CLAP_LIGHT_H__ */
Loading

0 comments on commit 0ddfe3c

Please sign in to comment.