diff options
-rw-r--r-- | shader.frag | 35 | ||||
-rw-r--r-- | sv.c | 191 |
2 files changed, 117 insertions, 109 deletions
diff --git a/shader.frag b/shader.frag index 71a7141..0494364 100644 --- a/shader.frag +++ b/shader.frag @@ -8,14 +8,19 @@ uniform float time; uniform float m; //uniform sampler2D backbuffer; -uniform sampler2D img0; -uniform sampler2D img1; -uniform sampler2D img2; -uniform sampler2D img3; -uniform float img0ratio; -uniform float img1ratio; -uniform float img2ratio; -uniform float img3ratio; +uniform sampler2D i0; +uniform sampler2D i1; +uniform sampler2D i2; +uniform sampler2D i3; +uniform float i0ratio; +uniform float i1ratio; +uniform float i2ratio; +uniform float i3ratio; + +uniform float p0; +uniform float p1; +uniform float p3; +uniform float p4; out vec4 fragColor; float random (in float x) { @@ -24,12 +29,12 @@ float random (in float x) { void main (void) { vec2 st = gl_FragCoord.xy/resolution.xy; - vec4 i1 = texture2D(img0,st*vec2(img0ratio,1.)); - vec4 i2 = texture2D(img1,st*vec2(img1ratio,1.)); - vec4 i3 = texture2D(img2,st*vec2(img2ratio,1.)); - vec4 i4 = texture2D(img3,st*vec2(img3ratio,1.)); - i1 = mix(i1,i2,sin(time)); - i2 = mix(i3,i4,cos(time)); - fragColor = mix(i1,i2,0.5); + vec4 img1 = texture2D(i0,st*vec2(i0ratio,1.)); + vec4 img2 = texture2D(i1,st*vec2(i1ratio,1.)); + vec4 img3 = texture2D(i2,st*vec2(i2ratio,1.)); + vec4 img4 = texture2D(i3,st*vec2(i3ratio,1.)); + img1 = mix(img1,img2,sin(time)); + img2 = mix(img3,img4,cos(time)); + fragColor = mix(img1,img2,p0); //gl_FragColor = i1; } @@ -7,8 +7,6 @@ #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; @@ -16,27 +14,31 @@ GLFWwindow* window; GLuint vertex; GLuint fragment; GLuint shaderProgram; -GLuint textures[4]; -struct param { - char name[10]; - float value; - UT_hash_handle hh; /* makes this structure hashable */ -}; -struct param *parameters = NULL; - -struct image { - int id; +typedef struct Shad { + GLuint id; char path[40]; - UT_hash_handle hh; /* makes this structure hashable */ -}; -struct image *images = NULL; + int new; +} Shader; -struct shader { +Shader shader; + +typedef struct Img { + GLuint id; char path[40]; - UT_hash_handle hh; /* makes this structure hashable */ -}; -struct shader *shaders = NULL; + float ratio; + int new; +} Image; + +Image images[4]; + +typedef struct Param { + char name[3]; + float value; + int new; +} Parameter; + +Parameter parameters[16]; static void error_callback(int error, const char* description) { fputs(description, stderr); } @@ -79,21 +81,21 @@ GLuint compileShader(const char * source, GLenum type) { GLuint linkShader(GLuint vertex, GLuint fragment) { glUseProgram(0); - glDeleteProgram(shaderProgram); - shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertex); - glAttachShader(shaderProgram, fragment); - glLinkProgram(shaderProgram); + glDeleteProgram(shader.id); + shader.id = glCreateProgram(); + glAttachShader(shader.id, vertex); + glAttachShader(shader.id, fragment); + glLinkProgram(shader.id); int success; char infoLog[512]; - glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); + glGetProgramiv(shader.id, GL_LINK_STATUS, &success); if (!success) { - glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); + glGetProgramInfoLog(shader.id, 512, NULL, infoLog); fputs("Shader linking failed:\n",stderr); fputs(infoLog,stderr); } - glDetachShader(shaderProgram, vertex); - glDetachShader(shaderProgram, fragment); + glDetachShader(shader.id, vertex); + glDetachShader(shader.id, fragment); glDeleteShader(vertex); glDeleteShader(fragment); } @@ -112,35 +114,44 @@ void createShader(char *vert, char *frag) { vertex = compileShader(readShader(vert), GL_VERTEX_SHADER); fragment = compileShader(readShader(frag),GL_FRAGMENT_SHADER); linkShader(vertex,fragment); - glUseProgram(shaderProgram); + printf("%i\n",shader.id); + glUseProgram(shader.id); }; void uniform1f(char * var, float f) { - int v = glGetUniformLocation(shaderProgram, var); + int v = glGetUniformLocation(shader.id, var); glUniform1f(v, f); } void uniform2f(char * var, float f0, float f1) { - int v = glGetUniformLocation(shaderProgram, var); + int v = glGetUniformLocation(shader.id, var); glUniform2f(v,f0,f1); } -void readImage(char *file, int i) { +void imageUniforms(int i) { + Image image = images[i]; + char name[2]; + sprintf(name,"i%i",i); + glUniform1i(glGetUniformLocation(shader.id, name), i); + char ratioName[7]; + sprintf(ratioName,"%sratio",name); + uniform1f(ratioName,images[i].ratio); +} + +void readImage(int i) { - char name[4]; - sprintf(name,"img%i",i); - glUniform1i(glGetUniformLocation(shaderProgram, name), i); + images[i].new = 1; int w,h,comp; stbi_set_flip_vertically_on_load(1); - unsigned char* pixels = stbi_load(file, &w, &h, &comp, STBI_rgb_alpha); + unsigned char* pixels = stbi_load(images[i].path, &w, &h, &comp, STBI_rgb_alpha); - char ratio[9]; - sprintf(ratio,"%sratio",name); - uniform1f(ratio,(float) w/h); + images[i].ratio = (float) w/h; + imageUniforms(i); glActiveTexture(GL_TEXTURE0+i); - glBindTexture(GL_TEXTURE_2D,textures[i]); + glGenTextures(1, &images[i].id); + glBindTexture(GL_TEXTURE_2D,images[i].id); 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_CLAMP_TO_BORDER); @@ -150,38 +161,49 @@ void readImage(char *file, int i) { } void *readStdin() { - while (1) { + while (1) { char * str; size_t l = 80; getline(&str,&l,stdin); char *n = strtok(str," "); - UT_string *name; - utstring_new(name); - utstring_printf(name, n); - - if (utstring_find(name,0,"i",1) == 0) { - struct image *i; - i = (struct image*)malloc(sizeof(struct image)); - i->id = str[1]-'0'; - strncpy(i->path, strtok(NULL,"\n"),40); - HASH_ADD_INT( images, id, i ); + + if (n[0] == 'i') { + int i = n[1]-'0'; + strncpy(images[i].path, strtok(NULL,"\n"),40); + images[i].new = 1; } - else if (utstring_find(name,0,"f",1) == 0) { - struct shader *s; - s = (struct shader*)malloc(sizeof(struct shader)); - strncpy(s->path, strtok(NULL,"\n"),40); - HASH_ADD_STR( shaders, path, s ); + else if (n[0] == 'f') { + strncpy(shader.path, strtok(NULL,"\n"),40); + shader.new = 1; } - else if (utstring_find(name,0,"q",1) == 0) { + else if (n[0] == 'p') { + int i = n[1]-'0'; + strncpy(parameters[i].name,n,3); + parameters[i].value = atof(strtok (NULL,"\n")); + parameters[i].new = 1; + } + else if (n[0] == 'q') { 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 ); + } +} + +void updateImages(int force) { + for (int i = 0; i<4; i++) { + if (images[i].new || force) { + readImage(i); + printf("%s\n",images[i].path); + images[i].new = 0; + } + } +} + +void updateUniforms(int force) { + for (int i = 0; i<16; i++) { + if (parameters[i].new || force) { + uniform1f(parameters[i].name,parameters[i].value); + parameters[i].new = 0; } } } @@ -189,13 +211,14 @@ void *readStdin() { int main(int argc, char **argv) { createWindow(); - //createShader("shader.vert","cross.frag"); createShader("shader.vert","shader.frag"); unsigned int VAO; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); - glGenTextures(4, textures); - for (int i = 0; i<4; i++) { readImage(argv[i+1],i); } + for (int i = 0; i<4; i++) { + strncpy(images[i].path, argv[i+1],40); + images[i].new = 1; + } uniform2f("resolution", width,height); pthread_t tid; @@ -203,35 +226,15 @@ int main(int argc, char **argv) { while (!glfwWindowShouldClose(window)) { uniform1f("time",glfwGetTime()); - // parameters mutex?? - // see uthash/doc/userguide.txt - - struct param *p, *tmp0; - HASH_ITER(hh, parameters, p, tmp0) { - //printf("%s %.f\n",p->name,p->value); - uniform1f(p->name,p->value); - HASH_DEL(parameters, p); - free(p); - } - - struct image *i, *tmp1; - HASH_ITER(hh, images, i, tmp1) { - printf("%i %s\n",i->id,i->path); - readImage(i->path,i->id); - HASH_DEL(images, i); - free(i); + if (shader.new) { + createShader("shader.vert",shader.path); + uniform2f("resolution", width,height); // important!! + for (int i = 0; i<4; i++) { imageUniforms(i); } + updateUniforms(1); + shader.new = 0; } - - struct shader *s, *tmp2; - HASH_ITER(hh, shaders, s, tmp2) { - //printf("%i %s\n",i->id,i->path); - createShader("shader.vert",s->path); - HASH_DEL(shaders, s); - free(s); - for (int i = 0; i<4; i++) { readImage(argv[i+1],i); } - uniform2f("resolution", width,height); - } - + updateImages(0); + updateUniforms(0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glfwSwapBuffers(window); glfwPollEvents(); |