mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 11:25:45 +01:00
provide a better method of embedding shaders within the source code
This commit is contained in:
65
src/io/render.c
Normal file
65
src/io/render.c
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2025 Quinn
|
||||
// Licensed under the MIT Licence. See LICENSE for details
|
||||
#include "render.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../util/vec/float2.h"
|
||||
#include "shader.h"
|
||||
|
||||
#define VERTC 6
|
||||
GLuint pipe;
|
||||
float2 verts[VERTC] = {
|
||||
{-1, -1 }, // pnt A
|
||||
{1, -1 }, // pnt B
|
||||
{1, -0.9F}, // pnt C
|
||||
|
||||
{-1, -0.9F}, // pnt D
|
||||
{1, -0.9F}, // pnt C
|
||||
{-1, -1 }, // pnt A
|
||||
};
|
||||
GLuint vbo; // vertex buffer object
|
||||
GLuint vao; // vertex array object
|
||||
|
||||
int render_init(void) {
|
||||
pipe = glCreateProgram();
|
||||
shader_init(pipe); // initialise and include the shaders
|
||||
glLinkProgram(pipe); // link the application
|
||||
glValidateProgram(pipe); // validate that what we've done is correct
|
||||
|
||||
// init the VBO
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
||||
|
||||
// init the VAO
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
// set VBO info
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float2), NULL);
|
||||
|
||||
glBindVertexArray(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void render_update(GLFWwindow* win) {
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
int w, h;
|
||||
glfwGetWindowSize(win, &w, &h);
|
||||
glViewport(0, 0, w, h);
|
||||
glClearColor(0.1F, 0.1F, 0.1F, 1.0F);
|
||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||
glUseProgram(pipe);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, VERTC);
|
||||
}
|
||||
10
src/io/render.h
Normal file
10
src/io/render.h
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) 2025 Quinn
|
||||
// Licensed under the MIT Licence. See LICENSE for details
|
||||
#pragma once
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
int render_init(void);
|
||||
void render_update(GLFWwindow* win);
|
||||
38
src/io/shader.c
Normal file
38
src/io/shader.c
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2025 Quinn
|
||||
// Licensed under the MIT Licence. See LICENSE for details
|
||||
#include "shader.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define NAM_S(name) _binary_res_##name##_start // name of a start variable
|
||||
#define NAM_E(name) _binary_res_##name##_end // name of an end variable
|
||||
|
||||
// NOTE: we are currently just sucking up the memory costs for ease. We can either include the source files themselves. Or use compression, where I'd prefer the latter for ease of installation.
|
||||
// NOLINTBEGIN (bugprone-reserved-identifier)
|
||||
extern char const NAM_S(default_vert_glsl)[];
|
||||
extern char const NAM_E(default_vert_glsl)[];
|
||||
extern char const NAM_S(default_frag_glsl)[];
|
||||
extern char const NAM_E(default_frag_glsl)[];
|
||||
// NOLINTEND
|
||||
|
||||
/* compile a shader */
|
||||
static GLuint shader_compile(GLenum type, char const* src, size_t len) {
|
||||
int ilen = len;
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, &ilen);
|
||||
glCompileShader(shader);
|
||||
return shader;
|
||||
}
|
||||
|
||||
#define COMPILE_NAME(_type, _name) shader_compile(_type, NAM_S(_name), NAM_E(_name) - NAM_S(_name))
|
||||
int shader_init(GLuint pipe) {
|
||||
GLuint vs = COMPILE_NAME(GL_VERTEX_SHADER, default_vert_glsl);
|
||||
GLuint fs = COMPILE_NAME(GL_FRAGMENT_SHADER, default_frag_glsl);
|
||||
|
||||
glAttachShader(pipe, vs);
|
||||
glAttachShader(pipe, fs);
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
return 1;
|
||||
}
|
||||
10
src/io/shader.h
Normal file
10
src/io/shader.h
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) 2025 Quinn
|
||||
// Licensed under the MIT Licence. See LICENSE for details
|
||||
#pragma once
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int shader_init(GLuint pipe);
|
||||
@@ -1,22 +1,16 @@
|
||||
// Copyright (c) 2025 Quinn
|
||||
// Licensed under the MIT Licence. See LICENSE for details
|
||||
#define GLAD_GL_IMPLEMENTATION
|
||||
#include "window.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../error.h"
|
||||
#include "../util/vec/float2.h"
|
||||
|
||||
// include system libraries
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../error.h"
|
||||
#include "input.h"
|
||||
#include "render.h"
|
||||
|
||||
// macros for ease of access
|
||||
#define WIN_NAME "MCA Selector Lite"
|
||||
@@ -43,13 +37,13 @@ int window_init(void) {
|
||||
win = glfwCreateWindow(WIN_DEFAULT_WIDTH, WIN_DEFAULT_HEIGHT, WIN_NAME, NULL, NULL);
|
||||
if (!win) return 1;
|
||||
|
||||
// setup OpenGL
|
||||
// setup OpenGL for the window
|
||||
glfwMakeContextCurrent(win);
|
||||
if (!gladLoadGL(glfwGetProcAddress)) return 1;
|
||||
glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh)
|
||||
|
||||
// configure callbacks
|
||||
glfwSetKeyCallback(win, key_callback);
|
||||
glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh)
|
||||
|
||||
// print the OpenGL version information
|
||||
debug("version info:");
|
||||
@@ -61,81 +55,14 @@ int window_init(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* initialize a shader */
|
||||
static uint32_t init_shader(GLenum type, char const* src) {
|
||||
uint32_t shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, NULL);
|
||||
glCompileShader(shader);
|
||||
return shader;
|
||||
}
|
||||
|
||||
/* initialize the graphics pipeline */
|
||||
static uint32_t init_pipe(void) {
|
||||
// create the graphics pipeline context
|
||||
uint32_t pipe = glCreateProgram();
|
||||
|
||||
uint32_t vs = init_shader(GL_VERTEX_SHADER, "#version 330 core\nin vec4 pos; void main() { gl_Position = vec4(pos.x, pos.y, pos.z, pos.w); }");
|
||||
uint32_t fs = init_shader(GL_FRAGMENT_SHADER, "#version 330 core\nout vec4 colour; void main() { colour = vec4(1.0F, 0.5F, 0.0F, 0.1F); }");
|
||||
|
||||
glAttachShader(pipe, vs);
|
||||
glAttachShader(pipe, fs);
|
||||
glLinkProgram(pipe);
|
||||
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
|
||||
glValidateProgram(pipe);
|
||||
|
||||
return pipe;
|
||||
}
|
||||
|
||||
void window_loop(void) {
|
||||
assert(win != NULL);
|
||||
|
||||
uint32_t pipe = init_pipe();
|
||||
|
||||
unsigned vertc = 6;
|
||||
float2 vert[] = {
|
||||
{-1, -1 },
|
||||
{1, -1 },
|
||||
{1, -0.9F},
|
||||
|
||||
{-1, -0.9F},
|
||||
{1, -0.9F},
|
||||
{-1, -1 },
|
||||
};
|
||||
|
||||
uint32_t vbo; // vertex buffer object
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_STATIC_DRAW);
|
||||
|
||||
GLuint vao; // vertex array object
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float2), NULL);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
render_init();
|
||||
while (!glfwWindowShouldClose(win)) {
|
||||
glfwWaitEvents(); // wait till an update has been given
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
int w, h;
|
||||
glfwGetWindowSize(win, &w, &h);
|
||||
glViewport(0, 0, w, h);
|
||||
glClearColor(0.1F, 0.1F, 0.1F, 1.0F);
|
||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||
glUseProgram(pipe);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertc);
|
||||
|
||||
render_update(win);
|
||||
glfwSwapBuffers(win);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user