From 7dd5ef4d6318582c4ece7ef74f6329c7b84de9df Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 16 May 2024 17:20:59 +0200 Subject: fragment shader demo working :tada: --- hello.frag | 12 ++++-------- main.c | 28 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/hello.frag b/hello.frag index a804a77..291c0f1 100644 --- a/hello.frag +++ b/hello.frag @@ -1,13 +1,9 @@ #version 330 core -out vec4 FragColor; + +#define WIDTH 800 +#define HEIGHT 600 void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + gl_FragColor = vec4(gl_FragCoord.x / WIDTH, gl_FragCoord.y / HEIGHT, 0, 1); } -// #version 330 core -// -// void main() { -// gl_FragColor = vec4(gl_FragCoord.xy, 0, 1); -// } -// diff --git a/main.c b/main.c index 8ac0ca2..c7a156b 100644 --- a/main.c +++ b/main.c @@ -8,18 +8,19 @@ #include "hello_vert.h" #include "hello_frag.h" -void prepare_tri(GLuint* VBO, GLuint* VAO) { +void prepare_tri() { const float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f, + -1, -1, 0, + 3, -1, 0, + -1, 3, 0, }; // initialize vertex {buffer,attribute} object buffers - glGenBuffers(1, VBO); - glBindBuffer(GL_ARRAY_BUFFER, *VBO); - glGenBuffers(1, VAO); - glBindVertexArray(*VAO); + 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); @@ -27,6 +28,10 @@ void prepare_tri(GLuint* VBO, GLuint* VAO) { // 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) { @@ -49,8 +54,7 @@ int main(int argc, char** argv) { glewInit(); // create single triangle - GLuint VBO, VAO; - prepare_tri(&VBO, &VAO); + prepare_tri(); // prepare shaders GLuint shader = link_shaders( @@ -62,11 +66,7 @@ int main(int argc, char** argv) { while (!glfwWindowShouldClose(window)) { glfwPollEvents(); - glClearColor(1.f, 0.f, 1.f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - glUseProgram(shader); - glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); -- cgit v1.2.3