query window size every frame so we have something that makes a little bit of sense
This commit is contained in:
parent
8885e6a7c3
commit
61adffe061
2
Makefile
2
Makefile
@ -6,7 +6,7 @@ SRC = glad.c gl_utils.c main.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
BIN = fallingCand
|
||||
|
||||
all: $(BIN)
|
||||
all: clean $(BIN)
|
||||
|
||||
$(BIN): $(OBJ)
|
||||
$(CC) $(OBJ) -o $@ $(LDFLAGS)
|
||||
|
||||
BIN
fallingCand
BIN
fallingCand
Binary file not shown.
44
main.c
44
main.c
@ -3,27 +3,26 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gl_utils.h"
|
||||
static int g_fbWidth = 800;
|
||||
static int g_fbHeight = 800;
|
||||
|
||||
int main(void) {
|
||||
int winW = 800;
|
||||
int winH = 600;
|
||||
|
||||
GLFWwindow* window = init_glfw_glad("Falling Sand - Fullscreen Quad", winW, winH);
|
||||
GLFWwindow* window = init_glfw_glad("Falling Sand - Fullscreen Quad", g_fbWidth, g_fbHeight);
|
||||
if (!window) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("Renderer: %s\n", (const char*)glGetString(GL_RENDERER));
|
||||
printf("OpenGL: %s\n", (const char*)glGetString(GL_VERSION));
|
||||
printf("GLSL: %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||
glfwGetFramebufferSize(window, &g_fbWidth, &g_fbHeight);
|
||||
glViewport(0, 0, g_fbWidth, g_fbHeight);
|
||||
printf("Renderer: %s\n", (const char*)glGetString(GL_RENDERER));
|
||||
printf("OpenGL: %s\n", (const char*)glGetString(GL_VERSION));
|
||||
printf("GLSL: %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||
|
||||
// --- Create fullscreen triangle geometry (VAO + VBO) ---
|
||||
float vertices[] = {
|
||||
// x, y
|
||||
-1.0f, -1.0f,
|
||||
3.0f, -1.0f,
|
||||
-1.0f, 3.0f
|
||||
};
|
||||
3.0f, -1.0f,
|
||||
-1.0f, 3.0f};
|
||||
|
||||
GLuint vao = 0, vbo = 0;
|
||||
glGenVertexArrays(1, &vao);
|
||||
@ -42,17 +41,18 @@ int main(void) {
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
2 * sizeof(float),
|
||||
(void*)0
|
||||
);
|
||||
(void*)0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
// --- Create shader program ---
|
||||
GLuint program = create_program_from_files(
|
||||
"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);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
@ -60,10 +60,20 @@ int main(void) {
|
||||
// --- Main loop ---
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
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);
|
||||
|
||||
glUseProgram(program);
|
||||
if (uResLoc != -1) {
|
||||
glUniform2f(uResLoc, (float)g_fbWidth, (float)g_fbHeight);
|
||||
}
|
||||
glBindVertexArray(vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
#version 430 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec2 u_resolution;
|
||||
void main()
|
||||
{
|
||||
// Normalized coordinates in [0,1]
|
||||
vec2 uv = gl_FragCoord.xy / vec2(800.0, 600.0); // we’ll fix this later with a uniform
|
||||
vec2 uv = gl_FragCoord.xy / u_resolution;
|
||||
vec3 color = vec3(uv.x, uv.y, 0.2);
|
||||
FragColor = vec4(color, 1.0);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user