Primitive frustum culling
This commit is contained in:
Executable
+2
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
cpulimit -l 20 -- ./wschod
|
||||
@@ -35,6 +35,7 @@ Chunk_random (Chunk *chunk, int xoffset, int zoffset)
|
||||
tree->position[0] = x + xoffset;
|
||||
tree->position[1] = chunk->heightmap [INDEX (x, z)];
|
||||
tree->position[2] = z + zoffset;
|
||||
tree->rotation = rand () % 180;
|
||||
EM_ENTADD (tree);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,35 @@ em_render_internal (gpointer data, gpointer userdata)
|
||||
{
|
||||
Entity *entity = data;
|
||||
mat4 *view = userdata;
|
||||
|
||||
if (glm_vec3_distance (game_state.camera.position, entity->position) > EM_RENDER_DISTANCE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* TODO: Fix this up. It still isn't perfect, when you look up,
|
||||
sometimes the entities get culled. */
|
||||
|
||||
vec4 planes[6];
|
||||
mat4 proj, view_proj;
|
||||
|
||||
Camera_get_projection (&game_state.camera, 640.0 / 480.0, proj);
|
||||
glm_mat4_mul (proj, *view, view_proj);
|
||||
glm_frustum_planes (view_proj, planes);
|
||||
|
||||
vec3 aabb[2] = {
|
||||
{ -entity->scale[0] + entity->position[0],
|
||||
-entity->scale[1] + entity->position[1] + (entity->origin == ORIGIN_BOTTOM ? entity->scale[1] : 0),
|
||||
-entity->scale[2] + entity->position[2] },
|
||||
{ +entity->scale[0] + entity->position[0],
|
||||
+entity->scale[1] + entity->position[1] + (entity->origin == ORIGIN_BOTTOM ? entity->scale[1] : 0),
|
||||
+entity->scale[2] + entity->position[2] },
|
||||
};
|
||||
|
||||
if (!glm_aabb_frustum (aabb, planes))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EntityMesh to_render;
|
||||
|
||||
@@ -87,7 +116,7 @@ em_render_internal (gpointer data, gpointer userdata)
|
||||
glm_vec3_sub (game_state.camera.position, entity->position, delta);
|
||||
delta[1] = 0.0;
|
||||
glm_normalize (delta);
|
||||
glm_rotate_at(model, entity->position, atan2f (delta[0], delta[2]), (vec3) { 0.0f, 1.0f, 0.0f });
|
||||
glm_rotate_at(model, entity->position, atan2f (delta[0], delta[2]), (vec3) { 0.0, 1.0, 0.0 });
|
||||
glm_translate (model, entity->position);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
#include "../util.h"
|
||||
#include "../resources.h"
|
||||
#include "../state.h"
|
||||
#include "../chunk/chunk.h"
|
||||
|
||||
#define EM_RENDER_DISTANCE (3*CHUNK_DIM/2)
|
||||
|
||||
#define EM_ENTADD(entity) (g_sequence_insert_sorted (entity_manager, entity, em_compare, NULL));
|
||||
|
||||
|
||||
@@ -59,10 +59,6 @@ GraphicsContext_render ()
|
||||
Camera_get_view (&game_state.camera, view);
|
||||
|
||||
/* Set views and projections in all programs. TODO: make this dynamic*/
|
||||
program_use (R_PRGGET ("billboard"));
|
||||
program_set_mat4 (R_PRGGET ("billboard"), "projection", projection);
|
||||
program_set_mat4 (R_PRGGET ("billboard"), "view", view);
|
||||
|
||||
program_use (R_PRGGET ("default"));
|
||||
program_set_mat4 (R_PRGGET ("default"), "projection", projection);
|
||||
program_set_mat4 (R_PRGGET ("default"), "view", view);
|
||||
|
||||
Reference in New Issue
Block a user