Fixed bugs in rendering code, began separating editor camera from game camera

dev
Shariq Shah 7 years ago
parent a28c5bbda7
commit 56482d06b2
  1. 1
      README.md
  2. 3
      assets/shaders/blinn_phong.frag
  3. 131
      src/libsymmetry/editor.c
  4. 6
      src/libsymmetry/entity.h
  5. 20
      src/libsymmetry/game.c
  6. 4
      src/libsymmetry/light.c
  7. 2
      src/libsymmetry/light.h
  8. 43
      src/libsymmetry/material.c
  9. 20
      src/libsymmetry/material.h
  10. 11
      src/libsymmetry/model.c
  11. 4
      src/libsymmetry/model.h
  12. 39
      src/libsymmetry/renderer.c
  13. 23
      src/libsymmetry/scene.c

@ -155,6 +155,7 @@
- ## TODO - ## TODO
- Remove model and replace all usages with static mesh
- Re-Implement player logic - Re-Implement player logic
- Re-Implement saving/loading scene to/from files - Re-Implement saving/loading scene to/from files
- Bring back functionality and complete overhaul - Bring back functionality and complete overhaul

@ -36,6 +36,7 @@ vec3 calc_point_light(in Light light)
vec3 specular_comp = vec3(0.0); vec3 specular_comp = vec3(0.0);
vec3 light_direction = vertex - light.position; vec3 light_direction = vertex - light.position;
float dist = abs(length(light_direction)); float dist = abs(length(light_direction));
if(dist <= light.radius) if(dist <= light.radius)
{ {
light_direction = normalize(light_direction); light_direction = normalize(light_direction);
@ -115,8 +116,6 @@ void main()
for(int i = 0; i < total_active_lights; i++) for(int i = 0; i < total_active_lights; i++)
{ {
if(i == total_active_lights) break;
if(lights[i].type == LT_POINT) if(lights[i].type == LT_POINT)
light_contribution += calc_point_light(lights[i]); light_contribution += calc_point_light(lights[i]);
else if(lights[i].type == LT_DIR) else if(lights[i].type == LT_DIR)

@ -19,6 +19,8 @@
#include "../common/num_types.h" #include "../common/num_types.h"
#include "../common/string_utils.h" #include "../common/string_utils.h"
#include "../common/common.h" #include "../common/common.h"
#include "input.h"
#include "scene.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -30,10 +32,13 @@
struct Editor_State struct Editor_State
{ {
bool enabled; bool enabled;
bool renderer_settings_window; bool renderer_settings_window;
int selected_entity_id; int selected_entity_id;
int top_panel_height; int top_panel_height;
float camera_turn_speed;
float camera_move_speed;
float camera_sprint_multiplier;
}; };
struct Debug_Variable struct Debug_Variable
@ -46,6 +51,7 @@ static struct Editor_State editor_state;
static struct Debug_Variable* debug_vars_list = NULL; static struct Debug_Variable* debug_vars_list = NULL;
static int* empty_indices = NULL; static int* empty_indices = NULL;
static void editor_camera_update(float dt);
static void editor_widget_color_combov3(struct nk_context* context, vec3* color, int width, int height); static void editor_widget_color_combov3(struct nk_context* context, vec3* color, int width, int height);
static void editor_widget_color_combov4(struct nk_context* context, vec4* color, int width, int height); static void editor_widget_color_combov4(struct nk_context* context, vec4* color, int width, int height);
static bool editor_widget_v3(struct nk_context* context, static bool editor_widget_v3(struct nk_context* context,
@ -65,8 +71,28 @@ void editor_init(void)
editor_state.renderer_settings_window = false; editor_state.renderer_settings_window = false;
editor_state.selected_entity_id = -1; editor_state.selected_entity_id = -1;
editor_state.top_panel_height = 20; editor_state.top_panel_height = 20;
editor_state.camera_turn_speed = 50.f;
editor_state.camera_move_speed = 10.f;
editor_state.camera_sprint_multiplier = 2.f;
debug_vars_list = array_new(struct Debug_Variable); debug_vars_list = array_new(struct Debug_Variable);
empty_indices = array_new(int); empty_indices = array_new(int);
struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR];
editor_camera->base.active = true;
vec3 cam_pos = {0.f, 15.f, -5.f};
transform_translate(editor_camera, &cam_pos, TS_WORLD);
editor_camera->clear_color.x = 0.3f;
editor_camera->clear_color.y = 0.6f;
editor_camera->clear_color.z = 0.9f;
editor_camera->clear_color.w = 1.f;
struct Hashmap* config = platform->config.get();
int render_width = hashmap_int_get(config, "render_width");
int render_height = hashmap_int_get(config, "render_height");
camera_attach_fbo(editor_camera, render_width, render_height, true, true, true);
vec3 axis = {1.f, 0.f, 0.f};
transform_rotate(editor_camera, &axis, -25.f, TS_WORLD);
} }
int editor_debugvar_slot_create(const char* name, int value_type) int editor_debugvar_slot_create(const char* name, int value_type)
@ -145,6 +171,8 @@ void editor_debugvar_slot_set_quat(int index, quat* value)
void editor_update(float dt) void editor_update(float dt)
{ {
if(!editor_state.enabled) return; if(!editor_state.enabled) return;
editor_camera_update(dt);
struct Game_State* game_state = game_state_get(); struct Game_State* game_state = game_state_get();
struct Gui_State* gui_state = gui_state_get(); struct Gui_State* gui_state = gui_state_get();
@ -538,6 +566,101 @@ void editor_toggle(void)
editor_state.enabled = !editor_state.enabled; editor_state.enabled = !editor_state.enabled;
} }
void editor_camera_update(float dt)
{
struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR];
float move_speed = editor_state.camera_move_speed, turn_speed = editor_state.camera_turn_speed;
vec3 offset = {0, 0, 0};
float turn_up_down = 0.f;
float turn_left_right = 0.f;
float max_up_down = 60.f;
static float total_up_down_rot = 0.f;
vec3 rot_axis_up_down = {1, 0, 0};
vec3 rot_axis_left_right = {0, 1, 0};
/* Look around */
if(input_map_state_get("Turn_Up", KS_PRESSED)) turn_up_down += turn_speed;
if(input_map_state_get("Turn_Down", KS_PRESSED)) turn_up_down -= turn_speed;
if(input_map_state_get("Turn_Right", KS_PRESSED)) turn_left_right += turn_speed;
if(input_map_state_get("Turn_Left", KS_PRESSED)) turn_left_right -= turn_speed;
if(input_mousebutton_state_get(MSB_RIGHT, KS_PRESSED))
{
const float scale = 0.1f;
int cursor_lr, cursor_ud;
input_mouse_delta_get(&cursor_lr, &cursor_ud);
if(input_mouse_mode_get() != MM_RELATIVE)
{
input_mouse_mode_set(MM_RELATIVE);
cursor_lr = cursor_ud = 0;
}
turn_up_down = -cursor_ud * turn_speed * dt * scale;
turn_left_right = cursor_lr * turn_speed * dt * scale;
// log_message("ud : %d, lr : %d", cursor_ud, cursor_lr);
}
else
{
input_mouse_mode_set(MM_NORMAL);
// log_message("ud : %.3f, lr : %.3f", turn_up_down, turn_left_right);
turn_up_down *= dt;
turn_left_right *= dt;
}
total_up_down_rot += turn_up_down;
if(total_up_down_rot >= max_up_down)
{
total_up_down_rot = max_up_down;
turn_up_down = 0.f;
}
else if(total_up_down_rot <= -max_up_down)
{
total_up_down_rot = -max_up_down;
turn_up_down = 0.f;
}
if(turn_left_right != 0.f)
{
/*transform_rotate(player_entity, &rot_axis_left_right, -turn_left_right, TS_WORLD);
vec3 up = {0.f, 0.f, 0.f};
vec3 forward = {0.f, 0.f, 0.f};
vec3 lookat = {0.f, 0.f, 0.f};
transform_get_up(player_entity, &up);
transform_get_forward(player_entity, &forward);
transform_get_lookat(player_entity, &lookat);*/
/* log_message("Up : %s", tostr_vec3(&up)); */
/* log_message("FR : %s", tostr_vec3(&forward)); */
}
if(turn_up_down != 0.f)
{
/*transform_rotate(player_entity, &rot_axis_up_down, turn_up_down, TS_LOCAL);
vec3 up = {0.f, 0.f, 0.f};
vec3 forward = {0.f, 0.f, 0.f};
vec3 lookat = {0.f, 0.f, 0.f};
transform_get_up(player_entity, &up);
transform_get_forward(player_entity, &forward);
transform_get_lookat(player_entity, &lookat);*/
/* log_message("Up : %s", tostr_vec3(&up)); */
/* log_message("FR : %s", tostr_vec3(&forward)); */
}
/* Movement */
if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= editor_state.camera_sprint_multiplier;
if(input_map_state_get("Move_Forward", KS_PRESSED)) offset.z -= move_speed;
if(input_map_state_get("Move_Backward", KS_PRESSED)) offset.z += move_speed;
if(input_map_state_get("Move_Left", KS_PRESSED)) offset.x -= move_speed;
if(input_map_state_get("Move_Right", KS_PRESSED)) offset.x += move_speed;
if(input_map_state_get("Move_Up", KS_PRESSED)) offset.y += move_speed;
if(input_map_state_get("Move_Down", KS_PRESSED)) offset.y -= move_speed;
vec3_scale(&offset, &offset, dt);
if(offset.x != 0 || offset.y != 0 || offset.z != 0)
{
transform_translate(editor_camera, &offset, TS_LOCAL);
//log_message("Position : %s", tostr_vec3(&transform->position));
}
}
void editor_widget_color_combov3(struct nk_context* context, vec3* color, int width, int height) void editor_widget_color_combov3(struct nk_context* context, vec3* color, int width, int height)
{ {
struct nk_color temp_color = nk_rgba_f(color->x, color->y, color->z, 1.f); struct nk_color temp_color = nk_rgba_f(color->x, color->y, color->z, 1.f);

@ -40,9 +40,9 @@ enum LightType
enum Camera_Type enum Camera_Type
{ {
CT_EDITOR = 0, CAM_EDITOR = 0,
CT_GAME, CAM_GAME,
CT_MAX CAM_MAX
}; };
struct Transform struct Transform

@ -99,13 +99,13 @@ bool game_init(struct Window* window, struct Platform_Api* platform_api)
texture_init(); texture_init();
framebuffer_init(); framebuffer_init();
geom_init(); geom_init();
editor_init();
platform->physics.init(); platform->physics.init();
platform->physics.gravity_set(0.f, -9.8f, 0.f); platform->physics.gravity_set(0.f, -9.8f, 0.f);
platform->physics.body_set_moved_callback(entity_rigidbody_on_move); platform->physics.body_set_moved_callback(entity_rigidbody_on_move);
platform->physics.body_set_collision_callback(entity_rigidbody_on_collision); platform->physics.body_set_collision_callback(entity_rigidbody_on_collision);
scene_init(game_state->scene); scene_init(game_state->scene);
editor_init();
renderer_init(game_state->renderer); renderer_init(game_state->renderer);
} }
@ -276,6 +276,24 @@ void scene_setup(void)
//struct Entity* ground = entity_find("Ground"); //struct Entity* ground = entity_find("Ground");
////entity_collision_shape_set(ground, plane); ////entity_collision_shape_set(ground, plane);
//entity_rigidbody_set(ground, ground_box); //entity_rigidbody_set(ground, ground_box);
struct Static_Mesh* ground = scene_static_mesh_create(game_state->scene, "Ground_Mesh", NULL, "default.pamesh", MAT_BLINN);
vec3 scale = {200.f, 1.f, 200.f};
transform_scale(ground, &scale);
ground->model.material_params[MMP_DIFFUSE_TEX].val_int = texture_create_from_file("white.tga", TU_DIFFUSE);
ground->model.material_params[MMP_DIFFUSE].val_float = 0.5f;
ground->model.material_params[MMP_SPECULAR].val_float = 1.f;
ground->model.material_params[MMP_SPECULAR_STRENGTH].val_float = 1.f;
vec3_fill(&ground->model.material_params[MMP_DIFFUSE_COL].val_vec3, 1.f, 1.f, 1.f);
struct Light* light = scene_light_create(game_state->scene, "Test_Light", NULL, LT_POINT);
light->color.x = 1.f;
light->color.y = 0.f;
light->color.z = 1.f;
light->radius = 20.f;
vec3 light_pos = {0.f, 5.f, 0.f};
transform_translate(light, &light_pos, TS_WORLD);
} }
void debug(float dt) void debug(float dt)

@ -7,7 +7,7 @@
void light_init(struct Light* light, int light_type) void light_init(struct Light* light, int light_type)
{ {
light->valid = true; light->valid = true;
light->cast_shadow = 0; light->cast_shadow = false;
light->depth_bias = 0.0005f; light->depth_bias = 0.0005f;
light->type = light_type; light->type = light_type;
light->pcf_enabled = false; light->pcf_enabled = false;
@ -15,7 +15,7 @@ void light_init(struct Light* light, int light_type)
light->falloff = 1.5f; light->falloff = 1.5f;
light->outer_angle = TO_RADIANS(30.f); light->outer_angle = TO_RADIANS(30.f);
light->inner_angle = TO_RADIANS(20.f); light->inner_angle = TO_RADIANS(20.f);
light->radius = 20; light->radius = 20.f;
vec3_fill(&light->color, 1.f, 1.f, 1.f); vec3_fill(&light->color, 1.f, 1.f, 1.f);
} }

@ -3,7 +3,7 @@
#define MAX_SHADOWMAPS 4 #define MAX_SHADOWMAPS 4
void light_init(struct Light* light); void light_init(struct Light* light, int light_type);
void light_reset(struct Light* light); void light_reset(struct Light* light);
#endif #endif

@ -4,7 +4,6 @@
#include "entity.h" #include "entity.h"
#include "../common/string_utils.h" #include "../common/string_utils.h"
#include "../common/log.h" #include "../common/log.h"
#include "model.h"
#include "texture.h" #include "texture.h"
#include "light.h" #include "light.h"
@ -17,7 +16,7 @@ bool material_init(struct Material* material, int material_type)
assert(material && material_type > -1 && material_type < MAT_MAX); assert(material && material_type > -1 && material_type < MAT_MAX);
material->type = material_type; material->type = material_type;
memset(material->registered_models, 0, sizeof(struct Model*) * MAX_MATERIAL_REGISTERED_MODELS); memset(material->registered_static_meshes, '\0', sizeof(struct Static_Mesh*) * MAX_MATERIAL_REGISTERED_STATIC_MESHES);
memset(material->model_params, 0, sizeof(struct Uniform) * MMP_MAX); memset(material->model_params, 0, sizeof(struct Uniform) * MMP_MAX);
memset(material->pipeline_params, 0, sizeof(struct Uniform) * MPP_MAX); memset(material->pipeline_params, 0, sizeof(struct Uniform) * MPP_MAX);
@ -53,7 +52,7 @@ bool material_init(struct Material* material, int material_type)
material->model_params[MMP_SPECULAR].location = shader_get_uniform_location(material->shader, "specular"); material->model_params[MMP_SPECULAR].location = shader_get_uniform_location(material->shader, "specular");
material->model_params[MMP_SPECULAR_STRENGTH].type = UT_FLOAT; material->model_params[MMP_SPECULAR_STRENGTH].type = UT_FLOAT;
material->model_params[MMP_SPECULAR_STRENGTH].location = shader_get_uniform_location(material->shader, "specular"); material->model_params[MMP_SPECULAR_STRENGTH].location = shader_get_uniform_location(material->shader, "specular_strength");
} }
break; break;
case MAT_UNSHADED: case MAT_UNSHADED:
@ -106,37 +105,37 @@ void material_reset(struct Material* material)
assert(material); assert(material);
material->type = -1; material->type = -1;
memset(material->registered_models, 0, sizeof(struct Model*) * MAX_MATERIAL_REGISTERED_MODELS); memset(material->registered_static_meshes, '\0', sizeof(struct Static_Mesh*) * MAX_MATERIAL_REGISTERED_STATIC_MESHES);
memset(material->model_params, 0, sizeof(struct Uniform) * MMP_MAX); memset(material->model_params, 0, sizeof(struct Uniform) * MMP_MAX);
memset(material->pipeline_params, 0, sizeof(struct Uniform) * MPP_MAX); memset(material->pipeline_params, 0, sizeof(struct Uniform) * MPP_MAX);
} }
bool material_register_model(struct Material* material, struct Model* model) bool material_register_static_mesh(struct Material* material, struct Static_Mesh* mesh)
{ {
assert(model && material); assert(material);
for(int i = 0; i < MAX_MATERIAL_REGISTERED_MODELS; i++) for(int i = 0; i < MAX_MATERIAL_REGISTERED_STATIC_MESHES; i++)
{ {
if(!material->registered_models[i]) if(!material->registered_static_meshes[i])
{ {
material->registered_models[i] = model; material->registered_static_meshes[i] = mesh;
// Set default values for instance parameters // Set default values for instance parameters
switch(material->type) switch(material->type)
{ {
case MAT_BLINN: case MAT_BLINN:
{ {
variant_assign_vec4f(&model->material_params[MMP_DIFFUSE_COL], 1.f, 0.f, 1.f, 1.f); variant_assign_vec4f(&mesh->model.material_params[MMP_DIFFUSE_COL], 1.f, 0.f, 1.f, 1.f);
variant_assign_float(&model->material_params[MMP_DIFFUSE], 1.f); variant_assign_float(&mesh->model.material_params[MMP_DIFFUSE], 1.f);
variant_assign_int(&model->material_params[MMP_DIFFUSE_TEX], texture_find("default.tga")); variant_assign_int(&mesh->model.material_params[MMP_DIFFUSE_TEX], texture_find("default.tga"));
variant_assign_float(&model->material_params[MMP_SPECULAR], 1.f); variant_assign_float(&mesh->model.material_params[MMP_SPECULAR], 1.f);
variant_assign_float(&model->material_params[MMP_SPECULAR_STRENGTH], 50.f); variant_assign_float(&mesh->model.material_params[MMP_SPECULAR_STRENGTH], 50.f);
} }
break; break;
case MAT_UNSHADED: case MAT_UNSHADED:
{ {
variant_assign_vec4f(&model->material_params[MMP_DIFFUSE_COL], 1.f, 0.f, 1.f, 1.f); variant_assign_vec4f(&mesh->model.material_params[MMP_DIFFUSE_COL], 1.f, 0.f, 1.f, 1.f);
variant_assign_int(&model->material_params[MMP_DIFFUSE_TEX], texture_find("default.tga")); variant_assign_int(&mesh->model.material_params[MMP_DIFFUSE_TEX], texture_find("default.tga"));
} }
break; break;
default: default:
@ -150,17 +149,17 @@ bool material_register_model(struct Material* material, struct Model* model)
return false; return false;
} }
void material_unregister_model(struct Material* material, struct Model* model) void material_unregister_static_mesh(struct Material* material, struct Static_Mesh* mesh)
{ {
assert(model && material); assert(material);
for(int i = 0; i < MAX_MATERIAL_REGISTERED_MODELS; i++) for(int i = 0; i < MAX_MATERIAL_REGISTERED_STATIC_MESHES; i++)
{ {
if(material->registered_models[i] == model) if(material->registered_static_meshes[i] == mesh)
{ {
material->registered_models[i] = NULL; material->registered_static_meshes[i] = NULL;
for(int i = 0; i < MMP_MAX; i++) for(int i = 0; i < MMP_MAX; i++)
variant_free(&model->material_params[i]); variant_free(&mesh->model.material_params[i]);
break; break;
} }
} }

@ -5,10 +5,10 @@
#include "../common/num_types.h" #include "../common/num_types.h"
#include "../common/variant.h" #include "../common/variant.h"
struct Model; struct Static_Mesh;
#define MAX_UNIFORM_NAME_LEN 64 #define MAX_UNIFORM_NAME_LEN 64
#define MAX_MATERIAL_REGISTERED_MODELS 1024 #define MAX_MATERIAL_REGISTERED_STATIC_MESHES 1024
struct Uniform struct Uniform
{ {
@ -52,18 +52,18 @@ enum Mat_Pipeline_Param
struct Material struct Material
{ {
int type; int type;
int shader; int shader;
struct Model* registered_models[MAX_MATERIAL_REGISTERED_MODELS]; struct Static_Mesh* registered_static_meshes[MAX_MATERIAL_REGISTERED_STATIC_MESHES];
bool lit; bool lit;
struct Uniform model_params[MMP_MAX]; struct Uniform model_params[MMP_MAX];
struct Uniform pipeline_params[MPP_MAX]; struct Uniform pipeline_params[MPP_MAX];
}; };
bool material_init(struct Material* material, int material_type); bool material_init(struct Material* material, int material_type);
void material_reset(struct Material* material); void material_reset(struct Material* material);
bool material_register_model(struct Material* material, struct Model* model); bool material_register_static_mesh(struct Material* material, struct Static_Mesh* mesh);
void material_unregister_model(struct Material* material, struct Model* model); void material_unregister_static_mesh(struct Material* material, struct Static_Mesh* mesh);
#endif #endif

@ -13,7 +13,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
void model_init(struct Model* model, const char* geometry_name, int material_type) void model_init(struct Model* model, struct Static_Mesh* mesh, const char* geometry_name, int material_type)
{ {
assert(model && material_type > -1 && material_type < MAT_MAX); assert(model && material_type > -1 && material_type < MAT_MAX);
@ -32,14 +32,15 @@ void model_init(struct Model* model, const char* geometry_name, int material_typ
} }
model->geometry_index = geo_index; model->geometry_index = geo_index;
if(!material_register_model(model, material_type)) struct Material* material = &game_state_get()->renderer->materials[material_type];
if(!material_register_static_mesh(material, mesh))
{ {
log_error("model:create", "Unable to register model with Unshaded material, component not added"); log_error("model:create", "Unable to register model with Unshaded material, component not added");
model_reset(model); model_reset(model, mesh);
} }
} }
void model_reset(struct Model* model) void model_reset(struct Model* model, struct Static_Mesh* mesh)
{ {
assert(model); assert(model);
geom_remove(model->geometry_index); geom_remove(model->geometry_index);
@ -48,6 +49,6 @@ void model_reset(struct Model* model)
if(model->material) if(model->material)
{ {
struct Material* material = &renderer->materials[model->material->type]; struct Material* material = &renderer->materials[model->material->type];
material_unregister_model(material, model); material_unregister_static_mesh(material, mesh);
} }
} }

@ -3,7 +3,7 @@
struct Model; struct Model;
void model_init(struct Model* model, const char* geometry_name, int material_type); void model_init(struct Model* model, struct Static_Mesh* mesh, const char* geometry_name, int material_type);
void model_reset(struct Model* model); void model_reset(struct Model* model, struct Static_Mesh* mesh);
#endif #endif

@ -41,6 +41,18 @@ void renderer_init(struct Renderer* renderer)
glCullFace(GL_BACK); glCullFace(GL_BACK);
platform->windowresize_callback_set(on_framebuffer_size_change); platform->windowresize_callback_set(on_framebuffer_size_change);
gui_init(); gui_init();
struct Hashmap* cvars = platform->config.get();
renderer->settings.fog.mode = hashmap_int_get(cvars, "fog_mode");
renderer->settings.fog.density = hashmap_float_get(cvars, "fog_density");
renderer->settings.fog.start_dist = hashmap_float_get(cvars, "fog_start_dist");
renderer->settings.fog.max_dist = hashmap_float_get(cvars, "fog_max_dist");
renderer->settings.fog.color = hashmap_vec3_get(cvars, "fog_color");
renderer->settings.debug_draw_enabled = hashmap_bool_get(cvars, "debug_draw_enabled");
renderer->settings.debug_draw_physics = hashmap_bool_get(cvars, "debug_draw_physics");
renderer->settings.debug_draw_mode = hashmap_int_get(cvars, "debug_draw_mode");
renderer->settings.debug_draw_color = hashmap_vec4_get(cvars, "debug_draw_color");
renderer->settings.ambient_light = hashmap_vec3_get(cvars, "ambient_light");
/* Quad geometry for final render */ /* Quad geometry for final render */
vec3* vertices = array_new(vec3); vec3* vertices = array_new(vec3);
@ -156,20 +168,17 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
{ {
/* for each material, get all the registered models and render them */ /* for each material, get all the registered models and render them */
struct Material* material = &renderer->materials[i]; struct Material* material = &renderer->materials[i];
if(array_len(material->registered_models) == 0)
continue;
GL_CHECK(shader_bind(material->shader)); GL_CHECK(shader_bind(material->shader));
if(material->lit) /* Set light information */ if(material->lit) /* Set light information */
{ {
char uniform_name[MAX_UNIFORM_NAME_LEN]; char uniform_name[MAX_UNIFORM_NAME_LEN];
memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN); memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN);
int light_count = 0; int light_count = -1;
for(int i = 0; i < MAX_LIGHTS; i++) for(int i = 0; i < MAX_LIGHTS; i++)
{ {
struct Light* light = &scene->lights[i]; /* TODO: Cull lights according to camera frustum */ struct Light* light = &scene->lights[i]; /* TODO: Cull lights according to camera frustum */
if(!light->base.active) continue; if(!light->base.active && light->valid) continue;
light_count++; light_count++;
vec3 light_pos = {0, 0, 0}; vec3 light_pos = {0, 0, 0};
@ -221,6 +230,7 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN); memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN);
} }
light_count++; // this variable is going to be used for looping an array so increase its length by one
GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_TOTAL_LIGHTS].type, material->pipeline_params[MPP_TOTAL_LIGHTS].location, &light_count)); GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_TOTAL_LIGHTS].type, material->pipeline_params[MPP_TOTAL_LIGHTS].location, &light_count));
vec3 camera_pos = {0, 0, 0}; vec3 camera_pos = {0, 0, 0};
transform_get_absolute_position(&camera->base, &camera_pos); transform_get_absolute_position(&camera->base, &camera_pos);
@ -237,10 +247,12 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
if(material->lit) GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_VIEW_MAT].type, material->pipeline_params[MPP_VIEW_MAT].location, &camera->view_mat)); if(material->lit) GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_VIEW_MAT].type, material->pipeline_params[MPP_VIEW_MAT].location, &camera->view_mat));
for(int j = 0; j < array_len(material->registered_models); j++) for(int j = 0; j < MAX_MATERIAL_REGISTERED_STATIC_MESHES; j++)
{ {
if(!material->registered_static_meshes[j]) continue;
/* for each registered model, set up uniforms and render */ /* for each registered model, set up uniforms and render */
struct Static_Mesh* mesh = material->registered_models[i]; struct Static_Mesh* mesh = material->registered_static_meshes[j];
struct Geometry* geometry = geom_get(mesh->model.geometry_index); struct Geometry* geometry = geom_get(mesh->model.geometry_index);
/* Check if model is in frustum */ /* Check if model is in frustum */
@ -262,12 +274,12 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
/* set material params for the model */ /* set material params for the model */
for(int k = 0; k < MMP_MAX; k++) for(int k = 0; k < MMP_MAX; k++)
{ {
switch(mesh->model.material_params[i].type) switch(mesh->model.material_params[k].type)
{ {
case VT_INT: GL_CHECK(shader_set_uniform(material->model_params[i].type, material->model_params[i].location, &mesh->model.material_params[i].val_int)); break; case VT_INT: GL_CHECK(shader_set_uniform(material->model_params[k].type, material->model_params[k].location, &mesh->model.material_params[k].val_int)); break;
case VT_FLOAT: GL_CHECK(shader_set_uniform(material->model_params[i].type, material->model_params[i].location, &mesh->model.material_params[i].val_float)); break; case VT_FLOAT: GL_CHECK(shader_set_uniform(material->model_params[k].type, material->model_params[k].location, &mesh->model.material_params[k].val_float)); break;
case VT_VEC3: GL_CHECK(shader_set_uniform(material->model_params[i].type, material->model_params[i].location, &mesh->model.material_params[i].val_vec3)); break; case VT_VEC3: GL_CHECK(shader_set_uniform(material->model_params[k].type, material->model_params[k].location, &mesh->model.material_params[k].val_vec3)); break;
case VT_VEC4: GL_CHECK(shader_set_uniform(material->model_params[i].type, material->model_params[i].location, &mesh->model.material_params[i].val_vec4)); break; case VT_VEC4: GL_CHECK(shader_set_uniform(material->model_params[k].type, material->model_params[k].location, &mesh->model.material_params[k].val_vec4)); break;
} }
} }
@ -279,6 +291,7 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
if(material->lit) if(material->lit)
{ {
GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_VIEW_MAT].type, material->pipeline_params[MPP_VIEW_MAT].location, &camera->view_mat)); GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_VIEW_MAT].type, material->pipeline_params[MPP_VIEW_MAT].location, &camera->view_mat));
GL_CHECK(shader_set_uniform(material->pipeline_params[MPP_MODEL_MAT].type, material->pipeline_params[MPP_MODEL_MAT].location, &mesh->base.transform.trans_mat));
mat4 inv_mat; mat4 inv_mat;
mat4_identity(&inv_mat); mat4_identity(&inv_mat);
mat4_inverse(&inv_mat, &mesh->base.transform.trans_mat); mat4_inverse(&inv_mat, &mesh->base.transform.trans_mat);
@ -437,7 +450,7 @@ void renderer_cleanup(struct Renderer* renderer)
void on_framebuffer_size_change(int width, int height) void on_framebuffer_size_change(int width, int height)
{ {
struct Scene* scene = &game_state_get()->scene; struct Scene* scene = game_state_get()->scene;
float aspect = (float)width / (float)height; float aspect = (float)width / (float)height;
for(int i = 0; i < MAX_CAMERAS; i++) for(int i = 0; i < MAX_CAMERAS; i++)
{ {

@ -7,6 +7,7 @@
#include "../common/common.h" #include "../common/common.h"
#include "../common/parser.h" #include "../common/parser.h"
#include "model.h" #include "model.h"
#include "light.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -47,7 +48,7 @@ void scene_init(struct Scene* scene)
scene->cameras[i].base.id = i; scene->cameras[i].base.id = i;
} }
scene->active_camera_index = CT_EDITOR; scene->active_camera_index = CAM_EDITOR;
} }
bool scene_load(struct Scene* scene, const char* filename, int dir_type); bool scene_load(struct Scene* scene, const char* filename, int dir_type);
@ -273,22 +274,22 @@ struct Camera* scene_camera_create(struct Scene* scene, const char* name, struct
struct Static_Model* scene_static_mesh_create(struct Scene* scene, const char* name, struct Entity* parent, const char* geometry_name, int material_type) struct Static_Model* scene_static_mesh_create(struct Scene* scene, const char* name, struct Entity* parent, const char* geometry_name, int material_type)
{ {
assert(scene); assert(scene);
struct Static_Mesh* new_static_model = NULL; struct Static_Mesh* new_static_mesh = NULL;
for(int i = 0; i < MAX_STATIC_MESHES; i++) for(int i = 0; i < MAX_STATIC_MESHES; i++)
{ {
struct Static_Mesh* static_model = &scene->static_meshes[i]; struct Static_Mesh* static_mesh = &scene->static_meshes[i];
if(!static_model->base.active) if(!static_mesh->base.active)
{ {
new_static_model = static_model; new_static_mesh = static_mesh;
break; break;
} }
} }
if(new_static_model) if(new_static_mesh)
{ {
entity_init(&new_static_model->base, name, parent); entity_init(&new_static_mesh->base, name, parent);
new_static_model->base.type = ET_STATIC_MODEL; new_static_mesh->base.type = ET_STATIC_MODEL;
model_init(&new_static_model->model, geometry_name, material_type); model_init(&new_static_mesh->model, new_static_mesh, geometry_name, material_type);
// TODO: handle creating collision mesh for the model at creation // TODO: handle creating collision mesh for the model at creation
} }
else else
@ -296,7 +297,7 @@ struct Static_Model* scene_static_mesh_create(struct Scene* scene, const char* n
log_error("scene:model_create", "Max model limit reached!"); log_error("scene:model_create", "Max model limit reached!");
} }
return new_static_model; return new_static_mesh;
} }
struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char* name, struct Entity* parent, const char* filename, int type, bool loop, bool play) struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char* name, struct Entity* parent, const char* filename, int type, bool loop, bool play)
@ -400,7 +401,7 @@ void scene_static_mesh_remove(struct Scene* scene, struct Static_Mesh* mesh)
if(mesh->collision.collision_shape) platform->physics.cs_remove(mesh->collision.collision_shape); if(mesh->collision.collision_shape) platform->physics.cs_remove(mesh->collision.collision_shape);
if(mesh->collision.rigidbody) platform->physics.body_remove(mesh->collision.rigidbody); if(mesh->collision.rigidbody) platform->physics.body_remove(mesh->collision.rigidbody);
model_reset(&mesh->model); model_reset(&mesh->model, mesh);
scene_entity_remove(scene, &mesh->base); scene_entity_remove(scene, &mesh->base);
} }

Loading…
Cancel
Save