summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shader.frag37
-rw-r--r--sv.c168
2 files changed, 138 insertions, 67 deletions
diff --git a/shader.frag b/shader.frag
index cf44e70..2084f1c 100644
--- a/shader.frag
+++ b/shader.frag
@@ -4,31 +4,22 @@ precision mediump float;
uniform vec2 resolution;
uniform float time;
-uniform float color;
+uniform float m;
+//uniform sampler2D backbuffer;
-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;
-}
+uniform sampler2D image1;
+uniform sampler2D image2;
+uniform sampler2D image3;
+//uniform vec2 u_tex0Resolution;
-float cross(in vec2 _st, float _size){
- return box(_st, vec2(_size,_size/4.)) +
- box(_st, vec2(_size/4.,_size));
+float random (in float x) {
+ return fract(sin(x)*43758.5453123)-0.5;
}
-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);
+void main (void) {
+ vec2 st = gl_FragCoord.xy/resolution.xy;
+ vec4 i1 = texture2D(image1,st*0.8);
+ vec4 i2 = texture2D(image2,st);
+ gl_FragColor = mix(i1,i2,m);
+ //gl_FragColor = texture2D(image1,st*sin(time*2.));
}
diff --git a/sv.c b/sv.c
index 669babb..6d8ab3e 100644
--- a/sv.c
+++ b/sv.c
@@ -5,6 +5,33 @@
#include <string.h>
#include <pthread.h>
+#define STB_IMAGE_IMPLEMENTATION
+#include "std/stb_image.h"
+#include "uthash/src/utstring.h"
+#include "uthash/src/uthash.h"
+
+int width = 1920;
+int height = 1080;
+GLFWwindow* window;
+GLuint vertex;
+GLuint fragment;
+GLuint shader;
+
+struct texture {
+ char name[10];
+ GLuint id;
+ UT_hash_handle hh; /* makes this structure hashable */
+};
+
+struct param {
+ char name[10];
+ float value;
+ UT_hash_handle hh; /* makes this structure hashable */
+};
+
+struct param *parameters = NULL;
+struct texture *textures = NULL;
+
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) {
@@ -29,102 +56,155 @@ const char * readShader(char * path) {
}
GLuint compileShader(const char * source, GLenum type) {
- GLuint shader = glCreateShader(type);
- glShaderSource(shader, 1, &source, NULL);
- glCompileShader(shader);
+ GLuint sh = glCreateShader(type);
+ glShaderSource(sh, 1, &source, NULL);
+ glCompileShader(sh);
int success;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
+ glGetShaderiv(sh, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
- glGetShaderInfoLog(shader, 512, NULL, infoLog);
+ glGetShaderInfoLog(sh, 512, NULL, infoLog);
fputs("Shader compilation failed:\n",stderr);
fputs(infoLog,stderr);
exit(EXIT_FAILURE);
}
- else { return shader; }
+ else { return sh; }
}
GLuint linkShader(GLuint vertex, GLuint fragment) {
- GLuint shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertex);
- glAttachShader(shaderProgram, fragment);
- glLinkProgram(shaderProgram);
+ shader = glCreateProgram();
+ glAttachShader(shader, vertex);
+ glAttachShader(shader, fragment);
+ glLinkProgram(shader);
int success;
char infoLog[512];
- glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
+ glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
- glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
+ glGetProgramInfoLog(shader, 512, NULL, infoLog);
fputs("Shader linking failed:\n",stderr);
fputs(infoLog,stderr);
}
- glDetachShader(shaderProgram, vertex);
- glDetachShader(shaderProgram, fragment);
+ glDetachShader(shader, vertex);
+ glDetachShader(shader, fragment);
glDeleteShader(vertex);
glDeleteShader(fragment);
- glUseProgram(shaderProgram);
- return shaderProgram;
+ glUseProgram(shader);
}
-GLFWwindow* createWindow() {
+void createWindow() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- GLFWwindow* window = glfwCreateWindow(1920,1080, "", NULL, NULL);
+ window = glfwCreateWindow(width,height, "", NULL, NULL);
glfwMakeContextCurrent(window);
- //glfwSetKeyCallback(window, key_callback);
+ 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);
+void createShader(char *vert, char *frag) {
+ vertex = compileShader(readShader(vert), GL_VERTEX_SHADER);
+ fragment = compileShader(readShader(frag),GL_FRAGMENT_SHADER);
+ linkShader(vertex,fragment);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
- return shaderProgram;
};
-void uniform1f(GLuint * shader, char * var, float f) {
- int v = glGetUniformLocation(*shader, var);
+void readImage(char *name, char *file) {
+ int n = HASH_COUNT(textures);
+ glGenTextures(1, &n);
+ glBindTexture(GL_TEXTURE_2D, n);
+ int comp;
+ unsigned char* pixels = stbi_load(file, &width, &height, &comp, STBI_rgb_alpha);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+ struct texture *t;
+ t = (struct texture*)malloc(sizeof(struct texture));
+ strncpy(t->name, name,10);
+ int v = glGetUniformLocation(shader, name);
+ printf("loc %.i\n",v);
+ glUniform1i(v, n);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ stbi_image_free(pixels);
+}
+
+void uniform1f(char * var, float f) {
+ int v = glGetUniformLocation(shader, var);
glUniform1f(v, f);
}
-void uniform2f(GLuint shader, char * var, float f0, float f1) {
+void uniform2f(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;
+void *readStdin() {
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;
+ char *n = strtok(str," ");
+ UT_string *name;
+ utstring_new(name);
+ utstring_printf(name, n);
+
+ if (utstring_find(name,0,"img",3) == 0) {
+ readImage(n,strtok(NULL,"\n"));
+ }
+ else if (utstring_find(name,0,"frag",4) == 0) {
+ createShader("shader.vert",strtok(NULL,"\n"));
+ }
+ else if (utstring_find(name,0,"quit",4) == 0) {
+ glfwSetWindowShouldClose(window, GL_TRUE);
+ }
+ else {
+ struct param *p;
+ p = (struct param*)malloc(sizeof(struct param));
+ strncpy(p->name, n,10);
+ p->value = atof(strtok (NULL,"\n"));
+ HASH_ADD_STR( parameters, name, p );
+ }
}
}
-int main(void) {
+void setTextureParams() {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+ stbi_set_flip_vertically_on_load(1);
+}
+
+int main(int argc, char **argv) {
- GLFWwindow* window = createWindow();
- GLuint shader = createShader();
- uniform2f(shader, "resolution", 1920,1080);
+ createWindow();
+ createShader("shader.vert","shader.frag");
+ setTextureParams();
+ for (int i = 1; i < argc ; i++) {
+ char name[0];
+ sprintf(name,"img%.i",i);
+ readImage(name,argv[i]);
+ }
+ uniform2f("resolution", width,height);
pthread_t tid;
- pthread_create(&tid, NULL, readStdin, &shader);
+ pthread_create(&tid, NULL, readStdin, NULL);
while (!glfwWindowShouldClose(window)) {
- uniform1f(&shader, "time",glfwGetTime());
- uniform1f(&shader, "color",color);
+ uniform1f("time",glfwGetTime());
+ // parameters mutex??
+ // see uthash/doc/userguide.txt
+ struct param *p, *tmp;
+ HASH_ITER(hh, parameters, p, tmp) {
+ printf("%s %.f\n",p->name,p->value);
+ uniform1f(p->name,p->value);
+ HASH_DEL(parameters, p);
+ free(p);
+ }
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window);
- //glfwPollEvents();
+ glfwPollEvents();
}
//pthread_join(tid, NULL);