diff options
author | David Thompson <dthompson2@worcester.edu> | 2019-10-28 08:29:33 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2019-10-28 08:29:33 -0400 |
commit | 130c8015a36d1dc07b9127729d94e96c49328326 (patch) | |
tree | 2baa992d456ba44f78098eea49e46f2be6b87205 | |
parent | e2940a5f70fb5b427b0e61273bfff035ae649527 (diff) |
render: phong: Add support for diffuse and specular maps.
And boilerplate for bump maps to be supported later.
-rw-r--r-- | chickadee/render/phong.scm | 80 |
1 files changed, 58 insertions, 22 deletions
diff --git a/chickadee/render/phong.scm b/chickadee/render/phong.scm index 07c0eeb..2341e7a 100644 --- a/chickadee/render/phong.scm +++ b/chickadee/render/phong.scm @@ -26,14 +26,21 @@ #:use-module (chickadee render) #:use-module (chickadee render color) #:use-module (chickadee render shader) + #:use-module (chickadee render texture) #:use-module (srfi srfi-9) #:export (make-phong-material phong-material? phong-material-name phong-material-ambient phong-material-diffuse + phong-material-diffuse-map + phong-material-use-diffuse-map? phong-material-specular + phong-material-specular-map? + phong-material-use-specular-map? phong-material-specular-exponent + phong-material-bump-map + phong-material-use-bump-map? default-phong-material load-phong-shader gpu-apply/phong)) @@ -49,15 +56,27 @@ (local-field name phong-material-name) (float-vec3 ambient phong-material-ambient) (float-vec3 diffuse phong-material-diffuse) + (local-field diffuse-map phong-material-diffuse-map) + (bool use-diffuse-map phong-material-use-diffuse-map?) (float-vec3 specular phong-material-specular) - (float shininess phong-material-shininess)) + (local-field specular-map phong-material-specular-map) + (bool use-specular-map phong-material-use-specular-map?) + (float shininess phong-material-shininess) + (local-field bump-map phong-material-bump-map) + (bool use-bump-map phong-material-use-bump-map?)) (define default-phong-material (make-phong-material #:name "default" #:ambient (vec3 0.5 0.5 0.5) - #:diffuse (vec3 0.5 0.5 0.5) - #:specular (vec3 0.5 0.5 0.5) - #:shininess 32.0)) + #:diffuse (vec3 0.8 0.8 0.8) + #:diffuse-map null-texture + #:use-diffuse-map #f + #:specular (vec3 0.3 0.3 0.3) + #:specular-map null-texture + #:use-specular-map #f + #:shininess 32.0 + #:bump-map null-texture + #:use-bump-map #f)) ;;; @@ -67,7 +86,6 @@ (define-shader-type <directional-light> make-directional-light directional-light? - (float-vec4 color directional-light-color) (float-vec3 direction directional-light-direction) (float-vec3 ambient directional-light-ambient) (float-vec3 diffuse directional-light-diffuse) @@ -75,12 +93,11 @@ (float shininess directional-light-shininess)) (define default-directional-light - (make-directional-light #:color white - #:direction (vec3 -1.0 0.0 -1.0) - #:ambient (vec3 0.5 0.5 0.5) - #:diffuse (vec3 0.5 0.5 0.5) - #:specular (vec3 0.5 0.5 0.5) - #:shininess 32.0)) + (make-directional-light #:direction (vec3 0.0 0.0 -1.0) + #:ambient (vec3 -1.0 0.0 0.0) + #:diffuse (vec3 0.0 0.0 0.0) + #:specular (vec3 0.0 0.0 0.0) + #:shininess 0.0)) ;;; @@ -117,12 +134,17 @@ void main() { struct Material { vec3 ambient; vec3 diffuse; + sampler2D diffuseMap; + bool useDiffuseMap; vec3 specular; + sampler2D specularMap; + bool useSpecularMap; float shininess; + sampler2D bumpMap; + bool useBumpMap; }; struct DirectionalLight { - vec4 color; vec3 direction; vec3 ambient; vec3 diffuse; @@ -136,14 +158,25 @@ uniform Material material; uniform DirectionalLight directionalLight; void main() { - vec3 lightColor = directionalLight.color.xyz; - vec3 ambientColor = material.ambient * lightColor; + vec3 baseDiffuseColor; + vec3 baseSpecularColor; + if(material.useDiffuseMap) { + baseDiffuseColor = texture2D(material.diffuseMap, fragTex).xyz; + } else { + baseDiffuseColor = vec3(1.0, 1.0, 1.0); + } + if(material.useDiffuseMap) { + baseSpecularColor = texture2D(material.specularMap, fragTex).xyz; + } else { + baseSpecularColor = vec3(1.0, 1.0, 1.0); + } + vec3 ambientColor = material.ambient * baseDiffuseColor; vec3 lightDir = normalize(-directionalLight.direction); float diffuseFactor = max(dot(lightDir, fragNorm), 0.0); - vec3 diffuseColor = diffuseFactor * material.diffuse * lightColor; + vec3 diffuseColor = diffuseFactor * baseDiffuseColor * material.diffuse; vec3 reflectDir = reflect(-lightDir, fragNorm); float specularFactor = pow(max(dot(lightDir, reflectDir), 0.0), material.shininess); - vec3 specularColor = specularFactor * material.specular * lightColor; + vec3 specularColor = specularFactor * baseSpecularColor * material.specular; gl_FragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0); } "))) @@ -152,9 +185,12 @@ void main() { (force phong-shader)) (define (gpu-apply/phong shader vertex-array material model-matrix view-matrix) - (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-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))))) |