diff options
author | mattkae <mattkae@protonmail.com> | 2023-08-13 16:02:22 -0400 |
---|---|---|
committer | mattkae <mattkae@protonmail.com> | 2023-08-13 16:02:22 -0400 |
commit | 056d4560f72a9ec281f8df31aa2a7d8241d6adf1 (patch) | |
tree | d0bcbe3ad5dd9e4efb662389b1aa6806cc4365ef /themes/src | |
parent | b6a666e96ffd04bd6d52be8fd9899faf27b751db (diff) |
Generating a cpp file for shaders
Diffstat (limited to 'themes/src')
-rw-r--r-- | themes/src/Renderer2d.cpp | 24 | ||||
-rw-r--r-- | themes/src/Renderer2d.h | 2 | ||||
-rw-r--r-- | themes/src/_shaders/renderer2d.frag | 5 | ||||
-rw-r--r-- | themes/src/_shaders/renderer2d.vert | 13 | ||||
-rw-r--r-- | themes/src/shaders/renderer2d_frag.cpp | 8 | ||||
-rw-r--r-- | themes/src/shaders/renderer2d_frag.h | 2 | ||||
-rw-r--r-- | themes/src/shaders/renderer2d_vert.cpp | 16 | ||||
-rw-r--r-- | themes/src/shaders/renderer2d_vert.h | 2 | ||||
-rw-r--r-- | themes/src/shaders/renderer3d_frag.cpp | 10 | ||||
-rw-r--r-- | themes/src/shaders/renderer3d_frag.h | 9 | ||||
-rw-r--r-- | themes/src/shaders/renderer3d_vert.cpp | 18 | ||||
-rw-r--r-- | themes/src/shaders/renderer3d_vert.h | 17 | ||||
-rw-r--r-- | themes/src/shaders/sun_vert.cpp | 3 | ||||
-rw-r--r-- | themes/src/shaders/sun_vert.h | 2 | ||||
-rw-r--r-- | themes/src/tools/shader.js | 23 |
15 files changed, 99 insertions, 55 deletions
diff --git a/themes/src/Renderer2d.cpp b/themes/src/Renderer2d.cpp index 06f0838..196ef7e 100644 --- a/themes/src/Renderer2d.cpp +++ b/themes/src/Renderer2d.cpp @@ -3,31 +3,15 @@ #include "WebglContext.h" #include "mathlib.h" #include <cstdio> +#include "shaders/renderer2d_vert.h" +#include "shaders/renderer2d_frag.h" // Note: In the 'transform' attribute, the transform.x is the scale, // transform.y is the rotation, and transform.zw is the translation. -const char* DEFAULT_VERTEX_SHADER = -"attribute vec2 position; \n" -"attribute vec4 color; \n" -"attribute mat4 vMatrix; \n" -"uniform mat4 projection; \n" -"uniform mat4 model; \n" -"varying lowp vec4 VertexColor; \n" -"void main() { \n" -" vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); \n" -" gl_Position = fragmentPosition; \n" -" VertexColor = color; \n" -"}"; - -const char* DEFAULT_FRAGMENT_SHADER = -"varying lowp vec4 VertexColor; \n" -"void main() { \n" -" gl_FragColor = VertexColor; \n" -"}"; void Renderer2d::load(WebglContext* inContext, const char* inVertexShader, const char* inFragmentShader) { - auto vertexShader = inVertexShader ? inVertexShader : DEFAULT_VERTEX_SHADER; - auto fragmentShader = inFragmentShader ? inFragmentShader : DEFAULT_FRAGMENT_SHADER; + auto vertexShader = inVertexShader ? inVertexShader : shader_renderer2d_vert; + auto fragmentShader = inFragmentShader ? inFragmentShader : shader_renderer2d_frag; context = inContext; printf("Compiling Renderer2d shader...\n"); shader = loadShader(vertexShader, fragmentShader); diff --git a/themes/src/Renderer2d.h b/themes/src/Renderer2d.h index 8a96503..56b79fc 100644 --- a/themes/src/Renderer2d.h +++ b/themes/src/Renderer2d.h @@ -17,6 +17,8 @@ struct Renderer2d { struct { i32 position; i32 color; + + // TODO: vMatrix is not standard and does not belong here i32 vMatrix; } attributes; diff --git a/themes/src/_shaders/renderer2d.frag b/themes/src/_shaders/renderer2d.frag index e69de29..8463e06 100644 --- a/themes/src/_shaders/renderer2d.frag +++ b/themes/src/_shaders/renderer2d.frag @@ -0,0 +1,5 @@ +varying lowp vec4 VertexColor; + +void main() { + gl_FragColor = VertexColor; +} diff --git a/themes/src/_shaders/renderer2d.vert b/themes/src/_shaders/renderer2d.vert index e69de29..76150f0 100644 --- a/themes/src/_shaders/renderer2d.vert +++ b/themes/src/_shaders/renderer2d.vert @@ -0,0 +1,13 @@ + +attribute vec2 position; +attribute vec4 color; +attribute mat4 vMatrix; +uniform mat4 projection; +uniform mat4 model; +varying lowp vec4 VertexColor; + +void main() { + vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); + gl_Position = fragmentPosition; + VertexColor = color; +} diff --git a/themes/src/shaders/renderer2d_frag.cpp b/themes/src/shaders/renderer2d_frag.cpp new file mode 100644 index 0000000..7650fce --- /dev/null +++ b/themes/src/shaders/renderer2d_frag.cpp @@ -0,0 +1,8 @@ +#include "renderer2d_frag.h" + +const char* shader_renderer2d_frag = "varying lowp vec4 VertexColor; \n" +" \n" +"void main() { \n" +" gl_FragColor = VertexColor; \n" +"} \n" +" \n"; diff --git a/themes/src/shaders/renderer2d_frag.h b/themes/src/shaders/renderer2d_frag.h index 6f5e472..20cff24 100644 --- a/themes/src/shaders/renderer2d_frag.h +++ b/themes/src/shaders/renderer2d_frag.h @@ -1,4 +1,4 @@ #ifndef SHADER_RENDERER2D_FRAG #define SHADER_RENDERER2D_FRAG -const char* shader_renderer2d_frag = " \n"; +extern const char* shader_renderer2d_frag; #endif diff --git a/themes/src/shaders/renderer2d_vert.cpp b/themes/src/shaders/renderer2d_vert.cpp new file mode 100644 index 0000000..9b4182e --- /dev/null +++ b/themes/src/shaders/renderer2d_vert.cpp @@ -0,0 +1,16 @@ +#include "renderer2d_vert.h" + +const char* shader_renderer2d_vert = " \n" +"attribute vec2 position; \n" +"attribute vec4 color; \n" +"attribute mat4 vMatrix; \n" +"uniform mat4 projection; \n" +"uniform mat4 model; \n" +"varying lowp vec4 VertexColor; \n" +" \n" +"void main() { \n" +" vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); \n" +" gl_Position = fragmentPosition; \n" +" VertexColor = color; \n" +"} \n" +" \n"; diff --git a/themes/src/shaders/renderer2d_vert.h b/themes/src/shaders/renderer2d_vert.h index b32cf9f..cfd09c5 100644 --- a/themes/src/shaders/renderer2d_vert.h +++ b/themes/src/shaders/renderer2d_vert.h @@ -1,4 +1,4 @@ #ifndef SHADER_RENDERER2D_VERT #define SHADER_RENDERER2D_VERT -const char* shader_renderer2d_vert = " \n"; +extern const char* shader_renderer2d_vert; #endif diff --git a/themes/src/shaders/renderer3d_frag.cpp b/themes/src/shaders/renderer3d_frag.cpp new file mode 100644 index 0000000..a83f2e6 --- /dev/null +++ b/themes/src/shaders/renderer3d_frag.cpp @@ -0,0 +1,10 @@ +#include "renderer3d_frag.h" + +const char* shader_renderer3d_frag = "varying lowp vec4 VertexColor; \n" +"varying lowp vec4 VertexNormal; \n" +" \n" +"void main() { \n" +" const lowp vec3 lightDirection = vec3(0.0, 1.0, 0.0); \n" +" gl_FragColor = vec4(VertexColor.xyz * dot(VertexNormal.xyz, lightDirection), 1); \n" +"} \n" +" \n"; diff --git a/themes/src/shaders/renderer3d_frag.h b/themes/src/shaders/renderer3d_frag.h index 6b3a936..25f1fdf 100644 --- a/themes/src/shaders/renderer3d_frag.h +++ b/themes/src/shaders/renderer3d_frag.h @@ -1,11 +1,4 @@ #ifndef SHADER_RENDERER3D_FRAG #define SHADER_RENDERER3D_FRAG -const char* shader_renderer3d_frag = "varying lowp vec4 VertexColor; \n" -"varying lowp vec4 VertexNormal; \n" -" \n" -"void main() { \n" -" const lowp vec3 lightDirection = vec3(0.0, 1.0, 0.0); \n" -" gl_FragColor = vec4(VertexColor.xyz * dot(VertexNormal.xyz, lightDirection), 1); \n" -"} \n" -" \n"; +extern const char* shader_renderer3d_frag; #endif diff --git a/themes/src/shaders/renderer3d_vert.cpp b/themes/src/shaders/renderer3d_vert.cpp new file mode 100644 index 0000000..083a69d --- /dev/null +++ b/themes/src/shaders/renderer3d_vert.cpp @@ -0,0 +1,18 @@ +#include "renderer3d_vert.h" + +const char* shader_renderer3d_vert = "attribute vec4 position; \n" +"attribute vec4 color; \n" +"attribute vec4 normal; \n" +"uniform mat4 projection; \n" +"uniform mat4 view; \n" +"uniform mat4 model; \n" +"varying lowp vec4 VertexColor; \n" +"varying lowp vec4 VertexNormal; \n" +" \n" +"void main() { \n" +" vec4 fragmentPosition = projection * view * model * position; \n" +" gl_Position = fragmentPosition; \n" +" VertexColor = color; \n" +" VertexNormal = normal; \n" +"} \n" +" \n"; diff --git a/themes/src/shaders/renderer3d_vert.h b/themes/src/shaders/renderer3d_vert.h index 20e69f9..760cad5 100644 --- a/themes/src/shaders/renderer3d_vert.h +++ b/themes/src/shaders/renderer3d_vert.h @@ -1,19 +1,4 @@ #ifndef SHADER_RENDERER3D_VERT #define SHADER_RENDERER3D_VERT -const char* shader_renderer3d_vert = "attribute vec4 position; \n" -"attribute vec4 color; \n" -"attribute vec4 normal; \n" -"uniform mat4 projection; \n" -"uniform mat4 view; \n" -"uniform mat4 model; \n" -"varying lowp vec4 VertexColor; \n" -"varying lowp vec4 VertexNormal; \n" -" \n" -"void main() { \n" -" vec4 fragmentPosition = projection * view * model * position; \n" -" gl_Position = fragmentPosition; \n" -" VertexColor = color; \n" -" VertexNormal = normal; \n" -"} \n" -" \n"; +extern const char* shader_renderer3d_vert; #endif diff --git a/themes/src/shaders/sun_vert.cpp b/themes/src/shaders/sun_vert.cpp new file mode 100644 index 0000000..0049bfd --- /dev/null +++ b/themes/src/shaders/sun_vert.cpp @@ -0,0 +1,3 @@ +#include "sun_vert.h" + +const char* shader_sun_vert = " \n"; diff --git a/themes/src/shaders/sun_vert.h b/themes/src/shaders/sun_vert.h index 2f9bf69..9c95b49 100644 --- a/themes/src/shaders/sun_vert.h +++ b/themes/src/shaders/sun_vert.h @@ -1,4 +1,4 @@ #ifndef SHADER_SUN_VERT #define SHADER_SUN_VERT -const char* shader_sun_vert = " \n"; +extern const char* shader_sun_vert; #endif diff --git a/themes/src/tools/shader.js b/themes/src/tools/shader.js index 27c8682..54ba6e0 100644 --- a/themes/src/tools/shader.js +++ b/themes/src/tools/shader.js @@ -15,16 +15,23 @@ files.forEach(file => { const splitText = text.split('\n'); const splitName = file.split('.'); const def = `SHADER_${splitName.join('_').toUpperCase()}`; - let result = `#ifndef ${def} \n`; - result += `#define ${def} \n`; - result += `const char* ${def.toLowerCase()} = ` + let header_result = `#ifndef ${def} \n`; + const filename = splitName.join('_'); + const header_name = filename + ".h"; + const cpp_name = filename + ".cpp"; + header_result += `#define ${def} \n`; + header_result += `extern const char* ${def.toLowerCase()};\n`; + header_result += '#endif\n'; + fs.writeFileSync(path.join(out_directory, header_name), header_result); + + let cpp_result = `#include "${header_name}"\n\n` + cpp_result += `const char* ${def.toLowerCase()} = ` splitText.forEach((line, index) => { - result += "\"" + line + " \\n\""; + cpp_result += "\"" + line + " \\n\""; if (index == splitText.length - 1) - result += ";\n"; + cpp_result += ";\n"; else - result += "\n"; + cpp_result += "\n"; }); - result += '#endif\n'; - fs.writeFileSync(path.join(out_directory, splitName.join('_') + '.h'), result); + fs.writeFileSync(path.join(out_directory, cpp_name), cpp_result); }); |