static blue pulsating noise

This commit is contained in:
Rakarake 2025-12-08 22:51:47 +01:00
parent 652d9fa921
commit 1a190594fe
2 changed files with 41 additions and 12 deletions

View file

@ -2,8 +2,13 @@
out vec4 FragColor;
in vec2 uv;
uniform vec2 WindowSize;
uniform float time;
void main()
{
FragColor = vec4(gl_FragCoord.xy / WindowSize, 0.0, 1.0);
float rand(vec2 co){
return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
}
void main() {
float glob = rand(gl_FragCoord.xy / WindowSize + time);
FragColor = vec4(glob * (sin(time)/2 + 0.5), glob, 1.0, 1.0); //vec4(gl_FragCoord.xy / WindowSize, 0.0, 1.0);
}

42
main.c
View file

@ -1,6 +1,7 @@
#define GL_GLEXT_PROTOTYPES
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <SDL3/SDL.h>
@ -51,7 +52,10 @@ GLuint load_shader(const char *path) {
long length = ftell(handle);
fseek(handle, 0L, SEEK_SET);
char *string = malloc(length * sizeof(char));
fread(string, sizeof(char), length, handle);
if (!fread(string, sizeof(char), length, handle)) {
printf("failed to read shader file");
exit(1);
}
fclose(handle);
GLuint shader = create_shader(string);
@ -60,7 +64,23 @@ GLuint load_shader(const char *path) {
return shader;
}
struct timespec program_start;
void init_timer() {
clock_gettime(CLOCK_MONOTONIC, &program_start);
}
double time_since_start() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (now.tv_sec - program_start.tv_sec) +
(now.tv_nsec - program_start.tv_nsec) / 1e9;
}
int main() {
init_timer();
printf("Good Morning 🍵\n");
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL3 OpenGL Cube",
@ -82,12 +102,11 @@ int main() {
SDL_Event e;
int running = 1;
float angle = 0.0;
GLuint shader = load_shader("fragment.glsl"); //create_shader(fragment_shader_source);
glUseProgram(shader);
int w, h;
double time;
while (running) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) running = false;
@ -100,14 +119,20 @@ int main() {
// Rendorrrr
glViewport(0, 0, w, h);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // Dark gray background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Shader parameters.
glUseProgram(shader);
int uniform_WindowSize = glGetUniformLocation(shader, "WindowSize");
glUniform2f(uniform_WindowSize, w, h);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // Dark gray background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//drawCube();
// Front
time = time_since_start();
int uniform_time = glGetUniformLocation(shader, "time");
glUniform1f(uniform_time, (float)time);
// Draw quad.
glBegin(GL_QUADS);
glVertex2f(-1, 1);
glVertex2f(-1,-1);
@ -117,7 +142,6 @@ int main() {
glEnd();
SDL_GL_SwapWindow(window);
angle += 0.5;
}
checkGlError();