summaryrefslogtreecommitdiff
path: root/themes/src
diff options
context:
space:
mode:
authormattkae <mattkae@protonmail.com>2023-07-31 07:31:52 -0400
committermattkae <mattkae@protonmail.com>2023-07-31 07:31:52 -0400
commitb6a666e96ffd04bd6d52be8fd9899faf27b751db (patch)
treeddc56ec77d0fa6a35a581ea49bcd1b4c3cac3756 /themes/src
parentffa1ada2b7f65e0b3afe561b3291219394df21f3 (diff)
Some initial shader loading work
Diffstat (limited to 'themes/src')
-rw-r--r--themes/src/Renderer2d.cpp10
-rw-r--r--themes/src/Renderer2d.h5
-rw-r--r--themes/src/_shaders/renderer2d.frag (renamed from themes/src/shaders/renderer2d.frag)0
-rw-r--r--themes/src/_shaders/renderer2d.vert (renamed from themes/src/shaders/renderer2d.vert)0
-rw-r--r--themes/src/_shaders/renderer3d.frag (renamed from themes/src/shaders/renderer3d.frag)0
-rw-r--r--themes/src/_shaders/renderer3d.vert (renamed from themes/src/shaders/renderer3d.vert)0
-rw-r--r--themes/src/_shaders/sun.vert0
-rw-r--r--themes/src/main.cpp5
-rw-r--r--themes/src/shaders/renderer2d_frag.h4
-rw-r--r--themes/src/shaders/renderer2d_vert.h4
-rw-r--r--themes/src/shaders/renderer3d_frag.h11
-rw-r--r--themes/src/shaders/renderer3d_vert.h19
-rw-r--r--themes/src/shaders/sun_vert.h4
-rw-r--r--themes/src/summer/SummerTheme.cpp3
-rw-r--r--themes/src/summer/SummerTheme.h2
-rw-r--r--themes/src/tools/shader.js30
16 files changed, 87 insertions, 10 deletions
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
index e69de29..e69de29 100644
--- a/themes/src/shaders/renderer2d.frag
+++ b/themes/src/_shaders/renderer2d.frag
diff --git a/themes/src/shaders/renderer2d.vert b/themes/src/_shaders/renderer2d.vert
index e69de29..e69de29 100644
--- a/themes/src/shaders/renderer2d.vert
+++ b/themes/src/_shaders/renderer2d.vert
diff --git a/themes/src/shaders/renderer3d.frag b/themes/src/_shaders/renderer3d.frag
index 2f50347..2f50347 100644
--- a/themes/src/shaders/renderer3d.frag
+++ b/themes/src/_shaders/renderer3d.frag
diff --git a/themes/src/shaders/renderer3d.vert b/themes/src/_shaders/renderer3d.vert
index 026285f..026285f 100644
--- a/themes/src/shaders/renderer3d.vert
+++ b/themes/src/_shaders/renderer3d.vert
diff --git a/themes/src/_shaders/sun.vert b/themes/src/_shaders/sun.vert
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/themes/src/_shaders/sun.vert
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.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.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 <vector>
-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);
+});