From 1a76ff6b213d9975921dc3ff707da549917cba3e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 28 Oct 2019 17:17:07 -0400 Subject: render: phong: Add ambient map to material. --- chickadee/render/phong.scm | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/chickadee/render/phong.scm b/chickadee/render/phong.scm index 2341e7a..cb649de 100644 --- a/chickadee/render/phong.scm +++ b/chickadee/render/phong.scm @@ -32,6 +32,8 @@ phong-material? phong-material-name phong-material-ambient + phong-material-ambient-map + phong-material-use-ambient-map? phong-material-diffuse phong-material-diffuse-map phong-material-use-diffuse-map? @@ -55,6 +57,8 @@ phong-material? (local-field name phong-material-name) (float-vec3 ambient phong-material-ambient) + (local-field ambient-map phong-material-ambient-map) + (bool use-ambient-map phong-material-use-ambient-map?) (float-vec3 diffuse phong-material-diffuse) (local-field diffuse-map phong-material-diffuse-map) (bool use-diffuse-map phong-material-use-diffuse-map?) @@ -68,6 +72,8 @@ (define default-phong-material (make-phong-material #:name "default" #:ambient (vec3 0.5 0.5 0.5) + #:ambient-map null-texture + #:use-ambient-map #f #:diffuse (vec3 0.8 0.8 0.8) #:diffuse-map null-texture #:use-diffuse-map #f @@ -133,6 +139,8 @@ void main() { struct Material { vec3 ambient; + sampler2D ambientMap; + bool useAmbientMap; vec3 diffuse; sampler2D diffuseMap; bool useDiffuseMap; @@ -158,10 +166,19 @@ uniform Material material; uniform DirectionalLight directionalLight; void main() { + vec3 baseAmbientColor; vec3 baseDiffuseColor; vec3 baseSpecularColor; + if(material.useAmbientMap) { + baseAmbientColor = texture2D(material.ambientMap, fragTex).xyz; + } else { + baseAmbientColor = vec3(1.0, 1.0, 1.0); + } if(material.useDiffuseMap) { - baseDiffuseColor = texture2D(material.diffuseMap, fragTex).xyz; + // discard transparent fragments. + vec4 color = texture2D(material.diffuseMap, fragTex); + if(color.a == 0.0) { discard; } + baseDiffuseColor = color.xyz; } else { baseDiffuseColor = vec3(1.0, 1.0, 1.0); } @@ -170,7 +187,7 @@ void main() { } else { baseSpecularColor = vec3(1.0, 1.0, 1.0); } - vec3 ambientColor = material.ambient * baseDiffuseColor; + vec3 ambientColor = material.ambient * baseAmbientColor * baseDiffuseColor; vec3 lightDir = normalize(-directionalLight.direction); float diffuseFactor = max(dot(lightDir, fragNorm), 0.0); vec3 diffuseColor = diffuseFactor * baseDiffuseColor * material.diffuse; @@ -185,12 +202,13 @@ void main() { (force phong-shader)) (define (gpu-apply/phong shader vertex-array material model-matrix view-matrix) - (with-texture 0 (phong-material-diffuse-map material) - (with-texture 1 (phong-material-specular-map material) - (with-texture 2 (phong-material-bump-map material) - (gpu-apply shader vertex-array - #:model model-matrix - #:view view-matrix - #:projection (current-projection) - #:material material - #:directional-light default-directional-light))))) + (with-texture 0 (phong-material-ambient-map material) + (with-texture 1 (phong-material-diffuse-map material) + (with-texture 2 (phong-material-specular-map material) + (with-texture 3 (phong-material-bump-map material) + (gpu-apply shader vertex-array + #:model model-matrix + #:view view-matrix + #:projection (current-projection) + #:material material + #:directional-light default-directional-light)))))) -- cgit v1.2.3