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/common.c

160 lines
3.8 KiB
C
Raw Normal View History

2016-02-03 12:53:39 +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.
*/
#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;
int param0 = 0;
int param1 = 0;
int param2 = 0;
2016-02-03 12:53:39 +01:00
static GLint time_loc;
static GLint res_loc;
static GLint param0_loc;
static GLint param1_loc;
static GLint param2_loc;
2016-02-03 12:53:39 +01:00
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");
param0_loc = glGetUniformLocation(program, "param0");
param1_loc = glGetUniformLocation(program, "param1");
param2_loc = glGetUniformLocation(program, "param2");
2016-02-03 12:53:39 +01:00
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);
glUniform1i(param0_loc, param0);
glUniform1i(param1_loc, param1);
glUniform1i(param2_loc, param2);
2016-02-03 12:53:39 +01:00
glBegin(GL_QUADS);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
}