Add core voxel rendering code.

This commit is contained in:
Paul Chote
2013-02-23 00:56:24 +13:00
parent 6cef4290df
commit a00696ec3b
12 changed files with 701 additions and 0 deletions

17
glsl/vxl.frag Normal file
View File

@@ -0,0 +1,17 @@
uniform sampler2D Palette, DiffuseTexture;
uniform vec2 PaletteRows;
uniform vec4 LightDirection;
uniform vec3 AmbientLight, DiffuseLight;
void main()
{
vec4 x = texture2D(DiffuseTexture, gl_TexCoord[0].st);
vec4 color = texture2D(Palette, vec2(dot(x, gl_TexCoord[1]), PaletteRows.x));
if (color.a < 0.01)
discard;
vec4 normal = (2.0*texture2D(Palette, vec2(dot(x, gl_TexCoord[2]), PaletteRows.y)) - 1.0);
vec3 intensity = AmbientLight + DiffuseLight*max(dot(normal, LightDirection), 0.0);
gl_FragColor = vec4(intensity*color.rgb, color.a);
}

18
glsl/vxl.vert Normal file
View File

@@ -0,0 +1,18 @@
uniform mat4 View;
uniform mat4 TransformMatrix;
vec4 DecodeChannelMask(float x)
{
if (x > 0.0)
return (x > 0.5) ? vec4(1,0,0,0) : vec4(0,1,0,0);
else
return (x < -0.5) ? vec4(0,0,0,1) : vec4(0,0,1,0);
}
void main()
{
gl_Position = View*TransformMatrix*gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = DecodeChannelMask(gl_MultiTexCoord0.z);
gl_TexCoord[2] = DecodeChannelMask(gl_MultiTexCoord0.w);
}

12
glsl/vxlshadow.frag Normal file
View File

@@ -0,0 +1,12 @@
uniform sampler2D Palette, DiffuseTexture;
uniform vec2 PaletteRows;
void main()
{
vec4 x = texture2D(DiffuseTexture, gl_TexCoord[0].st);
vec4 color = texture2D(Palette, vec2(dot(x, gl_TexCoord[1]), PaletteRows.x));
if (color.a < 0.01)
discard;
gl_FragColor = color;
}

26
glsl/vxlshadow.vert Normal file
View File

@@ -0,0 +1,26 @@
uniform mat4 View;
uniform mat4 TransformMatrix;
uniform vec4 LightDirection;
uniform float GroundZ;
uniform vec3 GroundNormal;
vec4 DecodeChannelMask(float x)
{
if (x > 0.0)
return (x > 0.5) ? vec4(1,0,0,0) : vec4(0,1,0,0);
else
return (x < -0.5) ? vec4(0,0,0,1) : vec4(0,0,1,0);
}
void main()
{
// Distance between vertex and ground
float d = dot(gl_Vertex.xyz - vec3(0.0,0.0,GroundZ), GroundNormal) / dot(LightDirection.xyz, GroundNormal);
// Project onto ground plane
vec3 shadow = gl_Vertex.xyz - d*LightDirection.xyz;
gl_Position = View*TransformMatrix*vec4(shadow, 1);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = DecodeChannelMask(gl_MultiTexCoord0.z);
gl_TexCoord[2] = DecodeChannelMask(gl_MultiTexCoord0.w);
}