diff options
author | Christoph Helma <helma@in-silico.ch> | 2017-07-18 16:45:28 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2017-07-18 16:45:28 +0200 |
commit | ceca3527ef89f4b6eea14b1fcc4c68d0b235778f (patch) | |
tree | 4222bcf9b68c0bca76cefc7bc4f6d1dffcc0abce | |
parent | 7fc650cc1375a96adf6f2ba7d0874228a4780752 (diff) |
multiple textures
-rw-r--r-- | shader.frag | 19 | ||||
-rw-r--r-- | sv.c | 110 |
2 files changed, 89 insertions, 40 deletions
diff --git a/shader.frag b/shader.frag index 2084f1c..53db428 100644 --- a/shader.frag +++ b/shader.frag @@ -1,3 +1,4 @@ +#version 330 core #ifdef GL_ES precision mediump float; #endif @@ -7,9 +8,10 @@ uniform float time; uniform float m; //uniform sampler2D backbuffer; -uniform sampler2D image1; -uniform sampler2D image2; -uniform sampler2D image3; +uniform sampler2D img0; +uniform sampler2D img1; +uniform sampler2D img2; +uniform sampler2D img3; //uniform vec2 u_tex0Resolution; float random (in float x) { @@ -18,8 +20,13 @@ float random (in float x) { 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); + vec4 i1 = texture2D(img0,st*0.8); + vec4 i2 = texture2D(img1,st); + vec4 i3 = texture2D(img2,st*1.2); + vec4 i4 = texture2D(img3,st); + i1 = mix(i1,i2,sin(time)); + i2 = mix(i3,i4,cos(time)); + //gl_FragColor = mix(i1,i2,0.5); + gl_FragColor = i1; //gl_FragColor = texture2D(image1,st*sin(time*2.)); } @@ -16,12 +16,7 @@ GLFWwindow* window; GLuint vertex; GLuint fragment; GLuint shader; - -struct texture { - char name[10]; - GLuint id; - UT_hash_handle hh; /* makes this structure hashable */ -}; +GLuint textures[4]; struct param { char name[10]; @@ -30,7 +25,6 @@ struct param { }; struct param *parameters = NULL; -struct texture *textures = NULL; static void error_callback(int error, const char* description) { fputs(description, stderr); } @@ -110,20 +104,30 @@ void createShader(char *vert, char *frag) { glBindVertexArray(VAO); }; -void readImage(char *name, char *file) { - int n = HASH_COUNT(textures); - glGenTextures(1, &n); - glBindTexture(GL_TEXTURE_2D, n); + +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); + glEnable(GL_TEXTURE_2D); + stbi_set_flip_vertically_on_load(1); +} +void readImage(char *file, char *name, int32_t t, int i) { + + GLuint tex; + glGenTextures(1, &tex); + glActiveTexture(GL_TEXTURE0+i); + glBindTexture(GL_TEXTURE_2D,tex); + setTextureParams(); + GLint v = glGetUniformLocation(shader, name); + printf("loc %.i\n",v); + glUniform1i(v, i); 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); } @@ -149,7 +153,7 @@ void *readStdin() { utstring_printf(name, n); if (utstring_find(name,0,"img",3) == 0) { - readImage(n,strtok(NULL,"\n")); + //readImage(n,strtok(NULL,"\n")); } else if (utstring_find(name,0,"frag",4) == 0) { createShader("shader.vert",strtok(NULL,"\n")); @@ -167,26 +171,55 @@ void *readStdin() { } } -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) { createWindow(); createShader("shader.vert","shader.frag"); + glUseProgram(shader); + /* + */ + readImage(argv[1],"img0",GL_TEXTURE0,0); + readImage(argv[2],"img1",GL_TEXTURE1,1); + readImage(argv[3],"img2",GL_TEXTURE2,2); + readImage(argv[4],"img3",GL_TEXTURE3,3); +/* + glEnable(GL_TEXTURE_2D); + //glBindTexture(GL_TEXTURE_2D,0); + GLuint tex0; + glGenTextures(1, &tex0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D,tex0); setTextureParams(); - for (int i = 1; i < argc ; i++) { - char name[0]; - sprintf(name,"img%.i",i); - readImage(name,argv[i]); - } + + GLint v = glGetUniformLocation(shader, "img0"); + printf("loc %.i\n",v); + glUniform1i(v, tex0); + int comp; + unsigned char* pixels = stbi_load(argv[1], &width, &height, &comp, STBI_rgb_alpha); + //printf("%i\n",textures[0]); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + stbi_image_free(pixels); + //glBindTexture(GL_TEXTURE_2D,0); + + + glBindTexture(GL_TEXTURE_2D,0); + glActiveTexture(GL_TEXTURE0); + GLuint tex1; + glGenTextures(1, &tex1); + v = glGetUniformLocation(shader, "img1"); + printf("loc %.i\n",v); + glUniform1i(v, tex1); + //glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D,tex1); + //glBindTexture(GL_TEXTURE_2D,textures[1]); + //glBindTexture(GL_TEXTURE_2D,1); + pixels = stbi_load(argv[2], &width, &height, &comp, STBI_rgb_alpha); + printf("%i\n",tex1); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + stbi_image_free(pixels); + //glBindTexture(GL_TEXTURE_2D,0); +*/ + uniform2f("resolution", width,height); pthread_t tid; pthread_create(&tid, NULL, readStdin, NULL); @@ -202,6 +235,15 @@ int main(int argc, char **argv) { HASH_DEL(parameters, p); free(p); } + /* + struct texture *t, *tmp2; + HASH_ITER(hh, textures, t, tmp2) { + int v = glGetUniformLocation(shader, t->name); + printf("loc %.i\n",v); + glUniform1i(v, t->id); + } + */ + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glfwSwapBuffers(window); glfwPollEvents(); |