From e142166c3e2d6f567ed0d4e4c414fa510271260f Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 7 May 2021 09:49:57 -0400 Subject: graphics: pbr: Extract shader code to separate files. --- Makefile.am | 4 ++- chickadee/graphics/pbr.scm | 81 ++-------------------------------------------- data/shaders/pbr-frag.glsl | 42 ++++++++++++++++++++++++ data/shaders/pbr-vert.glsl | 39 ++++++++++++++++++++++ 4 files changed, 87 insertions(+), 79 deletions(-) create mode 100644 data/shaders/pbr-frag.glsl create mode 100644 data/shaders/pbr-vert.glsl diff --git a/Makefile.am b/Makefile.am index 72c5ab2..be1a53f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -131,7 +131,9 @@ dist_fonts_DATA = \ shadersdir = $(pkgdatadir)/shaders dist_shaders_DATA = \ data/shaders/path-vert.glsl \ - data/shaders/path-frag.glsl + data/shaders/path-frag.glsl \ + data/shaders/pbr-vert.glsl \ + data/shaders/pbr-frag.glsl info_TEXINFOS = doc/chickadee.texi doc_chickadee_TEXINFOS = \ diff --git a/chickadee/graphics/pbr.scm b/chickadee/graphics/pbr.scm index 01a330c..1ed3777 100644 --- a/chickadee/graphics/pbr.scm +++ b/chickadee/graphics/pbr.scm @@ -22,6 +22,7 @@ ;;; Code: (define-module (chickadee graphics pbr) + #:use-module (chickadee config) #:use-module (chickadee math vector) #:use-module (chickadee graphics buffer) #:use-module (chickadee graphics color) @@ -87,84 +88,8 @@ #:double-sided? #f)) (define-graphics-variable pbr-shader - ;; TODO: Actually implement PBR. For now it's just the minimal amount - ;; of code needed to render the base texture of a mesh. - (strings->shader - " -#ifdef GLSL330 -layout (location = 0) in vec3 position; -layout (location = 1) in vec2 texcoord0; -layout (location = 2) in vec2 texcoord1; -layout (location = 3) in vec4 color0; -#elif defined(GLSL130) -in vec3 position; -in vec2 texcoord0; -in vec2 texcoord1; -in vec4 color0; -#elif defined(GLSL120) -attribute vec3 position; -attribute vec2 texcoord0; -attribute vec2 texcoord1; -attribute vec4 color0; -#endif -#ifdef GLSL120 -varying vec2 fragTexcoord0; -varying vec2 fragTexcoord1; -varying vec4 fragColor0; -#else -out vec2 fragTexcoord0; -out vec2 fragTexcoord1; -out vec4 fragColor0; -#endif -uniform mat4 model; -uniform mat4 view; -uniform mat4 projection; - -void main(void) { - fragTexcoord0 = texcoord0; - fragTexcoord1 = texcoord1; - fragColor0 = color0; - gl_Position = projection * view * model * vec4(position.xyz, 1.0); -} -" - " -#ifdef GLSL120 -attribute vec2 fragTexcoord0; -attribute vec2 fragTexcoord1 -attribute vec4 fragColor0; -#else -in vec2 fragTexcoord0; -in vec2 fragTexcoord1; -in vec4 fragColor0; -#endif -#ifdef GLSL330 -out vec4 fragColor; -#endif -uniform bool textured0; -uniform bool textured1; -uniform bool vertexColored; -uniform vec3 baseColorFactor; -uniform sampler2D texture0; -uniform sampler2D texture1; - -void main (void) { - vec4 finalColor = vec4(baseColorFactor, 1.0); - if(vertexColored) { - finalColor *= fragColor0; - } - if(textured0) { - finalColor *= texture(texture0, fragTexcoord0); - } - if(textured1) { - finalColor += texture(texture1, fragTexcoord1); - } -#ifdef GLSL330 - fragColor = finalColor; -#else - gl_FragColor = finalColor; -#endif -} -")) + (load-shader (scope-datadir "shaders/pbr-vert.glsl") + (scope-datadir "shaders/pbr-frag.glsl"))) (define (shader-apply/pbr vertex-array material model-matrix view-matrix) (let* ((shader (graphics-variable-ref pbr-shader)) diff --git a/data/shaders/pbr-frag.glsl b/data/shaders/pbr-frag.glsl new file mode 100644 index 0000000..813f9a9 --- /dev/null +++ b/data/shaders/pbr-frag.glsl @@ -0,0 +1,42 @@ +// -*- mode: c -*- + +#ifdef GLSL120 +attribute vec2 fragTexcoord0; +attribute vec2 fragTexcoord1 +attribute vec4 fragColor0; +#else +in vec2 fragTexcoord0; +in vec2 fragTexcoord1; +in vec4 fragColor0; +#endif + +#ifdef GLSL330 +out vec4 fragColor; +#endif + +uniform bool textured0; +uniform bool textured1; +uniform bool vertexColored; +uniform vec3 baseColorFactor; +uniform sampler2D texture0; +uniform sampler2D texture1; + +void main (void) { + vec4 finalColor = vec4(baseColorFactor, 1.0); + // Vertex coloring. + if(vertexColored) { + finalColor *= fragColor0; + } + // Texture sampling. + if(textured0) { + finalColor *= texture(texture0, fragTexcoord0); + } + if(textured1) { + finalColor += texture(texture1, fragTexcoord1); + } +#ifdef GLSL330 + fragColor = finalColor; +#else + gl_FragColor = finalColor; +#endif +} diff --git a/data/shaders/pbr-vert.glsl b/data/shaders/pbr-vert.glsl new file mode 100644 index 0000000..f622988 --- /dev/null +++ b/data/shaders/pbr-vert.glsl @@ -0,0 +1,39 @@ +// -*- mode: c -*- + +#ifdef GLSL330 +layout (location = 0) in vec3 position; +layout (location = 1) in vec2 texcoord0; +layout (location = 2) in vec2 texcoord1; +layout (location = 3) in vec4 color0; +#elif defined(GLSL130) +in vec3 position; +in vec2 texcoord0; +in vec2 texcoord1; +in vec4 color0; +#elif defined(GLSL120) +attribute vec3 position; +attribute vec2 texcoord0; +attribute vec2 texcoord1; +attribute vec4 color0; +#endif + +#ifdef GLSL120 +varying vec2 fragTexcoord0; +varying vec2 fragTexcoord1; +varying vec4 fragColor0; +#else +out vec2 fragTexcoord0; +out vec2 fragTexcoord1; +out vec4 fragColor0; +#endif + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main(void) { + fragTexcoord0 = texcoord0; + fragTexcoord1 = texcoord1; + fragColor0 = color0; + gl_Position = projection * view * model * vec4(position.xyz, 1.0); +} -- cgit v1.2.3