From 25a0f13ba221a3e97d50487a54ebcef9f4b540a5 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 11 Oct 2022 18:55:38 +0200 Subject: [PATCH] Shader IO --- shaders/fragment.glsl | 4 +++- shaders/vertex.glsl | 10 ++++++---- src/main.cpp | 7 +++++++ src/shaders.cpp | 2 -- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 7376f07..e9478f5 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,6 +1,8 @@ #version 330 core out vec4 FragColor; +uniform vec4 inputColor; + void main() { - FragColor = vec4(1.0f, 1.0f, 1.0f, 0.0f); + FragColor = inputColor; } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 2969ee2..27a18e0 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,7 +1,9 @@ #version 330 core layout (location = 0) in vec3 aPos; -void main() -{ - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); -} +out vec4 vertexColor; + +void main() { + gl_Position = vec4(aPos, 1.0); + vertexColor = vec4(1.0f, 1.0f, 1.0f, 0.0f); +} diff --git a/src/main.cpp b/src/main.cpp index ef76cb1..ec09097 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "shaders.cpp" @@ -81,7 +82,13 @@ void renderCallback(RenderObj ro) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); + float time = glfwGetTime(); + float gVal = (sin(time) / 1.5f) + 0.5f; + int vertColLocation = glGetUniformLocation(ro.shaderProg, "inputColor"); + glUseProgram(ro.shaderProg); + glUniform4f(vertColLocation, gVal, gVal, gVal, 1.0f); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } diff --git a/src/shaders.cpp b/src/shaders.cpp index 3880d69..7be0e55 100644 --- a/src/shaders.cpp +++ b/src/shaders.cpp @@ -25,8 +25,6 @@ namespace Shader { } unsigned int compile(GLenum shadertype, const char* shaderSource) { - printf("--------------SRC-PREVIEW--------------\n%s\n---------------------------------------\n", shaderSource); - unsigned int shader; shader = glCreateShader(shadertype); glShaderSource(shader, 1, &shaderSource, NULL);