From 049e735e1ab5cbf8395ede3d1029ef8e867929af Mon Sep 17 00:00:00 2001 From: Antoine Date: Fri, 14 May 2021 15:34:08 +0200 Subject: [PATCH] setUniform4f in Shader --- shaders/frag.frag | 5 ++++- shaders/vert.vert | 3 +++ src/engine/Engine.java | 2 +- src/engine/Primitive.java | 41 +++++++++++++++++++++++++++++++++++++++ src/engine/Scene.java | 5 +++++ src/engine/Shader.java | 5 +++++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/shaders/frag.frag b/shaders/frag.frag index 8728871..98b8780 100644 --- a/shaders/frag.frag +++ b/shaders/frag.frag @@ -1,7 +1,10 @@ #version 330 core + +uniform float time; + out vec4 FragColor; void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + FragColor = vec4(sin(time), sin(time/2), sin(time/4), 1.0f); } \ No newline at end of file diff --git a/shaders/vert.vert b/shaders/vert.vert index cdd6dd3..4c407e2 100644 --- a/shaders/vert.vert +++ b/shaders/vert.vert @@ -1,4 +1,7 @@ #version 330 core + +uniform float time; + layout (location = 0) in vec3 aPos; void main() diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 667430d..57691d0 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -63,7 +63,7 @@ public class Engine { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); System.out.println("OpenGL: " + glGetString(GL_VERSION)); - this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices); + this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.triangle, Primitive.triangle_indices); } diff --git a/src/engine/Primitive.java b/src/engine/Primitive.java index 34e1903..cb6477d 100644 --- a/src/engine/Primitive.java +++ b/src/engine/Primitive.java @@ -2,6 +2,33 @@ package engine; public class Primitive { + public float[] vertices; + public byte[] indices; + + public Primitive(Vector3f vertex1, Vector3f vertex2, Vector3f vertex3){ + vertices = new float[]{ + vertex1.x, vertex1.y, vertex1.z, + vertex2.x, vertex2.y, vertex2.z, + vertex3.x, vertex3.y, vertex3.z + }; + indices = new byte[]{ + 0, 1, 2 + }; + } + + public Primitive(Vector3f vertex1, Vector3f vertex2, Vector3f vertex3, Vector3f vertex4){ + vertices = new float[]{ + vertex1.x, vertex1.y, vertex1.z, + vertex2.x, vertex2.y, vertex2.z, + vertex3.x, vertex3.y, vertex3.z, + vertex4.x, vertex4.y, vertex4.z + }; + indices = new byte[]{ + 0, 1, 2, + 1, 2, 3 + }; + } + public static float[] triangle = new float[] { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, @@ -12,6 +39,20 @@ public class Primitive { 0, 1, 2 }; + public static float[] two_triangle = new float[] { + 0.0f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + -0.8f, -0.8f, 0.0f, + 0.8f, -0.8f, 0.0f, + 0.8f, -0.9f, 0.0f, + }; + + public static byte[] two_triangle_indices = new byte[]{ + 0, 1, 2, + 3, 4, 5 + }; + public static float[] rectangle = new float[] { 0.5f, 0.5f, 0.0f, // top right 0.5f, -0.5f, 0.0f, // bottom right diff --git a/src/engine/Scene.java b/src/engine/Scene.java index 3edf273..2608078 100644 --- a/src/engine/Scene.java +++ b/src/engine/Scene.java @@ -1,5 +1,9 @@ package engine; +import static java.lang.Math.cos; +import static java.lang.Math.sin; +import static org.lwjgl.glfw.GLFW.glfwGetTime; + public class Scene { float[] vertices; @@ -17,6 +21,7 @@ public class Scene { public void render(){ this.shader.enable(); + this.shader.setUniform1f("time", (float)glfwGetTime()); this.vertexArray.render(); this.shader.disable(); } diff --git a/src/engine/Shader.java b/src/engine/Shader.java index c573885..c656a3d 100644 --- a/src/engine/Shader.java +++ b/src/engine/Shader.java @@ -48,6 +48,11 @@ public class Shader { glUniform3f(getUniform(name), vector.x, vector.y, vector.z); } + public void setUniform4f(String name, float x, float y, float z, float w) { + if (!enabled) enable(); + glUniform4f(getUniform(name), x, y, z, w); + } + public void setUniformMat4f(String name, Matrix4f matrix){ if (!enabled) enable(); glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer());