small cleaning

This commit is contained in:
2025-09-12 10:34:32 +02:00
parent f1cd2b293c
commit 1c663b6a7c
3 changed files with 92 additions and 56 deletions
+1
View File
@@ -86,6 +86,7 @@ make -f Makefile.dev release-arch
- [x] Load static fragment shader into GLSL - [x] Load static fragment shader into GLSL
- [x] Add default uniforms - [x] Add default uniforms
- [ ] Read fragment shader from file - [ ] Read fragment shader from file
- [ ] Handle compilation errors
- [ ] Minimal working fragment sample - [ ] Minimal working fragment sample
- [ ] Hot-reload fragment shader - [ ] Hot-reload fragment shader
- [ ] Force fullscreen - [ ] Force fullscreen
+68 -56
View File
@@ -11,10 +11,6 @@
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
typedef struct Vertex {
vec2 pos;
} Vertex;
static const Vertex vertices[6] = {{{0.0f, 0.0f}}, {{0.0f, 1.0f}}, static const Vertex vertices[6] = {{{0.0f, 0.0f}}, {{0.0f, 1.0f}},
{{1.0f, 1.0f}}, {{0.0f, 0.0f}}, {{1.0f, 1.0f}}, {{0.0f, 0.0f}},
{{1.0f, 1.0f}}, {{1.0f, 0.0f}}}; {{1.0f, 1.0f}}, {{1.0f, 0.0f}}};
@@ -57,9 +53,7 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
} }
} }
void forge_run(parameters params) { void *init_window(GLFWwindow **window) {
GLFWwindow *window;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
fprintf(stdout, "[GLFW] %s\n", glfwGetVersionString()); fprintf(stdout, "[GLFW] %s\n", glfwGetVersionString());
@@ -73,72 +67,90 @@ void forge_run(parameters params) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(640, 480, PACKAGE " " VERSION, NULL, NULL); (*window) = glfwCreateWindow(640, 480, PACKAGE " " VERSION, NULL, NULL);
if (!window) { if (!(*window)) {
fprintf(stderr, "[GLFW] Window or context creation failed\n"); fprintf(stderr, "[GLFW] Window or context creation failed\n");
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent((*window));
gladLoadGL(glfwGetProcAddress); gladLoadGL(glfwGetProcAddress);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback((*window), key_callback);
glfwSwapInterval(1); glfwSwapInterval(1);
GLuint vertex_buffer; return window;
glGenBuffers(1, &vertex_buffer); }
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
ShaderProgram init_program() {
ShaderProgram program = {};
glGenBuffers(1, &program.vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, program.vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); program.vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glShaderSource(program.vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader); glCompileShader(program.vertex_shader);
const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); program.fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glShaderSource(program.fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader); glCompileShader(program.fragment_shader);
const GLuint program = glCreateProgram(); program.program = glCreateProgram();
glAttachShader(program, vertex_shader); glAttachShader(program.program, program.vertex_shader);
glAttachShader(program, fragment_shader); glAttachShader(program.program, program.fragment_shader);
glLinkProgram(program); glLinkProgram(program.program);
const GLint mvp_location = glGetUniformLocation(program, "mvp"); program.mvp_location = glGetUniformLocation(program.program, "mvp");
const GLint itime_location = glGetUniformLocation(program, "iTime"); program.itime_location = glGetUniformLocation(program.program, "iTime");
const GLint ires_location = glGetUniformLocation(program, "iResolution"); program.ires_location = glGetUniformLocation(program.program, "iResolution");
const GLint vpos_location = glGetAttribLocation(program, "vPos"); program.vpos_location = glGetAttribLocation(program.program, "vPos");
GLuint vertex_array; glGenVertexArrays(1, &program.vertex_array);
glGenVertexArrays(1, &vertex_array); glBindVertexArray(program.vertex_array);
glBindVertexArray(vertex_array); glEnableVertexAttribArray(program.vpos_location);
glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(program.vpos_location, 2, GL_FLOAT, GL_FALSE,
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), sizeof(Vertex), (void *)offsetof(Vertex, pos));
(void *)offsetof(Vertex, pos));
return program;
}
void loop(GLFWwindow *window, ShaderProgram program) {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / (float)height;
vec2 resolution = {(float)width, (float)height};
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
mat4x4 m, p, mvp;
mat4x4_identity(m);
mat4x4_ortho(p, 0, ratio, 0.0f, 1.0f, 1.0f, 0.0f);
mat4x4_mul(mvp, p, m);
glUseProgram(program.program);
glUniformMatrix4fv(program.mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glUniform1f(program.itime_location, (const GLfloat)glfwGetTime());
glUniform2fv(program.ires_location, 1, (const GLfloat *)&resolution);
glBindVertexArray(program.vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
}
void forge_run(parameters params) {
GLFWwindow *window;
init_window(&window);
ShaderProgram program = init_program();
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
int width, height; loop(window, program);
glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / (float)height;
vec2 resolution = {(float)width, (float)height};
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
mat4x4 m, p, mvp;
mat4x4_identity(m);
mat4x4_ortho(p, 0, ratio, 0.0f, 1.0f, 1.0f, 0.0f);
mat4x4_mul(mvp, p, m);
glUseProgram(program);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glUniform1f(itime_location, (const GLfloat)glfwGetTime());
glUniform2fv(ires_location, 1, (const GLfloat *)&resolution);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
} }
glfwTerminate(); glfwTerminate();
+23
View File
@@ -1,3 +1,6 @@
#include "linmath.h"
#include <glad/gl.h>
#ifndef TYPES_H #ifndef TYPES_H
#define TYPES_H #define TYPES_H
@@ -5,6 +8,26 @@ struct Parameters {
// TODO // TODO
}; };
typedef struct Vertex {
vec2 pos;
} Vertex;
typedef struct ShaderProgram {
GLuint program;
GLuint vertex_buffer;
GLuint vertex_shader;
GLuint fragment_shader;
GLuint mvp_location;
GLuint vpos_location;
GLuint itime_location;
GLuint ires_location;
GLuint vertex_array;
} ShaderProgram;
typedef struct Parameters parameters; typedef struct Parameters parameters;
#endif #endif