summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-10-20 23:23:40 -0700
committerVito Caputo <vcaputo@pengaru.com>2020-10-20 23:23:40 -0700
commit624b0a06edf7114950476a8ce79fff9927b1fc62 (patch)
tree159c9570df9c0bfdafe8f04c6b039fa8290b09a8
parent90ce0330f586dd7c0114beed6775ea6d47cf7c67 (diff)
pig: show sad face on shader errors
Introduces sad-node.[ch] implementing a simple sad face shader node, normally inactive but made active when the shader files fail to reload.
-rw-r--r--src/Makefile.am2
-rw-r--r--src/pig.c20
-rw-r--r--src/pig.h1
-rw-r--r--src/sad-node.c123
-rw-r--r--src/sad-node.h27
5 files changed, 171 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 168d4d1..0088044 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,6 +14,8 @@ pig_SOURCES = \
macros.h \
main.c \
m4f.h \
+ sad-node.c \
+ sad-node.h \
shader.c \
shader.h \
shader-node.c \
diff --git a/src/pig.c b/src/pig.c
index 1af910f..97d09fe 100644
--- a/src/pig.c
+++ b/src/pig.c
@@ -27,6 +27,7 @@
#include "m4f.h"
#include "macros.h"
#include "pig.h"
+#include "sad-node.h"
#include "shader-node.h"
#include "shader.h"
@@ -63,8 +64,12 @@ static void pig_uniforms_func(shader_t *shader, void *uniforms_ctxt, void *rende
unsigned t0, t1;
float r = randf();
- if (play_ticks_elapsed(play, PLAY_TICKS_TIMER2, 1000))
- shader_reload_files(shader);
+ if (play_ticks_elapsed(play, PLAY_TICKS_TIMER2, 1000)) {
+ if (shader_reload_files(shader) < 0)
+ stage_set_active(pig->sadface, 1);
+ else
+ stage_set_active(pig->sadface, 0);
+ }
/* this one just keeps increasing */
t0 = play_ticks(play, PLAY_TICKS_TIMER0);
@@ -212,6 +217,17 @@ static void * pig_init(play_t *play, int argc, char *argv[], unsigned flags)
&pig->color
);
+ pig->sadface = sad_node_new(&(stage_conf_t){
+ .parent = pig->stage,
+ .name = "sadface",
+ .layer = 3,
+ .active = 0,
+ .alpha = 1.f,
+ },
+ &pig->transform,
+ &pig->color
+ );
+
(void) shader_node_new_files(&(stage_conf_t){
.parent = pig->stage,
.name = "shader",
diff --git a/src/pig.h b/src/pig.h
index 3f0a24c..849e290 100644
--- a/src/pig.h
+++ b/src/pig.h
@@ -28,6 +28,7 @@ typedef struct pig_t {
SDL_Window *window;
SDL_GLContext *gl;
stage_t *stage;
+ stage_t *sadface;
unsigned window_width, window_height;
unsigned windowed:1;
m4f_t transform;
diff --git a/src/sad-node.c b/src/sad-node.c
new file mode 100644
index 0000000..6ec7a6c
--- /dev/null
+++ b/src/sad-node.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2018-2020 - Vito Caputo - <vcaputo@pengaru.com>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <SDL.h>
+
+#include <stage.h>
+
+#include "glad.h"
+#include "m4f.h"
+#include "sad-node.h"
+#include "shader-node.h"
+#include "v3f.h"
+
+
+static const char *sad_vs = ""
+ "#version 120\n"
+
+ "uniform mat4 model_x;"
+
+ "attribute vec3 i_vertex;"
+ "attribute vec2 i_texcoord;"
+
+ "void main()"
+ "{"
+ " gl_TexCoord[0].xy = i_texcoord;"
+ " gl_Position = model_x * vec4(i_vertex, 1.f);"
+ "}"
+"";
+
+
+static const char *sad_fs = ""
+ "#version 120\n"
+
+ "uniform float alpha;"
+ "uniform float time;"
+ "uniform float T;"
+ "uniform vec3 color;"
+ "uniform float seed; "
+ "uniform float rand;"
+
+ "const vec2 leye = vec2(-.5f, .5f);"
+ "const vec2 reye = vec2(.5f, .5f);"
+ "const vec2 frown = vec2(0.f, -1.2f);"
+ "const float eye_radius = .2f;"
+ "const float frown_radius = 1.f;"
+ "const float lip_thickness = .1f;"
+
+ "void eye(in vec2 center, inout vec4 c)"
+ "{"
+ " float r = length(center - gl_TexCoord[0].st);"
+
+ " if (r <= eye_radius) {"
+ " float v = (1.f / eye_radius) * r;"
+
+ " v = 1.f - pow(v, 20);"
+ " c = vec4(color * v, v);"
+ " }"
+ "}"
+
+ "void arc(vec2 center, inout vec4 c)"
+ "{"
+ " float r = length(center - gl_TexCoord[0].st);"
+
+ " if (r >= frown_radius - lip_thickness &&"
+ " r <= frown_radius + lip_thickness) {"
+ " float v = cos((r - (frown_radius + lip_thickness)) * 1.f / (lip_thickness * 2.f) * 6.28) * .5f + .5f;"
+
+ " v = 1.f - pow(v, 20);"
+ " c = vec4(color * v, v);"
+ " }"
+ "}"
+
+ "void main()"
+ "{"
+ " float r = length(gl_TexCoord[0].st);"
+ " vec4 c = vec4(0.f, 0.f, 0.f, 0.f);"
+
+ " eye(leye, c);"
+ " eye(reye, c);"
+ " arc(frown, c);"
+
+ " gl_FragColor = c;"
+ "}"
+"";
+
+
+static void sad_uniforms(shader_t *shader, void *uniforms_ctxt, void *render_ctxt, unsigned n_uniforms, const int *uniforms, const m4f_t *model_x, float alpha)
+{
+ v3f_t *color = uniforms_ctxt;
+
+ glUniform1f(uniforms[0], alpha);
+ glUniformMatrix4fv(uniforms[1], 1, GL_FALSE, &model_x->m[0][0]);
+ glUniform3f(uniforms[2], color->x, color->y, color->z);
+}
+
+
+/* create sad face rendering stage */
+stage_t * sad_node_new(const stage_conf_t *conf, const m4f_t *model_x, const v3f_t *color)
+{
+ assert(model_x);
+ assert(color);
+
+ return shader_node_new_src(conf, sad_vs, sad_fs, model_x, sad_uniforms, (void *)color, 3,
+ (const char *[]){
+ "alpha",
+ "model_x",
+ "color",
+ }
+ );
+}
diff --git a/src/sad-node.h b/src/sad-node.h
new file mode 100644
index 0000000..8bbfd9c
--- /dev/null
+++ b/src/sad-node.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2018-2020 - Vito Caputo - <vcaputo@pengaru.com>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _SAD_NODE_H
+#define _SAD_NODE_H
+
+typedef struct stage_t stage_t;
+typedef struct stage_conf_t stage_conf_t;
+typedef struct m4f_t m4f_t;
+typedef struct v3f_t v3f_t;
+
+stage_t * sad_node_new(const stage_conf_t *conf, const m4f_t *model_x, const v3f_t *color);
+
+#endif
© 2021 All Rights Reserved