texture resolutions

This commit is contained in:
Rakarake 2026-03-15 14:38:10 +01:00
parent a091a542f3
commit 641953bba1
4 changed files with 35 additions and 11 deletions

View file

@ -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)

View file

@ -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);
}

View file

@ -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);

View file

@ -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;