From 4ccd830229accad5da8c1c95ea626b53ed0aeec3 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 23 Sep 2021 07:19:01 -0400 Subject: graphics: Add make-phong-material and make-pbr-material procedures. --- chickadee/graphics/model.scm | 87 ++++++++++++++++++++------------------------ chickadee/graphics/pbr.scm | 70 +++++++++++++++++++++++++++-------- chickadee/graphics/phong.scm | 51 +++++++++++++++++--------- 3 files changed, 128 insertions(+), 80 deletions(-) diff --git a/chickadee/graphics/model.scm b/chickadee/graphics/model.scm index 92d5a28..309b00f 100644 --- a/chickadee/graphics/model.scm +++ b/chickadee/graphics/model.scm @@ -212,23 +212,24 @@ (define (maybe-add-material) (let ((name (assq-ref opts 'name))) (when name - (let* ((ambient (or (assq-ref opts 'ambient-map) (white-texture))) - (diffuse (or (assq-ref opts 'diffuse-map) (white-texture))) - (specular (or (assq-ref opts 'specular-map) (white-texture))) - (normals (or (assq-ref opts 'normal-map) (flat-texture))) - (properties (make-phong-properties - #:ambient (assq-ref opts 'ambient) - #:diffuse (assq-ref opts 'diffuse) - #:specular (assq-ref opts 'specular) - #:shininess (assq-ref opts 'shininess))) + (let* ((ambient-texture (or (assq-ref opts 'ambient-map) + (white-texture))) + (diffuse-texture (or (assq-ref opts 'diffuse-map) + (white-texture))) + (specular-texture (or (assq-ref opts 'specular-map) + (white-texture))) + (normal-texture (or (assq-ref opts 'normal-map) + (flat-texture))) (material - (make-material #:name name - #:shader (phong-shader) - #:texture-0 ambient - #:texture-1 diffuse - #:texture-2 specular - #:texture-3 normals - #:properties properties))) + (make-phong-material #:name name + #:ambient-texture ambient-texture + #:diffuse-texture diffuse-texture + #:specular-texture specular-texture + #:normal-texture normal-texture + #:ambient-factor (assq-ref opts 'ambient) + #:diffuse-factor (assq-ref opts 'diffuse) + #:specular-factor (assq-ref opts 'specular) + #:shininess (assq-ref opts 'shininess)))) (hash-set! material-map name material))))) (match (read-line port) ((? eof-object?) @@ -546,13 +547,7 @@ (hash-ref material-map "default")))))) ;; Register default material (hash-set! material-map "default" - (make-material #:name "default" - #:shader (phong-shader) - #:texture-0 (white-texture) - #:texture-1 (white-texture) - #:texture-2 (white-texture) - #:texture-3 (flat-texture) - #:properties default-phong-properties)) + (make-phong-material #:name "default")) ;; Parse file. (let loop ((material "default")) (match (read-line port) @@ -858,9 +853,9 @@ ;; BLEND. (alpha-mode (match (or (string-ref/optional obj "alphaMode") "OPAQUE") - ("OPAQUE" 0) - ("MASK" 1) - ("BLEND" 2))) + ("OPAQUE" 'opaque) + ("MASK" 'mask) + ("BLEND" 'blend))) (alpha-cutoff (or (number-ref/optional obj "alphaCutoff") 0.5)) (double-sided? (boolean-ref/optional obj "doubleSided")) (extensions (object-ref/optional obj "extensions")) @@ -881,30 +876,28 @@ (parse-texture obj "occlusionTexture" white-texture)) ((emissive-texture emissive-texcoord) (parse-texture obj "emissiveTexture" black-texture))) - (make-material + (make-pbr-material #:name name - #:shader (pbr-shader) - #:blend-mode (if (= alpha-mode 2) blend:alpha blend:replace) + #:blend-mode (if (eq? alpha-mode 'opaque) blend:alpha blend:replace) #:cull-face-mode (if double-sided? no-cull-face-mode back-cull-face-mode) - #:texture-0 base-color-texture - #:texture-1 metal-rough-texture - #:texture-2 normal-texture - #:texture-3 occlusion-texture - #:texture-4 emissive-texture - #:properties (make-pbr-properties - #:base-color-factor base-color-factor - #:base-color-texcoord base-color-texcoord - #:metallic-factor metallic-factor - #:roughness-factor roughness-factor - #:metallic-roughness-texcoord metal-rough-texcoord - #:normal-texcoord normal-texcoord - #:occlusion-texcoord occlusion-texcoord - #:emissive-factor emissive-factor - #:emissive-texcoord emissive-texcoord - #:alpha-mode alpha-mode - #:alpha-cutoff alpha-cutoff))))) + #:base-color-texture base-color-texture + #:metallic-roughness-texture metal-rough-texture + #:normal-texture normal-texture + #:occlusion-texture occlusion-texture + #:emissive-texture emissive-texture + #:base-color-factor base-color-factor + #:base-color-texcoord base-color-texcoord + #:metallic-factor metallic-factor + #:roughness-factor roughness-factor + #:metallic-roughness-texcoord metal-rough-texcoord + #:normal-texcoord normal-texcoord + #:occlusion-texcoord occlusion-texcoord + #:emissive-factor emissive-factor + #:emissive-texcoord emissive-texcoord + #:alpha-mode alpha-mode + #:alpha-cutoff alpha-cutoff)))) (define (attribute-name->index name) (let ((shader (pbr-shader))) (match name @@ -944,7 +937,7 @@ (#f #f) (n (vector-ref accessors n)))) (material (match (number-ref/optional obj "material") - (#f default-pbr-properties) + (#f (make-pbr-material)) (n (vector-ref materials n)))) (mode (match (or (number-ref/optional obj "mode") 4) (0 'points) diff --git a/chickadee/graphics/pbr.scm b/chickadee/graphics/pbr.scm index bc224e0..d73b6d9 100644 --- a/chickadee/graphics/pbr.scm +++ b/chickadee/graphics/pbr.scm @@ -27,9 +27,12 @@ #:use-module (chickadee graphics blend) #:use-module (chickadee graphics buffer) #:use-module (chickadee graphics color) + #:use-module (chickadee graphics depth) #:use-module (chickadee graphics engine) + #:use-module (chickadee graphics mesh) #:use-module (chickadee graphics polygon) #:use-module (chickadee graphics shader) + #:use-module (chickadee graphics stencil) #:use-module (chickadee graphics texture) #:use-module (srfi srfi-9) #:export (make-pbr-properties @@ -45,8 +48,8 @@ pbr-properties-emissive-texcoord pbr-properties-alpha-mode pbr-properties-alpha-cutoff - default-pbr-properties - pbr-shader)) + pbr-shader + make-pbr-material)) (define-shader-type make-pbr-properties @@ -63,19 +66,6 @@ (int alpha-mode pbr-properties-alpha-mode) (float alpha-cutoff pbr-properties-alpha-cutoff)) -(define default-pbr-properties - (make-pbr-properties #:base-color-factor (vec3 1.0 1.0 1.0) - #:base-color-texcoord 0 - #:metallic-factor 1.0 - #:roughness-factor 1.0 - #:metallic-roughness-texcoord 0 - #:normal-texcoord 0 - #:occlusion-texcoord 0 - #:emissive-factor (vec3 1.0 1.0 1.0) - #:emissive-texcoord 0 - #:alpha-mode 0 - #:alpha-cutoff 0.5)) - (define %pbr-shader (delay (load-shader (scope-datadir "shaders/pbr-vert.glsl") (scope-datadir "shaders/pbr-frag.glsl")))) @@ -83,6 +73,56 @@ (define (pbr-shader) (force %pbr-shader)) +(define* (make-pbr-material #:key (name "anonymous") + (blend-mode blend:replace) + (polygon-mode fill-polygon-mode) + (cull-face-mode back-cull-face-mode) + (depth-test basic-depth-test) + (stencil-test default-stencil-test) + multisample? + (base-color-factor (vec3 1.0 1.0 1.0)) + (base-color-texcoord 0) + (metallic-factor 1.0) + (roughness-factor 1.0) + (metallic-roughness-texcoord 0) + (normal-texcoord 0) + (occlusion-texcoord 0) + (emissive-factor (vec3 1.0 1.0 1.0)) + (emissive-texcoord 0) + (alpha-mode 'opaque) + (alpha-cutoff 0.5) + (base-color-texture (white-texture)) + (metallic-roughness-texture (white-texture)) + (normal-texture (flat-texture)) + (occlusion-texture (white-texture)) + (emissive-texture (black-texture))) + (let ((properties (make-pbr-properties + #:base-color-factor base-color-factor + #:base-color-texcoord base-color-texcoord + #:metallic-factor metallic-factor + #:roughness-factor roughness-factor + #:metallic-roughness-texcoord metallic-roughness-texcoord + #:normal-texcoord normal-texcoord + #:occlusion-texcoord occlusion-texcoord + #:emissive-factor emissive-factor + #:emissive-texcoord emissive-texcoord + #:alpha-mode (case alpha-mode + ((opaque) 0) + ((mask) 1) + ((blend) 2) + (else + (error "unsupport PBR alpha mode:" + alpha-mode))) + #:alpha-cutoff 0.5))) + (make-material #:name name + #:shader (pbr-shader) + #:texture-0 base-color-texture + #:texture-1 metallic-roughness-texture + #:texture-2 normal-texture + #:texture-3 occlusion-texture + #:texture-4 emissive-texture + #:properties properties))) + ;; TODO: Handle vertex colors ;; (buffer-view? ;; (assv-ref vattrs diff --git a/chickadee/graphics/phong.scm b/chickadee/graphics/phong.scm index e3deba2..75ed92e 100644 --- a/chickadee/graphics/phong.scm +++ b/chickadee/graphics/phong.scm @@ -24,10 +24,15 @@ (define-module (chickadee graphics phong) #:use-module (chickadee config) #:use-module (chickadee math vector) + #:use-module (chickadee graphics blend) #:use-module (chickadee graphics color) + #:use-module (chickadee graphics depth) #:use-module (chickadee graphics engine) #:use-module (chickadee graphics light) + #:use-module (chickadee graphics mesh) + #:use-module (chickadee graphics polygon) #:use-module (chickadee graphics shader) + #:use-module (chickadee graphics stencil) #:use-module (chickadee graphics texture) #:use-module (srfi srfi-9) #:export (make-phong-properties @@ -37,13 +42,8 @@ phong-properties-diffuse phong-properties-specular phong-properties-shininess - default-phong-properties - phong-shader)) - - -;;; -;;; Phong Properties -;;; + phong-shader + make-phong-material)) (define-shader-type make-phong-properties @@ -53,20 +53,35 @@ (float-vec3 specular phong-properties-specular) (float shininess phong-properties-shininess)) -(define default-phong-properties - (make-phong-properties #:ambient (vec3 1.0 1.0 1.0) - #:diffuse (vec3 1.0 1.0 1.0) - #:specular (vec3 1.0 1.0 1.0) - #:shininess 32.0)) - - -;;; -;;; Phong Shader -;;; - (define %phong-shader (delay (load-shader (scope-datadir "shaders/phong-vert.glsl") (scope-datadir "shaders/phong-frag.glsl")))) (define (phong-shader) (force %phong-shader)) + +(define* (make-phong-material #:key (name "anonymous") + (blend-mode blend:replace) + (polygon-mode fill-polygon-mode) + (cull-face-mode back-cull-face-mode) + (depth-test basic-depth-test) + (stencil-test default-stencil-test) + multisample? + (ambient-factor (vec3 1.0 1.0 1.0)) + (diffuse-factor (vec3 1.0 1.0 1.0)) + (specular-factor (vec3 1.0 1.0 1.0)) + (shininess 32.0) + (ambient-texture (white-texture)) + (diffuse-texture (white-texture)) + (specular-texture (white-texture)) + (normal-texture (flat-texture))) + (make-material #:name name + #:shader (phong-shader) + #:texture-0 ambient-texture + #:texture-1 diffuse-texture + #:texture-2 specular-texture + #:texture-3 normal-texture + #:properties (make-phong-properties #:ambient ambient-factor + #:diffuse diffuse-factor + #:specular specular-factor + #:shininess shininess))) -- cgit v1.2.3