Cameras can now be marked for automatic resize on window resize event

dev
Shariq Shah 9 years ago
parent 58b1c59eaa
commit 6775ebc612
  1. 6
      orgfile.org
  2. 39
      src/camera.c
  3. 2
      src/camera.h
  4. 2
      src/editor.c
  5. 25
      src/game.c
  6. 4
      src/model.c
  7. 29
      src/renderer.c

@ -109,6 +109,9 @@ x Font atlas proper cleanup
- Custom rendering for gui - Custom rendering for gui
** TODO Allow passsing base path as commandline argument? ** TODO Allow passsing base path as commandline argument?
** TODO Sprite sheet animations ** TODO Sprite sheet animations
** TODO Replace orgfile with simple text readme and reduce duplication?
** DONE Camera resize on window reisze
- State "DONE" from "TODO" [2017-03-20 Mon 15:22]
** DONE Resizable framebuffers and textures ** DONE Resizable framebuffers and textures
- State "DONE" from "TODO" [2017-03-16 Thu 22:50] - State "DONE" from "TODO" [2017-03-16 Thu 22:50]
** DONE Support for multiple color attachments in framebuffers? ** DONE Support for multiple color attachments in framebuffers?
@ -122,6 +125,9 @@ x Font atlas proper cleanup
** TODO Editor ** TODO Editor
** TODO Event Subsystem ** TODO Event Subsystem
** TODO Keybindings for gui? ** TODO Keybindings for gui?
** TODO Textual/Binary format for data serialization and persistance
** TODO Better logging
** TODO Hatching/Ink rendering style
** DONE Compile and test on windows ** DONE Compile and test on windows
- State "DONE" from "TODO" [2017-03-14 Tue 00:32] - State "DONE" from "TODO" [2017-03-14 Tue 00:32]
** TODO Array based string type comptible with cstring(char*) ** TODO Array based string type comptible with cstring(char*)

@ -40,9 +40,10 @@ void camera_remove(int index)
if(index > -1 && index < array_len(camera_list)) if(index > -1 && index < array_len(camera_list))
{ {
struct Camera* camera = &camera_list[index]; struct Camera* camera = &camera_list[index];
if(camera->fbo != -1) framebuffer_remove(camera->fbo); if(camera->fbo != -1) framebuffer_remove(camera->fbo);
if(camera->render_tex != -1) texture_remove(camera->render_tex); if(camera->render_tex != -1) texture_remove(camera->render_tex);
if(camera->depth_tex != -1) texture_remove(camera->depth_tex); if(camera->depth_tex != -1) texture_remove(camera->depth_tex);
camera->resizeable = 0;
camera->fbo = camera->render_tex = camera->depth_tex = camera->node = -1; camera->fbo = camera->render_tex = camera->depth_tex = camera->node = -1;
array_push(empty_indices, index, int); array_push(empty_indices, index, int);
} }
@ -71,16 +72,17 @@ int camera_create(int node, int width, int height)
new_camera = array_grow(camera_list, struct Camera); new_camera = array_grow(camera_list, struct Camera);
index = array_len(camera_list) - 1; index = array_len(camera_list) - 1;
} }
new_camera->fbo = -1; new_camera->fbo = -1;
new_camera->render_tex = -1; new_camera->render_tex = -1;
new_camera->depth_tex = -1; new_camera->depth_tex = -1;
new_camera->node = node; new_camera->node = node;
new_camera->farz = 1000.f; new_camera->farz = 1000.f;
new_camera->nearz = 0.1f; new_camera->nearz = 0.1f;
new_camera->fov = 60.f; new_camera->fov = 60.f;
new_camera->ortho = 0;
new_camera->resizeable = 1;
float aspect_ratio = (float)width / (float)height; float aspect_ratio = (float)width / (float)height;
new_camera->aspect_ratio = aspect_ratio <= 0.f ? (4.f / 3.f) : aspect_ratio; new_camera->aspect_ratio = aspect_ratio <= 0.f ? (4.f / 3.f) : aspect_ratio;
new_camera->ortho = 0;
mat4_identity(&new_camera->view_mat); mat4_identity(&new_camera->view_mat);
mat4_identity(&new_camera->proj_mat); mat4_identity(&new_camera->proj_mat);
mat4_identity(&new_camera->view_proj_mat); mat4_identity(&new_camera->view_proj_mat);
@ -98,10 +100,10 @@ void camera_update_view_proj(struct Camera* camera)
void camera_update_view(struct Camera* camera) void camera_update_view(struct Camera* camera)
{ {
struct Entity* entity = entity_get(camera->node); struct Entity* entity = entity_get(camera->node);
struct Transform* transform = entity_component_get(entity, C_TRANSFORM); struct Transform* transform = entity_component_get(entity, C_TRANSFORM);
vec3 lookat = {0.f, 0.f, 0.f}; vec3 lookat = {0.f, 0.f, 0.f};
vec3 up = {0.f, 0.f, 0.f}; vec3 up = {0.f, 0.f, 0.f};
vec3 position = {0.f, 0.f, 0.f}; vec3 position = {0.f, 0.f, 0.f};
transform_get_absolute_lookat(transform, &lookat); transform_get_absolute_lookat(transform, &lookat);
transform_get_absolute_up(transform, &up); transform_get_absolute_up(transform, &up);
@ -265,3 +267,16 @@ static void update_frustum(struct Camera* camera)
vec4_scale(&camera->frustum[i], &camera->frustum[i], (1.f / length)); vec4_scale(&camera->frustum[i], &camera->frustum[i], (1.f / length));
} }
} }
void camera_resize_all(int width, int height)
{
for(int i = 0; i < array_len(camera_list); i++)
{
struct Camera* camera = &camera_list[i];
if(!camera->resizeable) continue;
float aspect = (float)width / (float)height;
camera->aspect_ratio = aspect > 0.f ? aspect : 4.f / 3.f;
camera_update_proj(camera);
}
}

@ -19,6 +19,7 @@ struct Camera
int depth_tex; int depth_tex;
vec4 clear_color; vec4 clear_color;
vec4 frustum[6]; vec4 frustum[6];
int resizeable;
}; };
struct Camera* camera_get(int index); struct Camera* camera_get(int index);
@ -38,5 +39,6 @@ void camera_attach_fbo(struct Camera* camera,
int has_color, int has_color,
int resizeable); int resizeable);
void camera_set_primary_viewer(struct Camera* camera); void camera_set_primary_viewer(struct Camera* camera);
void camera_resize_all(int width, int height);
#endif #endif

@ -31,7 +31,7 @@ static void editor_color_combo(struct nk_context* context, vec4* color, int widt
void editor_init(void) void editor_init(void)
{ {
editor_state.enabled = 1; editor_state.enabled = 1;
editor_state.renderer_settings_window = 1; editor_state.renderer_settings_window = 0;
editor_state.top_panel_height = 30; editor_state.top_panel_height = 30;
} }

@ -160,16 +160,19 @@ void scene_setup(void)
transform_set_position(ground_tran, &pos); transform_set_position(ground_tran, &pos);
transform_scale(ground_tran, &scale_ground); transform_scale(ground_tran, &scale_ground);
struct Entity* screen = scene_add_new("Screen", NULL); /* struct Entity* screen = scene_add_new("Screen", NULL); */
struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); /* struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); */
screen_model->geometry_index = geom_find("Quad"); /* screen_model->geometry_index = geom_find("Quad"); */
struct Entity* screen_camera = scene_add_as_child("Screen_Camera", NULL, screen->node); /* struct Entity* screen_camera = scene_add_as_child("Screen_Camera", NULL, screen->node); */
struct Transform* screen_camera_tran = entity_component_get(screen_camera, C_TRANSFORM); /* struct Transform* screen_camera_tran = entity_component_get(screen_camera, C_TRANSFORM); */
transform_rotate(screen_camera_tran, &UNIT_Y, 180.f, TS_WORLD); /* transform_rotate(screen_camera_tran, &UNIT_Y, 180.f, TS_WORLD); */
struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 50, 50); /* struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 50, 50); */
camera_attach_fbo(cam, 128, 128, 1, 1, 0); /* cam->nearz = 0.1f; */
model_set_material_param(screen_model, "diffuse_color", &color); /* cam->farz = 50.f; */
model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); /* camera_update_proj(cam); */
/* camera_attach_fbo(cam, 128, 128, 1, 1, 0); */
/* model_set_material_param(screen_model, "diffuse_color", &color); */
/* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */
const int MAX_LIGHTS = 3; const int MAX_LIGHTS = 3;
for(int i = 0; i < MAX_LIGHTS; i++) for(int i = 0; i < MAX_LIGHTS; i++)
@ -355,7 +358,7 @@ void update(float dt, int* window_should_close)
if(input_key_state_get(KEY_F1, KS_PRESSED)) editor_toggle(); if(input_key_state_get(KEY_F1, KS_PRESSED)) editor_toggle();
debug(dt); debug(dt);
debug_gui(dt); //debug_gui(dt);
editor_update(dt); editor_update(dt);
} }

@ -267,8 +267,8 @@ void model_render_all(struct Camera* camera, enum Geometry_Draw_Mode draw_mode)
} }
/* Render the geometry */ /* Render the geometry */
//geom_render_in_frustum(model->geometry_index, &camera->frustum[0], transform); geom_render_in_frustum(model->geometry_index, &camera->frustum[0], transform, draw_mode);
geom_render(model->geometry_index, draw_mode); //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++)
{ {

@ -177,29 +177,6 @@ void renderer_draw(void)
texture_unbind(final_render_tex); texture_unbind(final_render_tex);
shader_unbind(); shader_unbind();
/* Debug Pass */
/* shader_bind(debug_shader); */
/* { */
/* glEnable(GL_DEPTH_TEST); */
/* glEnable(GL_CULL_FACE); */
/* glCullFace(GL_BACK); */
/* static vec3 wireframe_color = {0, 1, 0}; */
/* static mat4 mvp; */
/* shader_set_uniform_vec3(debug_shader, "wireframe_color", &wireframe_color); */
/* struct Model* model_list = model_get_all(); */
/* for(int i = 0; i < array_len(model_list); i++) */
/* { */
/* struct Model* model = &model_list[i]; */
/* struct Entity* entity = entity_get(model->node); */
/* struct Transform* transform = entity_component_get(entity, C_TRANSFORM); */
/* int geometry = model->geometry_index; */
/* mat4_identity(&mvp); */
/* mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat); */
/* shader_set_uniform_mat4(debug_shader, "mvp", &mvp); */
/* geom_render(geometry, GDM_LINES); */
/* } */
/* } */
/* shader_unbind(); */
if(settings.debug_draw_enabled) model_render_all_debug(active_camera, debug_shader, settings.debug_draw_mode, &settings.debug_draw_color); if(settings.debug_draw_enabled) model_render_all_debug(active_camera, debug_shader, settings.debug_draw_mode, &settings.debug_draw_color);
gui_render(NK_ANTI_ALIASING_ON, settings.max_gui_vertex_memory, settings.max_gui_element_memory); gui_render(NK_ANTI_ALIASING_ON, settings.max_gui_vertex_memory, settings.max_gui_element_memory);
@ -216,11 +193,7 @@ void renderer_cleanup(void)
void on_framebuffer_size_change(int width, int height) void on_framebuffer_size_change(int width, int height)
{ {
glViewport(0, 0, width, height); camera_resize_all(width, height);
struct Camera* camera = camera_get(0); /* TODO: This is an ugly hack, remove it as soon as possible */
float aspect = (float)width / (float)height;
camera->aspect_ratio = aspect > 0.f ? aspect : 4.f / 3.f;
camera_update_proj(camera);
framebuffer_resize_all(width, height); framebuffer_resize_all(width, height);
} }

Loading…
Cancel
Save