dev
Shariq Shah 9 years ago
parent 4d52269b68
commit 80a1f36bbd
  1. 7
      assets/shaders/blinn_phong.frag
  2. 3
      assets/shaders/commonFrag.glsl
  3. 44
      assets/shaders/fog.glsl
  4. 19
      assets/shaders/unshaded.frag
  5. 8
      src/game.c
  6. 53
      src/material.c
  7. 31
      src/model.c
  8. 13
      src/renderer.c
  9. 28
      src/renderer.h

@ -1,4 +1,4 @@
//include common.glsl commonFrag.glsl version.glsl //include fog.glsl common.glsl commonFrag.glsl version.glsl
struct Light struct Light
{ {
@ -21,8 +21,6 @@ const int LT_POINT = 2;
const int MAX_LIGHTS = 128; const int MAX_LIGHTS = 128;
uniform vec3 camera_pos;
uniform sampler2D diffuse_texture; uniform sampler2D diffuse_texture;
uniform Light lights[MAX_LIGHTS]; uniform Light lights[MAX_LIGHTS];
uniform int total_active_lights; uniform int total_active_lights;
@ -128,6 +126,5 @@ void main()
light_contribution += calc_spot_light(lights[i]); light_contribution += calc_spot_light(lights[i]);
} }
frag_color = (albedo_color * vec4(0.1, 0.1, 0.1, 1.0)) + frag_color = apply_fog((albedo_color * vec4(light_contribution + ambient_light, 1.0)));
(albedo_color * vec4(light_contribution, 1.0));
} }

@ -8,3 +8,6 @@ in vec3 vertex;
// Fragment Shader Output // Fragment Shader Output
//out vec4 frag_color; //out vec4 frag_color;
uniform vec3 ambient_light;
uniform vec3 camera_pos;

@ -1,41 +1,41 @@
struct Fog struct Fog
{ {
int fogMode; int mode;
float density; float density;
float start; float start_dist;
float max; float max_dist;
vec4 color; vec3 color;
}; };
uniform Fog fog; uniform Fog fog;
const int FOG_NONE = 0; const int FM_NONE = 0;
const int FOG_LINEAR = 1; const int FM_LINEAR = 1;
const int FOG_EXPONENTIAL = 2; const int FM_EXPONENTIAL = 2;
const int FOG_EXPONENTIAL_SQRD = 3; const int FM_EXPONENTIAL_SQRD = 3;
vec4 applyFog(vec4 color) vec4 apply_fog(vec4 color)
{ {
vec4 finalColor = color; vec4 final_color = color;
if(fog.fogMode != FOG_NONE) if(fog.mode != FM_NONE)
{ {
float fogFactor; float fog_factor;
float distFromEye = abs(length(vertex - eyePos)); float dist_from_eye = abs(length(vertex - camera_pos));
if(fog.fogMode == FOG_LINEAR) if(fog.mode == FM_LINEAR)
{ {
fogFactor = (fog.max - distFromEye) / (fog.max - fog.start); fog_factor = (fog.max_dist - dist_from_eye) / (fog.max_dist - fog.start_dist);
} }
else if(fog.fogMode == FOG_EXPONENTIAL) else if(fog.mode == FM_EXPONENTIAL)
{ {
fogFactor = exp(fog.density * -distFromEye); fog_factor = exp(fog.density * -dist_from_eye);
} }
else if(fog.fogMode == FOG_EXPONENTIAL_SQRD) else if(fog.mode == FM_EXPONENTIAL_SQRD)
{ {
fogFactor = exp(-pow(fog.density * distFromEye, 2)); fog_factor = exp(-pow(fog.density * dist_from_eye, 2));
} }
fogFactor = clamp(fogFactor, 0.0, 1.0); fog_factor = clamp(fog_factor, 0.0, 1.0);
finalColor = mix(fog.color, color, fogFactor); final_color = mix(vec4(fog.color, 1.0), color, fog_factor);
} }
return finalColor; return final_color;
} }

@ -1,23 +1,10 @@
//include common.glsl commonFrag.glsl version.glsl //include fog.glsl common.glsl commonFrag.glsl version.glsl
uniform sampler2D diffuse_texture; uniform sampler2D diffuse_texture;
out vec4 gbuffer[4]; out vec4 frag_color;
void main() void main()
{ {
// gl_FragData[0] = diffuse_color * texture(diffuse_texture, uv); frag_color = apply_fog(diffuse_color * texture(diffuse_texture, uv));
// gl_FragData[1] = vec4(vertex.xyz, 1.0);
// gl_FragData[2] = vec4(normal.xyz, 1.0);
// gl_FragData[3] = vec4(uv, 0.0, 1.0);
gbuffer[0] = diffuse_color * texture(diffuse_texture, uv);
gbuffer[1] = vec4(vertex.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;
// gbuffer[1] = vertex.xyz;
// gbuffer[2] = normal.xyz;
// gbuffer[3] = vec3(uv, 0.0);
} }

@ -157,7 +157,7 @@ void scene_setup(void)
/* model_set_material_param(screen_model, "diffuse_color", &color); */ /* model_set_material_param(screen_model, "diffuse_color", &color); */
/* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */ /* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */
const int MAX_LIGHTS = 2; const int MAX_LIGHTS = 3;
for(int i = 0; i < MAX_LIGHTS; i++) for(int i = 0; i < MAX_LIGHTS; i++)
{ {
int x = rand() % MAX_LIGHTS; int x = rand() % MAX_LIGHTS;
@ -172,9 +172,9 @@ void scene_setup(void)
light_comp->intensity = 1.f; light_comp->intensity = 1.f;
} }
struct Entity* sun = scene_add_new("Sun", NULL); /* struct Entity* sun = scene_add_new("Sun", NULL); */
struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); /* struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); */
sun_light->intensity = 0.8f; /* sun_light->intensity = 0.8f; */
} }
void debug(float dt) void debug(float dt)

@ -42,13 +42,28 @@ void material_init(void)
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name); uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform); uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("view_mat"); uniform->name = str_new("fog.mode");
uniform->type = UT_MAT4; uniform->type = UT_INT;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name); uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform); uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("inv_model_mat"); uniform->name = str_new("fog.density");
uniform->type = UT_MAT4; uniform->type = UT_FLOAT;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("fog.start_dist");
uniform->type = UT_FLOAT;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("fog.max_dist");
uniform->type = UT_FLOAT;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("fog.color");
uniform->type = UT_VEC3;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name); uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
/* Material params */ /* Material params */
@ -95,6 +110,36 @@ void material_init(void)
uniform->type = UT_MAT4; uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name); 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("fog.mode");
uniform->type = UT_INT;
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("fog.density");
uniform->type = UT_FLOAT;
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("fog.start_dist");
uniform->type = UT_FLOAT;
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("fog.max_dist");
uniform->type = UT_FLOAT;
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("fog.color");
uniform->type = UT_VEC3;
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("ambient_light");
uniform->type = UT_VEC3;
uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name);
/* Material params */ /* Material params */
uniform = array_grow(blinn_phong_mat->model_params, struct Uniform); uniform = array_grow(blinn_phong_mat->model_params, struct Uniform);
uniform->name = str_new("diffuse_color"); uniform->name = str_new("diffuse_color");

@ -138,6 +138,7 @@ void model_render_all(struct Camera* camera)
} }
/* Set pipeline uniforms */ /* Set pipeline uniforms */
struct Render_Settings* render_settings = renderer_get_settings();
for(int k = 0; k < array_len(material->pipeline_params); k++) for(int k = 0; k < array_len(material->pipeline_params); k++)
{ {
/* TODO: change this into something better */ /* TODO: change this into something better */
@ -167,6 +168,36 @@ void model_render_all(struct Camera* camera)
shader_set_uniform(uniform->type, uniform->location, &inv_mat); shader_set_uniform(uniform->type, uniform->location, &inv_mat);
renderer_check_glerror("model:render_all:material_pipeline"); renderer_check_glerror("model:render_all:material_pipeline");
} }
else if(strcmp(uniform->name, "fog.mode") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &render_settings->fog.mode);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "fog.density") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &render_settings->fog.density);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "fog.start_dist") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &render_settings->fog.start_dist);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "fog.max_dist") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &render_settings->fog.max_dist);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "fog.color") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &render_settings->fog.color);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "ambient_light") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &render_settings->ambient_light);
renderer_check_glerror("model:render_all:material_pipeline");
}
} }
if(material->lit) /* Set light information */ if(material->lit) /* Set light information */

@ -21,6 +21,7 @@ static int def_albedo_tex = -1;
static int def_depth_tex = -1; static int def_depth_tex = -1;
static int quad_geo = -1; static int quad_geo = -1;
static int composition_shader = -1; static int composition_shader = -1;
static struct Render_Settings settings;
void on_framebuffer_size_change(GLFWwindow* window, int width, int height); void on_framebuffer_size_change(GLFWwindow* window, int width, int height);
@ -33,6 +34,13 @@ void renderer_init(GLFWwindow* window)
glCullFace(GL_BACK); glCullFace(GL_BACK);
glfwSetFramebufferSizeCallback(window, on_framebuffer_size_change); glfwSetFramebufferSizeCallback(window, on_framebuffer_size_change);
settings.fog.mode = FM_EXPONENTIAL;
settings.fog.density = 0.01f;
settings.fog.start_dist = 50.f;
settings.fog.max_dist = 150.f;
vec3_fill(&settings.fog.color, 60.f/255.f, 60.f/255.f, 75.f/255.f);
vec3_fill(&settings.ambient_light, 0.1f, 0.1f, 0.12f);
/* Quad geometry for final render */ /* Quad geometry for final render */
vec3* vertices = array_new(vec3); vec3* vertices = array_new(vec3);
vec2* uvs = array_new(vec2); vec2* uvs = array_new(vec2);
@ -200,3 +208,8 @@ int renderer_check_glerror(const char* context)
return error; return error;
} }
struct Render_Settings* renderer_get_settings(void)
{
return &settings;
}

@ -1,8 +1,36 @@
#ifndef renderer_H #ifndef renderer_H
#define renderer_H #define renderer_H
#include "linmath.h"
enum Fog_Mode
{
FM_NONE = 0,
FM_LINEAR = 1,
FM_EXPONENTIAL = 2,
FM_EXPONENTIAL_SQRD = 3
};
struct Fog
{
int mode;
float density;
float start_dist;
float max_dist;
vec3 color;
};
struct Render_Settings
{
struct Fog fog;
vec3 ambient_light;
};
typedef struct GLFWwindow GLFWwindow; typedef struct GLFWwindow GLFWwindow;
struct Render_Settings* renderer_get_settings(void);
void renderer_init(GLFWwindow* window); void renderer_init(GLFWwindow* window);
void renderer_draw(void); void renderer_draw(void);
void renderer_cleanup(void); void renderer_cleanup(void);

Loading…
Cancel
Save