From 61cd8d99e6a3475eaf2bd28bc24892cf1aae0398 Mon Sep 17 00:00:00 2001
From: Matthew Kosarek <matthew@matthewkosarek.xyz>
Date: Tue, 15 Aug 2023 07:29:58 -0400
Subject: WIP on the summer theme

---
 themes/src/_shaders/sun.frag      | 48 ++++++++++++++++++++++++++++++++++++
 themes/src/_shaders/sun.vert      | 13 ++++++++++
 themes/src/shaders/sun_frag.cpp   | 51 +++++++++++++++++++++++++++++++++++++++
 themes/src/shaders/sun_frag.h     |  4 +++
 themes/src/shaders/sun_vert.cpp   | 15 +++++++++++-
 themes/src/summer/SummerTheme.cpp |  4 ++-
 6 files changed, 133 insertions(+), 2 deletions(-)
 create mode 100644 themes/src/_shaders/sun.frag
 create mode 100644 themes/src/shaders/sun_frag.cpp
 create mode 100644 themes/src/shaders/sun_frag.h

(limited to 'themes')

diff --git a/themes/src/_shaders/sun.frag b/themes/src/_shaders/sun.frag
new file mode 100644
index 0000000..dabd7ea
--- /dev/null
+++ b/themes/src/_shaders/sun.frag
@@ -0,0 +1,48 @@
+varying lowp vec4 VertexColor;
+
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+uniform vec2 u_resolution;
+
+vec2 skew (vec2 st) {
+    vec2 r = vec2(0.0);
+    r.x = 1.1547*st.x;
+    r.y = st.y+0.5*r.x;
+    return r;
+}
+
+vec3 simplexGrid (vec2 st) {
+    vec3 xyz = vec3(0.0);
+
+    vec2 p = fract(skew(st));
+    if (p.x > p.y) {
+        xyz.xy = 1.0-vec2(p.x,p.y-p.x);
+        xyz.z = p.y;
+    } else {
+        xyz.yz = 1.0-vec2(p.x-p.y,p.y);
+        xyz.x = p.x;
+    }
+
+    return fract(xyz);
+}
+
+void main() {
+    vec2 st = gl_FragCoord.xy/u_resolution.xy;
+    vec3 color = VertexColor.xyz
+
+    // Scale the space to see the grid
+    st *= 10.;
+
+    // Show the 2D grid
+    color.rg = fract(st);
+
+    // Skew the 2D grid
+    // color.rg = fract(skew(st));
+
+    // Subdivide the grid into to equilateral triangles
+    // color = simplexGrid(st);
+
+    gl_FragColor = vec4(color,1.0);
+}
diff --git a/themes/src/_shaders/sun.vert b/themes/src/_shaders/sun.vert
index e69de29..76150f0 100644
--- a/themes/src/_shaders/sun.vert
+++ b/themes/src/_shaders/sun.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/sun_frag.cpp b/themes/src/shaders/sun_frag.cpp
new file mode 100644
index 0000000..d5af4be
--- /dev/null
+++ b/themes/src/shaders/sun_frag.cpp
@@ -0,0 +1,51 @@
+#include "sun_frag.h"
+
+const char* shader_sun_frag = "varying lowp vec4 VertexColor; \n"
+" \n"
+"#ifdef GL_ES \n"
+"precision mediump float; \n"
+"#endif \n"
+" \n"
+"uniform vec2 u_resolution; \n"
+" \n"
+"vec2 skew (vec2 st) { \n"
+"    vec2 r = vec2(0.0); \n"
+"    r.x = 1.1547*st.x; \n"
+"    r.y = st.y+0.5*r.x; \n"
+"    return r; \n"
+"} \n"
+" \n"
+"vec3 simplexGrid (vec2 st) { \n"
+"    vec3 xyz = vec3(0.0); \n"
+" \n"
+"    vec2 p = fract(skew(st)); \n"
+"    if (p.x > p.y) { \n"
+"        xyz.xy = 1.0-vec2(p.x,p.y-p.x); \n"
+"        xyz.z = p.y; \n"
+"    } else { \n"
+"        xyz.yz = 1.0-vec2(p.x-p.y,p.y); \n"
+"        xyz.x = p.x; \n"
+"    } \n"
+" \n"
+"    return fract(xyz); \n"
+"} \n"
+" \n"
+"void main() { \n"
+"    vec2 st = gl_FragCoord.xy/u_resolution.xy; \n"
+"    vec3 color = VertexColor.xyz \n"
+" \n"
+"    // Scale the space to see the grid \n"
+"    st *= 10.; \n"
+" \n"
+"    // Show the 2D grid \n"
+"    color.rg = fract(st); \n"
+" \n"
+"    // Skew the 2D grid \n"
+"    // color.rg = fract(skew(st)); \n"
+" \n"
+"    // Subdivide the grid into to equilateral triangles \n"
+"    // color = simplexGrid(st); \n"
+" \n"
+"    gl_FragColor = vec4(color,1.0); \n"
+"} \n"
+" \n";
diff --git a/themes/src/shaders/sun_frag.h b/themes/src/shaders/sun_frag.h
new file mode 100644
index 0000000..e657cf6
--- /dev/null
+++ b/themes/src/shaders/sun_frag.h
@@ -0,0 +1,4 @@
+#ifndef SHADER_SUN_FRAG 
+#define SHADER_SUN_FRAG 
+extern const char* shader_sun_frag;
+#endif
diff --git a/themes/src/shaders/sun_vert.cpp b/themes/src/shaders/sun_vert.cpp
index 0049bfd..ca617c0 100644
--- a/themes/src/shaders/sun_vert.cpp
+++ b/themes/src/shaders/sun_vert.cpp
@@ -1,3 +1,16 @@
 #include "sun_vert.h"
 
-const char* shader_sun_vert = " \n";
+const char* shader_sun_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/summer/SummerTheme.cpp b/themes/src/summer/SummerTheme.cpp
index 81cc43b..6106c89 100644
--- a/themes/src/summer/SummerTheme.cpp
+++ b/themes/src/summer/SummerTheme.cpp
@@ -2,10 +2,12 @@
 #include "../Renderer2d.h"
 #include "../list.h"
 #include "../mathlib.h"
+#include "../shaders/sun_frag.h"
+#include "../shaders/sun_vert.h"
 #include <vector>
 
 void SummerTheme::load(Renderer2d* renderer, WebglContext* context) {
-    renderer->load(context);
+    renderer->load(context, shader_sun_vert, shader_sun_frag);
 	renderer->clearColor = Vector4(0, 181, 286, 255.f).toNormalizedColor();
 	sun.sectors = 180;
 	sun.radius = renderer->context->width / 4.f;
-- 
cgit v1.2.1