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 <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) {
|
||||||
|
@ -56,17 +54,10 @@ 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"
|
|
||||||
/* playback time in seconds */
|
|
||||||
"uniform float iTime;\n"
|
|
||||||
|
|
||||||
"out vec4 out_color;\n"
|
"out vec4 out_color;\n"
|
||||||
"void main() {\n"
|
"void main() {\n"
|
||||||
"vec2 xy = gl_FragCoord.xy + vec2(sin(iTime), cos(iTime));\n"
|
"vec2 xy = mod(gl_FragCoord.xy, vec2(200.0, 200.0));\n"
|
||||||
"out_color = vec4(sin(xy.x), sin(xy.y), sin(xy.x) + sin(xy.y), 1);\n"
|
"out_color = vec4(xy.x / 100.0, xy.x / 200.0, xy.y / 200.0, 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();
|
||||||
|
@ -84,45 +75,34 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
s32 create_framebuffer(s32 w, s32 h, GLuint *texture, GLuint *framebuffer) {
|
void render(Sdfps *s) {
|
||||||
glGenTextures(1, texture);
|
// setup render texture
|
||||||
glBindTexture(GL_TEXTURE_2D, *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_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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
||||||
|
fb_w, fb_h, // width height
|
||||||
glGenFramebuffers(1, framebuffer);
|
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, *framebuffer);
|
GLuint framebuf;
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *texture, 0);
|
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);
|
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);
|
|
||||||
glUniform1f(glGetUniformLocation(s -> shader_program, "iTime"), 0.001f * (f32) SDL_GetTicks());
|
|
||||||
|
|
||||||
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);
|
||||||
|
@ -162,13 +142,9 @@ 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
|
||||||
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
|
||||||
get data back out of a shader, there's probs a more proper way to do it
|
// that we could change to later
|
||||||
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,
|
||||||
|
|
Loading…
Reference in New Issue