read shader sources from filesystem
This commit is contained in:
parent
5eadf6016a
commit
6c57490bdb
14
src/fragment.glsl
Normal file
14
src/fragment.glsl
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform sampler2D textures[2];
|
||||||
|
|
||||||
|
varying float fade_factor;
|
||||||
|
varying vec2 texcoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_FragColor = mix(texture2D(textures[0], texcoord),
|
||||||
|
texture2D(textures[1], texcoord),
|
||||||
|
fade_factor
|
||||||
|
);
|
||||||
|
}
|
149
src/main.c
149
src/main.c
@ -10,54 +10,49 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
static void * read_file(const char * filename)
|
||||||
|
{
|
||||||
|
int fd = open(filename, O_RDONLY);
|
||||||
|
if (fd == -1) {
|
||||||
|
fprintf(stderr, "open(%s): %s\n", filename, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
off_t size = lseek(fd, 0, SEEK_END);
|
||||||
|
if (size == (off_t)-1) {
|
||||||
|
fprintf(stderr, "lseek(%s, SEEK_END): %s\n", filename, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
off_t start = lseek(fd, 0, SEEK_SET);
|
||||||
|
if (start == (off_t)-1) {
|
||||||
|
fprintf(stderr, "lseek(%s, SEEK_SET): %s\n", filename, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void * buf = malloc(size+1);
|
||||||
|
|
||||||
|
ssize_t read_size = read(fd, buf, size);
|
||||||
|
if (read_size == -1) {
|
||||||
|
fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
((char*)buf)[read_size] = 0;
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
{
|
{
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *vertexShaderSource = R"(
|
|
||||||
#version 120
|
|
||||||
|
|
||||||
attribute vec2 position;
|
|
||||||
|
|
||||||
varying vec2 texcoord;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
gl_Position = vec4(position, 0.0, 1.0);
|
|
||||||
|
|
||||||
vec2 tex = position * vec2(0.5) + vec2(0.5);
|
|
||||||
|
|
||||||
texcoord = vec2(tex.x * 400 / 512,
|
|
||||||
(1.0 - tex.y) * 300 / 512);
|
|
||||||
}
|
|
||||||
)"
|
|
||||||
;
|
|
||||||
|
|
||||||
const char *fragmentShaderSource = R"(
|
|
||||||
#version 120
|
|
||||||
|
|
||||||
uniform float fade_factor;
|
|
||||||
uniform sampler2D textures[2];
|
|
||||||
|
|
||||||
varying vec2 texcoord;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
gl_FragColor = mix(texture2D(textures[0], texcoord),
|
|
||||||
texture2D(textures[1], texcoord),
|
|
||||||
fade_factor
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
)"
|
|
||||||
;
|
|
||||||
|
|
||||||
unsigned int compile_shaders()
|
unsigned int compile_shaders()
|
||||||
{
|
{
|
||||||
|
void * vertexShaderSource = read_file("src/vertex.glsl");
|
||||||
unsigned int vertexShader;
|
unsigned int vertexShader;
|
||||||
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
glShaderSource(vertexShader, 1, (const char **)&vertexShaderSource, NULL);
|
||||||
glCompileShader(vertexShader);
|
glCompileShader(vertexShader);
|
||||||
{
|
{
|
||||||
int success;
|
int success;
|
||||||
@ -68,10 +63,12 @@ unsigned int compile_shaders()
|
|||||||
printf("vertex shader compile failed:\n%s\n", infoLog);
|
printf("vertex shader compile failed:\n%s\n", infoLog);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(vertexShaderSource);
|
||||||
|
|
||||||
|
void * fragmentShaderSource = read_file("src/fragment.glsl");
|
||||||
unsigned int fragmentShader;
|
unsigned int fragmentShader;
|
||||||
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
|
glShaderSource(fragmentShader, 1, (const char **)&fragmentShaderSource, NULL);
|
||||||
glCompileShader(fragmentShader);
|
glCompileShader(fragmentShader);
|
||||||
{
|
{
|
||||||
int success;
|
int success;
|
||||||
@ -82,6 +79,7 @@ unsigned int compile_shaders()
|
|||||||
printf("fragment shader compile failed:\n%s\n", infoLog);
|
printf("fragment shader compile failed:\n%s\n", infoLog);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(fragmentShaderSource);
|
||||||
|
|
||||||
unsigned int shaderProgram;
|
unsigned int shaderProgram;
|
||||||
shaderProgram = glCreateProgram();
|
shaderProgram = glCreateProgram();
|
||||||
@ -118,47 +116,16 @@ static int make_buffer(unsigned int target,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const float vertex_buffer_data[] = {
|
static const float vertex_buffer_data[] = {
|
||||||
-1.0f, -1.0f,
|
-1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
1.0f, -1.0f,
|
1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
-1.0f, 1.0f,
|
-1.0f, 1.0f, 0.0f, 1.0f,
|
||||||
1.0f, 1.0f
|
1.0f, 1.0f, 0.0f, 1.0f,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const unsigned short element_buffer_data[] = {
|
static const unsigned short element_buffer_data[] = {
|
||||||
0, 1, 2, 3
|
0, 1, 2, 3
|
||||||
};
|
};
|
||||||
|
|
||||||
static void * read_file(const char * filename)
|
|
||||||
{
|
|
||||||
int fd = open(filename, O_RDONLY);
|
|
||||||
if (fd == -1) {
|
|
||||||
fprintf(stderr, "open(%s): %s\n", filename, strerror(errno));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
off_t size = lseek(fd, 0, SEEK_END);
|
|
||||||
if (size == (off_t)-1) {
|
|
||||||
fprintf(stderr, "lseek(%s, SEEK_END): %s\n", filename, strerror(errno));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
off_t start = lseek(fd, 0, SEEK_SET);
|
|
||||||
if (start == (off_t)-1) {
|
|
||||||
fprintf(stderr, "lseek(%s, SEEK_SET): %s\n", filename, strerror(errno));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void * buf = malloc(size);
|
|
||||||
|
|
||||||
ssize_t read_size = read(fd, buf, size);
|
|
||||||
if (read_size == -1) {
|
|
||||||
fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int make_texture(const char * filename)
|
static unsigned int make_texture(const char * filename)
|
||||||
{
|
{
|
||||||
unsigned int texture;
|
unsigned int texture;
|
||||||
@ -188,7 +155,7 @@ static unsigned int make_texture(const char * filename)
|
|||||||
|
|
||||||
struct location {
|
struct location {
|
||||||
struct {
|
struct {
|
||||||
unsigned int fade_factor;
|
unsigned int time;
|
||||||
unsigned int textures[2];
|
unsigned int textures[2];
|
||||||
} uniform;
|
} uniform;
|
||||||
struct {
|
struct {
|
||||||
@ -236,12 +203,12 @@ int main()
|
|||||||
|
|
||||||
struct location location;
|
struct location location;
|
||||||
|
|
||||||
location.uniform.fade_factor = glGetUniformLocation(shaderProgram, "fade_factor");
|
location.uniform.time = glGetUniformLocation(shaderProgram, "time");
|
||||||
location.uniform.textures[0] = glGetUniformLocation(shaderProgram, "textures[0]");
|
location.uniform.textures[0] = glGetUniformLocation(shaderProgram, "textures[0]");
|
||||||
location.uniform.textures[1] = glGetUniformLocation(shaderProgram, "textures[1]");
|
location.uniform.textures[1] = glGetUniformLocation(shaderProgram, "textures[1]");
|
||||||
|
|
||||||
printf("uniforms:\n fade_factor: %u\n textures_0: %u\n textures_1: %u\n",
|
printf("uniforms:\n time: %u\n textures_0: %u\n textures_1: %u\n",
|
||||||
location.uniform.fade_factor,
|
location.uniform.time,
|
||||||
location.uniform.textures[0],
|
location.uniform.textures[0],
|
||||||
location.uniform.textures[1]);
|
location.uniform.textures[1]);
|
||||||
|
|
||||||
@ -249,7 +216,7 @@ int main()
|
|||||||
printf("attributes:\n position %u\n",
|
printf("attributes:\n position %u\n",
|
||||||
location.attrib.position);
|
location.attrib.position);
|
||||||
|
|
||||||
float fade_factor = 0;
|
float time = 0;
|
||||||
|
|
||||||
struct timespec last;
|
struct timespec last;
|
||||||
struct timespec cur;
|
struct timespec cur;
|
||||||
@ -257,17 +224,23 @@ int main()
|
|||||||
clock_gettime(CLOCK_MONOTONIC, &last);
|
clock_gettime(CLOCK_MONOTONIC, &last);
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &cur);
|
||||||
|
uint64_t last_msec = (last.tv_sec * 1000) + (last.tv_nsec * 1e-06);
|
||||||
|
uint64_t cur_msec = (cur.tv_sec * 1000) + (cur.tv_nsec * 1e-06);
|
||||||
|
uint64_t dt = cur_msec - last_msec;
|
||||||
|
time = (float)((double)dt * 0.001);
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
//
|
//
|
||||||
// render
|
// render
|
||||||
//
|
//
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
glUniform1f(location.uniform.fade_factor, fade_factor);
|
glUniform1f(location.uniform.time, time);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture_0);
|
glBindTexture(GL_TEXTURE_2D, texture_0);
|
||||||
@ -279,10 +252,10 @@ int main()
|
|||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||||
glVertexAttribPointer(location.attrib.position,
|
glVertexAttribPointer(location.attrib.position,
|
||||||
2,
|
4,
|
||||||
GL_FLOAT,
|
GL_FLOAT,
|
||||||
GL_FALSE,
|
GL_FALSE,
|
||||||
(sizeof (float)) * 2,
|
(sizeof (float)) * 4,
|
||||||
(void*)0
|
(void*)0
|
||||||
);
|
);
|
||||||
glEnableVertexAttribArray(location.attrib.position);
|
glEnableVertexAttribArray(location.attrib.position);
|
||||||
@ -298,14 +271,6 @@ int main()
|
|||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &cur);
|
|
||||||
uint64_t last_msec = (last.tv_sec * 1000) + (last.tv_nsec * 1e-06);
|
|
||||||
uint64_t cur_msec = (cur.tv_sec * 1000) + (cur.tv_nsec * 1e-06);
|
|
||||||
uint64_t dt = cur_msec - last_msec;
|
|
||||||
fade_factor = sin(dt * 0.001f) * 0.5 + 0.5;
|
|
||||||
|
|
||||||
//last = cur;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
20
src/vertex.glsl
Normal file
20
src/vertex.glsl
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform float time;
|
||||||
|
|
||||||
|
attribute vec4 position;
|
||||||
|
|
||||||
|
varying float fade_factor;
|
||||||
|
varying vec2 texcoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = position;
|
||||||
|
|
||||||
|
vec2 tex = position.xy * vec2(0.5) + vec2(0.5);
|
||||||
|
|
||||||
|
texcoord = vec2(tex.x * 400 / 512,
|
||||||
|
(1.0 - tex.y) * 300 / 512);
|
||||||
|
|
||||||
|
fade_factor = sin(time) * 0.5 + 0.5;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user