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;
void main() { uniform vec2 WindowSize;
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 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 #define PI 3.1415926538
//out vec4 FragColor;
//uniform vec2 WindowSize; // whatever that corner contributes
//uniform float time; float c_dot(vec2 point, vec2 corner) {
// ivec2 c = ivec2(round(corner * 100.0));
//float rand(ivec2 co) { float angle = rand(c) * 2.0*PI;
// return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); vec2 random_corner_vec = vec2(cos(angle), sin(angle));
//} vec2 offset = (point - corner) / cell_size;
// float dot_product = dot(random_corner_vec, offset);
//float cell_size = 0.01; // for now just return something
// return dot_product;
//#define PI 3.1415926538 }
//
//// whatever that corner contributes float lerp(float a, float b, float x) {
//float c_dot(vec2 point, vec2 corner) { return a + x*(b-a);
// ivec2 c = ivec2(round(corner * 100)); }
// float angle = rand(c) * 2*PI;
// vec2 random_corner_vec = vec2(cos(angle), sin(angle)); // better than built in smoothstep.
// vec2 offset = (point - corner) / cell_size; float fade(float t) {
// float dot_product = dot(random_corner_vec, offset); return ((6.0*t - 15.0)*t + 10.0)*t*t*t;
// // for now just return something }
// return dot_product;
//} void main() {
// // We'll make origin in the top-left.
//float lerp(float a, float b, float x) { vec2 uv = gl_FragCoord.xy / WindowSize;
// return a + x*(b-a); uv.y = 1.0 - uv.y;
//} vec2 grid_edge_offset = mod(uv, cell_size);
// // c: corner
//// better than built in smoothstep. vec2 c_tl = uv - grid_edge_offset;
//float fade(float t) { vec2 c_tr = uv - grid_edge_offset + vec2(cell_size, 0);
// return ((6*t - 15)*t + 10)*t*t*t; 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() { //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);
//} //}

105
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) {
@ -228,22 +270,19 @@ SDL_Log("GL_RENDERER: %s", renderer);
// Shader parameters. // Shader parameters.
glUseProgram(shader); glUseProgram(shader);
//int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize"); int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize");
//glUniform2f(uniform_WindowSize, w, h); glUniform2f(uniform_WindowSize, w, h);
//time = time_since_start(); time = time_since_start();
//int uniform_time = glGetUniformLocation(shader, "time"); int uniform_time = glGetUniformLocation(shader, "time");
//glUniform1f(uniform_time, (float)time); glUniform1f(uniform_time, (float)time);
// Load the vertex data //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
GLfloat vVertices[] = { //glEnableVertexAttribArray(0);
0.0f, 0.5f, 0.0f, //glDrawArrays(GL_TRIANGLES, 0, 6);
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f glBindVertexArray(vao);
}; glDrawArrays(GL_TRIANGLES, 0, 6);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
checkGlError(); checkGlError();