From b6a666e96ffd04bd6d52be8fd9899faf27b751db Mon Sep 17 00:00:00 2001 From: mattkae Date: Mon, 31 Jul 2023 07:31:52 -0400 Subject: Some initial shader loading work --- themes/src/Renderer2d.cpp | 10 ++++++---- themes/src/Renderer2d.h | 5 ++++- themes/src/_shaders/renderer2d.frag | 0 themes/src/_shaders/renderer2d.vert | 0 themes/src/_shaders/renderer3d.frag | 7 +++++++ themes/src/_shaders/renderer3d.vert | 15 +++++++++++++++ themes/src/_shaders/sun.vert | 0 themes/src/main.cpp | 5 ++--- themes/src/shaders/renderer2d.frag | 0 themes/src/shaders/renderer2d.vert | 0 themes/src/shaders/renderer2d_frag.h | 4 ++++ themes/src/shaders/renderer2d_vert.h | 4 ++++ themes/src/shaders/renderer3d.frag | 7 ------- themes/src/shaders/renderer3d.vert | 15 --------------- themes/src/shaders/renderer3d_frag.h | 11 +++++++++++ themes/src/shaders/renderer3d_vert.h | 19 +++++++++++++++++++ themes/src/shaders/sun_vert.h | 4 ++++ themes/src/summer/SummerTheme.cpp | 3 ++- themes/src/summer/SummerTheme.h | 2 +- themes/src/tools/shader.js | 30 ++++++++++++++++++++++++++++++ 20 files changed, 109 insertions(+), 32 deletions(-) create mode 100644 themes/src/_shaders/renderer2d.frag create mode 100644 themes/src/_shaders/renderer2d.vert create mode 100644 themes/src/_shaders/renderer3d.frag create mode 100644 themes/src/_shaders/renderer3d.vert create mode 100644 themes/src/_shaders/sun.vert delete mode 100644 themes/src/shaders/renderer2d.frag delete mode 100644 themes/src/shaders/renderer2d.vert create mode 100644 themes/src/shaders/renderer2d_frag.h create mode 100644 themes/src/shaders/renderer2d_vert.h delete mode 100644 themes/src/shaders/renderer3d.frag delete mode 100644 themes/src/shaders/renderer3d.vert create mode 100644 themes/src/shaders/renderer3d_frag.h create mode 100644 themes/src/shaders/renderer3d_vert.h create mode 100644 themes/src/shaders/sun_vert.h create mode 100644 themes/src/tools/shader.js (limited to 'themes/src') diff --git a/themes/src/Renderer2d.cpp b/themes/src/Renderer2d.cpp index c303e83..06f0838 100644 --- a/themes/src/Renderer2d.cpp +++ b/themes/src/Renderer2d.cpp @@ -6,7 +6,7 @@ // Note: In the 'transform' attribute, the transform.x is the scale, // transform.y is the rotation, and transform.zw is the translation. -const char* Vertex2DShader = +const char* DEFAULT_VERTEX_SHADER = "attribute vec2 position; \n" "attribute vec4 color; \n" "attribute mat4 vMatrix; \n" @@ -19,16 +19,18 @@ const char* Vertex2DShader = " VertexColor = color; \n" "}"; -const char* renderer2dFragmentShader = +const char* DEFAULT_FRAGMENT_SHADER = "varying lowp vec4 VertexColor; \n" "void main() { \n" " gl_FragColor = VertexColor; \n" "}"; -void Renderer2d::load(WebglContext* inContext) { +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; context = inContext; printf("Compiling Renderer2d shader...\n"); - shader = loadShader(Vertex2DShader, renderer2dFragmentShader); + shader = loadShader(vertexShader, fragmentShader); useShader(shader); attributes.position = getShaderAttribute(shader, "position"); diff --git a/themes/src/Renderer2d.h b/themes/src/Renderer2d.h index 909f088..8a96503 100644 --- a/themes/src/Renderer2d.h +++ b/themes/src/Renderer2d.h @@ -7,6 +7,7 @@ struct WebglContext; +/// Responsible for rendering Mesh2Ds struct Renderer2d { WebglContext* context = NULL; Mat4x4 projection; @@ -24,7 +25,9 @@ struct Renderer2d { i32 model; } uniforms; - void load(WebglContext* context); + /// Load with the provided context and shader programs. If the shaders are NULL, the default + /// shader is used + void load(WebglContext* context, const char* vertexShader = NULL, const char* fragmentShader = NULL); void render(); void unload(); }; diff --git a/themes/src/_shaders/renderer2d.frag b/themes/src/_shaders/renderer2d.frag new file mode 100644 index 0000000..e69de29 diff --git a/themes/src/_shaders/renderer2d.vert b/themes/src/_shaders/renderer2d.vert new file mode 100644 index 0000000..e69de29 diff --git a/themes/src/_shaders/renderer3d.frag b/themes/src/_shaders/renderer3d.frag new file mode 100644 index 0000000..2f50347 --- /dev/null +++ b/themes/src/_shaders/renderer3d.frag @@ -0,0 +1,7 @@ +varying lowp vec4 VertexColor; +varying lowp vec4 VertexNormal; + +void main() { + const lowp vec3 lightDirection = vec3(0.0, 1.0, 0.0); + gl_FragColor = vec4(VertexColor.xyz * dot(VertexNormal.xyz, lightDirection), 1); +} diff --git a/themes/src/_shaders/renderer3d.vert b/themes/src/_shaders/renderer3d.vert new file mode 100644 index 0000000..026285f --- /dev/null +++ b/themes/src/_shaders/renderer3d.vert @@ -0,0 +1,15 @@ +attribute vec4 position; +attribute vec4 color; +attribute vec4 normal; +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; +varying lowp vec4 VertexColor; +varying lowp vec4 VertexNormal; + +void main() { + vec4 fragmentPosition = projection * view * model * position; + gl_Position = fragmentPosition; + VertexColor = color; + VertexNormal = normal; +} diff --git a/themes/src/_shaders/sun.vert b/themes/src/_shaders/sun.vert new file mode 100644 index 0000000..e69de29 diff --git a/themes/src/main.cpp b/themes/src/main.cpp index 4e1a646..297a498 100644 --- a/themes/src/main.cpp +++ b/themes/src/main.cpp @@ -74,8 +74,7 @@ void load(Theme theme) { break; } case Theme::Summer: - renderer2d.load(&context); - summerTheme.load(&renderer2d); + summerTheme.load(&renderer2d, &context); break; default: break; @@ -177,4 +176,4 @@ EM_BOOL selectSummer(int eventType, const EmscriptenMouseEvent* mouseEvent, void printf("Summer theme selected\n"); load(Theme::Summer); return true; -} \ No newline at end of file +} diff --git a/themes/src/shaders/renderer2d.frag b/themes/src/shaders/renderer2d.frag deleted file mode 100644 index e69de29..0000000 diff --git a/themes/src/shaders/renderer2d.vert b/themes/src/shaders/renderer2d.vert deleted file mode 100644 index e69de29..0000000 diff --git a/themes/src/shaders/renderer2d_frag.h b/themes/src/shaders/renderer2d_frag.h new file mode 100644 index 0000000..6f5e472 --- /dev/null +++ b/themes/src/shaders/renderer2d_frag.h @@ -0,0 +1,4 @@ +#ifndef SHADER_RENDERER2D_FRAG +#define SHADER_RENDERER2D_FRAG +const char* shader_renderer2d_frag = " \n"; +#endif diff --git a/themes/src/shaders/renderer2d_vert.h b/themes/src/shaders/renderer2d_vert.h new file mode 100644 index 0000000..b32cf9f --- /dev/null +++ b/themes/src/shaders/renderer2d_vert.h @@ -0,0 +1,4 @@ +#ifndef SHADER_RENDERER2D_VERT +#define SHADER_RENDERER2D_VERT +const char* shader_renderer2d_vert = " \n"; +#endif diff --git a/themes/src/shaders/renderer3d.frag b/themes/src/shaders/renderer3d.frag deleted file mode 100644 index 2f50347..0000000 --- a/themes/src/shaders/renderer3d.frag +++ /dev/null @@ -1,7 +0,0 @@ -varying lowp vec4 VertexColor; -varying lowp vec4 VertexNormal; - -void main() { - const lowp vec3 lightDirection = vec3(0.0, 1.0, 0.0); - gl_FragColor = vec4(VertexColor.xyz * dot(VertexNormal.xyz, lightDirection), 1); -} diff --git a/themes/src/shaders/renderer3d.vert b/themes/src/shaders/renderer3d.vert deleted file mode 100644 index 026285f..0000000 --- a/themes/src/shaders/renderer3d.vert +++ /dev/null @@ -1,15 +0,0 @@ -attribute vec4 position; -attribute vec4 color; -attribute vec4 normal; -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; -varying lowp vec4 VertexColor; -varying lowp vec4 VertexNormal; - -void main() { - vec4 fragmentPosition = projection * view * model * position; - gl_Position = fragmentPosition; - VertexColor = color; - VertexNormal = normal; -} diff --git a/themes/src/shaders/renderer3d_frag.h b/themes/src/shaders/renderer3d_frag.h new file mode 100644 index 0000000..6b3a936 --- /dev/null +++ b/themes/src/shaders/renderer3d_frag.h @@ -0,0 +1,11 @@ +#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"; +#endif diff --git a/themes/src/shaders/renderer3d_vert.h b/themes/src/shaders/renderer3d_vert.h new file mode 100644 index 0000000..20e69f9 --- /dev/null +++ b/themes/src/shaders/renderer3d_vert.h @@ -0,0 +1,19 @@ +#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"; +#endif diff --git a/themes/src/shaders/sun_vert.h b/themes/src/shaders/sun_vert.h new file mode 100644 index 0000000..2f9bf69 --- /dev/null +++ b/themes/src/shaders/sun_vert.h @@ -0,0 +1,4 @@ +#ifndef SHADER_SUN_VERT +#define SHADER_SUN_VERT +const char* shader_sun_vert = " \n"; +#endif diff --git a/themes/src/summer/SummerTheme.cpp b/themes/src/summer/SummerTheme.cpp index 406cd22..81cc43b 100644 --- a/themes/src/summer/SummerTheme.cpp +++ b/themes/src/summer/SummerTheme.cpp @@ -4,7 +4,8 @@ #include "../mathlib.h" #include -void SummerTheme::load(Renderer2d* renderer) { +void SummerTheme::load(Renderer2d* renderer, WebglContext* context) { + renderer->load(context); renderer->clearColor = Vector4(0, 181, 286, 255.f).toNormalizedColor(); sun.sectors = 180; sun.radius = renderer->context->width / 4.f; diff --git a/themes/src/summer/SummerTheme.h b/themes/src/summer/SummerTheme.h index 4a9f76b..dad4ac9 100644 --- a/themes/src/summer/SummerTheme.h +++ b/themes/src/summer/SummerTheme.h @@ -16,7 +16,7 @@ struct Sun { struct SummerTheme { Sun sun; - void load(Renderer2d* renderer); + void load(Renderer2d*, WebglContext*); void update(f32 dtSeconds); void render(Renderer2d* renderer); void unload(); diff --git a/themes/src/tools/shader.js b/themes/src/tools/shader.js new file mode 100644 index 0000000..27c8682 --- /dev/null +++ b/themes/src/tools/shader.js @@ -0,0 +1,30 @@ +/* + Responsible for generating .h files from GLSL files. + */ + + +const path = require('path'); +const fs = require('fs'); +const directory = path.join(__dirname, "..", "_shaders"); +const out_directory = path.join(__dirname, "..", "shaders"); + +const files = fs.readdirSync(directory); +files.forEach(file => { + const filePath = path.join(directory, file); + const text = String(fs.readFileSync(filePath)); + 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()} = ` + splitText.forEach((line, index) => { + result += "\"" + line + " \\n\""; + if (index == splitText.length - 1) + result += ";\n"; + else + result += "\n"; + }); + result += '#endif\n'; + fs.writeFileSync(path.join(out_directory, splitName.join('_') + '.h'), result); +}); -- cgit v1.2.1