diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag new file mode 100644 index 0000000..a582303 --- /dev/null +++ b/assets/shaders/blinn_phong.frag @@ -0,0 +1,87 @@ +//include common.glsl commonFrag.glsl version.glsl + +struct Light +{ + vec3 position; + float outer_angle; + float inner_angle; + float falloff; + float intensity; + vec4 color; + uint pcf_enabled; + int type; + int radius; + float depth_bias; +}; + +const int LT_SPOT = 0; +const int LT_DIR = 1; +const int LT_POINT = 2; + +const int MAX_LIGHTS = 128; + +uniform vec3 camera_pos; + +uniform sampler2D diffuse_texture; +uniform Light lights[MAX_LIGHTS]; +uniform int total_active_lights; + +uniform float specular; +uniform float diffuse; +uniform float specular_strength; + +out vec4 frag_color; + +vec4 calc_point_light(in Light point_light) +{ + vec4 diffuse_comp = vec4(0.0); + vec4 specular_comp = vec4(0.0); + vec3 light_direction = vertex - point_light.position; + float dist = abs(length(light_direction)); + + if(dist <= point_light.radius) + { + light_direction = normalize(light_direction); + vec3 normalized_normal = normalize(normal); + float cos_ang_incidence = dot(normalized_normal, -light_direction); + cos_ang_incidence = clamp(cos_ang_incidence, 0, 1); + + if(cos_ang_incidence > 0) + { + diffuse_comp = point_light.color * diffuse * cos_ang_incidence; + vec3 vertex_to_eye = normalize(camera_pos - vertex); + vec3 light_reflect = normalize(reflect(light_direction, normalized_normal)); + float specular_factor = max(0.0, dot(vertex_to_eye, light_reflect)); + specular_factor = pow(specular_factor, specular_strength); + specular_comp = point_light.color * specular * specular_factor; + } + float attenuation = pow(max(0.0, (1.0 - (dist / point_light.radius))), point_light.falloff + 1.0f); + return (((diffuse_comp + specular_comp) * attenuation) * point_light.intensity); + } + else + { + return vec4(0.0); + } +} + +void main() +{ + vec4 albedo_color = diffuse_color * texture(diffuse_texture, uv); + vec4 light_contribution = vec4(0.0, 0.0, 0.0, 1.0); + + for(int i = 0; i < total_active_lights; i++) + { + if(i == total_active_lights) break; + + // if(lights[i].type == LT_POINT) + light_contribution += calc_point_light(lights[i]); + } + + frag_color = (albedo_color * vec4(0.1, 0.1, 0.1, 1.0)) + + (albedo_color * light_contribution); + //frag_color = lights[0].color; + //frag_color = vec4(lights[0].intensity, 0.0, 0.0, 1.0); + //frag_color = vec4(lights[0].position, 1.0); + //frag_color = vec4(camera_pos, 1.0); + //frag_color = vec4(total_active_lights); +} diff --git a/assets/shaders/blinn_phong.vert b/assets/shaders/blinn_phong.vert new file mode 100644 index 0000000..482a834 --- /dev/null +++ b/assets/shaders/blinn_phong.vert @@ -0,0 +1,7 @@ +//include commonVert.glsl version.glsl + +void main() +{ + gl_Position = transformPosition(vPosition); + setOutputs(); +} diff --git a/assets/shaders/common.glsl b/assets/shaders/common.glsl index 6e2de33..61ee441 100644 --- a/assets/shaders/common.glsl +++ b/assets/shaders/common.glsl @@ -2,4 +2,4 @@ // Common Uniforms // uniform vec4 ambientLight; // uniform vec3 eyePos; -uniform vec4 diffuse_color; \ No newline at end of file +uniform vec4 diffuse_color; diff --git a/assets/shaders/commonVert.glsl b/assets/shaders/commonVert.glsl index c4b7dfe..be16844 100644 --- a/assets/shaders/commonVert.glsl +++ b/assets/shaders/commonVert.glsl @@ -27,6 +27,8 @@ void setOutputs() //Normal and vertex sent to the fragment shader should be in the same space! normal = vec4(model_mat * vec4(vNormal, 0.0)).xyz; vertex = vec4(model_mat * vec4(vPosition, 1.0)).xyz; + // normal = vNormal; + // vertex = vPosition; vertCamSpace = vec4(view_mat * vec4(vPosition, 1.0)).xyz; vertLightSpace = vec4((lightVPMat * model_mat) * vec4(vPosition, 1.0)); } diff --git a/assets/shaders/fbo.frag b/assets/shaders/fbo.frag index 2784e90..ccc071e 100644 --- a/assets/shaders/fbo.frag +++ b/assets/shaders/fbo.frag @@ -4,17 +4,13 @@ in vec2 uv; out vec4 frag_color; -//uniform sampler2D sampler; uniform sampler2D albedo_map; -uniform sampler2D position_map; -uniform sampler2D normal_map; -uniform sampler2D uv_map; +//uniform sampler2D light_map; void main() { - //frag_color = texture2D(sampler, uv); - frag_color = texture(albedo_map, uv); - frag_color = texture(position_map, uv); - frag_color = texture(normal_map, uv); - frag_color = texture(uv_map, uv); + vec4 albedo_color = texture(albedo_map, uv); + //frag_color = albedo_color * texture(light_map, uv); + //frag_color += (albedo_color * vec4(0.2, 0.2, 0.2, 1.0)); + frag_color = albedo_color; } diff --git a/assets/shaders/unshaded.frag b/assets/shaders/unshaded.frag index 4b8a9a6..517a975 100644 --- a/assets/shaders/unshaded.frag +++ b/assets/shaders/unshaded.frag @@ -13,7 +13,7 @@ void main() gbuffer[0] = diffuse_color * texture(diffuse_texture, uv); gbuffer[1] = vec4(vertex.xyz, 1.0); - gbuffer[2] = vec4(normal.xyz, 1.0); + gbuffer[2] = vec4(normalize(normal.xyz), 1.0); gbuffer[3] = vec4(uv, 0.0, 1.0); // gbuffer[0] = (diffuse_color * texture(diffuse_texture, uv)).xyz; diff --git a/src/components.h b/src/components.h index 2c3d4b0..0829320 100644 --- a/src/components.h +++ b/src/components.h @@ -6,6 +6,7 @@ enum Component C_TRANSFORM = 0, C_MODEL, C_CAMERA, + C_LIGHT, C_RIGIDBODY, MAX_COMPONENTS }; @@ -18,6 +19,7 @@ inline static const char* comp_to_str(enum Component component) case C_TRANSFORM : str = "TRANSFORM"; break; case C_MODEL : str = "MODEL"; break; case C_CAMERA : str = "CAMERA"; break; + case C_LIGHT : str = "LIGHT"; break; case C_RIGIDBODY : str = "RIGIDBODY"; break; case MAX_COMPONENTS : str = "MAX_COMPONENTS"; break; } diff --git a/src/entity.c b/src/entity.c index 2ac3757..bc735d7 100644 --- a/src/entity.c +++ b/src/entity.c @@ -4,6 +4,7 @@ #include "string_utils.h" #include "transform.h" #include "camera.h" +#include "light.h" #include "model.h" #include @@ -126,6 +127,7 @@ int entity_component_remove(struct Entity* entity, enum Component component) case C_TRANSFORM: log_error("entity:remove_component", "Cannot remove TRANSFORM"); break; case C_MODEL: if(comp_index != -1) model_remove(comp_index); break; case C_CAMERA: if(comp_index != -1) camera_remove(comp_index); break; + case C_LIGHT: if(comp_index != -1) light_remove(comp_index); break; case C_RIGIDBODY: break; default: @@ -150,6 +152,7 @@ void* entity_component_get(struct Entity* entity, enum Component component) case C_TRANSFORM: comp_obj = transform_get(comp_index); break; case C_MODEL: comp_obj = model_get(comp_index); break; case C_CAMERA: comp_obj = camera_get(comp_index); break; + case C_LIGHT: comp_obj = light_get(comp_index); break; case C_RIGIDBODY: break; default: log_error("entity:component_get", "Invalid component type"); break; @@ -178,7 +181,8 @@ void* entity_component_add(struct Entity* entity, enum Component component, ...) case C_MODEL: { const char* filename = va_arg(args, const char*); - new_comp_index = model_create(entity->node, filename); + const char* material_name = va_arg(args, const char*); + new_comp_index = model_create(entity->node, filename, material_name); new_comp = model_get(new_comp_index); } break; @@ -190,6 +194,13 @@ void* entity_component_add(struct Entity* entity, enum Component component, ...) new_comp = camera_get(new_comp_index); } break; + case C_LIGHT: + { + int light_type = va_arg(args, int); + new_comp_index = light_create(entity->node, light_type); + new_comp = light_get(new_comp_index); + } + break; case C_RIGIDBODY: break; default: diff --git a/src/framebuffer.c b/src/framebuffer.c index d7f9e7e..27cc0c0 100644 --- a/src/framebuffer.c +++ b/src/framebuffer.c @@ -1,6 +1,5 @@ #include "framebuffer.h" #include "array.h" -#include "num_types.h" #include "renderer.h" #include "log.h" #include "texture.h" @@ -161,3 +160,9 @@ int framebuffer_get_texture(int index) assert(index < array_len(fbo_list) && index > -1); return fbo_list[index].texture; } + +uint framebuffer_get_gl_handle(int index) +{ + assert(index < array_len(fbo_list) && index > -1); + return fbo_list[index].handle; +} diff --git a/src/framebuffer.h b/src/framebuffer.h index 9347571..6b49398 100644 --- a/src/framebuffer.h +++ b/src/framebuffer.h @@ -1,6 +1,8 @@ #ifndef framebuffer_H #define framebuffer_H +#include "num_types.h" + void framebuffer_init(void); void framebuffer_cleanup(void); int framebuffer_create(int width, int height, int has_depth, int has_color); @@ -11,5 +13,6 @@ int framebuffer_get_width(int index); int framebuffer_get_height(int index); void framebuffer_set_texture(int index, int texture, int attachment); int framebuffer_get_texture(int index); +uint framebuffer_get_gl_handle(int index); #endif diff --git a/src/game.c b/src/game.c index 2777813..cff2ea9 100644 --- a/src/game.c +++ b/src/game.c @@ -70,6 +70,7 @@ void scene_setup(void) int turn_left_keys[1] = {'J'}; int turn_up_keys[1] = {'I'}; int turn_down_keys[1] = {'K'}; + int sprint_keys[2] = {GLFW_KEY_LEFT_SHIFT, GLFW_KEY_RIGHT_SHIFT}; input_map_create("Move_Forward", forward_keys, 2); input_map_create("Move_Backward", backward_keys, 2); input_map_create("Move_Up", up_keys, 1); @@ -80,12 +81,13 @@ void scene_setup(void) input_map_create("Turn_Left", turn_left_keys, 1); input_map_create("Turn_Up", turn_up_keys, 1); input_map_create("Turn_Down", turn_down_keys, 1); + input_map_create("Sprint", sprint_keys, 2); struct Entity* player = scene_add_new("player", "None"); player_node = player->node; - vec3 viewer_pos = {0, 0, 10}; + vec3 viewer_pos = {5, 4, 20}; struct Transform* viewer_tran = entity_component_get(player, C_TRANSFORM); - struct Model* player_model = entity_component_add(player, C_MODEL, "sphere.pamesh"); + struct Model* player_model = entity_component_add(player, C_MODEL, "sphere.pamesh", NULL); vec4 color = {0.f, 1.f, 1.f, 1.f }; model_set_material_param(player_model, "diffuse_color", &color); vec4_fill(&color, 1.f, 1.f, 1.f, 1.f); @@ -102,30 +104,30 @@ void scene_setup(void) struct Transform* tran = entity_component_get(new_ent, C_TRANSFORM); vec3 position = {0, 0, -5}; 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", "Blinn_Phong"); model_set_material_param(box_model, "diffuse_color", &color); struct Transform* model_tran = entity_component_get(new_ent, C_TRANSFORM); vec3 scale = {1, 1, 2}; transform_scale(model_tran, &scale); 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", "Blinn_Phong"); model_set_material_param(suz_model, "diffuse_color", &color); struct Transform* s_tran = entity_component_get(suz, C_TRANSFORM); vec3 s_pos = {3, 0, 0}; transform_translate(s_tran, &s_pos, TS_WORLD); 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, "default.pamesh", "Blinn_Phong"); model_set_material_param(ground_model, "diffuse_color", &color); struct Transform* ground_tran = entity_component_get(ground, C_TRANSFORM); vec3 pos = {0, -3, -3}; - vec3 scale_ground = {20.f, 10.f, 20.f}; + vec3 scale_ground = {200.f, 0.2f, 200.f}; transform_set_position(ground_tran, &pos); transform_scale(ground_tran, &scale_ground); struct Entity* screen = scene_add_new("Screen", NULL); - struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL); + struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); screen_model->geometry_index = geom_find("Quad"); 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); @@ -134,6 +136,20 @@ void scene_setup(void) 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); + + const int MAX_LIGHTS = 10; + for(int i = 0; i < MAX_LIGHTS; i++) + { + int x = rand() % MAX_LIGHTS; + int z = rand() % MAX_LIGHTS; + x++; z++; + struct Entity* light_ent = scene_add_new("Light_Ent", NULL); + struct Transform* light_tran = entity_component_get(light_ent, C_TRANSFORM); + vec3 lt_pos = {x * 2, 3, z * 2}; + transform_set_position(light_tran, <_pos); + struct Light* light_comp = entity_component_add(light_ent, C_LIGHT, LT_POINT); + vec4_fill(&light_comp->color, 1.f / (float)x, 1, 1.f / (float)z, 1); + } } void debug(float dt) @@ -143,7 +159,7 @@ void debug(float dt) struct Camera* cam = entity_component_get(entity, C_CAMERA); camera_set_primary_viewer(cam); struct Transform* transform = entity_component_get(entity, C_TRANSFORM); - float move_speed = 5.f, turn_speed = 50.f; + float move_speed = 5.f, move_scale = 3.f, turn_speed = 50.f; vec3 offset = {0, 0, 0}; float turn_up_down = 0.f; float turn_left_right = 0.f; @@ -213,6 +229,7 @@ void debug(float dt) } /* Movement */ + if(input_map_state_get("Sprint", GLFW_PRESS)) move_speed *= move_scale; if(input_map_state_get("Move_Forward", GLFW_PRESS)) offset.z -= move_speed; if(input_map_state_get("Move_Backward", GLFW_PRESS)) offset.z += move_speed; if(input_map_state_get("Move_Left", GLFW_PRESS)) offset.x -= move_speed; diff --git a/src/light.c b/src/light.c index ff6f10b..915e226 100644 --- a/src/light.c +++ b/src/light.c @@ -4,6 +4,8 @@ static struct Light* light_list; static int* empty_indices; +static int* valid_light_indices; +static int max_lights; struct Light* light_get(int index) { @@ -20,8 +22,11 @@ struct Light* light_get_all(void) void light_init(void) { - light_list = array_new(struct Light); - empty_indices = array_new(int); + max_lights = 128; + light_list = array_new_cap(struct Light, max_lights); + for(int i = 0; i < max_lights; i++) light_list[i].valid = 0; + empty_indices = array_new_cap(int, max_lights); + valid_light_indices = array_new_cap(int, max_lights); } void light_cleanup(void) @@ -30,6 +35,7 @@ void light_cleanup(void) light_remove(i); array_free(light_list); array_free(empty_indices); + array_free(valid_light_indices); } void light_remove(int index) @@ -63,10 +69,34 @@ int light_create(int node, int light_type) new_light->depth_bias = 0.0005f; new_light->type = light_type; new_light->pcf_enabled = 0; - new_light->intensity = 1.f; + new_light->intensity = 0.5f; new_light->falloff = 1.5f; new_light->outer_angle = TO_RADIANS(30.f); new_light->inner_angle = TO_RADIANS(20.f); new_light->radius = 30; return index; } + +int light_get_max_lights(void) +{ + return max_lights; +} + +int* light_get_valid_indices(int* valid_light_count) +{ + /* First, get all the valid(active) lights, then sort them in the + order directional, point, spot + */ + int light_count = 0; + for(int i = 0; i < array_len(light_list); i++) + { + if(light_list[i].valid) + { + valid_light_indices[light_count] = i; + light_count++; + } + } + *valid_light_count = light_count; + + return valid_light_indices; +} diff --git a/src/light.h b/src/light.h index 89244a8..ea28063 100644 --- a/src/light.h +++ b/src/light.h @@ -37,5 +37,7 @@ void light_cleanup(void); void light_remove(int index); int light_create(int node, int light_type); void light_set_radius(struct Light* light, int radius); +int light_get_max_lights(void); +int* light_get_valid_indices(int* valid_light_count); #endif diff --git a/src/linmath.c b/src/linmath.c index 50017cd..95779b8 100644 --- a/src/linmath.c +++ b/src/linmath.c @@ -546,6 +546,141 @@ void mat4_assign(mat4* res, const mat4* m) memcpy(res->mat, m->mat, sizeof(float) * 16); } + +void mat4_inverse(mat4* res, mat4* mat) +{ + mat4 tmp; + float det; + int i; + + tmp.mat[0] = mat->mat[5] * mat->mat[10] * mat->mat[15] - + mat->mat[5] * mat->mat[11] * mat->mat[14] - + mat->mat[9] * mat->mat[6] * mat->mat[15] + + mat->mat[9] * mat->mat[7] * mat->mat[14] + + mat->mat[13] * mat->mat[6] * mat->mat[11] - + mat->mat[13] * mat->mat[7] * mat->mat[10]; + + tmp.mat[4] = -mat->mat[4] * mat->mat[10] * mat->mat[15] + + mat->mat[4] * mat->mat[11] * mat->mat[14] + + mat->mat[8] * mat->mat[6] * mat->mat[15] - + mat->mat[8] * mat->mat[7] * mat->mat[14] - + mat->mat[12] * mat->mat[6] * mat->mat[11] + + mat->mat[12] * mat->mat[7] * mat->mat[10]; + + tmp.mat[8] = mat->mat[4] * mat->mat[9] * mat->mat[15] - + mat->mat[4] * mat->mat[11] * mat->mat[13] - + mat->mat[8] * mat->mat[5] * mat->mat[15] + + mat->mat[8] * mat->mat[7] * mat->mat[13] + + mat->mat[12] * mat->mat[5] * mat->mat[11] - + mat->mat[12] * mat->mat[7] * mat->mat[9]; + + tmp.mat[12] = -mat->mat[4] * mat->mat[9] * mat->mat[14] + + mat->mat[4] * mat->mat[10] * mat->mat[13] + + mat->mat[8] * mat->mat[5] * mat->mat[14] - + mat->mat[8] * mat->mat[6] * mat->mat[13] - + mat->mat[12] * mat->mat[5] * mat->mat[10] + + mat->mat[12] * mat->mat[6] * mat->mat[9]; + + tmp.mat[1] = -mat->mat[1] * mat->mat[10] * mat->mat[15] + + mat->mat[1] * mat->mat[11] * mat->mat[14] + + mat->mat[9] * mat->mat[2] * mat->mat[15] - + mat->mat[9] * mat->mat[3] * mat->mat[14] - + mat->mat[13] * mat->mat[2] * mat->mat[11] + + mat->mat[13] * mat->mat[3] * mat->mat[10]; + + tmp.mat[5] = mat->mat[0] * mat->mat[10] * mat->mat[15] - + mat->mat[0] * mat->mat[11] * mat->mat[14] - + mat->mat[8] * mat->mat[2] * mat->mat[15] + + mat->mat[8] * mat->mat[3] * mat->mat[14] + + mat->mat[12] * mat->mat[2] * mat->mat[11] - + mat->mat[12] * mat->mat[3] * mat->mat[10]; + + tmp.mat[9] = -mat->mat[0] * mat->mat[9] * mat->mat[15] + + mat->mat[0] * mat->mat[11] * mat->mat[13] + + mat->mat[8] * mat->mat[1] * mat->mat[15] - + mat->mat[8] * mat->mat[3] * mat->mat[13] - + mat->mat[12] * mat->mat[1] * mat->mat[11] + + mat->mat[12] * mat->mat[3] * mat->mat[9]; + + tmp.mat[13] = mat->mat[0] * mat->mat[9] * mat->mat[14] - + mat->mat[0] * mat->mat[10] * mat->mat[13] - + mat->mat[8] * mat->mat[1] * mat->mat[14] + + mat->mat[8] * mat->mat[2] * mat->mat[13] + + mat->mat[12] * mat->mat[1] * mat->mat[10] - + mat->mat[12] * mat->mat[2] * mat->mat[9]; + + tmp.mat[2] = mat->mat[1] * mat->mat[6] * mat->mat[15] - + mat->mat[1] * mat->mat[7] * mat->mat[14] - + mat->mat[5] * mat->mat[2] * mat->mat[15] + + mat->mat[5] * mat->mat[3] * mat->mat[14] + + mat->mat[13] * mat->mat[2] * mat->mat[7] - + mat->mat[13] * mat->mat[3] * mat->mat[6]; + + tmp.mat[6] = -mat->mat[0] * mat->mat[6] * mat->mat[15] + + mat->mat[0] * mat->mat[7] * mat->mat[14] + + mat->mat[4] * mat->mat[2] * mat->mat[15] - + mat->mat[4] * mat->mat[3] * mat->mat[14] - + mat->mat[12] * mat->mat[2] * mat->mat[7] + + mat->mat[12] * mat->mat[3] * mat->mat[6]; + + tmp.mat[10] = mat->mat[0] * mat->mat[5] * mat->mat[15] - + mat->mat[0] * mat->mat[7] * mat->mat[13] - + mat->mat[4] * mat->mat[1] * mat->mat[15] + + mat->mat[4] * mat->mat[3] * mat->mat[13] + + mat->mat[12] * mat->mat[1] * mat->mat[7] - + mat->mat[12] * mat->mat[3] * mat->mat[5]; + + tmp.mat[14] = -mat->mat[0] * mat->mat[5] * mat->mat[14] + + mat->mat[0] * mat->mat[6] * mat->mat[13] + + mat->mat[4] * mat->mat[1] * mat->mat[14] - + mat->mat[4] * mat->mat[2] * mat->mat[13] - + mat->mat[12] * mat->mat[1] * mat->mat[6] + + mat->mat[12] * mat->mat[2] * mat->mat[5]; + + tmp.mat[3] = -mat->mat[1] * mat->mat[6] * mat->mat[11] + + mat->mat[1] * mat->mat[7] * mat->mat[10] + + mat->mat[5] * mat->mat[2] * mat->mat[11] - + mat->mat[5] * mat->mat[3] * mat->mat[10] - + mat->mat[9] * mat->mat[2] * mat->mat[7] + + mat->mat[9] * mat->mat[3] * mat->mat[6]; + + tmp.mat[7] = mat->mat[0] * mat->mat[6] * mat->mat[11] - + mat->mat[0] * mat->mat[7] * mat->mat[10] - + mat->mat[4] * mat->mat[2] * mat->mat[11] + + mat->mat[4] * mat->mat[3] * mat->mat[10] + + mat->mat[8] * mat->mat[2] * mat->mat[7] - + mat->mat[8] * mat->mat[3] * mat->mat[6]; + + tmp.mat[11] = -mat->mat[0] * mat->mat[5] * mat->mat[11] + + mat->mat[0] * mat->mat[7] * mat->mat[9] + + mat->mat[4] * mat->mat[1] * mat->mat[11] - + mat->mat[4] * mat->mat[3] * mat->mat[9] - + mat->mat[8] * mat->mat[1] * mat->mat[7] + + mat->mat[8] * mat->mat[3] * mat->mat[5]; + + tmp.mat[15] = mat->mat[0] * mat->mat[5] * mat->mat[10] - + mat->mat[0] * mat->mat[6] * mat->mat[9] - + mat->mat[4] * mat->mat[1] * mat->mat[10] + + mat->mat[4] * mat->mat[2] * mat->mat[9] + + mat->mat[8] * mat->mat[1] * mat->mat[6] - + mat->mat[8] * mat->mat[2] * mat->mat[5]; + + det = mat->mat[0] * tmp.mat[0] + mat->mat[1] * tmp.mat[4] + mat->mat[2] * tmp.mat[8] + mat->mat[3] * tmp.mat[12]; + + if (det == 0) { + return; + } + + det = 1.0 / det; + + for (i = 0; i < 16; i++) { + res->mat[i] = tmp.mat[i] * det; + } +} + + + + void quat_fill(quat* res, float x, float y, float z, float w) { res->x = x; diff --git a/src/linmath.h b/src/linmath.h index bfcadca..e507d14 100644 --- a/src/linmath.h +++ b/src/linmath.h @@ -110,6 +110,7 @@ void mat4_lookat(mat4* res, const vec3* eye, const vec3* center, const vec3* up_ void mat4_translate(mat4* res, float x, float y, float z); void mat4_mul(mat4* res, const mat4* mat1, const mat4* mat2); void mat4_identity(mat4* res); +void mat4_inverse(mat4* res, mat4* mat); /* quat */ float quat_get_roll(const quat* q); diff --git a/src/material.c b/src/material.c index e9bffc8..9390483 100644 --- a/src/material.c +++ b/src/material.c @@ -5,6 +5,7 @@ #include "log.h" #include "model.h" #include "texture.h" +#include "light.h" #include #include @@ -27,6 +28,7 @@ void material_init(void) unshaded_mat->model_params = array_new(struct Uniform); unshaded_mat->pipeline_params = array_new(struct Uniform); unshaded_mat->active = 1; + unshaded_mat->lit = 0; /* Pipeline params/uniforms */ struct Uniform* uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform); @@ -44,16 +46,75 @@ void material_init(void) uniform->type = UT_MAT4; uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name); - /* Materail params */ + /* Material params */ uniform = array_grow(unshaded_mat->model_params, struct Uniform); uniform->name = str_new("diffuse_color"); uniform->type = UT_VEC4; + vec4_fill(&uniform->d_vec4, 1.0f, 1.0f, 1.0f, 1.0f); uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name); uniform = array_grow(unshaded_mat->model_params, struct Uniform); uniform->name = str_new("diffuse_texture"); uniform->type = UT_TEX; + uniform->d_int = texture_find("default.tga"); uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name); + + /* Simple blinn_phong material */ + struct Material* blinn_phong_mat = array_grow(material_list, struct Material); + blinn_phong_mat->name = str_new("Blinn_Phong"); + blinn_phong_mat->shader = shader_create("blinn_phong.vert", "blinn_phong.frag"); + blinn_phong_mat->registered_models = array_new(int); + blinn_phong_mat->model_params = array_new(struct Uniform); + blinn_phong_mat->pipeline_params = array_new(struct Uniform); + blinn_phong_mat->active = 1; + blinn_phong_mat->lit = 1; + + /* Pipeline params/uniforms */ + uniform = array_grow(blinn_phong_mat->pipeline_params, struct Uniform); + uniform->name = str_new("mvp"); + uniform->type = UT_MAT4; + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + uniform = array_grow(blinn_phong_mat->pipeline_params, struct Uniform); + uniform->name = str_new("model_mat"); + uniform->type = UT_MAT4; + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + uniform = array_grow(blinn_phong_mat->pipeline_params, struct Uniform); + uniform->name = str_new("view_mat"); + uniform->type = UT_MAT4; + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + /* Material params */ + uniform = array_grow(blinn_phong_mat->model_params, struct Uniform); + uniform->name = str_new("diffuse_color"); + uniform->type = UT_VEC4; + vec4_fill(&uniform->d_vec4, 1.0f, 1.0f, 1.0f, 1.0f); + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + uniform = array_grow(blinn_phong_mat->model_params, struct Uniform); + uniform->name = str_new("diffuse_texture"); + uniform->type = UT_TEX; + uniform->d_int = texture_find("default.tga"); + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + uniform = array_grow(blinn_phong_mat->model_params, struct Uniform); + uniform->name = str_new("specular"); + uniform->type = UT_FLOAT; + uniform->d_float = 1.f; + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + uniform = array_grow(blinn_phong_mat->model_params, struct Uniform); + uniform->name = str_new("diffuse"); + uniform->type = UT_FLOAT; + uniform->d_float = 1.f; + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); + + uniform = array_grow(blinn_phong_mat->model_params, struct Uniform); + uniform->name = str_new("specular_strength"); + uniform->type = UT_FLOAT; + uniform->d_float = 50.f; + uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); } struct Material* material_get_all_materials(void) @@ -92,23 +153,23 @@ int material_register_model(struct Model* model, int model_index, const char* ma { case UT_INT: param->value = malloc(sizeof(int)); - *((int*)param->value) = -1; + *((int*)param->value) = uniform->d_int; break; case UT_FLOAT: param->value = malloc(sizeof(float)); - *((float*)param->value) = -1.f; + *((float*)param->value) = uniform->d_float; break; case UT_VEC2: param->value = malloc(sizeof(vec2)); - vec2_fill((vec2*)param->value, 0.f, 0.f); + vec2_assign((vec2*)param->value, &uniform->d_vec2); break; case UT_VEC3: param->value = malloc(sizeof(vec3)); - vec3_fill((vec3*)param->value, 0.f, 0.f, 0.f); + vec3_assign((vec3*)param->value, &uniform->d_vec3); break; case UT_VEC4: param->value = malloc(sizeof(vec4)); - vec4_fill((vec4*)param->value, 0.f, 0.f, 0.f, 0.f); + vec4_assign((vec4*)param->value, &uniform->d_vec4); break; case UT_MAT4: param->value = malloc(sizeof(mat4)); diff --git a/src/material.h b/src/material.h index 679f73c..4aae0e3 100644 --- a/src/material.h +++ b/src/material.h @@ -1,13 +1,23 @@ #ifndef material_H #define material_H +#include "linmath.h" + struct Model; struct Uniform { int location; - int type; char* name; + int type; + union /* Default values */ + { + vec2 d_vec2; + vec3 d_vec3; + vec4 d_vec4; + int d_int; + float d_float; + }; }; struct Material_Param @@ -22,6 +32,7 @@ struct Material int shader; int* registered_models; int active; + int lit; /* If material uses light information */ struct Uniform* model_params; /* uniforms related to models */ struct Uniform* pipeline_params; /* general uniforms like matrices etc */ }; diff --git a/src/model.c b/src/model.c index c82c142..549fd37 100644 --- a/src/model.c +++ b/src/model.c @@ -9,6 +9,7 @@ #include "texture.h" #include "renderer.h" #include "material.h" +#include "light.h" #include "GL/glew.h" #include "GLFW/glfw3.h" @@ -16,6 +17,7 @@ #include #include #include +#include static struct Model* model_list; static int* empty_indices; @@ -35,7 +37,7 @@ void model_init(void) empty_indices = array_new(int); } -int model_create(int node, const char* geo_name) +int model_create(int node, const char* geo_name, const char* material_name) { /* if no name is given for geometry, use default */ if(!geo_name) geo_name = "default.pamesh"; @@ -57,7 +59,7 @@ int model_create(int node, const char* geo_name) } new_model->node = node; new_model->geometry_index = geo_index; - if(!material_register_model(new_model, index, "Unshaded")) + if(!material_register_model(new_model, index, material_name ? material_name : "Unshaded")) { log_error("model:create", "Unable to register model with Unshaded material, component not added"); model_remove(index); @@ -156,8 +158,66 @@ void model_render_all(struct Camera* camera) renderer_check_glerror("model:render_all:material_pipeline"); } } + + if(material->lit) /* Set light information */ + { + int valid_light_count = 0; + int* light_index_list = light_get_valid_indices(&valid_light_count); + const int max_name_len = 64; + char uniform_name[max_name_len]; + memset(uniform_name, '\0', max_name_len); + for(int i = 0; i < valid_light_count; i++) + { + struct Light* light = light_get(light_index_list[i]); /* TODO: Cull lights according to camera frustum */ + struct Entity* light_entity = entity_get(light->node); + struct Transform* transform = entity_component_get(light_entity, C_TRANSFORM); + vec3 light_pos = {0, 0, 0}; + transform_get_absolute_pos(transform, &light_pos); + + 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].color", i); + shader_set_uniform_vec4(material->shader, uniform_name, &light->color); + 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].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); + + 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); + } + + shader_set_uniform_int(material->shader, "total_active_lights", valid_light_count); + struct Entity* camera_entity = entity_get(camera->node); + struct Transform* camera_tran = entity_component_get(camera_entity, C_TRANSFORM); + vec3 camera_pos = {0, 0, 0}; + transform_get_absolute_pos(camera_tran, &camera_pos); + shader_set_uniform_vec3(material->shader, "camera_pos", &camera_pos); + } + /* 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); + geom_render(model->geometry_index); for(int k = 0; k < array_len(model->material_params); k++) { diff --git a/src/model.h b/src/model.h index d8c115f..0c804c6 100644 --- a/src/model.h +++ b/src/model.h @@ -16,7 +16,7 @@ struct Model struct Model* model_get(int index); void model_init(void); -int model_create(int node, const char* geo_name); +int model_create(int node, const char* geo_name, const char* material_name); void model_remove(int index); void model_cleanup(void); void model_render_all(struct Camera* camera); diff --git a/src/renderer.c b/src/renderer.c index 6946137..462b1e3 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -12,8 +12,13 @@ #include "shader.h" #include "num_types.h" #include "window_system.h" +#include "light.h" +#include "entity.h" +#include "transform.h" static int def_fbo = -1; +static int light_fbo = -1; +static int light_tex = -1; static int def_albedo_tex = -1; static int def_position_tex = -1; static int def_normal_tex = -1; @@ -21,6 +26,7 @@ static int def_uv_tex = -1; static int def_depth_tex = -1; static int quad_geo = -1; static int fbo_shader = -1; +static int composition_shader = -1; void on_framebuffer_size_change(GLFWwindow* window, int width, int height); @@ -115,7 +121,7 @@ void renderer_init(GLFWwindow* window) texture_set_param(def_uv_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR); def_depth_tex = texture_create("def_depth_texture", - TU_DIFFUSE, + TU_SHADOWMAP4, width, height, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT32F, @@ -130,12 +136,28 @@ void renderer_init(GLFWwindow* window) def_fbo = framebuffer_create(width, height, 1, 1); framebuffer_set_texture(def_fbo, def_albedo_tex, GL_COLOR_ATTACHMENT0); - framebuffer_set_texture(def_fbo, def_position_tex, GL_COLOR_ATTACHMENT0 + 1); - framebuffer_set_texture(def_fbo, def_normal_tex, GL_COLOR_ATTACHMENT0 + 2); - framebuffer_set_texture(def_fbo, def_uv_tex, GL_COLOR_ATTACHMENT0 + 3); + /* framebuffer_set_texture(def_fbo, def_position_tex, GL_COLOR_ATTACHMENT0 + 1); */ + /* framebuffer_set_texture(def_fbo, def_normal_tex, GL_COLOR_ATTACHMENT0 + 2); */ + /* framebuffer_set_texture(def_fbo, def_uv_tex, GL_COLOR_ATTACHMENT0 + 3); */ framebuffer_set_texture(def_fbo, def_depth_tex, GL_DEPTH_ATTACHMENT); - - fbo_shader = shader_create("fbo.vert", "fbo.frag"); + + + light_tex = texture_create("light_tex", + TU_SHADOWMAP1, + width, height, + GL_RGB, + GL_RGB16F, + GL_FLOAT, + NULL); + texture_set_param(light_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + texture_set_param(light_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + texture_set_param(light_tex, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + texture_set_param(light_tex, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + light_fbo = framebuffer_create(width, height, 0, 1); + framebuffer_set_texture(light_fbo, light_tex, GL_COLOR_ATTACHMENT0); + + composition_shader = shader_create("fbo.vert", "fbo.frag"); + //fbo_shader = shader_create("deferred_light.vert", "deferred_light.frag"); } void renderer_draw(void) @@ -151,23 +173,22 @@ void renderer_draw(void) framebuffer_bind(fbo); { glViewport(0, 0, framebuffer_get_width(fbo), framebuffer_get_height(fbo)); - GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT0, - GL_COLOR_ATTACHMENT1, - GL_COLOR_ATTACHMENT2, - GL_COLOR_ATTACHMENT3}; - glDrawBuffers(4, &draw_buffers[0]); - //glDrawBuffer(GL_COLOR_ATTACHMENT0); + /* GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT0, */ + /* GL_COLOR_ATTACHMENT1, */ + /* GL_COLOR_ATTACHMENT2, */ + /* GL_COLOR_ATTACHMENT3}; */ + /* glDrawBuffers(4, &draw_buffers[0]); */ + glDrawBuffer(GL_COLOR_ATTACHMENT0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); - glEnable(GL_BLEND); - glBlendEquation(GL_FUNC_ADD); glClearColor(camera->clear_color.x, camera->clear_color.y, camera->clear_color.z, camera->clear_color.w); + glEnable(GL_CULL_FACE ); + glCullFace(GL_BACK); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); model_render_all(camera); - glDisable(GL_BLEND); } framebuffer_unbind(); } @@ -176,52 +197,148 @@ void renderer_draw(void) window_get_size(&width, &height); glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - shader_bind(fbo_shader); + shader_bind(composition_shader); - shader_set_uniform_int(fbo_shader, "albedo_map", (GL_TEXTURE0 + TU_DIFFUSE) - GL_TEXTURE0); + //shader_set_uniform_int(composition_shader, "albedo_map", (GL_TEXTURE0 + TU_DIFFUSE) - GL_TEXTURE0); texture_bind(def_albedo_tex); + + geom_render(quad_geo); + + shader_unbind(); + + texture_unbind(def_albedo_tex); + + /* int width, height; */ + /* window_get_size(&width, &height); */ + /* struct Camera* active_camera = camera_get_primary(); */ + /* struct Entity* camera_entity = entity_get(active_camera->node); */ + /* struct Transform* camera_tran = entity_component_get(camera_entity, C_TRANSFORM); */ + + /* framebuffer_bind(light_fbo); */ + /* { */ + /* glViewport(0, 0, width, height); */ + /* glClearColor(0.f, 0.f, 0.f, 1.0f); */ + /* glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); */ + /* //glEnable(GL_BLEND); */ + /* //glEnable(GL_DEPTH_TEST ); */ + /* /\* glDisable(GL_DEPTH_TEST ); *\/ */ + /* /\* glEnable(GL_CULL_FACE ); *\/ */ + /* /\* glCullFace(GL_BACK); *\/ */ + /* /\* glDepthFunc(GL_LEQUAL); *\/ */ + /* /\* glBlendEquation(GL_FUNC_ADD); *\/ */ + /* /\* glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); *\/ */ + + /* /\* uint gbuffer_fbo_handle = 0; *\/ */ + /* /\* gbuffer_fbo_handle = framebuffer_get_gl_handle(def_fbo); *\/ */ + /* /\* glBindFramebuffer(GL_READ_FRAMEBUFFER, gbuffer_fbo_handle); *\/ */ + /* /\* glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); *\/ */ + /* /\* glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST); *\/ */ + /* /\* glBindFramebuffer(GL_FRAMEBUFFER, 0); *\/ */ + + + /* shader_bind(fbo_shader); */ - shader_set_uniform_int(fbo_shader, "position_map", (GL_TEXTURE0 + TU_SHADOWMAP1) - GL_TEXTURE0); - texture_bind(def_position_tex); + /* vec3 camera_pos = {0, 0, 0}; */ + /* transform_get_absolute_pos(camera_tran, &camera_pos); */ + /* shader_set_uniform_vec3(fbo_shader, "camera_pos", &camera_pos); */ + + /* shader_set_uniform_int(fbo_shader, "albedo_map", (GL_TEXTURE0 + TU_DIFFUSE) - GL_TEXTURE0); */ + /* texture_bind(def_albedo_tex); */ + + /* shader_set_uniform_int(fbo_shader, "position_map", (GL_TEXTURE0 + TU_SHADOWMAP1) - GL_TEXTURE0); */ + /* texture_bind(def_position_tex); */ - shader_set_uniform_int(fbo_shader, "normal_map", (GL_TEXTURE0 + TU_SHADOWMAP2) - GL_TEXTURE0); - texture_bind(def_normal_tex); + /* shader_set_uniform_int(fbo_shader, "normal_map", (GL_TEXTURE0 + TU_SHADOWMAP2) - GL_TEXTURE0); */ + /* texture_bind(def_normal_tex); */ - shader_set_uniform_int(fbo_shader, "uv_map", (GL_TEXTURE0 + TU_SHADOWMAP3) - GL_TEXTURE0); - texture_bind(def_uv_tex); + /* /\* shader_set_uniform_int(fbo_shader, "depth_map", (GL_TEXTURE0 + TU_SHADOWMAP4) - GL_TEXTURE0); *\/ */ + /* /\* texture_bind(def_depth_tex); *\/ */ + + /* /\* shader_set_uniform_int(fbo_shader, "uv_map", (GL_TEXTURE0 + TU_SHADOWMAP3) - GL_TEXTURE0); *\/ */ + /* /\* texture_bind(def_uv_tex); *\/ */ + + /* struct Light* active_lights = light_get_all(); */ + /* int sphere_geo = geom_find("sphere.pamesh"); */ + /* mat4 mvp; */ + /* struct Transform sphere_tran; */ + + /* vec2 screen_size = {width, height}; */ + /* shader_set_uniform_vec2(fbo_shader, "screen_size", &screen_size); */ + + /* for(int i = 0; i < array_len(active_lights); i++) */ + /* { */ + /* if(i == 0) */ + /* glBlendFunc(GL_ZERO, GL_ONE); */ + /* else */ + /* glBlendFunc(GL_ONE, GL_ONE); */ + + /* /\* Set light uniform *\/ */ + /* struct Light* light = light_get(i); /\* TODO: Remove this and do it properly *\/ */ + /* struct Entity* light_entity = entity_get(light->node); */ + /* struct Transform* transform = entity_component_get(light_entity, C_TRANSFORM); */ + /* vec3 light_pos = {0, 0, 0}; */ + /* transform_get_absolute_pos(transform, &light_pos); */ + /* shader_set_uniform_vec3(fbo_shader, "light.position", &light_pos); */ + /* shader_set_uniform_vec4(fbo_shader, "light.color", &light->color); */ + /* shader_set_uniform_float(fbo_shader, "light.outer_angle", light->outer_angle); */ + /* shader_set_uniform_float(fbo_shader, "light.inner_angle", light->inner_angle); */ + /* shader_set_uniform_float(fbo_shader, "light.falloff", light->falloff); */ + /* shader_set_uniform_float(fbo_shader, "light.intensity", light->intensity); */ + /* shader_set_uniform_int(fbo_shader, "light.type", light->type); */ + /* shader_set_uniform_int(fbo_shader, "light.radius", light->radius); */ + + /* /\* vec2 planes = {active_camera->nearz, active_camera->farz}; *\/ */ + /* /\* //vec2 planes = {active_camera->farz, active_camera->nearz}; *\/ */ + /* /\* shader_set_uniform_vec2(fbo_shader, "planes", &planes); *\/ */ + + /* mat4_identity(&mvp); */ + /* transform_set_position(&sphere_tran, &light_pos); */ + /* vec3 light_scale = {light->radius, light->radius, light->radius}; */ + /* transform_scale(&sphere_tran, &light_scale); */ + /* mat4_mul(&mvp, &active_camera->view_proj_mat, &sphere_tran.trans_mat); */ + /* shader_set_uniform_mat4(fbo_shader, "mvp", &mvp); */ + /* /\* mat4 inv_proj_mat; *\/ */ + /* /\* mat4_identity(&inv_proj_mat); *\/ */ + /* /\* mat4_inverse(&inv_proj_mat, &active_camera->view_proj_mat); *\/ */ + /* /\* shader_set_uniform_mat4(fbo_shader, "inv_proj_mat", &inv_proj_mat); *\/ */ + /* geom_render(quad_geo); */ + /* //geom_render(sphere_geo); */ + /* } */ - geom_render(quad_geo); - //struct Camera* primary_camera = camera_get_primary(); - //texture_bind(primary_camera->render_tex); - /* glViewport(0, height / 2, width / 2, height / 2); */ - /* texture_bind(def_albedo_tex); */ - /* geom_render(quad_geo); */ - /* texture_unbind(def_albedo_tex); */ - - /* glViewport(width / 2, height / 2, width / 2, height / 2); */ - /* texture_bind(def_position_tex); */ - /* geom_render(quad_geo); */ - /* texture_unbind(def_position_tex); */ - - /* glViewport(0, 0, width / 2, height / 2); */ - /* texture_bind(def_normal_tex); */ - /* geom_render(quad_geo); */ - /* texture_unbind(def_normal_tex); */ - - /* glViewport(width / 2, 0, width / 2, height / 2); */ - /* texture_bind(def_depth_tex); */ - /* geom_render(quad_geo); */ - /* texture_unbind(def_depth_tex); */ + /* //struct Camera* primary_camera = camera_get_primary(); */ + /* //texture_bind(primary_camera->render_tex); */ + /* /\* glViewport(0, height / 2, width / 2, height / 2); *\/ */ + /* /\* texture_bind(def_albedo_tex); *\/ */ + /* /\* geom_render(quad_geo); *\/ */ + /* /\* texture_unbind(def_albedo_tex); *\/ */ + + /* /\* glViewport(width / 2, height / 2, width / 2, height / 2); *\/ */ + /* /\* texture_bind(def_position_tex); *\/ */ + /* /\* geom_render(quad_geo); *\/ */ + /* /\* texture_unbind(def_position_tex); *\/ */ + + /* /\* glViewport(0, 0, width / 2, height / 2); *\/ */ + /* /\* texture_bind(def_normal_tex); *\/ */ + /* /\* geom_render(quad_geo); *\/ */ + /* /\* texture_unbind(def_normal_tex); *\/ */ + + /* /\* glViewport(width / 2, 0, width / 2, height / 2); *\/ */ + /* /\* texture_bind(def_depth_tex); *\/ */ + /* /\* geom_render(quad_geo); *\/ */ + /* /\* texture_unbind(def_depth_tex); *\/ */ - shader_unbind(); + /* shader_unbind(); */ - texture_unbind(def_albedo_tex); - texture_unbind(def_position_tex); - texture_unbind(def_normal_tex); - texture_unbind(def_uv_tex); + + + /* texture_unbind(def_albedo_tex); */ + /* texture_unbind(def_position_tex); */ + /* texture_unbind(def_normal_tex); */ + /* texture_unbind(def_uv_tex); */ + /* glDisable(GL_BLEND); */ + /* } */ + /* framebuffer_unbind(); */ } void renderer_cleanup(void) diff --git a/src/transform.c b/src/transform.c index c078de1..34a6a7c 100644 --- a/src/transform.c +++ b/src/transform.c @@ -156,25 +156,28 @@ void transform_update_transmat(struct Transform* transform) mat4_mul(&transform->trans_mat, &transform->trans_mat, &scale); struct Entity* entity = entity_get(transform->node); - struct Entity* parent = entity_get(entity->parent); - if(parent) + if(entity) /* Only update if transform is attached to an entity */ { - struct Transform* parent_tran = entity_component_get(parent, C_TRANSFORM); - mat4_mul(&transform->trans_mat, &transform->trans_mat, &parent_tran->trans_mat); - } + struct Entity* parent = entity_get(entity->parent); + if(parent) + { + struct Transform* parent_tran = entity_component_get(parent, C_TRANSFORM); + mat4_mul(&transform->trans_mat, &transform->trans_mat, &parent_tran->trans_mat); + } - /* Update all children */ - if(array_len(entity->children) > 0) - { - for(int i = 0; i < array_len(entity->children); i++) + /* Update all children */ + if(array_len(entity->children) > 0) { - struct Entity* child = entity_get(entity->children[i]); - struct Transform* child_tran = entity_component_get(child, C_TRANSFORM); - transform_update_transmat(child_tran); + for(int i = 0; i < array_len(entity->children); i++) + { + struct Entity* child = entity_get(entity->children[i]); + struct Transform* child_tran = entity_component_get(child, C_TRANSFORM); + transform_update_transmat(child_tran); + } } - } - entity_sync_components(entity); + entity_sync_components(entity); + } } struct Transform* transform_get(int index)