diff options
author | Christoph Helma <helma@in-silico.ch> | 2017-07-19 16:35:48 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2017-07-19 16:35:48 +0200 |
commit | 22fad493eafe61ba5fa042c85db6c5f253c90517 (patch) | |
tree | 72cdc3f2facf5980b5eea41f005c852cb2665b82 | |
parent | 1baee0d835b144e388ceff53d2c373bc7e887178 (diff) |
image reload
-rw-r--r-- | shader.frag | 21 | ||||
-rw-r--r-- | sv.c | 83 |
2 files changed, 65 insertions, 39 deletions
diff --git a/shader.frag b/shader.frag index bba52a5..71a7141 100644 --- a/shader.frag +++ b/shader.frag @@ -1,4 +1,4 @@ -#version 330 core +#version 440 core #ifdef GL_ES precision mediump float; #endif @@ -12,21 +12,24 @@ uniform sampler2D img0; uniform sampler2D img1; uniform sampler2D img2; uniform sampler2D img3; -//uniform vec2 u_tex0Resolution; +uniform float img0ratio; +uniform float img1ratio; +uniform float img2ratio; +uniform float img3ratio; +out vec4 fragColor; float random (in float x) { return fract(sin(x)*43758.5453123)-0.5; } void main (void) { vec2 st = gl_FragCoord.xy/resolution.xy; - vec4 i1 = texture2D(img0,st*0.8); - vec4 i2 = texture2D(img1,st); - vec4 i3 = texture2D(img2,st*1.2); - vec4 i4 = texture2D(img3,st); + 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)); - gl_FragColor = mix(i1,i2,0.5); - //gl_FragColor = i2; - //gl_FragColor = texture2D(image1,st*sin(time*2.)); + fragColor = mix(i1,i2,0.5); + //gl_FragColor = i1; } @@ -16,6 +16,7 @@ GLFWwindow* window; GLuint vertex; GLuint fragment; GLuint shader; +GLuint textures[4]; struct param { char name[10]; @@ -24,6 +25,13 @@ struct param { }; struct param *parameters = NULL; +struct image { + int id; + char path[40]; + UT_hash_handle hh; /* makes this structure hashable */ +}; +struct image *images = 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) { @@ -64,6 +72,7 @@ GLuint compileShader(const char * source, GLenum type) { } GLuint linkShader(GLuint vertex, GLuint fragment) { + glDeleteProgram(shader); shader = glCreateProgram(); glAttachShader(shader, vertex); glAttachShader(shader, fragment); @@ -102,44 +111,46 @@ void createShader(char *vert, char *frag) { glBindVertexArray(VAO); }; +void uniform1f(char * var, float f) { + int v = glGetUniformLocation(shader, var); + glUniform1f(v, f); +} + +void uniform2f(char * var, float f0, float f1) { + int v = glGetUniformLocation(shader, var); + glUniform2f(v,f0,f1); +} void readImage(char *file, int i) { - GLuint tex; - glGenTextures(1, &tex); + glUseProgram(shader); + char name[4]; + sprintf(name,"img%i",i); + glUniform1i(glGetUniformLocation(shader, name), i); + //glBindTexture(GL_TEXTURE_2D, 0); + //glDeleteTextures(1,&textures[i]); + //GLuint tex; + //glGenTextures(1, &tex); + //textures[i] = tex; glActiveTexture(GL_TEXTURE0+i); - glBindTexture(GL_TEXTURE_2D,tex); + glBindTexture(GL_TEXTURE_2D,textures[i]); + //glBindTexture(GL_TEXTURE_2D,tex); 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); - glEnable(GL_TEXTURE_2D); + //glEnable(GL_TEXTURE_2D);//Only use this if not using shaders. - char name[4]; - sprintf(name,"img%i",i); - printf("%s: %s\n",name,file); - GLint v = glGetUniformLocation(shader, name); - glUniform1i(v, i); - - int comp; + int w,h,comp; stbi_set_flip_vertically_on_load(1); - 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); + unsigned char* pixels = stbi_load(file, &w, &h, &comp, STBI_rgb_alpha); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + char ratio[9]; + sprintf(ratio,"%sratio",name); + uniform1f(ratio,(float) w/h); stbi_image_free(pixels); } -void uniform1f(char * var, float f) { - int v = glGetUniformLocation(shader, var); - glUniform1f(v, f); -} - -void uniform2f(char * var, float f0, float f1) { - int v = glGetUniformLocation(shader, var); - glUniform2f(v,f0,f1); -} - void *readStdin() { while (1) { @@ -152,7 +163,11 @@ void *readStdin() { utstring_printf(name, n); if (utstring_find(name,0,"img",3) == 0) { - readImage(strtok(NULL,"\n"),str[3]-'0'); + struct image *i; + i = (struct image*)malloc(sizeof(struct image)); + i->id = str[3]-'0'; + strncpy(i->path, strtok(NULL,"\n"),40); + HASH_ADD_INT( images, id, i ); } else if (utstring_find(name,0,"frag",4) == 0) { createShader("shader.vert",strtok(NULL,"\n")); @@ -173,7 +188,9 @@ void *readStdin() { int main(int argc, char **argv) { createWindow(); + //createShader("shader.vert","cross.frag"); createShader("shader.vert","shader.frag"); + glGenTextures(4, textures); for (int i = 0; i<4; i++) { readImage(argv[i+1],i); } uniform2f("resolution", width,height); @@ -184,21 +201,27 @@ int main(int argc, char **argv) { uniform1f("time",glfwGetTime()); // parameters mutex?? // see uthash/doc/userguide.txt - struct param *p, *tmp; - HASH_ITER(hh, parameters, p, tmp) { + 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); + HASH_DEL(images, i); + readImage(i->path,i->id); + free(i); + } glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glfwSwapBuffers(window); glfwPollEvents(); } - //pthread_join(tid, NULL); - //printf("After Thread\n"); + pthread_cancel(tid); glfwDestroyWindow(window); glfwTerminate(); |