From 22e600bde83f04fd4f140bcf2c7f7b90d5920e55 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 3 Feb 2016 12:53:39 +0100 Subject: Restructure glslview/glslwrite code --- common.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 common.c (limited to 'common.c') diff --git a/common.c b/common.c new file mode 100644 index 0000000..25fc56a --- /dev/null +++ b/common.c @@ -0,0 +1,144 @@ +/* + Copyright (c) 2016, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "common.h" + +#include + +#include + +#include +#include + + +char *filename; +float current_time = 0; + +static GLint time_loc; +static GLint res_loc; + + +static char * readfile(const char *name) { + FILE *f = fopen(name, "r"); + if (!f) + return NULL; + + size_t size = 1024; + char *buffer = malloc(size+1); + size_t count = 0, r; + + do { + if (count == size) { + size *= 2; + buffer = realloc(buffer, size+1); + } + + r = fread(buffer+count, 1, size-count, f); + count += r; + } while (r); + + fclose(f); + + buffer[count] = 0; + + return buffer; +} + +static void printShaderInfoLog(GLuint obj) { + GLint length = 0; + + glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length); + + if(length > 0) { + char log[length]; + + glGetShaderInfoLog(obj, sizeof(log), NULL, log); + fprintf(stderr, "%s\n", log); + } +} + +static void printProgramInfoLog(GLuint obj) { + GLint length = 0; + + glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length); + + if(length > 0) { + char log[length]; + + glGetProgramInfoLog(obj, sizeof(log), NULL, log); + fprintf(stderr, "%s\n", log); + } +} + +void load(void) { + char *shader = readfile(filename); + if (!shader) { + fprintf(stderr, "Error: unable to read '%s'\n", filename); + return; + } + + GLuint frag = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(frag, 1, (const GLchar **)&shader, NULL); + glCompileShader(frag); + printShaderInfoLog(frag); + + GLuint program = glCreateProgram(); + glAttachShader(program, frag); + + glLinkProgram(program); + printProgramInfoLog(program); + + glBindFragDataLocation(program, 0, "fragColor"); + time_loc = glGetUniformLocation(program, "time"); + res_loc = glGetUniformLocation(program, "res"); + + extra_load(program); + + glUseProgram(program); + + free(shader); + glDeleteShader(frag); + glDeleteProgram(program); +} + +void init(void) { + glewInit(); + glDisable(GL_DEPTH_TEST); + glColor4f(0, 0, 0, 0); +} + +void render(int width, int height) { + glViewport(0, 0, (GLsizei)width, (GLsizei)height); + + glUniform1f(time_loc, current_time); + glUniform2f(res_loc, width, height); + + glBegin(GL_QUADS); + glVertex2f(-1, -1); + glVertex2f(1, -1); + glVertex2f(1, 1); + glVertex2f(-1, 1); + glEnd(); +} -- cgit v1.2.3