Fix indentation
This commit is contained in:
parent
4b03ea2a9c
commit
a240df3979
1 changed files with 82 additions and 82 deletions
164
glslview.c
164
glslview.c
|
@ -23,117 +23,117 @@ static GLint res_loc;
|
||||||
|
|
||||||
|
|
||||||
static char * readfile(const char *filename) {
|
static char * readfile(const char *filename) {
|
||||||
FILE *f = fopen(filename, "r");
|
FILE *f = fopen(filename, "r");
|
||||||
if (!f)
|
if (!f)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
size_t size = 1024;
|
size_t size = 1024;
|
||||||
char *buffer = malloc(size+1);
|
char *buffer = malloc(size+1);
|
||||||
size_t count = 0, r;
|
size_t count = 0, r;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (count == size) {
|
if (count == size) {
|
||||||
size *= 2;
|
size *= 2;
|
||||||
buffer = realloc(buffer, size+1);
|
buffer = realloc(buffer, size+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = fread(buffer+count, 1, size-count, f);
|
r = fread(buffer+count, 1, size-count, f);
|
||||||
count += r;
|
count += r;
|
||||||
} while (r);
|
} while (r);
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
buffer[count] = 0;
|
buffer[count] = 0;
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printShaderInfoLog(GLuint obj) {
|
static void printShaderInfoLog(GLuint obj) {
|
||||||
GLint length = 0;
|
GLint length = 0;
|
||||||
|
|
||||||
glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length);
|
glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
|
||||||
if(length > 0) {
|
if(length > 0) {
|
||||||
char log[length];
|
char log[length];
|
||||||
|
|
||||||
glGetShaderInfoLog(obj, sizeof(log), NULL, log);
|
glGetShaderInfoLog(obj, sizeof(log), NULL, log);
|
||||||
fprintf(stderr, "%s\n", log);
|
fprintf(stderr, "%s\n", log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printProgramInfoLog(GLuint obj) {
|
static void printProgramInfoLog(GLuint obj) {
|
||||||
GLint length = 0;
|
GLint length = 0;
|
||||||
|
|
||||||
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length);
|
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
|
||||||
if(length > 0) {
|
if(length > 0) {
|
||||||
char log[length];
|
char log[length];
|
||||||
|
|
||||||
glGetProgramInfoLog(obj, sizeof(log), NULL, log);
|
glGetProgramInfoLog(obj, sizeof(log), NULL, log);
|
||||||
fprintf(stderr, "%s\n", log);
|
fprintf(stderr, "%s\n", log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init(char *filename) {
|
static void init(char *filename) {
|
||||||
glewInit();
|
glewInit();
|
||||||
|
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
char *shader = readfile(filename);
|
char *shader = readfile(filename);
|
||||||
if (!shader) {
|
if (!shader) {
|
||||||
fprintf(stderr, "Error: unable to read '%s'\n", filename);
|
fprintf(stderr, "Error: unable to read '%s'\n", filename);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
|
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
glShaderSource(frag, 1, (const GLchar **)&shader, NULL);
|
glShaderSource(frag, 1, (const GLchar **)&shader, NULL);
|
||||||
glCompileShader(frag);
|
glCompileShader(frag);
|
||||||
printShaderInfoLog(frag);
|
printShaderInfoLog(frag);
|
||||||
|
|
||||||
GLuint program = glCreateProgram();
|
GLuint program = glCreateProgram();
|
||||||
glAttachShader(program, frag);
|
glAttachShader(program, frag);
|
||||||
|
|
||||||
glLinkProgram(program);
|
glLinkProgram(program);
|
||||||
printProgramInfoLog(program);
|
printProgramInfoLog(program);
|
||||||
|
|
||||||
glBindFragDataLocation(program, 0, "fragColor");
|
glBindFragDataLocation(program, 0, "fragColor");
|
||||||
time_loc = glGetUniformLocation(program, "time");
|
time_loc = glGetUniformLocation(program, "time");
|
||||||
res_loc = glGetUniformLocation(program, "res");
|
res_loc = glGetUniformLocation(program, "res");
|
||||||
|
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GLSLWRITE
|
#ifdef GLSLWRITE
|
||||||
static void savePNG(int width, int height, const char *output_dir) {
|
static void savePNG(int width, int height, const char *output_dir) {
|
||||||
uint8_t pixels[width*height*4];
|
uint8_t pixels[width*height*4];
|
||||||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels);
|
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels);
|
||||||
|
|
||||||
char filename[strlen(output_dir) + 20];
|
char filename[strlen(output_dir) + 20];
|
||||||
snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, frame_time);
|
snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, frame_time);
|
||||||
|
|
||||||
FILE *f = fopen(filename, "wb");
|
FILE *f = fopen(filename, "wb");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
fprintf(stderr, "unable to open PNG file\n");
|
fprintf(stderr, "unable to open PNG file\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||||
if (!png_ptr) {
|
if (!png_ptr) {
|
||||||
fprintf(stderr, "unable to open PNG file\n");
|
fprintf(stderr, "unable to open PNG file\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
png_set_swap_alpha(png_ptr);
|
png_set_swap_alpha(png_ptr);
|
||||||
|
|
||||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||||
if (!info_ptr) {
|
if (!info_ptr) {
|
||||||
fprintf(stderr, "unable to create PNG info struct\n");
|
fprintf(stderr, "unable to create PNG info struct\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||||
fprintf(stderr, "unable to write PNG file\n");
|
fprintf(stderr, "unable to write PNG file\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
png_init_io(png_ptr, f);
|
png_init_io(png_ptr, f);
|
||||||
|
@ -157,11 +157,11 @@ static void render(int width, int height) {
|
||||||
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
|
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
|
||||||
|
|
||||||
#ifdef GLSLWRITE
|
#ifdef GLSLWRITE
|
||||||
glUniform1f(time_loc, frame_time);
|
glUniform1f(time_loc, frame_time);
|
||||||
#else
|
#else
|
||||||
glUniform1f(time_loc, SDL_GetTicks());
|
glUniform1f(time_loc, SDL_GetTicks());
|
||||||
#endif
|
#endif
|
||||||
glUniform2f(res_loc, width, height);
|
glUniform2f(res_loc, width, height);
|
||||||
|
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
glVertex2f(-1, -1);
|
glVertex2f(-1, -1);
|
||||||
|
@ -171,7 +171,7 @@ static void render(int width, int height) {
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
#ifdef GLSLWRITE
|
#ifdef GLSLWRITE
|
||||||
frame_time += 20;
|
frame_time += 20;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,23 +180,23 @@ int main(int argc, char *argv[]) {
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
#ifdef GLSLWRITE
|
#ifdef GLSLWRITE
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
|
fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Usage: glslview <shader> <output directory>\n");
|
fprintf(stderr, "Usage: glslview <shader> <output directory>\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
|
||||||
SDL_Window *window = SDL_CreateWindow("glslwrite", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
|
SDL_Window *window = SDL_CreateWindow("glslwrite", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
|
||||||
SDL_GL_CreateContext(window);
|
SDL_GL_CreateContext(window);
|
||||||
|
|
||||||
init(argv[1]);
|
init(argv[1]);
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
@ -204,18 +204,18 @@ int main(int argc, char *argv[]) {
|
||||||
while( SDL_PollEvent(&e)) {
|
while( SDL_PollEvent(&e)) {
|
||||||
if(e.type == SDL_QUIT) {
|
if(e.type == SDL_QUIT) {
|
||||||
running = false;
|
running = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int width, height;
|
int width, height;
|
||||||
SDL_GetWindowSize(window, &width, &height);
|
SDL_GetWindowSize(window, &width, &height);
|
||||||
|
|
||||||
render(width, height);
|
render(width, height);
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
|
|
||||||
#ifdef GLSLWRITE
|
#ifdef GLSLWRITE
|
||||||
savePNG(width, height, argv[2]);
|
savePNG(width, height, argv[2]);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue