query window size every frame so we have something that makes a little bit of sense

This commit is contained in:
Andrew Cohn 2025-11-29 12:39:05 -08:00
parent 8885e6a7c3
commit 61adffe061
4 changed files with 30 additions and 20 deletions

View File

@ -6,7 +6,7 @@ SRC = glad.c gl_utils.c main.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
BIN = fallingCand BIN = fallingCand
all: $(BIN) all: clean $(BIN)
$(BIN): $(OBJ) $(BIN): $(OBJ)
$(CC) $(OBJ) -o $@ $(LDFLAGS) $(CC) $(OBJ) -o $@ $(LDFLAGS)

Binary file not shown.

36
main.c
View File

@ -3,16 +3,16 @@
#include <stdlib.h> #include <stdlib.h>
#include "gl_utils.h" #include "gl_utils.h"
static int g_fbWidth = 800;
static int g_fbHeight = 800;
int main(void) { int main(void) {
int winW = 800; GLFWwindow* window = init_glfw_glad("Falling Sand - Fullscreen Quad", g_fbWidth, g_fbHeight);
int winH = 600;
GLFWwindow* window = init_glfw_glad("Falling Sand - Fullscreen Quad", winW, winH);
if (!window) { if (!window) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
glfwGetFramebufferSize(window, &g_fbWidth, &g_fbHeight);
glViewport(0, 0, g_fbWidth, g_fbHeight);
printf("Renderer: %s\n", (const char*)glGetString(GL_RENDERER)); printf("Renderer: %s\n", (const char*)glGetString(GL_RENDERER));
printf("OpenGL: %s\n", (const char*)glGetString(GL_VERSION)); printf("OpenGL: %s\n", (const char*)glGetString(GL_VERSION));
printf("GLSL: %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION)); printf("GLSL: %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
@ -22,8 +22,7 @@ int main(void) {
// x, y // x, y
-1.0f, -1.0f, -1.0f, -1.0f,
3.0f, -1.0f, 3.0f, -1.0f,
-1.0f, 3.0f -1.0f, 3.0f};
};
GLuint vao = 0, vbo = 0; GLuint vao = 0, vbo = 0;
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
@ -42,17 +41,18 @@ int main(void) {
GL_FLOAT, GL_FLOAT,
GL_FALSE, GL_FALSE,
2 * sizeof(float), 2 * sizeof(float),
(void*)0 (void*)0);
);
glBindVertexArray(0); glBindVertexArray(0);
// --- Create shader program --- // --- Create shader program ---
GLuint program = create_program_from_files( GLuint program = create_program_from_files(
"shaders/fullscreen.vert", "shaders/fullscreen.vert",
"shaders/gradient.frag" "shaders/gradient.frag");
); GLint uResLoc = glGetUniformLocation(program, "u_resolution");
if (uResLoc == -1) {
fprintf(stderr, "[warn] u_resolution uniform not found (maybe optimized out?)\n");
}
glUseProgram(program); glUseProgram(program);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@ -60,10 +60,20 @@ int main(void) {
// --- Main loop --- // --- Main loop ---
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glfwPollEvents(); glfwPollEvents();
// Re-query framebuffer size each frame (cheap and simple)
int fbw, fbh;
glfwGetFramebufferSize(window, &fbw, &fbh);
if (fbw != g_fbWidth || fbh != g_fbHeight) {
g_fbWidth = fbw;
g_fbHeight = fbh;
glViewport(0, 0, g_fbWidth, g_fbHeight);
}
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program); glUseProgram(program);
if (uResLoc != -1) {
glUniform2f(uResLoc, (float)g_fbWidth, (float)g_fbHeight);
}
glBindVertexArray(vao); glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);

View File

@ -1,11 +1,11 @@
#version 430 core #version 430 core
out vec4 FragColor; out vec4 FragColor;
uniform vec2 u_resolution;
void main() void main()
{ {
// Normalized coordinates in [0,1] // Normalized coordinates in [0,1]
vec2 uv = gl_FragCoord.xy / vec2(800.0, 600.0); // well fix this later with a uniform vec2 uv = gl_FragCoord.xy / u_resolution;
vec3 color = vec3(uv.x, uv.y, 0.2); vec3 color = vec3(uv.x, uv.y, 0.2);
FragColor = vec4(color, 1.0); FragColor = vec4(color, 1.0);
} }