Tidying OpenGLExample shader code.

This commit is contained in:
David Williams 2014-05-26 21:10:07 +02:00
parent 40e528d782
commit fd3879faeb

View File

@ -102,16 +102,17 @@ int main(int argc, char *argv[])
uniform mat4 modelToWorldMatrix; uniform mat4 modelToWorldMatrix;
out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals
out vec4 worldNormal; out vec3 normalFromVS;
flat out ivec2 outMaterial; flat out ivec2 outMaterial;
void main() void main()
{ {
worldPosition = modelToWorldMatrix * position; worldPosition = modelToWorldMatrix * position;
worldNormal = normal;
outMaterial = ivec2(material.x, material.y);
vec4 cameraPosition = worldToCameraMatrix * worldPosition; vec4 cameraPosition = worldToCameraMatrix * worldPosition;
gl_Position = cameraToClipMatrix * cameraPosition; gl_Position = cameraToClipMatrix * cameraPosition;
normalFromVS = normal.xyz;
outMaterial = ivec2(material.x, material.y);
} }
)")) )"))
{ {
@ -123,16 +124,16 @@ int main(int argc, char *argv[])
#version 130 #version 130
in vec4 worldPosition; //Passed in from the vertex shader in vec4 worldPosition; //Passed in from the vertex shader
in vec4 worldNormal; in vec3 normalFromVS;
flat in ivec2 outMaterial; flat in ivec2 outMaterial;
out vec4 outputColor; out vec4 outputColor;
void main() void main()
{ {
vec3 normal = worldNormal.xyz; // The first byte of our voxel data is the material.
vec4 surfaceColor = vec4(0.0, 0.0, 0.0, 0.0); // We use this to decide how to color the fragment.
vec4 surfaceColor;
switch(outMaterial.x) switch(outMaterial.x)
{ {
case 1: case 1:
@ -155,13 +156,15 @@ int main(int argc, char *argv[])
break; break;
} }
vec3 lightDir = vec3(1.0, 0.0, 0.0); // Quick and dirty lighting, obviously a real implementation
float diffuse = dot(lightDir, normal); // should pass light properties as shader parameters, etc.
diffuse *= 0.6; vec3 lightDir = vec3(0.0, 0.0, 1.0);
float ambient = 0.4; float diffuse = clamp(dot(lightDir, normalFromVS), 0.0, 1.0);
float lightVal = diffuse + ambient; diffuse *= 0.7; // Dim the diffuse a bit
float ambient = 0.3; // Add some ambient
float lightIntensity = diffuse + ambient; // Compute the final light intensity
outputColor = surfaceColor * lightVal; outputColor = surfaceColor * lightIntensity; //Compute final rendered color
} }
)")) )"))
{ {