/* 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 #include #include #include #include #include #include #ifdef USE_INOTIFY # include # include # include #endif #ifdef GLSLWRITE # include #endif #include #include #include #ifndef GLSLWRITE static float speed = 1.0; static unsigned previous_ticks = 0; static GLint param_k = 0; static GLint param_l = 0; static GLint param_m = 0; static GLint param_k_loc; static GLint param_l_loc; static GLint param_m_loc; #ifdef USE_INOTIFY static int inotify_fd = -1; static int inotify_watch = -1; #endif #endif static char *filename; static 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); } } static 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"); #ifndef GLSLWRITE param_k_loc = glGetUniformLocation(program, "param_k"); param_l_loc = glGetUniformLocation(program, "param_l"); param_m_loc = glGetUniformLocation(program, "param_m"); #endif glUseProgram(program); free(shader); glDeleteShader(frag); glDeleteProgram(program); } #if defined(USE_INOTIFY) && !defined(GLSLWRITE) static void reset_watch(void) { if (inotify_watch >= 0) { if (inotify_rm_watch(inotify_fd, inotify_watch)) { fprintf(stderr, "unable to remove watch\n"); exit(1); } } inotify_watch = inotify_add_watch(inotify_fd, filename, IN_CLOSE_WRITE); if (inotify_watch < 0) { fprintf(stderr, "unable to watch '%s' for changes\n", filename); return; } } #endif static void init(void) { #if defined(USE_INOTIFY) && !defined(GLSLWRITE) inotify_fd = inotify_init(); if (inotify_fd < 0) { fprintf(stderr, "unable to initialize inotify\n"); exit(1); } fcntl(inotify_fd, F_SETFL, fcntl(inotify_fd, F_GETFL)|O_NONBLOCK); reset_watch(); #endif glewInit(); glDisable(GL_DEPTH_TEST); glColor4f(0, 0, 0, 0); load(); } static void check_reload(void) { #if defined(USE_INOTIFY) && !defined(GLSLWRITE) char buf[sizeof(struct inotify_event) + NAME_MAX + 1]; if (read(inotify_fd, buf, sizeof(buf)) > 0) load(); #endif } #ifdef GLSLWRITE static void savePNG(int width, int height, const char *output_dir) { uint8_t pixels[width*height*4]; glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels); char filename[strlen(output_dir) + 20]; snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, (unsigned)current_time); FILE *f = fopen(filename, "wb"); if (!f) { fprintf(stderr, "unable to open PNG file\n"); exit(1); } png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { fprintf(stderr, "unable to open PNG file\n"); exit(1); } png_set_swap_alpha(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { fprintf(stderr, "unable to create PNG info struct\n"); exit(1); } if (setjmp(png_jmpbuf(png_ptr))) { fprintf(stderr, "unable to write PNG file\n"); exit(1); } png_init_io(png_ptr, f); png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); uint8_t *row_pointers[height]; for (size_t i = 0; i < height; i++) row_pointers[i] = &pixels[4*(height-1-i)*width]; png_set_rows(png_ptr, info_ptr, row_pointers); png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(f); } #endif static void render(int width, int height) { glViewport(0, 0, (GLsizei)width, (GLsizei)height); #ifndef GLSLWRITE unsigned ticks = SDL_GetTicks(); current_time += speed * (ticks - previous_ticks); previous_ticks = ticks; glUniform1i(param_k_loc, param_k); glUniform1i(param_l_loc, param_l); glUniform1i(param_m_loc, param_m); #endif 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(); } #ifndef GLSLWRITE static void handle_input(const char *input) { for (; *input; input++) { switch (*input) { case 'l': #ifdef USE_INOTIFY reset_watch(); #endif load(); break; case 'r': current_time = 0; break; case 'R': param_k = param_l = param_m = 0; break; case '+': speed *= 1.1; break; case '-': speed /= 1.1; break; case '=': if (speed < 0) speed = -1.0; else speed = 1.0; break; case '<': if (speed > 0) speed = -speed; break; case '>': if (speed < 0) speed = -speed; } } } #endif int main(int argc, char *argv[]) { #ifdef GLSLWRITE if (argc != 3) { fprintf(stderr, "Usage: glslwrite \n"); exit(1); } #else if (argc != 2) { fprintf(stderr, "Usage: glslview \n"); exit(1); } #endif SDL_Init(SDL_INIT_VIDEO); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_Window *window = SDL_CreateWindow("glslview", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); SDL_GLContext ctx = SDL_GL_CreateContext(window); filename = argv[1]; init(); SDL_StartTextInput(); while (true) { SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_KEYDOWN: switch (e.key.keysym.sym) { #ifndef GLSLWRITE case SDLK_PAGEDOWN: param_k++; break; case SDLK_PAGEUP: param_k--; break; case SDLK_RIGHT: param_l++; break; case SDLK_LEFT: param_l--; break; case SDLK_DOWN: param_m++; break; case SDLK_UP: param_m--; break; #endif case SDLK_ESCAPE: goto quit; } break; #ifndef GLSLWRITE case SDL_TEXTINPUT: handle_input(e.text.text); break; case SDL_MOUSEWHEEL: speed /= pow(1.1, e.wheel.y); break; #endif case SDL_QUIT: goto quit; } } check_reload(); int width, height; SDL_GetWindowSize(window, &width, &height); render(width, height); SDL_GL_SwapWindow(window); #ifdef GLSLWRITE savePNG(width, height, argv[2]); current_time += 20; #endif } quit: SDL_StopTextInput(); SDL_GL_DeleteContext(ctx); SDL_DestroyWindow(window); SDL_Quit(); return 0; }