Skip to content

Commit

Permalink
moblur and crispy lighting added, shaders updated
Browse files Browse the repository at this point in the history
  • Loading branch information
psastras committed Aug 18, 2011
1 parent d233e5a commit afdc212
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 83 deletions.
6 changes: 6 additions & 0 deletions README 100644 → 100755
@@ -0,0 +1,6 @@
OpenGL 4.0 Tessellation Test

Just playing around with the new tessellation shaders.

Requires OpenGL 4.x and a relatively new graphics card.

2 changes: 2 additions & 0 deletions Rocky.files
Expand Up @@ -26,3 +26,5 @@ shaders/normals.glsl
3rdparty/fftw/fftw3.h
src/glfftwater.h
src/glfftwater.cpp
shaders/post0.glsl
shaders/post1.glsl
7 changes: 2 additions & 5 deletions shaders/default.glsl
Expand Up @@ -21,10 +21,7 @@ void main(void) {
in vec3 pass_TexCoord;
out vec3 out_Color;
void main() {
vec3 color = texture(tex, pass_TexCoord.st).xyz;
out_Color = color;//vec4(abs(pass_TexCoord.s),abs(pass_TexCoord.t),0.0,0.0);
vec4 color = texture(tex, pass_TexCoord.st);
out_Color = color.xyz;
}
#endif


//vec3 color = texture(tex, vec3(pass_TexCoord.st, 1.0)).xxx;
10 changes: 6 additions & 4 deletions shaders/icosohedron.glsl
Expand Up @@ -58,16 +58,18 @@ out vec4 out_Color1;
vec4 atmosphere(vec3 pos) {
vec4 c0 = vec4(0.172, 0.290, 0.486, 1.000);
vec4 c1 = vec4(0.321, 0.482, 0.607, 1.000);
vec4 s0 = vec4(1.0, 1.0, 1.0, 1.0); //sun color
float d = length(pos - lightPos)*20.0;
vec4 s0 = vec4(5.0, 5.0, 5.0, 1.0) * 0.2; //sun color
float d = length(pos - lightPos)*100.0;
if(pos.y >= 0.0)
return mix(mix(c1,c0,pos.y), s0, clamp(1.0/pow(d,1.25), 0.0, 1.0));
return mix(mix(c1,c0,pos.y), s0, clamp(1.0/pow(d,1.1), 0.0, 1.0));
else
return mix(c1, s0, clamp(1.0/pow(d,1.25), 0.0, 1.0));
return mix(c1, s0, clamp(1.0/pow(d,1.1), 0.0, 1.0));
}

void main() {
out_Color0 = atmosphere(tePosition / 5000.0);
out_Color0.w = 1.0;
out_Color1 = vec4(tePosition.xyz, 0.0);
}

#endif
2 changes: 1 addition & 1 deletion shaders/perlin.glsl
Expand Up @@ -12,7 +12,7 @@ uniform int octaves;
uniform float lacunarity;
uniform float gain;
uniform float offset;
uniform float heightScale = 100.0;
uniform float heightScale = 120.0;
uniform vec2 scale; //the scale of the tile (in world space)
uniform vec2 offsets[8]; //the offsets of the tiles (in world space)

Expand Down
92 changes: 92 additions & 0 deletions shaders/post0.glsl
@@ -0,0 +1,92 @@
#version 400 core

uniform sampler2D tex;
uniform sampler2D posTex;
uniform mat4 modelviewMatrix;
uniform mat4 projMatrix;

uniform vec3 lightPos;
uniform mat4 modelviewMatrixPrev;
uniform mat4 projMatrixPrev;
uniform mat4 modelviewMatrixCurr;
uniform mat4 projMatrixCurr;
#ifdef _VERTEX_
in vec3 in_Position;
in vec3 in_Normal;
in vec3 in_TexCoord;
out vec3 pass_TexCoord;
void main(void) {
pass_TexCoord = in_TexCoord;
gl_Position = projMatrix * modelviewMatrix * vec4(in_Position,1.0);
}

#endif

#ifdef _FRAGMENT_
in vec3 pass_TexCoord;
out vec4 out_Color;

vec4 moblur(vec2 velocity) {
velocity = clamp(velocity, vec2(-0.003, -0.003), vec2(0.003, 0.003));
vec2 texCoord = pass_TexCoord.st;
vec4 color = texture(tex, texCoord);
int nSamples = 3;
texCoord += velocity;
for(int i = 1; i < nSamples; ++i, texCoord += velocity) {
color += texture(tex, clamp(texCoord, vec2(0.001, 0.001),
vec2(0.999, 0.999)));
}
return color / nSamples;
}


vec3 godrays(vec2 texCoord) {
vec4 lightPosSS = projMatrixCurr * modelviewMatrixCurr * vec4(lightPos*5000, 1.0);
lightPosSS /= lightPosSS.w;
float Exposure = .001f;

vec3 color = vec3(0.0);

if(lightPosSS.z < 1.0) { //if were facing the light source
lightPosSS += 1.0;
lightPosSS /= 2.0;
const int samples = 70;
float density = 0.975;
float Weight = 6.65;

float Decay = 1.0;
vec2 deltaTexCoord = (texCoord - lightPosSS.xy);
deltaTexCoord *= 1.0f / samples * density;
vec2 tc = texCoord;
float illuminationDecay = 1.0;
for (int i = 0; i < samples; i++) {
tc -= deltaTexCoord;
vec4 textureColor = texture(tex, tc);
vec3 sampled = textureColor.xyz * textureColor.w;
sampled *= illuminationDecay * Weight;
color += sampled;
illuminationDecay *= Decay;
}
}

//return vec3(color * Exposure);
return vec3(texture(tex, texCoord).xyz + color * Exposure);
}

void main() {
/* vec4 pos = texture(posTex, pass_TexCoord.st);
vec4 previousPos = projMatrixPrev * modelviewMatrixPrev * pos;
previousPos /= previousPos.w;
vec2 currentPos = vec2(pass_TexCoord.x * 2 - 1, (pass_TexCoord.y) * 2 - 1);
vec2 velocity = (currentPos-previousPos.st) * 0.5;
out_Color = moblur(velocity).xyz;*/
// out_Color = texture(tex, pass_TexCoord.st).xyz*1.1;

const vec3 luminace = vec3(0.2125f, 0.7154f, 0.0721f);
out_Color = vec4(godrays(pass_TexCoord.st), 1.0);
out_Color.w = log(dot(out_Color.xyz, luminace)+0.1);
}
#endif


//vec3 color = texture(tex, vec3(pass_TexCoord.st, 1.0)).xxx;
56 changes: 56 additions & 0 deletions shaders/post1.glsl
@@ -0,0 +1,56 @@
/*float4 ToneMapping( PS_INPUT psInput ) : SV_Target
{
int2 iScreenCoord = int2(psInput.Tex * depthInfo.yz);
float4 sample = tex2D.Load(int3(iScreenCoord, 0));
float4 ramp = colorramp.Sample(linearSampler, float2(cOffset, 0.f));
sample.r += ramp.r - 0.5 * (ramp.g + ramp.b);
sample.g += ramp.g - 0.5 * (ramp.r + ramp.b);
sample.b += ramp.b - 0.5 * (ramp.r + ramp.g);
float Y = dot(sample, CIEXYZ);
float Yw = 0.95;
float Yp = exp(tex2D.Load(int3(0,0,maxMipmapLevel-1)).w); //adaptation luminance
float Yr = alpha * Y / Yp;
float D = (Yr * (1.0 + Yr / (Yw * Yw)) / (1 + Yr));
return sample * D / Y;
}
*/


#version 400 core

uniform sampler2D tex;

uniform mat4 modelviewMatrix;
uniform mat4 projMatrix;

#ifdef _VERTEX_
in vec3 in_Position;
in vec3 in_Normal;
in vec3 in_TexCoord;
out vec3 pass_TexCoord;
void main(void) {
pass_TexCoord = in_TexCoord;
gl_Position = projMatrix * modelviewMatrix * vec4(in_Position,1.0);
}

#endif

#ifdef _FRAGMENT_
in vec3 pass_TexCoord;
out vec3 out_Color;
uniform float alpha = 0.75;
vec4 tonemap(vec4 color) {
const vec3 luminace = vec3(0.2125f, 0.7154f, 0.0721f);
float Y = dot(color.xyz, luminace);
float Yw = 0.95;
float Yp = exp(textureLod(tex, vec2(0.5, 0.5), 10.0).w); //adaptation luminance
float Yr = alpha * Y / Yp;
float D = (Yr * (1.0 + Yr / (Yw * Yw)) / (1 + Yr));
return color * D / Y;
}

void main() {
vec4 color = tonemap(texture(tex, pass_TexCoord.st));
out_Color = color.xyz;
}
#endif
29 changes: 17 additions & 12 deletions shaders/recttess.glsl
Expand Up @@ -10,7 +10,7 @@ uniform bool wireframe = false;
uniform vec3 lightPos;
uniform vec3 cameraPos;
uniform float waterLevel = 0.0;
const float tile =0.005;
const float tile =0.015;
const float attenuation = 0.075;
uniform float LOD;
#ifdef _VERTEX_
Expand Down Expand Up @@ -199,18 +199,19 @@ void main() {
in vec3 fTexCoord;
in vec4 fPosition;
in vec3 fTriDistance;
out vec4 FragColor;
out vec4 out_Color0;
out vec4 out_Color1;
const vec4 wireframeColor = vec4(0.7, 0.3, 0.3, 1);

vec4 atmosphere(vec3 pos) {
vec4 c0 = vec4(0.172, 0.290, 0.486, 1.000);
vec4 c1 = vec4(0.321, 0.482, 0.607, 1.000);
vec4 s0 = vec4(5.0, 5.0, 5.0, 1.0); //sun color
float d = length(pos - lightPos)*20.0;
vec4 s0 = vec4(5.0, 5.0, 5.0, 1.0) * 1.; //sun color
float d = length(pos - lightPos)*70.0;
if(pos.y >= 0.0)
return mix(mix(c1,c0,pos.y), s0, clamp(1.0/pow(d,1.25), 0.0, 1.0));
return mix(mix(c1,c0,pos.y), s0, clamp(1.0/pow(d,1.1), 0.0, 1.0));
else
return mix(c1, s0, clamp(1.0/pow(d,1.25), 0.0, 1.0));
return mix(c1, s0, clamp(1.0/pow(d,1.1), 0.0, 1.0));
}

vec4 water(vec3 normal, vec4 pos) {
Expand All @@ -219,14 +220,14 @@ vec4 water(vec3 normal, vec4 pos) {
vec3 eyeDir = normalize(-cameraPos + pos.xyz);
vec3 reflDir = reflect(eyeDir, norm);
vec4 transColor = vec4(0.0, 0.278, 0.321, 0.0);
float cos_angle = dot(norm, eyeDir);
float cos_angle = max(dot(norm, eyeDir), 0.3);
vec4 waterColor = mix(baseColor, transColor*transColor, cos_angle);
reflDir = normalize(reflDir);
vec4 atmoColor = atmosphere(reflDir);

//add foam if near shore

return mix(waterColor, atmoColor, 0.3);
return mix(waterColor, atmoColor, 0.6);
}

float amplify(float d, float scale, float offset) {
Expand All @@ -252,13 +253,15 @@ vec4 refractTerrain(vec3 disp, vec3 normal, float depth) { //fake refraction bas
vec3 coord = fTexCoord;
coord.xz += disp.xz*depth*depth*normal.y*0.002;
float h = texture(tex, coord).x;
return vec4(vec3(h*0.01+0.75), 1.0);
float pVal = h*0.01+0.75;
return vec4(.7, .7, .6, 1.0)*pVal;
}

void main() {

float h = texture(tex, fTexCoord).x;
FragColor = vec4(vec3(h*0.01+0.75), 1.0);//vec4(1.0, 1.0, 1.0, 1.0);
float pVal = (1.0-(h*0.01+0.25));
out_Color0 = vec4(.7, .7, .6, 1.0)*pVal + vec4(0.3, 0.5, 0.1, 1.0) * clamp((1.0-pVal-0.5), 0.0, 1.0);
float dH = abs(h - waterLevel)*attenuation;


Expand All @@ -267,15 +270,17 @@ void main() {
vec3 I = fPosition.xyz - cameraPos.xyz;
vec3 N = computeFFTNormal(fPosition.xz, dH).xyz;
vec3 displacement = texture(waterTex, fPosition.xz*tile).xyz;
FragColor = mix(water(N, fPosition),
out_Color0 = mix(water(N, fPosition),
refractTerrain(displacement, N, dH), waterStrength);
}

if(wireframe) {
float d1 = min(min(fTriDistance.x, fTriDistance.y), fTriDistance.z);
d1 = 1 - amplify(d1, 50, -1.0);
FragColor = mix(FragColor, wireframeColor, d1);
out_Color0 = mix(out_Color0, wireframeColor, d1);
}
out_Color1 = fPosition;
out_Color0.w = 0.0;
}

#endif
2 changes: 1 addition & 1 deletion src/glcommon.h
Expand Up @@ -18,7 +18,7 @@
GLenum err = glGetError(); \
if(err != GL_NO_ERROR) { \
cerr << "Error in " << DESC << ":" << endl; \
cerr << "\t>> " << gluErrorString(err) << endl; \
cerr << "\t>> No is: " << err << " String is: " << gluErrorString(err) << endl; \
assert(err == GL_NO_ERROR); \
} \
}
Expand Down

0 comments on commit afdc212

Please sign in to comment.