Restructure glslview/glslwrite code
This commit is contained in:
parent
17d48c425c
commit
22e600bde8
5 changed files with 354 additions and 206 deletions
8
Makefile
8
Makefile
|
@ -5,8 +5,8 @@ USE_INOTIFY = -DUSE_INOTIFY
|
|||
CFLAGS = -O3 -Wall $(USE_INOTIFY)
|
||||
LIBS = -lSDL2 -lGL -lGLEW -lm
|
||||
|
||||
glslview : glslview.c
|
||||
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
|
||||
glslview : glslview.c common.c common.h
|
||||
$(CC) $(CFLAGS) -o $@ glslview.c common.c $(LIBS)
|
||||
|
||||
glslwrite : glslview.c
|
||||
$(CC) $(CFLAGS) -DGLSLWRITE -o $@ $^ $(LIBS) -lpng
|
||||
glslwrite : glslwrite.c common.c common.h
|
||||
$(CC) $(CFLAGS) -o $@ glslwrite.c common.c $(LIBS) -lpng
|
||||
|
|
144
common.c
Normal file
144
common.c
Normal file
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
|
||||
|
||||
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();
|
||||
}
|
42
common.h
Normal file
42
common.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
|
||||
|
||||
extern char *filename;
|
||||
extern float current_time;
|
||||
|
||||
|
||||
void load(void);
|
||||
void init(void);
|
||||
void render(int width, int height);
|
||||
|
||||
void extra_load(GLuint program);
|
222
glslview.c
222
glslview.c
|
@ -23,12 +23,13 @@
|
|||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -38,17 +39,6 @@
|
|||
# include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#ifdef GLSLWRITE
|
||||
# include <png.h>
|
||||
#endif
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
|
||||
|
||||
#ifndef GLSLWRITE
|
||||
|
||||
static float speed = 1.0;
|
||||
static unsigned previous_ticks = 0;
|
||||
|
@ -61,108 +51,13 @@ 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)) {
|
||||
|
@ -177,10 +72,8 @@ static void reset_watch(void) {
|
|||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void init(void) {
|
||||
#if defined(USE_INOTIFY) && !defined(GLSLWRITE)
|
||||
static void init_watch(void) {
|
||||
inotify_fd = inotify_init();
|
||||
if (inotify_fd < 0) {
|
||||
fprintf(stderr, "unable to initialize inotify\n");
|
||||
|
@ -190,77 +83,30 @@ static void init(void) {
|
|||
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);
|
||||
#else
|
||||
|
||||
char filename[strlen(output_dir) + 20];
|
||||
snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, (unsigned)current_time);
|
||||
static void reset_watch(void) {}
|
||||
static void init_watch(void) {}
|
||||
static void check_reload(void) {}
|
||||
|
||||
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
|
||||
void extra_load(GLuint program) {
|
||||
param_k_loc = glGetUniformLocation(program, "param_k");
|
||||
param_l_loc = glGetUniformLocation(program, "param_l");
|
||||
param_m_loc = glGetUniformLocation(program, "param_m");
|
||||
}
|
||||
|
||||
static void update(void) {
|
||||
unsigned ticks = SDL_GetTicks();
|
||||
current_time += speed * (ticks - previous_ticks);
|
||||
previous_ticks = ticks;
|
||||
|
@ -268,27 +114,13 @@ static void render(int width, int height) {
|
|||
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;
|
||||
|
||||
|
@ -326,20 +158,12 @@ static void handle_input(const char *input) {
|
|||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
#ifdef GLSLWRITE
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: glslview <shader>\n");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
|
@ -352,7 +176,10 @@ int main(int argc, char *argv[]) {
|
|||
SDL_GLContext ctx = SDL_GL_CreateContext(window);
|
||||
|
||||
filename = argv[1];
|
||||
|
||||
init_watch();
|
||||
init();
|
||||
load();
|
||||
|
||||
SDL_StartTextInput();
|
||||
|
||||
|
@ -363,7 +190,6 @@ int main(int argc, char *argv[]) {
|
|||
switch (e.type) {
|
||||
case SDL_KEYDOWN:
|
||||
switch (e.key.keysym.sym) {
|
||||
#ifndef GLSLWRITE
|
||||
case SDLK_PAGEDOWN:
|
||||
param_k++;
|
||||
break;
|
||||
|
@ -388,14 +214,11 @@ int main(int argc, char *argv[]) {
|
|||
param_m--;
|
||||
break;
|
||||
|
||||
#endif
|
||||
|
||||
case SDLK_ESCAPE:
|
||||
goto quit;
|
||||
}
|
||||
break;
|
||||
|
||||
#ifndef GLSLWRITE
|
||||
case SDL_TEXTINPUT:
|
||||
handle_input(e.text.text);
|
||||
break;
|
||||
|
@ -403,7 +226,6 @@ int main(int argc, char *argv[]) {
|
|||
case SDL_MOUSEWHEEL:
|
||||
speed /= pow(1.1, e.wheel.y);
|
||||
break;
|
||||
#endif
|
||||
|
||||
case SDL_QUIT:
|
||||
goto quit;
|
||||
|
@ -415,13 +237,9 @@ int main(int argc, char *argv[]) {
|
|||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
|
||||
update();
|
||||
render(width, height);
|
||||
SDL_GL_SwapWindow(window);
|
||||
|
||||
#ifdef GLSLWRITE
|
||||
savePNG(width, height, argv[2]);
|
||||
current_time += 20;
|
||||
#endif
|
||||
}
|
||||
|
||||
quit:
|
||||
|
|
144
glslwrite.c
Normal file
144
glslwrite.c
Normal file
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <png.h>
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
|
||||
|
||||
void extra_load(GLuint program) {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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("glslwrite", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
|
||||
SDL_GLContext ctx = SDL_GL_CreateContext(window);
|
||||
|
||||
filename = argv[1];
|
||||
init();
|
||||
load();
|
||||
|
||||
while (true) {
|
||||
SDL_Event e;
|
||||
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_KEYDOWN:
|
||||
switch (e.key.keysym.sym) {
|
||||
|
||||
case SDLK_ESCAPE:
|
||||
goto quit;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_QUIT:
|
||||
goto quit;
|
||||
}
|
||||
}
|
||||
|
||||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
|
||||
render(width, height);
|
||||
SDL_GL_SwapWindow(window);
|
||||
|
||||
savePNG(width, height, argv[2]);
|
||||
current_time += 20;
|
||||
}
|
||||
|
||||
quit:
|
||||
SDL_GL_DeleteContext(ctx);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
Reference in a new issue