summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shader.frag34
-rw-r--r--shader.vert4
-rw-r--r--sv.c136
3 files changed, 174 insertions, 0 deletions
diff --git a/shader.frag b/shader.frag
new file mode 100644
index 0000000..cf44e70
--- /dev/null
+++ b/shader.frag
@@ -0,0 +1,34 @@
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+uniform vec2 resolution;
+uniform float time;
+uniform float color;
+
+float box(in vec2 _st, in vec2 _size){
+ _size = vec2(0.5) - _size*0.5;
+ vec2 uv = smoothstep(_size,
+ _size+vec2(0.001),
+ _st);
+ uv *= smoothstep(_size,
+ _size+vec2(0.001),
+ vec2(1.0)-_st);
+ return uv.x*uv.y;
+}
+
+float cross(in vec2 _st, float _size){
+ return box(_st, vec2(_size,_size/4.)) +
+ box(_st, vec2(_size/4.,_size));
+}
+
+void main(){
+ vec2 st = gl_FragCoord.xy/resolution.xy;
+ vec3 c = vec3(0.);
+
+ c += vec3(cross(st,0.5*abs(cos(time))));
+ c.r -= color;
+ //c *= u_color;
+
+ gl_FragColor = vec4(c,1.0);
+}
diff --git a/shader.vert b/shader.vert
new file mode 100644
index 0000000..24eb59c
--- /dev/null
+++ b/shader.vert
@@ -0,0 +1,4 @@
+#version 420 core
+#extension GL_ARB_shading_language_420pack : require
+const vec2 quadVertices[4] = { vec2(-1.0, -1.0), vec2(1.0, -1.0), vec2(-1.0, 1.0), vec2(1.0, 1.0) };
+void main() { gl_Position = vec4(quadVertices[gl_VertexID], 0.0, 1.0); }
diff --git a/sv.c b/sv.c
new file mode 100644
index 0000000..669babb
--- /dev/null
+++ b/sv.c
@@ -0,0 +1,136 @@
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <pthread.h>
+
+static void error_callback(int error, const char* description) { fputs(description, stderr); }
+
+static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
+ if (key == GLFW_KEY_Q && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE);
+}
+
+const char * readShader(char * path) {
+ fputs(path,stderr);
+ fputs("\n",stderr);
+ FILE *file = fopen(path, "r");
+ if (!file) {
+ fputs("Cannot read ",stderr);
+ fputs(path,stderr);
+ exit(EXIT_FAILURE);
+ }
+ char source[2000];
+ int res = fread(source,1,2000-1,file);
+ source[res] = 0;
+ fclose(file);
+ const char *c_str = source;
+ return c_str;
+}
+
+GLuint compileShader(const char * source, GLenum type) {
+ GLuint shader = glCreateShader(type);
+ glShaderSource(shader, 1, &source, NULL);
+ glCompileShader(shader);
+ int success;
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
+ if (!success) {
+ char infoLog[512];
+ glGetShaderInfoLog(shader, 512, NULL, infoLog);
+ fputs("Shader compilation failed:\n",stderr);
+ fputs(infoLog,stderr);
+ exit(EXIT_FAILURE);
+ }
+ else { return shader; }
+}
+
+GLuint linkShader(GLuint vertex, GLuint fragment) {
+ GLuint shaderProgram = glCreateProgram();
+ glAttachShader(shaderProgram, vertex);
+ glAttachShader(shaderProgram, fragment);
+ glLinkProgram(shaderProgram);
+ int success;
+ char infoLog[512];
+ glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
+ if (!success) {
+ glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
+ fputs("Shader linking failed:\n",stderr);
+ fputs(infoLog,stderr);
+ }
+ glDetachShader(shaderProgram, vertex);
+ glDetachShader(shaderProgram, fragment);
+ glDeleteShader(vertex);
+ glDeleteShader(fragment);
+ glUseProgram(shaderProgram);
+ return shaderProgram;
+}
+
+GLFWwindow* createWindow() {
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
+ GLFWwindow* window = glfwCreateWindow(1920,1080, "", NULL, NULL);
+ glfwMakeContextCurrent(window);
+ //glfwSetKeyCallback(window, key_callback);
+ glfwSetErrorCallback(error_callback);
+ glewInit();
+ return window;
+}
+
+GLuint createShader() {
+ GLuint vertexShader = compileShader(readShader("shader.vert"), GL_VERTEX_SHADER);
+ GLuint fragmentShader = compileShader(readShader("shader.frag"),GL_FRAGMENT_SHADER);
+ GLuint shaderProgram = linkShader(vertexShader,fragmentShader);
+ unsigned int VAO;
+ glGenVertexArrays(1, &VAO);
+ glBindVertexArray(VAO);
+ return shaderProgram;
+};
+
+void uniform1f(GLuint * shader, char * var, float f) {
+ int v = glGetUniformLocation(*shader, var);
+ glUniform1f(v, f);
+}
+
+void uniform2f(GLuint shader, char * var, float f0, float f1) {
+ int v = glGetUniformLocation(shader, var);
+ glUniform2f(v,f0,f1);
+}
+
+float color = 0.9;
+
+void *readStdin(void * s) {
+ GLuint *shader = s;
+ while (1) {
+ char * str;
+ size_t l = 80;
+ getline(&str,&l,stdin);
+ char * variable = strtok (str," ");
+ float value = atof(strtok (NULL,"\n"));
+ printf("%.1f\n",value);
+ color = value;
+ }
+}
+
+int main(void) {
+
+ GLFWwindow* window = createWindow();
+ GLuint shader = createShader();
+ uniform2f(shader, "resolution", 1920,1080);
+ pthread_t tid;
+ pthread_create(&tid, NULL, readStdin, &shader);
+
+ while (!glfwWindowShouldClose(window)) {
+ uniform1f(&shader, "time",glfwGetTime());
+ uniform1f(&shader, "color",color);
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+ glfwSwapBuffers(window);
+ //glfwPollEvents();
+ }
+
+ //pthread_join(tid, NULL);
+ //printf("After Thread\n");
+ glfwDestroyWindow(window);
+ glfwTerminate();
+
+ exit(EXIT_SUCCESS);
+}