got uniform iResolution working

master
bx 2022-03-21 21:56:24 +00:00
parent 7de4bb9fb9
commit f850e3f082
1 changed files with 42 additions and 21 deletions

63
sdfps.c
View File

@ -4,6 +4,8 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <stdio.h>
#include "types.h" #include "types.h"
GLuint create_shader(GLenum shader_type, const char *src) { GLuint create_shader(GLenum shader_type, const char *src) {
@ -54,10 +56,15 @@ void init(Sdfps* s) { /* TODO: Error checks */
s -> frag_shader = create_shader(GL_FRAGMENT_SHADER, s -> frag_shader = create_shader(GL_FRAGMENT_SHADER,
"#version 330\n" "#version 330\n"
"precision highp float;\n" "precision highp float;\n"
/* viewport resolution (in pixels), z = 1 */
"uniform vec3 iResolution;\n"
"out vec4 out_color;\n" "out vec4 out_color;\n"
"void main() {\n" "void main() {\n"
"vec2 xy = mod(gl_FragCoord.xy, vec2(200.0, 200.0));\n" "vec2 xy = gl_FragCoord.xy;\n"
"out_color = vec4(xy.x / 100.0, xy.x / 200.0, xy.y / 200.0, 1);\n" "out_color = vec4(sin(xy.x), sin(xy.y), sin(xy.x) + sin(xy.y), 1);\n"
"if(xy.x > iResolution.x / 2.0) out_color = out_color.zxyw;\n"
"}\n"); "}\n");
s -> shader_program = glCreateProgram(); s -> shader_program = glCreateProgram();
@ -75,34 +82,44 @@ void init(Sdfps* s) { /* TODO: Error checks */
} }
void deinit(Sdfps *s) { void deinit(Sdfps *s) {
glDeleteProgram(s -> shader_program);
glDeleteShader(s -> vert_shader);
glDeleteShader(s -> frag_shader);
SDL_GL_DeleteContext(s -> context); SDL_GL_DeleteContext(s -> context);
SDL_DestroyWindow(s -> window); SDL_DestroyWindow(s -> window);
} }
void render(Sdfps *s) { s32 create_framebuffer(s32 w, s32 h, GLuint *texture, GLuint *framebuffer) {
// setup render texture glGenTextures(1, texture);
const s32 fb_w = 512, fb_h = 512; glBindTexture(GL_TEXTURE_2D, *texture);
GLuint render_tex;
glGenTextures(1, &render_tex);
glBindTexture(GL_TEXTURE_2D, render_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
fb_w, fb_h, // width height
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, framebuffer);
GLuint framebuf; glBindFramebuffer(GL_FRAMEBUFFER, *framebuffer);
glGenFramebuffers(1, &framebuf); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *texture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, framebuf);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, render_tex, 0);
GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER); GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fb_status != GL_FRAMEBUFFER_COMPLETE) if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
puts("FRAME BUFFER FUCKED UP SOMEWHERE"); puts("FRAME BUFFER FUCKED UP SOMEWHERE");
return 1;
}
return 0;
}
void render(Sdfps *s) {
// setup render texture
const s32 fb_w = 4, fb_h = 4;
GLuint render_tex, framebuf;
create_framebuffer(fb_w, fb_h, &render_tex, &framebuf);
glViewport(0, 0, fb_w, fb_h); glViewport(0, 0, fb_w, fb_h);
GLuint iResolution = glGetUniformLocation(s -> shader_program, "iResolution");
// TODO: error check?
glUniform3f(iResolution, (f32) fb_w, (f32) fb_h, 1.0f);
glClearColor(0.1f, 0.25f, 0.24f, 1); glClearColor(0.1f, 0.25f, 0.24f, 1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -142,9 +159,13 @@ void render(Sdfps *s) {
); );
#if 1 #if 1
// just here to check that it works, since it can be used as an easy way to /*
// get data back out of a shader, there's probs a more proper way to do it just here to check that it works, since it can be used as an easy way to
// that we could change to later get data back out of a shader, there's probs a more proper way to do it
that we could change to later
UPDATE: looking at shader toy's source it seems it uses this function to
get output for sound shaders
*/
f32 pixels[16 * 16 * 4] = { 0.0f }; f32 pixels[16 * 16 * 4] = { 0.0f };
glReadPixels( glReadPixels(
10, // x pixel starting bottom left, 10, // x pixel starting bottom left,