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; precision mediump float;
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;
#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() { void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // 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;
//#version 330 core // lerp index
//out vec4 FragColor; float u = fade(tile_uv.x);
//uniform vec2 WindowSize; float v = fade(tile_uv.y);
//uniform float time; //float u = tile_uv.x;
// //float v = tile_uv.y;
//float rand(ivec2 co) { //float u = smoothstep(0, 1, tile_uv.x);
// return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); //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 cell_size = 0.01; //float val = tile_uv.y;
// //float val = c_bl_dot*2 + 0.5;
//#define PI 3.1415926538 //val = val * 20;
// float out_val = val /2.0 + 0.5;
//// whatever that corner contributes color = vec4(out_val, out_val, out_val, 1.0);
//float c_dot(vec2 point, vec2 corner) {
// ivec2 c = ivec2(round(corner * 100)); //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;
// float angle = rand(c) * 2*PI; //FragColor = vec4(yes, yes, yes, 1);
// vec2 random_corner_vec = vec2(cos(angle), sin(angle)); }
// vec2 offset = (point - corner) / cell_size;
// float dot_product = dot(random_corner_vec, offset); //#version 320 es
// // for now just return something //precision mediump float;
// return dot_product; //layout(location = 0) out vec4 outColor;
//}
//
//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;
//}
// //
//void main() { //void main() {
// // We'll make origin in the top-left. // outColor = vec4(1.0, 0.0, 0.0, 1.0);
// 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);
//}

107
main.c
View file

@ -99,11 +99,18 @@ GLuint compile_shader(GLuint type, const char *src) {
} }
const char *vertex_shader_src = const char *vertex_shader_src =
"attribute vec4 vPosition; \n" //"attribute vec4 vPosition; \n"
"void main() \n" //"void main() \n"
"{ \n" //"{ \n"
" gl_Position = vPosition; \n" //" gl_Position = vPosition; \n"
"} \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. /// returns 0 if failed.
GLuint create_shader(const char *fragment_src) { GLuint create_shader(const char *fragment_src) {
@ -137,20 +144,20 @@ GLuint load_shader(const char *path) {
GLuint shader = create_shader(string); GLuint shader = create_shader(string);
if (shader == 0) { //if (shader == 0) {
// print file // // print file
int line_count = 1; // int line_count = 1;
printf("%i\t", line_count); // printf("%i\t", line_count);
line_count++; // line_count++;
for (int i = 0; i < length; i++) { // for (int i = 0; i < length; i++) {
printf("%c", string[i]); // printf("%c", string[i]);
if (string[i] == '\n') { // if (string[i] == '\n') {
printf("%i\t", line_count); // printf("%i\t", line_count);
line_count++; // line_count++;
} // }
} // }
printf("\n"); // printf("\n");
} //}
free(string); free(string);
return shader; return shader;
@ -208,6 +215,41 @@ SDL_Log("GL_RENDERER: %s", renderer);
GLuint shader = load_shader("fragment.glsl"); //create_shader(fragment_shader_source); 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; int w, h;
double time; double time;
while (running) { while (running) {
@ -227,23 +269,20 @@ SDL_Log("GL_RENDERER: %s", renderer);
// Shader parameters. // Shader parameters.
glUseProgram(shader); glUseProgram(shader);
//int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize");
//glUniform2f(uniform_WindowSize, w, h);
//time = time_since_start(); int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize");
//int uniform_time = glGetUniformLocation(shader, "time"); glUniform2f(uniform_WindowSize, w, h);
//glUniform1f(uniform_time, (float)time);
// Load the vertex data time = time_since_start();
GLfloat vVertices[] = { int uniform_time = glGetUniformLocation(shader, "time");
0.0f, 0.5f, 0.0f, glUniform1f(uniform_time, (float)time);
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
}; //glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices); //glDrawArrays(GL_TRIANGLES, 0, 6);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
checkGlError(); checkGlError();