gles running

This commit is contained in:
Rakarake 2025-12-13 15:20:34 +01:00
parent 9cbcd7acca
commit 75dc9659dd
2 changed files with 146 additions and 103 deletions

View file

@ -1,76 +1,80 @@
#version 320 es
precision mediump float;
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
uniform vec2 WindowSize;
uniform float time;
layout(location = 0) out vec4 color;
float rand(ivec2 co) {
return fract(sin(dot(vec2(co), vec2(12.9898, 78.233))) * 43758.5453);
}
float cell_size = 0.01;
//#version 330 core
//out vec4 FragColor;
//uniform vec2 WindowSize;
//uniform float time;
//
//float rand(ivec2 co) {
// return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
//}
//
//float cell_size = 0.01;
//
//#define PI 3.1415926538
//
//// whatever that corner contributes
//float c_dot(vec2 point, vec2 corner) {
// ivec2 c = ivec2(round(corner * 100));
// float angle = rand(c) * 2*PI;
// vec2 random_corner_vec = vec2(cos(angle), sin(angle));
// vec2 offset = (point - corner) / cell_size;
// float dot_product = dot(random_corner_vec, offset);
// // for now just return something
// return dot_product;
//}
//
//float lerp(float a, float b, float x) {
// return a + x*(b-a);
//}
//
//// better than built in smoothstep.
//float fade(float t) {
// return ((6*t - 15)*t + 10)*t*t*t;
//}
#define PI 3.1415926538
// whatever that corner contributes
float c_dot(vec2 point, vec2 corner) {
ivec2 c = ivec2(round(corner * 100.0));
float angle = rand(c) * 2.0*PI;
vec2 random_corner_vec = vec2(cos(angle), sin(angle));
vec2 offset = (point - corner) / cell_size;
float dot_product = dot(random_corner_vec, offset);
// for now just return something
return dot_product;
}
float lerp(float a, float b, float x) {
return a + x*(b-a);
}
// better than built in smoothstep.
float fade(float t) {
return ((6.0*t - 15.0)*t + 10.0)*t*t*t;
}
void main() {
// We'll make origin in the top-left.
vec2 uv = gl_FragCoord.xy / WindowSize;
uv.y = 1.0 - uv.y;
vec2 grid_edge_offset = mod(uv, cell_size);
// c: corner
vec2 c_tl = uv - grid_edge_offset;
vec2 c_tr = uv - grid_edge_offset + vec2(cell_size, 0);
vec2 c_bl = uv - grid_edge_offset + vec2(0, cell_size);
vec2 c_br = uv - grid_edge_offset + vec2(cell_size, cell_size);
float c_tl_dot = c_dot(uv, c_tl);
float c_tr_dot = c_dot(uv, c_tr);
float c_bl_dot = c_dot(uv, c_bl);
float c_br_dot = c_dot(uv, c_br);
// the uv within the tile (0 to 1)
vec2 tile_uv = (uv - c_tl) / cell_size;
// lerp index
float u = fade(tile_uv.x);
float v = fade(tile_uv.y);
//float u = tile_uv.x;
//float v = tile_uv.y;
//float u = smoothstep(0, 1, tile_uv.x);
//float v = smoothstep(0, 1, tile_uv.y);
//float val = smoothstep(tile_uv.x, smoothstep(tile_uv.y, c_bl_dot, c_tl_dot), smoothstep(tile_uv.y, c_br_dot, c_tr_dot));
float val = lerp(lerp(c_tl_dot, c_bl_dot, v), lerp(c_tr_dot, c_br_dot, v), u);
//float val = tile_uv.y;
//float val = c_bl_dot*2 + 0.5;
//val = val * 20;
float out_val = val /2.0 + 0.5;
color = vec4(out_val, out_val, out_val, 1.0);
//float yes = 0.5 + (c_dot(uv, c_tl) + c_dot(uv, c_tr) + c_dot(uv, c_bl) + c_dot(uv, c_br)) / 2;
//FragColor = vec4(yes, yes, yes, 1);
}
//#version 320 es
//precision mediump float;
//layout(location = 0) out vec4 outColor;
//
//void main() {
// // We'll make origin in the top-left.
// vec2 uv = gl_FragCoord.xy / WindowSize;
// uv.y = 1.0 - uv.y;
// vec2 grid_edge_offset = mod(uv, cell_size);
// // c: corner
// vec2 c_tl = uv - grid_edge_offset;
// vec2 c_tr = uv - grid_edge_offset + vec2(cell_size, 0);
// vec2 c_bl = uv - grid_edge_offset + vec2(0, cell_size);
// vec2 c_br = uv - grid_edge_offset + vec2(cell_size, cell_size);
// float c_tl_dot = c_dot(uv, c_tl);
// float c_tr_dot = c_dot(uv, c_tr);
// float c_bl_dot = c_dot(uv, c_bl);
// float c_br_dot = c_dot(uv, c_br);
//
// // the uv within the tile (0 to 1)
// vec2 tile_uv = (uv - c_tl) / cell_size;
//
// // lerp index
// float u = fade(tile_uv.x);
// float v = fade(tile_uv.y);
// //float u = tile_uv.x;
// //float v = tile_uv.y;
// //float u = smoothstep(0, 1, tile_uv.x);
// //float v = smoothstep(0, 1, tile_uv.y);
// //float val = smoothstep(tile_uv.x, smoothstep(tile_uv.y, c_bl_dot, c_tl_dot), smoothstep(tile_uv.y, c_br_dot, c_tr_dot));
// float val = lerp(lerp(c_tl_dot, c_bl_dot, v), lerp(c_tr_dot, c_br_dot, v), u);
// //float val = tile_uv.y;
// //float val = c_bl_dot*2 + 0.5;
// //val = val * 20;
// float out_val = val /2 + 0.5;
// FragColor = vec4(out_val, out_val, out_val, 1);
//
// //float yes = 0.5 + (c_dot(uv, c_tl) + c_dot(uv, c_tr) + c_dot(uv, c_bl) + c_dot(uv, c_br)) / 2;
// //FragColor = vec4(yes, yes, yes, 1);
// outColor = vec4(1.0, 0.0, 0.0, 1.0);
//}

105
main.c
View file

@ -99,11 +99,18 @@ GLuint compile_shader(GLuint type, const char *src) {
}
const char *vertex_shader_src =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";
//"attribute vec4 vPosition; \n"
//"void main() \n"
//"{ \n"
//" gl_Position = vPosition; \n"
//"} \n";
"#version 320 es\n"
"\n"
"layout(location = 0) in vec2 aPos;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
"}\n";
/// returns 0 if failed.
GLuint create_shader(const char *fragment_src) {
@ -137,20 +144,20 @@ GLuint load_shader(const char *path) {
GLuint shader = create_shader(string);
if (shader == 0) {
// print file
int line_count = 1;
printf("%i\t", line_count);
line_count++;
for (int i = 0; i < length; i++) {
printf("%c", string[i]);
if (string[i] == '\n') {
printf("%i\t", line_count);
line_count++;
}
}
printf("\n");
}
//if (shader == 0) {
// // print file
// int line_count = 1;
// printf("%i\t", line_count);
// line_count++;
// for (int i = 0; i < length; i++) {
// printf("%c", string[i]);
// if (string[i] == '\n') {
// printf("%i\t", line_count);
// line_count++;
// }
// }
// printf("\n");
//}
free(string);
return shader;
@ -208,6 +215,41 @@ SDL_Log("GL_RENDERER: %s", renderer);
GLuint shader = load_shader("fragment.glsl"); //create_shader(fragment_shader_source);
// buffers
// Load the vertex data
GLfloat vertices[] = {
-1.0, 1.0,
-1.0, -1.0,
1.0, -1.0,
1.0, -1.0,
1.0, 1.0,
-1.0, 1.0,
};
GLuint vao, vbo;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER,
sizeof(vertices),
vertices,
GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0, // location
2, // vec2
GL_FLOAT,
GL_FALSE,
0,
(void *)0
);
int w, h;
double time;
while (running) {
@ -228,22 +270,19 @@ SDL_Log("GL_RENDERER: %s", renderer);
// Shader parameters.
glUseProgram(shader);
//int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize");
//glUniform2f(uniform_WindowSize, w, h);
int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize");
glUniform2f(uniform_WindowSize, w, h);
//time = time_since_start();
//int uniform_time = glGetUniformLocation(shader, "time");
//glUniform1f(uniform_time, (float)time);
time = time_since_start();
int uniform_time = glGetUniformLocation(shader, "time");
glUniform1f(uniform_time, (float)time);
// Load the vertex data
GLfloat vVertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
//glEnableVertexAttribArray(0);
//glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
SDL_GL_SwapWindow(window);
checkGlError();