diff --git a/README.md b/README.md index 7cfc955..de23569 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,9 @@ Required libraries are: libwayland, wayland-scanner, wayland-protocols, pkg-conf Run `make` to output the binary `glonkers`. ## TODO -- Add support for some structures in shadertoy. -- Texture support? (requires a pre-preprocessor) +- Add support for more structures in shadertoy. - Wrapper that will curl a shadertoy shader then using that with glonkers. -- Custom uniforms. +- Fix line numbers reported on shader compilation failure. Swag: ![Showcase of glonkers](showcase.png) diff --git a/examples/texture.glsl b/examples/texture.glsl index 26cf759..9df71b0 100644 --- a/examples/texture.glsl +++ b/examples/texture.glsl @@ -1,5 +1,13 @@ void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = fragCoord / iResolution; + uv = vec2(uv.x, 1.0 - uv.y); // mixes two textures - fragColor = mix(texture(iChannel0, uv), texture(iChannel1, uv), 0.2); + //fragColor = mix(texture(iChannel0, uv), texture(iChannel1, uv), 0.2); + + vec2 tex_uv = vec2(fragCoord.x, 1.0 - fragCoord.y); + // test resolutions + //fragColor = texture(iChannel0, tex_uv / iChannelResolution[0].xy); + + // combine them! + fragColor = mix(texture(iChannel0, tex_uv / iChannelResolution[0].xy), texture(iChannel1, tex_uv / iChannelResolution[1].xy), 0.2); } diff --git a/renderer.c b/renderer.c index b2cf76b..e2061ee 100644 --- a/renderer.c +++ b/renderer.c @@ -52,6 +52,7 @@ const char *fragment_header_src = "uniform sampler2D iChannel1;\n" "uniform sampler2D iChannel2;\n" "uniform sampler2D iChannel3;\n" +"uniform vec3 iChannelResolution[4];\n" "void mainImage(out vec4 fragColor, in vec2 fragCoord);\n" "void main() {\n" " mainImage(color, gl_FragCoord.xy);\n" @@ -167,8 +168,8 @@ Renderer new_renderer(char **textures, int texture_count) { // textures for (int i = 0; i < texture_count; i++) { - glGenTextures(1, &renderer.textures[i]); - glBindTexture(GL_TEXTURE_2D, renderer.textures[i]); + glGenTextures(1, &renderer.textures[i].texture); + glBindTexture(GL_TEXTURE_2D, renderer.textures[i].texture); 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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); @@ -178,6 +179,10 @@ Renderer new_renderer(char **textures, int texture_count) { if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); + renderer.textures[i].width = width; + renderer.textures[i].height = height; + renderer.textures[i].nr_channels = nr_channels; + printf("texture info | width: %d, height: %d, nr_channels: %d\n", width, height, nr_channels); } else { fprintf(stderr, "failed to load texture %d\n", i); @@ -226,22 +231,22 @@ void render(Renderer *state, int w, int h, double time, char *shader_path, int r switch (i) { case 0: glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, state->textures[i]); + glBindTexture(GL_TEXTURE_2D, state->textures[i].texture); glUniform1i(glGetUniformLocation(state->shader, "iChannel0"), i); break; case 1: glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, state->textures[i]); + glBindTexture(GL_TEXTURE_2D, state->textures[i].texture); glUniform1i(glGetUniformLocation(state->shader, "iChannel1"), i); break; case 2: glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, state->textures[i]); + glBindTexture(GL_TEXTURE_2D, state->textures[i].texture); glUniform1i(glGetUniformLocation(state->shader, "iChannel2"), i); break; case 3: glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, state->textures[i]); + glBindTexture(GL_TEXTURE_2D, state->textures[i].texture); glUniform1i(glGetUniformLocation(state->shader, "iChannel3"), i); break; default: @@ -251,6 +256,15 @@ void render(Renderer *state, int w, int h, double time, char *shader_path, int r } } + GLfloat iChannelResolutions[4 * 3]; + for (int i = 0; i < state->texture_count; i++) { + iChannelResolutions[i * 3] = state->textures[i].width; + iChannelResolutions[i * 3 + 1] = state->textures[i].height; + iChannelResolutions[i * 3 + 2] = 1.0; + } + int uniform_iChannelResolution = glGetUniformLocation(state->shader, "iChannelResolution"); + glUniform3fv(uniform_iChannelResolution, state->texture_count, iChannelResolutions); + int uniform_iResolution = glGetUniformLocation(state->shader, "iResolution"); glUniform2f(uniform_iResolution, w, h); diff --git a/renderer.h b/renderer.h index 1f484b7..ecb5d8f 100644 --- a/renderer.h +++ b/renderer.h @@ -10,7 +10,10 @@ typedef struct Renderer { /// The currently rendered frame. unsigned int frame_nr; struct stat shader_file_modified; - unsigned int textures[4]; + struct texture { + int width, height, nr_channels; + unsigned int texture; + } textures[4]; int texture_count; } Renderer;