This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
glslview/glslview.c

386 lines
8.1 KiB
C
Raw Normal View History

2016-02-02 20:35:25 +01:00
/*
Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
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.
*/
2016-02-02 08:20:15 +01:00
#include <fcntl.h>
#include <limits.h>
2016-02-02 09:00:05 +01:00
#include <math.h>
2016-02-01 10:19:13 +01:00
#include <stdbool.h>
2016-02-01 14:39:58 +01:00
#include <stdint.h>
2016-02-01 10:19:13 +01:00
#include <stdio.h>
#include <unistd.h>
2016-02-02 08:20:15 +01:00
#ifdef USE_INOTIFY
# include <sys/inotify.h>
# include <sys/stat.h>
# include <sys/types.h>
#endif
2016-02-01 14:39:58 +01:00
#ifdef GLSLWRITE
# include <png.h>
#endif
2016-02-01 10:19:13 +01:00
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#ifndef GLSLWRITE
2016-02-02 08:20:15 +01:00
2016-02-02 09:00:05 +01:00
static float speed = 1.0;
static unsigned previous_ticks = 0;
2016-02-02 08:20:15 +01:00
#ifdef USE_INOTIFY
static int inotify_fd = -1;
static int inotify_watch = -1;
2016-02-01 14:39:58 +01:00
#endif
2016-02-02 08:20:15 +01:00
#endif
2016-02-01 10:19:13 +01:00
2016-02-02 08:20:15 +01:00
static char *filename;
static float current_time = 0;
2016-02-01 14:39:58 +01:00
static GLint time_loc;
static GLint res_loc;
2016-02-01 10:19:13 +01:00
2016-02-02 08:20:15 +01:00
static char * readfile(const char *name) {
FILE *f = fopen(name, "r");
2016-02-02 06:12:13 +01:00
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);
}
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
r = fread(buffer+count, 1, size-count, f);
count += r;
} while (r);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
fclose(f);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
buffer[count] = 0;
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
return buffer;
2016-02-01 10:19:13 +01:00
}
2016-02-01 14:03:21 +01:00
static void printShaderInfoLog(GLuint obj) {
2016-02-02 06:12:13 +01:00
GLint length = 0;
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
if(length > 0) {
char log[length];
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glGetShaderInfoLog(obj, sizeof(log), NULL, log);
fprintf(stderr, "%s\n", log);
}
2016-02-01 10:19:13 +01:00
}
2016-02-01 14:03:21 +01:00
static void printProgramInfoLog(GLuint obj) {
2016-02-02 06:12:13 +01:00
GLint length = 0;
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
if(length > 0) {
char log[length];
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glGetProgramInfoLog(obj, sizeof(log), NULL, log);
fprintf(stderr, "%s\n", log);
}
2016-02-01 10:19:13 +01:00
}
2016-02-02 08:20:15 +01:00
static void load(void) {
2016-02-02 06:12:13 +01:00
char *shader = readfile(filename);
if (!shader) {
fprintf(stderr, "Error: unable to read '%s'\n", filename);
2016-02-02 08:20:15 +01:00
return;
2016-02-02 06:12:13 +01:00
}
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(frag, 1, (const GLchar **)&shader, NULL);
glCompileShader(frag);
printShaderInfoLog(frag);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
GLuint program = glCreateProgram();
glAttachShader(program, frag);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glLinkProgram(program);
printProgramInfoLog(program);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glBindFragDataLocation(program, 0, "fragColor");
time_loc = glGetUniformLocation(program, "time");
res_loc = glGetUniformLocation(program, "res");
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
glUseProgram(program);
2016-02-02 08:20:15 +01:00
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
2016-02-02 08:20:15 +01:00
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();
2016-02-02 08:20:15 +01:00
#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
2016-02-01 14:39:58 +01:00
}
2016-02-01 10:19:13 +01:00
2016-02-01 14:39:58 +01:00
#ifdef GLSLWRITE
static void savePNG(int width, int height, const char *output_dir) {
2016-02-02 06:12:13 +01:00
uint8_t pixels[width*height*4];
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels);
2016-02-01 10:19:13 +01:00
2016-02-02 06:12:13 +01:00
char filename[strlen(output_dir) + 20];
snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, (unsigned)current_time);
2016-02-01 14:39:58 +01:00
2016-02-02 06:12:13 +01:00
FILE *f = fopen(filename, "wb");
2016-02-01 14:39:58 +01:00
if (!f) {
2016-02-02 06:12:13 +01:00
fprintf(stderr, "unable to open PNG file\n");
exit(1);
}
2016-02-01 14:39:58 +01:00
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
2016-02-02 06:12:13 +01:00
fprintf(stderr, "unable to open PNG file\n");
exit(1);
}
2016-02-01 14:39:58 +01:00
2016-02-02 06:12:13 +01:00
png_set_swap_alpha(png_ptr);
2016-02-01 14:39:58 +01:00
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
fprintf(stderr, "unable to create PNG info struct\n");
2016-02-02 06:12:13 +01:00
exit(1);
2016-02-01 14:39:58 +01:00
}
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "unable to write PNG file\n");
2016-02-02 06:12:13 +01:00
exit(1);
2016-02-01 14:39:58 +01:00
}
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];
2016-02-01 10:19:13 +01:00
2016-02-01 14:39:58 +01:00
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
2016-02-01 10:19:13 +01:00
2016-02-01 14:39:58 +01:00
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(f);
}
#endif
static void render(int width, int height) {
2016-02-01 10:19:13 +01:00
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
#ifndef GLSLWRITE
2016-02-02 09:00:05 +01:00
unsigned ticks = SDL_GetTicks();
current_time += speed * (ticks - previous_ticks);
previous_ticks = ticks;
2016-02-01 14:39:58 +01:00
#endif
2016-02-02 09:00:05 +01:00
glUniform1f(time_loc, current_time);
2016-02-02 06:12:13 +01:00
glUniform2f(res_loc, width, height);
2016-02-01 10:19:13 +01:00
glBegin(GL_QUADS);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
}
2016-02-02 09:00:05 +01:00
#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 '+':
speed *= 1.1;
break;
case '-':
speed /= 1.1;
break;
case '=':
2016-02-02 20:32:26 +01:00
if (speed < 0)
speed = -1.0;
else
speed = 1.0;
break;
case '<':
if (speed > 0)
speed = -speed;
break;
case '>':
if (speed < 0)
speed = -speed;
2016-02-02 09:00:05 +01:00
}
}
}
#endif
2016-02-01 10:19:13 +01:00
int main(int argc, char *argv[]) {
2016-02-01 14:39:58 +01:00
#ifdef GLSLWRITE
2016-02-02 06:12:13 +01:00
if (argc != 3) {
fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
exit(1);
}
2016-02-01 14:39:58 +01:00
#else
2016-02-02 06:12:13 +01:00
if (argc != 2) {
2016-02-02 08:20:15 +01:00
fprintf(stderr, "Usage: glslview <shader>\n");
2016-02-02 06:12:13 +01:00
exit(1);
}
2016-02-01 14:39:58 +01:00
#endif
2016-02-01 10:19:13 +01:00
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);
2016-02-02 08:20:15 +01:00
SDL_GLContext ctx = SDL_GL_CreateContext(window);
2016-02-01 10:19:13 +01:00
2016-02-02 08:20:15 +01:00
filename = argv[1];
init();
2016-02-01 10:19:13 +01:00
2016-02-02 09:00:05 +01:00
SDL_StartTextInput();
2016-02-02 08:20:15 +01:00
while (true) {
2016-02-01 10:19:13 +01:00
SDL_Event e;
2016-02-02 08:20:15 +01:00
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
goto quit;
2016-02-02 09:00:05 +01:00
}
break;
2016-02-02 08:20:15 +01:00
#ifndef GLSLWRITE
2016-02-02 09:00:05 +01:00
case SDL_TEXTINPUT:
handle_input(e.text.text);
break;
2016-02-02 08:20:15 +01:00
2016-02-02 09:00:05 +01:00
case SDL_MOUSEWHEEL:
speed /= pow(1.1, e.wheel.y);
2016-02-02 06:12:13 +01:00
break;
2016-02-02 09:00:05 +01:00
#endif
2016-02-02 08:20:15 +01:00
case SDL_QUIT:
goto quit;
2016-02-01 10:19:13 +01:00
}
}
2016-02-02 08:20:15 +01:00
check_reload();
2016-02-02 06:12:13 +01:00
int width, height;
SDL_GetWindowSize(window, &width, &height);
2016-02-01 14:39:58 +01:00
render(width, height);
2016-02-02 06:12:13 +01:00
SDL_GL_SwapWindow(window);
2016-02-01 14:39:58 +01:00
#ifdef GLSLWRITE
2016-02-02 06:12:13 +01:00
savePNG(width, height, argv[2]);
current_time += 20;
2016-02-01 14:39:58 +01:00
#endif
2016-02-01 10:19:13 +01:00
}
2016-02-02 08:20:15 +01:00
quit:
2016-02-02 09:00:05 +01:00
SDL_StopTextInput();
2016-02-02 08:20:15 +01:00
SDL_GL_DeleteContext(ctx);
SDL_DestroyWindow(window);
SDL_Quit();
2016-02-01 10:19:13 +01:00
return 0;
}