From e74b821cf13869a99ac2933493207680d6666186 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 18 May 2024 10:40:34 +0200 Subject: WIP bug (bug still exists --- main.c | 100 +++++++++++++++++++++++++---------------------------------- visuals.frag | 15 ++------- 2 files changed, 46 insertions(+), 69 deletions(-) diff --git a/main.c b/main.c index 705c35f..4b8aa32 100644 --- a/main.c +++ b/main.c @@ -1,89 +1,75 @@ +#include #include #include #include -#include "shader.h" -#include "die.h" -#include "uniform.h" - #include "fill_vert.h" #include "visuals_frag.h" -void resize_handler(GLFWwindow* window, int width, int height) { - glViewport(0, 0, width, height); -} - -GLFWwindow* init_window() { +int main(int argc, char** argv) { + // initialize window + glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - // creating a non-resizable floating window makes the window float with i3 - glfwWindowHint(GLFW_FLOATING, GL_TRUE); - glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); - - // initialize window - int width = 800, height = 600; // default size - GLFWwindow* window = glfwCreateWindow(width, height, "vis", NULL, NULL); - if (window == NULL) - die("error: could not create window\n"); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + GLFWwindow* window = glfwCreateWindow(800, 600, "test", NULL, NULL); glfwMakeContextCurrent(window); - glfwSetFramebufferSizeCallback(window, resize_handler); - - // but i still want a resizable window - glfwSetWindowAttrib(window, GLFW_RESIZABLE, GL_TRUE); - - return window; -} + glewInit(); -void init_tri() { + // initialize triangle const float vertices[] = { 0, 1, 0, 1, -1, 0, -1, -1, 0, }; - - // initialize vertex {buffer,attribute} object buffers GLuint VBO, VAO; glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); - - // copy vertex data into VBO glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - // set VAO pointers glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - - // draw (only) this triangle glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(VAO); -} -int main(int argc, char** argv) { - glfwInit(); - GLFWwindow* window = init_window(); - glewInit(); - - init_tri(); - GLuint shader = link_shaders( - vert_shader(fill_vert, fill_vert_size), - frag_shader(visuals_frag, visuals_frag_size) - ); - init_uniform(shader, window); - - // main draw loop - while (!glfwWindowShouldClose(window)) { - glfwPollEvents(); - - update_uniform(); - glDrawArrays(GL_TRIANGLES, 0, 3); - - glfwSwapBuffers(window); + // initialize shaders + GLuint vert = glCreateShader(GL_VERTEX_SHADER); + glShaderBinary(1, &vert, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, fill_vert, fill_vert_size); + glSpecializeShaderARB(vert, "main", 0, NULL, NULL); + GLuint frag = glCreateShader(GL_FRAGMENT_SHADER); + glShaderBinary(1, &frag, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, visuals_frag, visuals_frag_size); + glSpecializeShaderARB(frag, "main", 0, NULL, NULL); + GLuint shader = glCreateProgram(); + glAttachShader(shader, vert); + glAttachShader(shader, frag); + glLinkProgram(shader); + glUseProgram(shader); // <- used the program before getting uniform name + glDeleteShader(vert); + glDeleteShader(frag); + + // print driver info + printf("vendor: %s\n", glGetString(GL_VENDOR)); + printf("renderer: %s\n", glGetString(GL_RENDERER)); + printf("version: %s\n", glGetString(GL_VERSION)); + printf("glsl version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); + printf("------------------------------\n"); + + // list all ACTIVE uniforms in shader: + GLint count; + GLchar name[80]; + GLsizei length; + glGetProgramiv(shader, GL_ACTIVE_UNIFORMS, &count); + for (unsigned i = 0; i < count; i++) { + glGetActiveUniformName(shader, i, 80, &length, name); + printf("[%u] = \"%.*s\"\n", i, length, name); } - return EXIT_SUCCESS; + // directly get uniform location + GLint uniform_location = glGetUniformLocation(shader, "test"); + printf("`test` uniform location = %d\n", uniform_location); + + return 0; } diff --git a/visuals.frag b/visuals.frag index d680834..8e83f6e 100644 --- a/visuals.frag +++ b/visuals.frag @@ -1,18 +1,9 @@ #version 330 core -uniform float time; -uniform ivec2 window; - -// adapted from -float hash12(vec2 p) { - vec3 p3 = fract(vec3(p.xyx) * .1031); - p3 += dot(p3, p3.yzx + 33.33); - return fract((p3.x + p3.y) * p3.z); -} +uniform float test; void main() { - vec2 uv = gl_FragCoord.xy / window; - vec2 pos = gl_FragCoord.xy + time; - gl_FragColor = vec4(hash12(pos), uv.xy, 1.0); + vec2 uv = gl_FragCoord.xy / ivec2(800, 600); + gl_FragColor = vec4(uv.xy, test, 1.0); } -- cgit v1.2.3