Made minor modifications to rendering models for performance increase

dev
shariq 8 years ago
parent 66de3159e9
commit a42954a764
  1. 2
      src/editor.c
  2. 2
      src/game.c
  3. 26
      src/geometry.c
  4. 23
      src/geometry.h
  5. 157
      src/model.c
  6. 4
      src/renderer.h

@ -257,7 +257,7 @@ void editor_update(float dt)
if(editor_state.debug_vars_window) if(editor_state.debug_vars_window)
{ {
static char variant_str[MAX_VARIANT_STR_LEN] = {'\0'}; static char variant_str[MAX_VARIANT_STR_LEN] = {'\0'};
if(nk_begin_titled(context, "Debug_Variables_Window", "Debug Variables", nk_rect(20, 20, 300, 300), default_window_flags)) if(nk_begin_titled(context, "Debug_Variables_Window", "Debug Variables", nk_rect(725, 30, 300, 300), default_window_flags))
{ {
nk_layout_row_static(context, 250, 250, 2); nk_layout_row_static(context, 250, 250, 2);
if(nk_group_begin(context, "Name", NK_WINDOW_BORDER | NK_WINDOW_SCROLL_AUTO_HIDE)) if(nk_group_begin(context, "Name", NK_WINDOW_BORDER | NK_WINDOW_SCROLL_AUTO_HIDE))

@ -167,7 +167,7 @@ void scene_setup(void)
/* model_set_material_param(screen_model, "diffuse_color", &color); */ /* model_set_material_param(screen_model, "diffuse_color", &color); */
/* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */ /* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */
const int MAX_LIGHTS = 3; const int MAX_LIGHTS = 6;
for(int i = 0; i < MAX_LIGHTS; i++) for(int i = 0; i < MAX_LIGHTS; i++)
{ {
int x = rand() % MAX_LIGHTS; int x = rand() % MAX_LIGHTS;

@ -4,7 +4,6 @@
#include "file_io.h" #include "file_io.h"
#include "log.h" #include "log.h"
#include "renderer.h" #include "renderer.h"
#include "bounding_volumes.h"
#include "transform.h" #include "transform.h"
#include "gl_load.h" #include "gl_load.h"
@ -15,27 +14,6 @@
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
struct Geometry
{
char* filename;
int draw_indexed;
uint vao;
uint vertex_vbo;
uint uv_vbo;
uint normal_vbo;
uint color_vbo;
uint index_vbo;
int ref_count;
vec3* vertices;
vec3* vertex_colors;
vec3* normals;
vec2* uvs;
uint* indices;
struct Bounding_Box bounding_box;
struct Bounding_Sphere bounding_sphere;
};
/* Data */ /* Data */
static struct Geometry* geometry_list; static struct Geometry* geometry_list;
static int* empty_indices; static int* empty_indices;
@ -391,8 +369,8 @@ int geom_render_in_frustum(int index,
return indices_rendered; return indices_rendered;
} }
struct Bounding_Sphere* geom_bounding_sphere_get(int index) struct Geometry* geom_get(int index)
{ {
assert(index > -1 && index < array_len(geometry_list)); assert(index > -1 && index < array_len(geometry_list));
return &geometry_list[index].bounding_sphere; return &geometry_list[index];
} }

@ -3,6 +3,7 @@
#include "num_types.h" #include "num_types.h"
#include "linmath.h" #include "linmath.h"
#include "bounding_volumes.h"
struct Entity; struct Entity;
@ -14,6 +15,26 @@ enum Geometry_Draw_Mode
GDM_NUM_DRAWMODES GDM_NUM_DRAWMODES
}; };
struct Geometry
{
char* filename;
int draw_indexed;
uint vao;
uint vertex_vbo;
uint uv_vbo;
uint normal_vbo;
uint color_vbo;
uint index_vbo;
int ref_count;
vec3* vertices;
vec3* vertex_colors;
vec3* normals;
vec2* uvs;
uint* indices;
struct Bounding_Box bounding_box;
struct Bounding_Sphere bounding_sphere;
};
void geom_init(void); void geom_init(void);
int geom_create_from_file(const char* name); int geom_create_from_file(const char* name);
int geom_find(const char* filename); int geom_find(const char* filename);
@ -22,6 +43,7 @@ void geom_cleanup(void);
void geom_render(int index, enum Geometry_Draw_Mode); void geom_render(int index, enum Geometry_Draw_Mode);
void geom_bounding_volume_generate(int index); void geom_bounding_volume_generate(int index);
void geom_bounding_volume_generate_all(void); void geom_bounding_volume_generate_all(void);
struct Geometry* geom_get(int index);
int geom_render_in_frustum(int index, int geom_render_in_frustum(int index,
vec4* frustum, vec4* frustum,
struct Entity* transform, struct Entity* transform,
@ -32,6 +54,5 @@ int geom_create(const char* name,
vec3* normals, vec3* normals,
uint* indices, uint* indices,
vec3* vertex_colors); vec3* vertex_colors);
struct Bounding_Sphere* geom_bounding_sphere_get(int index);
#endif #endif

@ -75,12 +75,92 @@ void model_render_all(struct Entity* camera_entity, int draw_mode)
shader_bind(material->shader); shader_bind(material->shader);
renderer_check_glerror("model:render_all:shader_bind"); renderer_check_glerror("model:render_all:shader_bind");
if(material->lit) /* Set light information */
{
int valid_light_count = 0;
int* light_index_list = light_get_valid_indices(&valid_light_count);
char uniform_name[MAX_NAME_LEN];
memset(uniform_name, '\0', MAX_NAME_LEN);
for(int i = 0; i < valid_light_count; i++)
{
struct Entity* light_entity = entity_get(light_index_list[i]);
struct Light* light = &light_entity->light; /* TODO: Cull lights according to camera frustum */
vec3 light_pos = {0, 0, 0};
transform_get_absolute_pos(light_entity, &light_pos);
if(light->type != LT_POINT)
{
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].direction", i);
transform_get_absolute_lookat(light_entity, &light_pos);
vec3_norm(&light_pos, &light_pos);
shader_set_uniform_vec3(material->shader, uniform_name, &light_pos);
memset(uniform_name, '\0', MAX_NAME_LEN);
}
if(light->type != LT_DIR)
{
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].position", i);
shader_set_uniform_vec3(material->shader, uniform_name, &light_pos);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].outer_angle", i);
shader_set_uniform_float(material->shader, uniform_name, light->outer_angle);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].inner_angle", i);
shader_set_uniform_float(material->shader, uniform_name, light->inner_angle);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].falloff", i);
shader_set_uniform_float(material->shader, uniform_name, light->falloff);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].radius", i);
shader_set_uniform_int(material->shader, uniform_name, light->radius);
memset(uniform_name, '\0', MAX_NAME_LEN);
}
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].color", i);
shader_set_uniform_vec3(material->shader, uniform_name, &light->color);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].intensity", i);
shader_set_uniform_float(material->shader, uniform_name, light->intensity);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].type", i);
shader_set_uniform_int(material->shader, uniform_name, light->type);
memset(uniform_name, '\0', MAX_NAME_LEN);
}
shader_set_uniform_int(material->shader, "total_active_lights", valid_light_count);
vec3 camera_pos = {0, 0, 0};
transform_get_absolute_pos(camera_entity, &camera_pos);
shader_set_uniform_vec3(material->shader, "camera_pos", &camera_pos);
}
for(int j = 0; j < array_len(material->registered_models); j++) for(int j = 0; j < array_len(material->registered_models); j++)
{ {
/* for each registered model, set up uniforms and render */ /* for each registered model, set up uniforms and render */
struct Entity* entity = entity_get(material->registered_models[j]); struct Entity* entity = entity_get(material->registered_models[j]);
struct Model* model = &entity->model; struct Model* model = &entity->model;
struct Transform* transform = &entity->transform; struct Transform* transform = &entity->transform;
struct Geometry* geometry = geom_get(model->geometry_index);
/* Check if model is in frustum */
int intersection = bv_intersect_frustum_sphere(camera_entity->camera.frustum, &geometry->bounding_sphere, entity);
if(intersection == IT_OUTSIDE)
{
num_culled++;
continue;
}
else
{
num_indices += array_len(geometry->indices);
num_rendered++;
}
/* set material params for the model */ /* set material params for the model */
for(int k = 0; k < array_len(model->material_params); k++) for(int k = 0; k < array_len(model->material_params); k++)
@ -154,82 +234,9 @@ void model_render_all(struct Entity* camera_entity, int draw_mode)
} }
} }
if(material->lit) /* Set light information */
{
int valid_light_count = 0;
int* light_index_list = light_get_valid_indices(&valid_light_count);
char uniform_name[MAX_NAME_LEN];
memset(uniform_name, '\0', MAX_NAME_LEN);
for(int i = 0; i < valid_light_count; i++)
{
struct Entity* light_entity = entity_get(light_index_list[i]);
struct Light* light = &light_entity->light; /* TODO: Cull lights according to camera frustum */
vec3 light_pos = {0, 0, 0};
transform_get_absolute_pos(light_entity, &light_pos);
if(light->type != LT_POINT)
{
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].direction", i);
transform_get_absolute_lookat(light_entity, &light_pos);
vec3_norm(&light_pos, &light_pos);
shader_set_uniform_vec3(material->shader, uniform_name, &light_pos);
memset(uniform_name, '\0', MAX_NAME_LEN);
}
if(light->type != LT_DIR)
{
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].position", i);
shader_set_uniform_vec3(material->shader, uniform_name, &light_pos);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].outer_angle", i);
shader_set_uniform_float(material->shader, uniform_name, light->outer_angle);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].inner_angle", i);
shader_set_uniform_float(material->shader, uniform_name, light->inner_angle);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].falloff", i);
shader_set_uniform_float(material->shader, uniform_name, light->falloff);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].radius", i);
shader_set_uniform_int(material->shader, uniform_name, light->radius);
memset(uniform_name, '\0', MAX_NAME_LEN);
}
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].color", i);
shader_set_uniform_vec3(material->shader, uniform_name, &light->color);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].intensity", i);
shader_set_uniform_float(material->shader, uniform_name, light->intensity);
memset(uniform_name, '\0', MAX_NAME_LEN);
snprintf(uniform_name, MAX_NAME_LEN, "lights[%d].type", i);
shader_set_uniform_int(material->shader, uniform_name, light->type);
memset(uniform_name, '\0', MAX_NAME_LEN);
}
shader_set_uniform_int(material->shader, "total_active_lights", valid_light_count);
vec3 camera_pos = {0, 0, 0};
transform_get_absolute_pos(camera_entity, &camera_pos);
shader_set_uniform_vec3(material->shader, "camera_pos", &camera_pos);
}
/* Render the geometry */ /* Render the geometry */
int indices = geom_render_in_frustum(model->geometry_index, &camera_entity->camera.frustum[0], entity, draw_mode); //int indices = geom_render_in_frustum(model->geometry_index, &camera_entity->camera.frustum[0], entity, draw_mode);
if(indices > 0) geom_render(model->geometry_index, draw_mode);
{
num_rendered++;
num_indices += indices;
}
else
{
num_culled++;
}
//geom_render(model->geometry_index, draw_mode);
for(int k = 0; k < array_len(model->material_params); k++) for(int k = 0; k < array_len(model->material_params); k++)
{ {

@ -1,5 +1,5 @@
#ifndef renderer_H #ifndef RENDERER_H
#define renderer_H #define RENDERER_H
#include "linmath.h" #include "linmath.h"

Loading…
Cancel
Save