From f425383178f69591f47ba2a2fb7f90bf57a66db9 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 7 May 2021 08:34:32 -0400 Subject: model: Add support for vertex colored glTF models. --- chickadee/graphics/model.scm | 14 ++++++++------ chickadee/graphics/pbr.scm | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/chickadee/graphics/model.scm b/chickadee/graphics/model.scm index 43f7c3d..58204d6 100644 --- a/chickadee/graphics/model.scm +++ b/chickadee/graphics/model.scm @@ -953,15 +953,17 @@ ("POSITION" (attribute-location (hash-ref (shader-attributes shader) "position"))) - ("NORMAL" 1) - ("TANGENT" 2) + ("NORMAL" 9) + ("TANGENT" 10) ("TEXCOORD_0" (attribute-location (hash-ref (shader-attributes shader) "texcoord0"))) - ("TEXCOORD_1" 4) - ("COLOR_0" 5) - ("JOINTS_0" 6) - ("WEIGHTS_0" 7)))) + ("TEXCOORD_1" 11) + ("COLOR_0" + (attribute-location + (hash-ref (shader-attributes shader) "color0"))) + ("JOINTS_0" 12) + ("WEIGHTS_0" 13)))) (define (parse-primitive obj materials accessors) (let ((attributes (map (match-lambda ((name . n) diff --git a/chickadee/graphics/pbr.scm b/chickadee/graphics/pbr.scm index 5e87135..73f057b 100644 --- a/chickadee/graphics/pbr.scm +++ b/chickadee/graphics/pbr.scm @@ -23,6 +23,7 @@ (define-module (chickadee graphics pbr) #:use-module (chickadee math vector) + #:use-module (chickadee graphics buffer) #:use-module (chickadee graphics color) #:use-module (chickadee graphics engine) #:use-module (chickadee graphics shader) @@ -93,17 +94,22 @@ #ifdef GLSL330 layout (location = 0) in vec3 position; layout (location = 1) in vec2 texcoord0; +layout (location = 2) in vec4 color0; #elif defined(GLSL130) in vec3 position; in vec2 texcoord0; +in vec4 color0; #elif defined(GLSL120) attribute vec3 position; attribute vec2 texcoord0; +attribute vec4 color0; #endif #ifdef GLSL120 varying vec2 fragTex; +varying vec4 fragVertColor; #else out vec2 fragTex; +out vec4 fragVertColor; #endif uniform mat4 model; uniform mat4 view; @@ -111,37 +117,55 @@ uniform mat4 projection; void main(void) { fragTex = texcoord0; + fragVertColor = color0; gl_Position = projection * view * model * vec4(position.xyz, 1.0); } " " #ifdef GLSL120 attribute vec2 fragTex; +attribute vec4 fragVertColor; #else in vec2 fragTex; +in vec4 fragVertColor; #endif #ifdef GLSL330 out vec4 fragColor; #endif +uniform bool textured; +uniform bool vertexColored; uniform vec3 baseColorFactor; uniform sampler2D baseColorTexture; void main (void) { + vec4 finalColor = vec4(baseColorFactor, 1.0); + if(vertexColored) { + finalColor *= fragVertColor; + } + if(textured) { + finalColor *= texture(baseColorTexture, fragTex); + } #ifdef GLSL330 - fragColor = texture(baseColorTexture, fragTex) * - vec4(baseColorFactor, 1.0); + fragColor = finalColor; #else - gl_FragColor = texture2D(baseColorTexture, fragTex) * - vec4(baseColorFactor, 1.0); + gl_FragColor = finalColor; #endif } ")) (define (shader-apply/pbr vertex-array material model-matrix view-matrix) - (let ((shader (graphics-variable-ref pbr-shader))) - (with-graphics-state ((g:texture-0 (pbr-material-base-color-texture material))) + (let* ((shader (graphics-variable-ref pbr-shader)) + (base-color-texture (pbr-material-base-color-texture material)) + (vattrs (vertex-array-attributes vertex-array)) + (sattrs (shader-attributes shader))) + (with-graphics-state ((g:texture-0 base-color-texture)) (shader-apply shader vertex-array #:model model-matrix #:view view-matrix #:projection (current-projection) + #:textured (not (eq? base-color-texture null-texture)) + #:vertex-colored (buffer-view? + (assv-ref vattrs + (attribute-location + (hash-ref sattrs "color0")))) #:base-color-factor (pbr-material-base-color-factor material))))) -- cgit v1.2.3