From 294cc9a5b17b4a84d3fc25fb0a269b4c37cc5a9f Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 29 Aug 2022 07:11:23 +0200 Subject: Add example of uint texture for testing --- src/main.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index b30280a..0241e8f 100644 --- a/src/main.c +++ b/src/main.c @@ -31,8 +31,8 @@ typedef struct Context { GLFWwindow *window; u32 gl_version_major; u32 gl_version_minor; - u32 win_width; - u32 win_height; + s32 win_width; + s32 win_height; char win_title[256]; bool win_resizable; @@ -131,6 +131,12 @@ init_context(Context *ctx) { ctx->window = window; } +void +init_debug_overlay(Context *ctx) { + // TODO: Add debug text to context and initialize here + (void)ctx; +} + void setup_callbacks(Context *ctx) { glfwSetErrorCallback(glfw_error_callback); @@ -167,12 +173,130 @@ update(Context *ctx) { void render(Context *ctx) { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glfwGetFramebufferSize(ctx->window, &ctx->win_width, &ctx->win_height); + glViewport(0, 0, ctx->win_width, ctx->win_height); + + + // TODO: Only need to be done once on the initialization function. + // Prepare quad rendering. + f32 vertices[] = { + // position // tex_coords + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, 0.0f, + -1.0f, -1.0f, 0.0f, 0.0f, + -1.0f, 1.0f, 0.0f, 1.0f, + }; + GLuint indices[] = { + 0, 1, 3, + 1, 2, 3 + }; + + // Vertex array object. + u32 vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + // Vertex buffer object setup. + u32 vbo; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void*)(2 * sizeof(f32))); + + // Element buffer object setup. + u32 ebo; + glGenBuffers(1, &ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + + // Texture atlas. + u8 texture[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + }; + u32 tex_id; + glGenTextures(1, &tex_id); + glBindTexture(GL_TEXTURE_2D, tex_id); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 16, 8, 0, GL_RED, + GL_UNSIGNED_BYTE, &texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D, 0); + + // Unbind objects to avoid accidental modifications. + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + const char* vert_src = +"#version 330 core\n" +"layout (location = 0) in vec2 position;\n" +"layout (location = 1) in vec2 tex_coords;\n" +"out vec2 tex;\n" +"void main() {\n" +" gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" +" tex = tex_coords;\n" +"}"; + + // Fragment shader. + const char* frag_src = +"#version 330 core\n" +"in vec2 tex;\n" +"out vec4 frag;\n" +"uniform usampler2D tex_sampler;\n" +"void main() {\n" +" uint val = texture(tex_sampler, tex).r;\n" +" frag = vec4(val, val, val, 1.0);\n" +"}\n"; + + u32 program = compile_program(ctx, vert_src, frag_src); + + glUseProgram(program); + glBindVertexArray(vao); + glBindTexture(GL_TEXTURE_2D, tex_id); + + // TODO: How to pass the parameters for each box? + // NOTE: We want to render the text as a single draw call if possible. + // glEnable(GL_BLEND); + // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + // // glBlendFunc(GL_ONE, GL_ONE); + // // glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA); + // // glBlendFunc(GL_SRC_ALPHA, GL_ONE); + // // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // // glBlendFunc(GL_ONE, GL_ZERO); + // // glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + // // glBlendFunc(GL_DST_ALPHA,GL_ONE); + // // glBlendFunc(GL_DST_ALPHA, GL_SRC_ALPHA); + // // glBlendFunc(GL_DST_ALPHA, GL_DST_ALPHA); + // // glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA); + // // glBlendEquation(GL_FUNC_ADD); + // + // glBindTexture(GL_TEXTURE_2D, text_tex); // TODO: Bind the texture atlas. + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + // glDisable(GL_BLEND); + glfwSwapBuffers(ctx->window); } int main(void) { + + // + // Initialization. + // + Context ctx = (Context){ .gl_version_major = 3, .gl_version_minor = 2, @@ -182,6 +306,7 @@ main(void) { }; init_context(&ctx); setup_callbacks(&ctx); + init_debug_overlay(&ctx); // // Main loop. -- cgit v1.2.1