diff options
author | Joshua Rahm <joshua.rahm@colorado.edu> | 2014-04-17 22:08:15 -0600 |
---|---|---|
committer | Joshua Rahm <joshua.rahm@colorado.edu> | 2014-04-17 22:08:15 -0600 |
commit | 73daf65aaa31b5fb59f4a91d9185387f63c7b09f (patch) | |
tree | 681036c0cdd6f7981164ac189fed92da900ee3e7 /shaders | |
parent | e083553a455d30374f21aa0c34d9ae827470d490 (diff) | |
download | terralloc-73daf65aaa31b5fb59f4a91d9185387f63c7b09f.tar.gz terralloc-73daf65aaa31b5fb59f4a91d9185387f63c7b09f.tar.bz2 terralloc-73daf65aaa31b5fb59f4a91d9185387f63c7b09f.zip |
added real water
Diffstat (limited to 'shaders')
-rw-r--r-- | shaders/.basic.frag.swp | bin | 0 -> 12288 bytes | |||
-rw-r--r-- | shaders/.water.frag.swp | bin | 0 -> 12288 bytes | |||
-rw-r--r-- | shaders/basic.frag | 83 | ||||
-rw-r--r-- | shaders/basic.vert | 27 | ||||
-rw-r--r-- | shaders/forest.frag | 53 | ||||
-rw-r--r-- | shaders/forest.geom | 69 | ||||
-rw-r--r-- | shaders/forest.vert | 44 | ||||
-rw-r--r-- | shaders/sky.frag | 18 | ||||
-rw-r--r-- | shaders/sky.vert | 18 | ||||
-rw-r--r-- | shaders/water.frag | 9 | ||||
-rw-r--r-- | shaders/water.vert | 16 |
11 files changed, 337 insertions, 0 deletions
diff --git a/shaders/.basic.frag.swp b/shaders/.basic.frag.swp Binary files differnew file mode 100644 index 0000000..66aabb3 --- /dev/null +++ b/shaders/.basic.frag.swp diff --git a/shaders/.water.frag.swp b/shaders/.water.frag.swp Binary files differnew file mode 100644 index 0000000..73b3be2 --- /dev/null +++ b/shaders/.water.frag.swp diff --git a/shaders/basic.frag b/shaders/basic.frag new file mode 100644 index 0000000..4d7683c --- /dev/null +++ b/shaders/basic.frag @@ -0,0 +1,83 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) out vec4 frag_color ; +layout(location = 6) uniform vec4 lightPos ; + +layout(location = 8) uniform vec4 globalAmbient ; +uniform float dX ; +uniform float dY ; + +uniform mat4 mvMatrix ; +uniform mat4 pjMatrix ; + +in vec3 normal ; + +uniform sampler2D textures[8] ; + +in float texture_blend[8] ; +in vec2 texcoord ; +in vec4 position ; + +vec3 sample(float xc,float yc) { + vec3 color = vec3(0); + for ( int i = 0 ; i < 8 ; ++ i ) { + vec4 tmp = texture2D(textures[i], texcoord + vec2(xc,yc)) ; + color += vec3(tmp) * texture_blend[i] ; + } + return color ; +} + +vec3 sample2(int tex, float xc,float yc) { + vec3 color = vec3(0); + vec4 tmp = texture2D(textures[tex], texcoord + vec2(xc,yc)) ; + color += vec3(tmp) ; + return color ; +} + +int dominentTexture() { + float m = 0.0 ; + int ret = 0; + for( int i = 0 ; i < 8 ; ++ i ) { + if( texture_blend [i] > m ) { + m = texture_blend[i] ; + ret = i ; + } + } + return ret ; +} + +vec3 calNormChange( vec3 norm, vec3 down, vec3 right ) { + int dom = dominentTexture() ; + float x00 = length(sample2(dom,-dX, dY)); + float x01 = length(sample2(dom, 0, dY)); + float x02 = length(sample2(dom, dX, dY)); + + float x10 = length(sample2(dom,-dX, 0)); + float x11 = length(sample2(dom, 0, 0)); + float x12 = length(sample2(dom, dX, 0)); + + float x20 = length(sample2(dom,-dX,-dY)); + float x21 = length(sample2(dom, 0,-dY)); + float x22 = length(sample2(dom, dX,-dY)); + + down = ((x11 - x00) + (x11 - x01) + (x11 - x02) - (x11 - x20) - (x11 - x21) - (x11 - x22)) * down ; + right = ((x11 - x00) + (x11 - x10) + (x11 - x20) - (x11 - x02) - (x11 - x12) - (x11 - x22)) * right ; + + return (norm + down + right) / 3.0 ; +} + +void main() { + vec3 down = vec3( 0.0, -1.0, 0.0 ) ; + vec3 right = normalize(cross( normal, down )) ; + down = normalize(cross( normal, right ) ); + vec3 newNorm = calNormChange(normal,down,right) ; + + vec3 color = sample(0,0) ; + + float prod = dot( normalize(-newNorm), normalize(vec3(lightPos - position))); + vec3 intensity = vec3(prod,prod,max(prod,0.4)) ; + + frag_color = vec4(color * intensity,1) * vec4(normalize(globalAmbient.xyz),1.0); +} diff --git a/shaders/basic.vert b/shaders/basic.vert new file mode 100644 index 0000000..e1abeb5 --- /dev/null +++ b/shaders/basic.vert @@ -0,0 +1,27 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) in vec3 in_position ; +layout(location = 2) in vec4 in_color ; +layout(location = 1) in vec3 in_normal ; +layout(location = 3) in vec2 in_texcoord ; + +layout(location = 4) uniform mat4 pjMatrix ; +layout(location = 5) uniform mat4 mvMatrix ; +layout(location = 7) uniform mat3 normalMatrix ; + +out vec2 texcoord ; +out vec4 position ; +out vec3 normal ; + +out float texture_blend[8] ; + +void main() { + gl_Position = pjMatrix * (position = mvMatrix * vec4(in_position,1.0)) ; + texcoord = in_texcoord ; + normal = normalMatrix * in_normal ; + for ( int i = 0 ; i < 8 ; ++ i ) + texture_blend[i] = 0 ; + texture_blend[int(clamp(round(in_color.a),0,8))] = 1.0 ; +} diff --git a/shaders/forest.frag b/shaders/forest.frag new file mode 100644 index 0000000..1cad806 --- /dev/null +++ b/shaders/forest.frag @@ -0,0 +1,53 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) out vec4 frag_color ; + +uniform mat4 mvMatrix ; +uniform mat4 pjMatrix ; +layout(location = 6) uniform sampler2D texture ; +layout(location = 7) uniform vec4 light ; +layout(location = 10) uniform vec4 globalAmbient ; + +uniform float dX ; +uniform float dY ; + +in vec2 texposition ; +in vec3 normal ; +in vec4 frag_position ; + +vec4 sample(float xc,float yc) { + return texture2D(texture,texposition + vec2(xc,yc)); +} + +vec3 calNormChange( vec3 norm, vec3 down, vec3 right ) { + float x00 = 1 - sample(-dX, dY).a ; + float x01 = 1 - sample( 0, dY).a ; + float x02 = 1 - sample( dX, dY).a ; + + float x10 = 1 - sample(-dX, 0).a ; + float x11 = 1 - sample( 0, 0).a ; + float x12 = 1 - sample( dX, 0).a ; + + float x20 = 1 - sample(-dX,-dY).a ; + float x21 = 1 - sample( 0,-dY).a ; + float x22 = 1 - sample( dX,-dY).a ; + + down = ((x11 - x00) + (x11 - x01) + (x11 - x02) - (x11 - x20) - (x11 - x21) - (x11 - x22)) * down ; + right = ((x11 - x00) + (x11 - x10) + (x11 - x20) - (x11 - x02) - (x11 - x12) - (x11 - x22)) * right ; + + return (right*2 + down*2 + norm) / 5.0 ; +} + +void main() { + vec3 down = vec3( 0, -1, 0 ) ; + vec3 right = normalize(cross( normal, down )) ; + down = normalize(cross( normal, right ) ); + vec3 newNorm = calNormChange( normal, down, right ) ; + + vec4 col = texture2D(texture,texposition) ; + float coef = max(dot( normalize(newNorm), + normalize(vec3(frag_position - light)) ),0) + (globalAmbient.a/4.0) ; + frag_color = vec4( col.xyz * coef * globalAmbient.xyz, col.a); +} diff --git a/shaders/forest.geom b/shaders/forest.geom new file mode 100644 index 0000000..0c0cafb --- /dev/null +++ b/shaders/forest.geom @@ -0,0 +1,69 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(points) in ; +layout(triangle_strip, max_vertices=82) out; + +layout(location = 4) uniform mat4 pjMatrix ; +layout(location = 5) uniform mat4 mvMatrix ; + +out vec2 texposition; +out vec3 normal ; +out vec4 frag_position ; + +void vertex( vec4 pos ) { + normal = - inverse(transpose(mat3(mvMatrix))) * vec3( pos.x, 0, pos.z ) ; + gl_Position = pjMatrix * (frag_position = gl_in[0].gl_Position + (mvMatrix * pos)) ; + EmitVertex() ; +} + +void main() { + float r = 0.045 ; + float th = 0.00 ; + float h = 0.4 ; + for( ; th < 6.4 ; th += 1.0 ) { + float c = r * cos( th ) ; + float s = r * sin( th ) ; + float c2 = r * cos( th + 1.0 ) ; + float s2 = r * sin( th + 1.0 ) ; + + float tex_x = th / 6.4 / 2.0; + float tex_x2 = (th+1.0) / 6.4 / 2.0 ; + texposition = vec2(tex_x, 0); + vertex( vec4(c, 0.0, s, 0) ) ; + texposition = vec2(tex_x, 1); + vertex( vec4(c, h, s, 0) ) ; + texposition = vec2(tex_x2, 0); + vertex( vec4(c2, h, s2, 0) ) ; + + texposition = vec2(tex_x, 0); + vertex( vec4(c, 0.0, s, 0) ) ; + texposition = vec2(tex_x2, 0); + vertex( vec4(c2, 0, s2, 0) ) ; + texposition = vec2(tex_x2, 1); + vertex( vec4(c2, h, s2, 0) ) ; + } + + for( th = 0; th < 6.4 ; th += 1.0 ) { + float c = (r*4) * cos( th ) ; + float s = (r*4) * sin( th ) ; + float tex_x = th / 6.4 / 2.0 + 0.5; + texposition = vec2(tex_x, 1); + vertex( vec4(0,h*2,0,0) ) ; + texposition = vec2(tex_x, 0); + vertex( vec4(s,h/2,c,0) ) ; + } + + for( th = 0; th < 6.4 ; th += 1.0 ) { + float c = (r*6) * cos( th ) ; + float s = (r*6) * sin( th ) ; + float tex_x = th / 6.4 / 2.0 + 0.5; + texposition = vec2(tex_x, 1); + vertex( vec4(0,h,0,0) ) ; + texposition = vec2(tex_x, 0); + vertex( vec4(s,h/4,c,0) ) ; + } + + EndPrimitive(); +} diff --git a/shaders/forest.vert b/shaders/forest.vert new file mode 100644 index 0000000..8fb9528 --- /dev/null +++ b/shaders/forest.vert @@ -0,0 +1,44 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) in vec3 in_position ; +layout(location = 2) in vec4 in_color ; +layout(location = 1) in vec3 in_normal ; +layout(location = 3) in vec2 in_texcoord ; + +layout(location = 4) uniform mat4 pjMatrix ; +layout(location = 5) uniform mat4 mvMatrix ; +layout(location = 8) uniform float time ; +layout(location = 9) uniform mat3 normalMatrix ; + +layout(location = 10) in vec3 in_translation ; +layout(location = 11) in vec3 in_scale ; +layout(location = 12) in vec2 in_sincos_rot ; +layout(location = 13) in float noise ; + +out vec2 texposition ; +out vec3 normal ; +out vec4 frag_position ; + +void main() { + float s = in_sincos_rot.x ; + float c = in_sincos_rot.y ; + + mat3 rot = mat3( c,0,s, + 0,1,0, + -s,0,c ) ; + normal =-rot * normalMatrix * in_normal ; + texposition = in_texcoord ; + + vec3 real_pos1 = (rot * in_position) * in_scale ; + + float val = sin(( (time+noise) * noise) / 100.0) / 30.0 * pow(real_pos1.y,2) ; + s = sin( val ) ; c = cos( val ) ; + rot = mat3( c, -s, 0, + s, c, 0, + 0, 0, 1 ); + + vec3 real_pos = (rot * real_pos1) + in_translation ; + gl_Position = pjMatrix * (frag_position = mvMatrix * vec4(real_pos,1.0) ); +} diff --git a/shaders/sky.frag b/shaders/sky.frag new file mode 100644 index 0000000..743f538 --- /dev/null +++ b/shaders/sky.frag @@ -0,0 +1,18 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) out vec4 frag_color ; +layout(location = 1) uniform vec4 globalAmbient ; + +uniform sampler2D texture ; +uniform sampler2D night_tex ; +in vec2 texcoord; + +void main() { + vec3 color2 = texture2D(texture,texcoord).xyz ; + frag_color = + mix(texture2D(night_tex,texcoord) * (1-globalAmbient.a), + texture2D(texture,texcoord) * vec4(normalize(globalAmbient.xyz),1), + (globalAmbient.a + 1) / 2) ; +} diff --git a/shaders/sky.vert b/shaders/sky.vert new file mode 100644 index 0000000..87d919b --- /dev/null +++ b/shaders/sky.vert @@ -0,0 +1,18 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) in vec3 in_position ; +layout(location = 2) in vec4 in_color ; +layout(location = 1) in vec3 in_normal ; +layout(location = 3) in vec2 in_texcoord ; + +uniform mat4 mvMatrix ; +uniform mat4 pjMatrix ; + +out vec2 texcoord ; + +void main() { + gl_Position = pjMatrix * mvMatrix * vec4(in_position,1.0); + texcoord = in_texcoord ; +} diff --git a/shaders/water.frag b/shaders/water.frag new file mode 100644 index 0000000..8a0dd0b --- /dev/null +++ b/shaders/water.frag @@ -0,0 +1,9 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) out vec4 frag_color ; + +void main() { + frag_color = vec4(0.0,0.3,0.7,0.5) ; +} diff --git a/shaders/water.vert b/shaders/water.vert new file mode 100644 index 0000000..1db6dc5 --- /dev/null +++ b/shaders/water.vert @@ -0,0 +1,16 @@ +#version 150 +#extension GL_ARB_explicit_attrib_location : enable +#extension GL_ARB_explicit_uniform_location : enable + +layout(location = 0) in vec3 in_position ; +layout(location = 2) in vec4 in_color ; +layout(location = 1) in vec3 in_normal ; +layout(location = 3) in vec2 in_texcoord ; + +layout(location = 4) uniform mat4 pjMatrix ; +layout(location = 5) uniform mat4 mvMatrix ; +layout(location = 7) uniform mat3 normalMatrix ; + +void main() { + gl_Position = pjMatrix * mvMatrix * vec4( in_position, 1.0 ); +} |