作者:i_dovelemon
日期:2016 / 07 / 02
来源:CSDN
主题:Render to Texture, Post process, Glare, Glow, Multi-pass rendering
void glb_display() {
glb_draw_normal_scene();
glb_draw_render_target_gauss_blur_h();
glb_draw_render_target_gauss_blur_v();
glb_draw_blend();
glutSwapBuffers();
}
light_pixel.vs
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Phong lights's vertex shader
//--------------------------------------------------------------------
#version 330
in vec3 vertex;
in vec3 normal;
uniform mat4 proj;
uniform mat4 view;
uniform mat4 world;
uniform mat4 trans_inv_world;
out vec3 vs_vertex;
out vec3 vs_normal;
void main() {
gl_Position = proj * view * world * vec4(vertex, 1.0);
vs_vertex = (world * vec4(vertex, 1.0)).xyz;
vs_normal = (trans_inv_world * vec4(normal, 0.0)).xyz;
}
light_pixel.ps
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Phong lights's pixel shader
//-------------------------------------------------------------------
#version 330
in vec3 vs_vertex;
in vec3 vs_normal;
out vec4 color;
uniform vec3 parallel_light_dir;
uniform vec3 parallel_light_ambient;
uniform vec3 parallel_light_diffuse;
uniform vec3 parallel_light_specular;
uniform vec3 material_ambient;
uniform vec3 material_diffuse;
uniform vec3 material_specular;
uniform float material_specular_pow;
uniform vec3 eye_pos;
vec3 calc_diffuse(vec3 light_vec, vec3 normal, vec3 diffuse, vec3 light_color) {
float ratio = dot(light_vec, normal);
ratio = max(ratio, 0.0);
return diffuse * light_color * ratio;
}
vec3 calc_specular(vec3 light_vec, vec3 normal, vec3 view_vec, vec3 specular, vec3 light_color, float pow_value) {
vec3 ref_light_vec = reflect(light_vec, normal);
float ratio = dot(ref_light_vec, view_vec);
ratio = max(ratio, 0.0);
ratio = pow(ratio, pow_value);
return specular * light_color * ratio;
}
void main() {
vec3 light_vec = -parallel_light_dir;
vec3 normal = normalize(vs_normal);
vec3 view_vec = normalize(eye_pos - vs_vertex);
vec3 ambient = material_ambient * parallel_light_ambient;
vec3 diffuse = calc_diffuse(light_vec, normal, material_diffuse, parallel_light_diffuse);
vec3 specular = calc_specular(light_vec, normal, view_vec, material_specular, parallel_light_specular, material_specular_pow);
color = vec4(ambient + diffuse + specular, 1.0);
}
gauss_blur.vs
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Gauss blur pass through vertex shader
//--------------------------------------------------------------------
#version 330
in vec3 vertex;
in vec2 texcoord;
out vec2 vs_texcoord;
void main() {
gl_Position = vec4(vertex, 1.0);
vs_texcoord = texcoord;
}
gauss_blurh.ps
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Gauss blur horizontal pass shader
//--------------------------------------------------------------------
#version 330
in vec2 vs_texcoord;
out vec4 color;
uniform sampler2D tex;
uniform float tex_width;
uniform float gauss_num[21];
void main() {
color = texture2D(tex, vs_texcoord) * gauss_num[0];
float step = 1.0 / tex_width;
for (int i = 1; i < 21; i++) {
if (vs_texcoord.x - i * step >= 0.0) {
color += texture2D(tex, vec2(vs_texcoord.x - i * step, vs_texcoord.y)) * gauss_num[i];
}
if (vs_texcoord.x + i * step <= 1.0) {
color += texture2D(tex, vec2(vs_texcoord.x + i * step, vs_texcoord.y)) * gauss_num[i];
}
}
}
gauss_blurv.ps
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Gauss blur vertical pass shader
//--------------------------------------------------------------------
#version 330
in vec2 vs_texcoord;
out vec4 color;
uniform sampler2D tex;
uniform float tex_height;
uniform float gauss_num[21];
void main() {
color = texture2D(tex, vs_texcoord) * gauss_num[0];
float step = 1.0 / tex_height;
for (int i = 0; i <21; i++) {
if (vs_texcoord.y - i * step >= 0.0) {
color += texture2D(tex, vec2(vs_texcoord.x, vs_texcoord.y - i * step)) * gauss_num[i];
}
if (vs_texcoord.y + i * step <= 1.0) {
color += texture2D(tex, vec2(vs_texcoord.x, vs_texcoord.y + i * step)) * gauss_num[i];
}
}
}
float glb_gauss_num(int x) {
float pi = 3.1415927f;
float e = 2.71828f;
float theta = 0.1f;
float theta2 = theta * theta;
float temp1 = 1.0f / (theta * sqrt(2 * pi));
float temp2 = pow(e, -(x * x) / 2 * theta2);
return temp1 * temp2;
}
void glb_calc_gauss_nums() {
g_GaussNum[0] = 1.0f;
for (int32_t i = 1; i < sizeof(g_GaussNum) / sizeof(g_GaussNum[0]); i++) {
g_GaussNum[i] = glb_gauss_num(i);
}
float total = 0.0f;
for (int32_t i = 0; i < sizeof(g_GaussNum) / sizeof(g_GaussNum[0]); i++) {
total += g_GaussNum[i];
}
for (int32_t i = 0; i < sizeof(g_GaussNum) / sizeof(g_GaussNum[0]); i++) {
g_GaussNum[i] = g_GaussNum[i] / total;
}
}
blend.vs
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Blend pass through vertex shader
//--------------------------------------------------------------------
#version 330
in vec3 vertex;
in vec2 texcoord;
out vec2 vs_texcoord;
void main() {
gl_Position = vec4(vertex, 1.0);
vs_texcoord = texcoord;
}
blend.ps
//--------------------------------------------------------------------
// Declaration: Copyright (c), by i_dovelemon, 2016. All right reserved.
// Author: i_dovelemon[[email protected]]
// Date: 2016 / 06 / 29
// Brief: Alpha blend shader
//--------------------------------------------------------------------
#version 330
in vec2 vs_texcoord;
out vec4 color;
uniform sampler2D blur_tex;
uniform sampler2D scene_tex;
void main() {
vec4 blur_color = texture2D(blur_tex, vs_texcoord);
vec4 scene_color = texture2D(scene_tex, vs_texcoord);
color = blur_color * 0.5 + scene_color * 0.5;
}