boilerplate in shader reduction
This commit is contained in:
parent
f6706982ce
commit
2383faf456
2 changed files with 42 additions and 22 deletions
39
renderer.c
39
renderer.c
|
|
@ -28,7 +28,7 @@ GLuint compile_shader(GLuint type, const char *src) {
|
|||
}
|
||||
|
||||
const char *vertex_shader_src =
|
||||
"#version 330 core\n"
|
||||
"#version 300 es\n"
|
||||
"\n"
|
||||
"layout(location = 0) in vec2 aPos;\n"
|
||||
"\n"
|
||||
|
|
@ -36,6 +36,21 @@ const char *vertex_shader_src =
|
|||
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
const char *fragment_header_src =
|
||||
"#version 300 es\n"
|
||||
"precision highp float;\n"
|
||||
"precision highp int;\n"
|
||||
"precision lowp sampler2D;\n"
|
||||
"precision lowp samplerCube;\n"
|
||||
"layout(location = 0) out vec4 color;\n"
|
||||
"uniform vec2 iResolution;\n"
|
||||
"uniform float iTime;\n"
|
||||
"void mainImage(out vec4 fragColor, in vec2 fragCoord);\n"
|
||||
"void main() {\n"
|
||||
" mainImage(color, gl_FragCoord.xy);\n"
|
||||
"}\n"
|
||||
"\n";
|
||||
|
||||
/// returns 0 if failed.
|
||||
GLuint create_shader(const char *fragment_src) {
|
||||
GLuint program = glCreateProgram();
|
||||
|
|
@ -60,14 +75,24 @@ GLuint load_shader(const char *path) {
|
|||
printf("failed to open shader file: '%s'\n", path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fseek(handle, 0L, SEEK_END);
|
||||
long length = ftell(handle);
|
||||
long file_length = ftell(handle);
|
||||
fseek(handle, 0L, SEEK_SET);
|
||||
|
||||
long header_length = strlen(fragment_header_src);
|
||||
long length = header_length + file_length;
|
||||
|
||||
char *string = malloc((length + 1) * sizeof(char));
|
||||
if (!fread(string, sizeof(char), length, handle)) {
|
||||
|
||||
// Copy header.
|
||||
strcpy(string, fragment_header_src);
|
||||
|
||||
if (!fread(string + header_length, sizeof(char), file_length, handle)) {
|
||||
printf("failed to read shader file\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// I love C
|
||||
string[length] = '\0';
|
||||
fclose(handle);
|
||||
|
|
@ -164,11 +189,11 @@ void render(Renderer *state, int w, int h, double time, char *shader_path, int r
|
|||
// Shader parameters.
|
||||
glUseProgram(state->shader);
|
||||
|
||||
int uniform_WindowSize = glGetUniformLocation(state->shader, "WindowSize");
|
||||
glUniform2f(uniform_WindowSize, w, h);
|
||||
int uniform_iResolution = glGetUniformLocation(state->shader, "iResolution");
|
||||
glUniform2f(uniform_iResolution, w, h);
|
||||
|
||||
int uniform_time = glGetUniformLocation(state->shader, "time");
|
||||
glUniform1f(uniform_time, (float)time);
|
||||
int uniform_iTime = glGetUniformLocation(state->shader, "iTime");
|
||||
glUniform1f(uniform_iTime, (float)time);
|
||||
|
||||
glBindVertexArray(state->vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue