texture support

This commit is contained in:
Rakarake 2026-03-12 14:03:37 +01:00
parent d4a5fea127
commit b17759b34a
6 changed files with 8087 additions and 14 deletions

View file

@ -3,8 +3,8 @@
#include <string.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <libpng16/png.h>
#include "renderer.h"
#include "stb_image.h"
/// returns 0 if failed.
@ -48,6 +48,10 @@ const char *fragment_header_src =
"uniform vec2 iResolution;\n"
"uniform float iTime;\n"
"uniform int iFrame;\n"
"uniform sampler2D iChannel0;\n"
"uniform sampler2D iChannel1;\n"
"uniform sampler2D iChannel2;\n"
"uniform sampler2D iChannel3;\n"
"void mainImage(out vec4 fragColor, in vec2 fragCoord);\n"
"void main() {\n"
" mainImage(color, gl_FragCoord.xy);\n"
@ -121,7 +125,7 @@ GLuint load_shader(const char *path) {
return shader;
}
Renderer new_renderer() {
Renderer new_renderer(char **textures, int texture_count) {
const char *version_str = (const char *)glGetString(GL_VERSION);
const char *renderer_str = (const char *)glGetString(GL_RENDERER);
@ -159,6 +163,28 @@ Renderer new_renderer() {
);
Renderer renderer;
renderer.texture_count = texture_count;
// textures
for (int i = 0; i < texture_count; i++) {
glGenTextures(1, &renderer.textures[i]);
glBindTexture(GL_TEXTURE_2D, renderer.textures[i]);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nr_channels;
unsigned char *data = stbi_load(textures[i], &width, &height, &nr_channels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
fprintf(stderr, "failed to load texture %d\n", i);
}
stbi_image_free(data);
}
renderer.shader_path = "";
renderer.shader = 0;
renderer.vao = vao;
@ -195,6 +221,36 @@ void render(Renderer *state, int w, int h, double time, char *shader_path, int r
// Shader parameters.
glUseProgram(state->shader);
// Bind texture
for (int i = 0; i < state->texture_count; i++) {
switch (i) {
case 0:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, state->textures[i]);
glUniform1i(glGetUniformLocation(state->shader, "iChannel0"), i);
break;
case 1:
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, state->textures[i]);
glUniform1i(glGetUniformLocation(state->shader, "iChannel1"), i);
break;
case 2:
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, state->textures[i]);
glUniform1i(glGetUniformLocation(state->shader, "iChannel2"), i);
break;
case 3:
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, state->textures[i]);
glUniform1i(glGetUniformLocation(state->shader, "iChannel3"), i);
break;
default:
fprintf(stderr, "texture index > 3 not allowed\n");
exit(1);
break;
}
}
int uniform_iResolution = glGetUniformLocation(state->shader, "iResolution");
glUniform2f(uniform_iResolution, w, h);