file reading

This commit is contained in:
Rakarake 2025-12-08 20:05:54 +01:00
parent bfb53024e2
commit 652d9fa921
2 changed files with 25 additions and 14 deletions

View file

@ -1,3 +1,9 @@
void main() { #version 330 core
gl_FragColor = vec4(1.0,0.0,1.0,1.0); out vec4 FragColor;
in vec2 uv;
uniform vec2 WindowSize;
void main()
{
FragColor = vec4(gl_FragCoord.xy / WindowSize, 0.0, 1.0);
} }

29
main.c
View file

@ -1,6 +1,7 @@
#define GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
@ -11,17 +12,6 @@ void checkGlError() {
} }
} }
const char *fragment_shader_source =
"#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 uv;\n"
"uniform vec2 WindowSize;\n"
"\n"
"void main()\n"
"{\n"
" FragColor = vec4(gl_FragCoord.xy / WindowSize, 0.0, 1.0);\n"
"}\n";
GLuint compile_shader(GLuint type, const char *src) { GLuint compile_shader(GLuint type, const char *src) {
GLuint id = glCreateShader(GL_FRAGMENT_SHADER); GLuint id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(id, 1, &src, 0); glShaderSource(id, 1, &src, 0);
@ -55,6 +45,21 @@ GLuint create_shader(const char *fragment_src) {
return program; return program;
} }
GLuint load_shader(const char *path) {
FILE *handle = fopen(path, "r");
fseek(handle, 0L, SEEK_END);
long length = ftell(handle);
fseek(handle, 0L, SEEK_SET);
char *string = malloc(length * sizeof(char));
fread(string, sizeof(char), length, handle);
fclose(handle);
GLuint shader = create_shader(string);
free(string);
return shader;
}
int main() { int main() {
printf("Good Morning 🍵\n"); printf("Good Morning 🍵\n");
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
@ -79,7 +84,7 @@ int main() {
int running = 1; int running = 1;
float angle = 0.0; float angle = 0.0;
GLuint shader = create_shader(fragment_shader_source); GLuint shader = load_shader("fragment.glsl"); //create_shader(fragment_shader_source);
glUseProgram(shader); glUseProgram(shader);
int w, h; int w, h;