Added separate clear color for each camera

dev
Shariq Shah 10 years ago
parent ed1edf44ce
commit 69122f081d
  1. 10
      src/camera.c
  2. 1
      src/camera.h
  3. 43
      src/game.c
  4. 6
      src/model.c
  5. 4
      src/renderer.c
  6. 7
      src/scene.c
  7. 2
      src/scene.h

@ -37,13 +37,19 @@ void camera_remove(int index)
{ {
if(index > -1 && index < array_len(camera_list)) if(index > -1 && index < array_len(camera_list))
{ {
camera_list->node = -1; struct Camera* camera = &camera_list[index];
if(camera->fbo != -1) framebuffer_remove(camera->fbo);
if(camera->render_tex != -1) texture_remove(camera->render_tex);
if(camera->depth_tex != -1) texture_remove(camera->depth_tex);
camera->fbo = camera->render_tex = camera->depth_tex = camera->node = -1;
array_push(empty_indices, index, int); array_push(empty_indices, index, int);
} }
} }
void camera_cleanup(void) void camera_cleanup(void)
{ {
for(int i = 0; i < array_len(camera_list); i++)
if(camera_list[i].node != -1) camera_remove(i);
array_free(camera_list); array_free(camera_list);
array_free(empty_indices); array_free(empty_indices);
} }
@ -78,7 +84,7 @@ int camera_create(int node, int width, int height)
mat4_identity(&new_camera->view_proj_mat); mat4_identity(&new_camera->view_proj_mat);
camera_update_view(new_camera); camera_update_view(new_camera);
camera_update_proj(new_camera); camera_update_proj(new_camera);
vec4_fill(&new_camera->clear_color, 1.f, 1.f, 1.f, 1.f);
return index; return index;
} }

@ -17,6 +17,7 @@ struct Camera
int fbo; int fbo;
int render_tex; int render_tex;
int depth_tex; int depth_tex;
vec4 clear_color;
}; };
struct Camera* camera_get(int index); struct Camera* camera_get(int index);

@ -89,6 +89,7 @@ void scene_setup(void)
render_height = 600; render_height = 600;
struct Camera* camera = entity_component_add(player, C_CAMERA, render_width, render_height); struct Camera* camera = entity_component_add(player, C_CAMERA, render_width, render_height);
camera_attach_fbo(camera, render_width, render_height, 1, 1); camera_attach_fbo(camera, render_width, render_height, 1, 1);
vec4_fill(&camera->clear_color, 0.3f, 0.6f, 0.9f, 1.0f);
camera_set_primary_viewer(camera); camera_set_primary_viewer(camera);
struct Entity* new_ent = scene_add_new("Model_Entity", NULL); struct Entity* new_ent = scene_add_new("Model_Entity", NULL);
@ -97,28 +98,38 @@ void scene_setup(void)
vec4 color = {1.f, 1.f, 1.f, 1.f }; vec4 color = {1.f, 1.f, 1.f, 1.f };
transform_translate(tran, &position, TS_WORLD); transform_translate(tran, &position, TS_WORLD);
struct Model* box_model = entity_component_add(new_ent, C_MODEL, "default.pamesh"); struct Model* box_model = entity_component_add(new_ent, C_MODEL, "default.pamesh");
model_set_material_param(box_model, "diffuse_color", &color);
struct Transform* model_tran = entity_component_get(new_ent, C_TRANSFORM); struct Transform* model_tran = entity_component_get(new_ent, C_TRANSFORM);
vec3 scale = {1, 1, 2}; vec3 scale = {1, 1, 2};
transform_scale(model_tran, &scale); transform_scale(model_tran, &scale);
struct Entity* suz = scene_add_as_child("Suzanne", NULL, new_ent); struct Entity* suz = scene_add_as_child("Suzanne", NULL, new_ent->node);
struct Model* suz_model = entity_component_add(suz, C_MODEL, "suzanne.pamesh"); struct Model* suz_model = entity_component_add(suz, C_MODEL, "suzanne.pamesh");
model_set_material_param(suz_model, "diffuse_color", &color);
struct Transform* s_tran = entity_component_get(suz, C_TRANSFORM); struct Transform* s_tran = entity_component_get(suz, C_TRANSFORM);
vec3 s_pos = {3, 0, 0}; vec3 s_pos = {3, 0, 0};
transform_translate(s_tran, &s_pos, TS_WORLD); transform_translate(s_tran, &s_pos, TS_WORLD);
struct Entity* ground = scene_add_new("Ground", NULL); struct Entity* ground = scene_add_new("Ground", NULL);
struct Model* ground_model = entity_component_add(ground, C_MODEL, "plane.pamesh"); struct Model* ground_model = entity_component_add(ground, C_MODEL, "plane.pamesh");
model_set_material_param(ground_model, "diffuse_color", &color);
struct Transform* ground_tran = entity_component_get(ground, C_TRANSFORM); struct Transform* ground_tran = entity_component_get(ground, C_TRANSFORM);
vec3 pos = {0, -3, -3}; vec3 pos = {0, -3, -3};
vec3 scale_ground = {0.5f, 0.5f, 3.f}; vec3 scale_ground = {0.5f, 0.5f, 3.f};
transform_set_position(ground_tran, &pos); transform_set_position(ground_tran, &pos);
transform_scale(ground_tran, &scale_ground); transform_scale(ground_tran, &scale_ground);
/* Set material params */ struct Entity* screen = scene_add_new("Screen", NULL);
model_set_material_param(ground_model, "diffuse_color", &color); struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL);
model_set_material_param(suz_model, "diffuse_color", &color); screen_model->geometry_index = geom_find("Quad");
model_set_material_param(box_model, "diffuse_color", &color); 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);
vec3 screen_cam_tran = {0, -5, 5};
transform_translate(screen_camera_tran, &screen_camera_tran, TS_PARENT);
struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 1024, 1024);
camera_attach_fbo(cam, 1024, 1024, 1, 1);
model_set_material_param(screen_model, "diffuse_color", &color);
model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex);
} }
void debug(float dt) void debug(float dt)
@ -212,7 +223,7 @@ void debug(float dt)
if(input_key_state_get(GLFW_KEY_SPACE, GLFW_PRESS)) if(input_key_state_get(GLFW_KEY_SPACE, GLFW_PRESS))
{ {
struct Entity* model = entity_get(2); struct Entity* model = scene_find("Screen");
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
vec3 x_axis = {1, 0, 0}; vec3 x_axis = {1, 0, 0};
transform_rotate(mod_tran, &x_axis, 25.f * dt, TS_WORLD); transform_rotate(mod_tran, &x_axis, 25.f * dt, TS_WORLD);
@ -220,22 +231,22 @@ void debug(float dt)
if(input_key_state_get(GLFW_KEY_M, GLFW_PRESS)) if(input_key_state_get(GLFW_KEY_M, GLFW_PRESS))
{ {
struct Entity* model = entity_get(2); struct Entity* model = scene_find("Screen");
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
vec3 y_axis = {0, 0, 1}; //vec3 y_axis = {0, 0, 1};
transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_LOCAL); //transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_LOCAL);
vec3 amount = {0, 0, -5 * dt};
transform_translate(mod_tran, &amount, TS_LOCAL);
} }
if(input_key_state_get(GLFW_KEY_N, GLFW_PRESS)) if(input_key_state_get(GLFW_KEY_N, GLFW_PRESS))
{ {
/* struct Entity* model = entity_get(3); */ struct Entity* model = scene_find("Screen");
/* struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); */
/* vec3 amount = {0, 0, -5 * dt}; */
/* transform_translate(mod_tran, amount, TS_LOCAL); */
struct Entity* model = entity_get(2);
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
vec3 y_axis = {0, 0, 1}; /* vec3 y_axis = {0, 0, 1}; */
transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_WORLD); /* transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_WORLD); */
vec3 amount = {0, 0, 5 * dt};
transform_translate(mod_tran, &amount, TS_LOCAL);
} }
} }

@ -37,7 +37,8 @@ void model_init(void)
int model_create(int node, const char* geo_name) int model_create(int node, const char* geo_name)
{ {
assert(geo_name); /* if no name is given for geometry, use default */
if(!geo_name) geo_name = "default.pamesh";
int geo_index = geom_create_from_file(geo_name); int geo_index = geom_create_from_file(geo_name);
int index = -1; int index = -1;
struct Model* new_model = NULL; struct Model* new_model = NULL;
@ -196,7 +197,8 @@ int model_set_material_param(struct Model* model, const char* name, void* value)
mat4_assign((mat4*)param->value, (mat4*)value); mat4_assign((mat4*)param->value, (mat4*)value);
break; break;
case UT_TEX: case UT_TEX:
log_message("Not implemented yet!"); log_message("Tex Val : %d", *((int*)value));
*((int*)param->value) = *((int*)value);
break; break;
default: default:
log_error("model:set_material_param", "Invalid parameter type"); log_error("model:set_material_param", "Invalid parameter type");

@ -110,6 +110,10 @@ void renderer_draw(void)
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD); glBlendEquation(GL_FUNC_ADD);
glClearColor(camera->clear_color.x,
camera->clear_color.y,
camera->clear_color.z,
camera->clear_color.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
model_render_all(camera); model_render_all(camera);
glDisable(GL_BLEND); glDisable(GL_BLEND);

@ -18,14 +18,15 @@ void scene_init(void)
struct Entity* scene_add_new(const char* name, const char* tag) struct Entity* scene_add_new(const char* name, const char* tag)
{ {
return scene_add_as_child(name, tag, scene_get_root()); return scene_add_as_child(name, tag, root_node);
} }
struct Entity* scene_add_as_child(const char* name, const char* tag, struct Entity* parent) struct Entity* scene_add_as_child(const char* name, const char* tag, int parent_node)
{ {
assert(parent); assert(parent_node > -1);
struct Entity* new_entity = NULL; struct Entity* new_entity = NULL;
new_entity = entity_create(name, tag); new_entity = entity_create(name, tag);
struct Entity* parent = entity_get(parent_node);
new_entity->parent = parent->node; new_entity->parent = parent->node;
array_push(parent->children, new_entity->node, int); array_push(parent->children, new_entity->node, int);
struct Transform* new_ent_tran = entity_component_get(new_entity, C_TRANSFORM); struct Transform* new_ent_tran = entity_component_get(new_entity, C_TRANSFORM);

@ -8,7 +8,7 @@ void scene_remove(struct Entity* entity);
void scene_reset_parent(struct Entity* entity, struct Entity* new_parent); void scene_reset_parent(struct Entity* entity, struct Entity* new_parent);
void scene_cleanup(void); void scene_cleanup(void);
struct Entity* scene_add_new(const char* name, const char* tag); /* Add as child of Root */ struct Entity* scene_add_new(const char* name, const char* tag); /* Add as child of Root */
struct Entity* scene_add_as_child(const char* name, const char* tag, struct Entity* parent); struct Entity* scene_add_as_child(const char* name, const char* tag, int parent_node);
struct Entity* scene_find(const char* name); struct Entity* scene_find(const char* name);
struct Entity* scene_get_root(void); struct Entity* scene_get_root(void);
struct Entity* scene_get_child_by_name(struct Entity* parent, const char* name); struct Entity* scene_get_child_by_name(struct Entity* parent, const char* name);

Loading…
Cancel
Save