add "shadertoy" template

This commit is contained in:
Zack Buhman 2025-10-19 14:12:53 -05:00
parent d567af85b3
commit cc5b84c13e
7 changed files with 246 additions and 38 deletions

201
src/shadertoy.c Normal file
View File

@ -0,0 +1,201 @@
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <GLFW/glfw3.h>
#include <GLES2/gl2.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)
{
glViewport(0, 0, width, height);
}
unsigned int compile_shaders()
{
void * vertexShaderSource = read_file("src/shadertoy.vp.glsl");
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, (const char **)&vertexShaderSource, NULL);
glCompileShader(vertexShader);
{
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
printf("vertex shader compile failed:\n%s\n", infoLog);
}
}
free(vertexShaderSource);
void * fragmentShaderSource = read_file("src/shadertoy.fp.glsl");
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, (const char **)&fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
{
int success;
char infoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
printf("fragment shader compile failed:\n%s\n", infoLog);
}
}
free(fragmentShaderSource);
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
{
int success;
char infoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
printf("program link failed:\n%s\n", infoLog);
}
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
static int make_buffer(unsigned int target,
const void * data,
size_t size)
{
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(target, buffer);
glBufferData(target, size, data, GL_STATIC_DRAW);
return buffer;
}
static const float vertex_buffer_data[] = {
// position
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};
struct location {
struct {
unsigned int position;
} attrib;
};
int main()
{
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
//glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
GLFWwindow* window = glfwCreateWindow(1200, 1200, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
const char* description;
glfwGetError(&description);
printf("Failed to create GLFW window: %s\n", description);
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glViewport(0, 0, 1200, 1200);
//glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
unsigned int vertex_buffer = make_buffer(GL_ARRAY_BUFFER,
vertex_buffer_data,
(sizeof (vertex_buffer_data)));
printf("compile start\n");
unsigned int shaderProgram = compile_shaders();
printf("compile end\n");
struct location location;
location.attrib.position = glGetAttribLocation(shaderProgram, "position");
printf("attributes:\n position %u\n",
location.attrib.position);
while(!glfwWindowShouldClose(window)) {
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
//glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
//glClear(GL_COLOR_BUFFER_BIT);
//
// render
//
glUseProgram(shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(location.attrib.position,
3,
GL_FLOAT,
GL_FALSE,
(sizeof (float)) * 3,
(void*)0
);
glEnableVertexAttribArray(location.attrib.position);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

8
src/shadertoy.fp.glsl Normal file
View File

@ -0,0 +1,8 @@
#version 120
varying vec3 pos_out;
void main()
{
gl_FragColor = vec4(pos_out, 1);
}

11
src/shadertoy.vp.glsl Normal file
View File

@ -0,0 +1,11 @@
#version 120
attribute vec3 position;
varying vec3 pos_out;
void main()
{
gl_Position = vec4(position, 1);
pos_out = vec3(position);
}

View File

@ -134,7 +134,8 @@ struct location {
} attrib;
struct {
unsigned int texture1;
unsigned int theta;
unsigned int theta1;
unsigned int theta2;
} uniform;
};
@ -187,26 +188,6 @@ const face faces[] = {
{5,1}, {1,4}, {2,2},
};
static inline vec3 rotate(vec3 vertex, float theta)
{
float x = vertex.x;
float y = vertex.y;
float z = vertex.z;
float t;
t = y * cosf(theta) - z * sinf(theta);
z = y * sinf(theta) + z * cosf(theta);
y = t;
float theta2 = 3.14 * sinf(theta / 2);
t = x * cosf(theta2) - z * sinf(theta2);
z = x * sinf(theta2) + z * cosf(theta2);
x = t;
return (vec3){x, y, z};
}
int main()
{
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
@ -265,10 +246,12 @@ int main()
location.attrib.texture);
location.uniform.texture1 = glGetUniformLocation(shaderProgram, "texture1");
location.uniform.theta = glGetUniformLocation(shaderProgram, "theta");
printf("uniforms:\n texture %u\n theta %u\n",
location.uniform.theta1 = glGetUniformLocation(shaderProgram, "theta1");
location.uniform.theta2 = glGetUniformLocation(shaderProgram, "theta2");
printf("uniforms:\n texture %u\n theta1 %u\n theta2 %u\n",
location.uniform.texture1,
location.uniform.theta);
location.uniform.theta1,
location.uniform.theta2);
unsigned int texture1;
glGenTextures(1, &texture1);
@ -290,7 +273,7 @@ int main()
glfwSetWindowShouldClose(window, true);
glClearDepthf(-1000.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_GREATER);
glEnable(GL_DEPTH_TEST);
@ -318,7 +301,8 @@ int main()
glEnableVertexAttribArray(location.attrib.position);
glEnableVertexAttribArray(location.attrib.texture);
glUniform1f(location.uniform.theta, theta);
glUniform1f(location.uniform.theta1, theta);
glUniform1f(location.uniform.theta2, 3.14 * sin(theta / 2));
theta += 0.005f;
glUniform1i(location.uniform.texture1, 0);

View File

@ -3,23 +3,28 @@
attribute vec3 pos;
attribute vec2 tex;
uniform float theta;
uniform float theta1;
uniform float theta2;
vec3 rotate(vec3 v)
{
float x = v.x;
float y = v.y;
float z = v.z;
float ct1 = cos(theta1);
float st1 = sin(theta1);
float x1 = x;
float y1 = y * cos(theta) - z * sin(theta);
float z1 = y * sin(theta) + z * cos(theta);
float ct2 = cos(theta2);
float st2 = sin(theta2);
float theta2 = 3.14 * sin(theta / 2);
float x0 = v.x;
float y0 = v.y;
float z0 = v.z;
float x2 = x1 * cos(theta2) - z1 * sin(theta2);
float x1 = x0;
float y1 = y0 * ct1 - z0 * st1;
float z1 = y0 * st1 + z0 * ct1;
float x2 = x1 * ct2 - z1 * st2;
float y2 = y1;
float z2 = x1 * sin(theta2) + z1 * cos(theta2);
float z2 = x1 * st2 + z1 * ct2;
return vec3(x2 * 0.2,
y2 * 0.2,

View File

@ -131,6 +131,7 @@ struct location {
int main()
{
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

View File

@ -3,8 +3,6 @@
attribute vec3 position;
attribute vec3 color;
//varying vec3 color_out;
void main()
{
gl_Position = vec4(position, 1);