Compare commits
No commits in common. "c5ee25113f3500053c047e97a95e7aa07c5fdb6c" and "7de4bb9fb9fd33434ff3d23d1963d3d9fbdff100" have entirely different histories.
c5ee25113f
...
7de4bb9fb9
66
sdfps.c
66
sdfps.c
|
@ -4,8 +4,6 @@
|
|||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
GLuint create_shader(GLenum shader_type, const char *src) {
|
||||
|
@ -56,17 +54,10 @@ void init(Sdfps* s) { /* TODO: Error checks */
|
|||
s -> frag_shader = create_shader(GL_FRAGMENT_SHADER,
|
||||
"#version 330\n"
|
||||
"precision highp float;\n"
|
||||
|
||||
/* viewport resolution (in pixels), z = 1 */
|
||||
"uniform vec3 iResolution;\n"
|
||||
/* playback time in seconds */
|
||||
"uniform float iTime;\n"
|
||||
|
||||
"out vec4 out_color;\n"
|
||||
"void main() {\n"
|
||||
"vec2 xy = gl_FragCoord.xy + vec2(sin(iTime), cos(iTime));\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"
|
||||
"vec2 xy = mod(gl_FragCoord.xy, vec2(200.0, 200.0));\n"
|
||||
"out_color = vec4(xy.x / 100.0, xy.x / 200.0, xy.y / 200.0, 1);\n"
|
||||
"}\n");
|
||||
|
||||
s -> shader_program = glCreateProgram();
|
||||
|
@ -84,45 +75,34 @@ void init(Sdfps* s) { /* TODO: Error checks */
|
|||
}
|
||||
|
||||
void deinit(Sdfps *s) {
|
||||
glDeleteProgram(s -> shader_program);
|
||||
glDeleteShader(s -> vert_shader);
|
||||
glDeleteShader(s -> frag_shader);
|
||||
SDL_GL_DeleteContext(s -> context);
|
||||
SDL_DestroyWindow(s -> window);
|
||||
}
|
||||
|
||||
s32 create_framebuffer(s32 w, s32 h, GLuint *texture, GLuint *framebuffer) {
|
||||
glGenTextures(1, texture);
|
||||
glBindTexture(GL_TEXTURE_2D, *texture);
|
||||
void render(Sdfps *s) {
|
||||
// setup render texture
|
||||
const s32 fb_w = 512, fb_h = 512;
|
||||
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_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
|
||||
glGenFramebuffers(1, framebuffer);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, *framebuffer);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *texture, 0);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
||||
fb_w, fb_h, // width height
|
||||
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
GLuint framebuf;
|
||||
glGenFramebuffers(1, &framebuf);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuf);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, render_tex, 0);
|
||||
GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
|
||||
if (fb_status != GL_FRAMEBUFFER_COMPLETE)
|
||||
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);
|
||||
|
||||
GLuint iResolution = glGetUniformLocation(s -> shader_program, "iResolution");
|
||||
// TODO: error check?
|
||||
glUniform3f(iResolution, (f32) fb_w, (f32) fb_h, 1.0f);
|
||||
glUniform1f(glGetUniformLocation(s -> shader_program, "iTime"), 0.001f * (f32) SDL_GetTicks());
|
||||
|
||||
|
||||
|
||||
glClearColor(0.1f, 0.25f, 0.24f, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
@ -162,13 +142,9 @@ void render(Sdfps *s) {
|
|||
);
|
||||
|
||||
#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
|
||||
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
|
||||
*/
|
||||
// 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
|
||||
// that we could change to later
|
||||
f32 pixels[16 * 16 * 4] = { 0.0f };
|
||||
glReadPixels(
|
||||
10, // x pixel starting bottom left,
|
||||
|
|
Loading…
Reference in New Issue