diff --git a/shaders/ObjectGl/frag.glsl b/shaders/ObjectGl/frag.glsl index 9713fc4..f76853f 100644 --- a/shaders/ObjectGl/frag.glsl +++ b/shaders/ObjectGl/frag.glsl @@ -2,6 +2,8 @@ out vec4 FragColor; +uniform float time; + void main() { FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); diff --git a/shaders/ObjectGlColor/frag.glsl b/shaders/ObjectGlColor/frag.glsl index 45e8b9c..3a1ca99 100644 --- a/shaders/ObjectGlColor/frag.glsl +++ b/shaders/ObjectGlColor/frag.glsl @@ -4,6 +4,8 @@ in vec3 color; out vec4 FragColor; +uniform float time; + void main() { FragColor = vec4(color, 1.0f); diff --git a/shaders/ObjectGlTex/frag.glsl b/shaders/ObjectGlTex/frag.glsl index b3d6cfd..a13c996 100644 --- a/shaders/ObjectGlTex/frag.glsl +++ b/shaders/ObjectGlTex/frag.glsl @@ -1,10 +1,11 @@ -#version 330 core +#version 410 core out vec4 FragColor; in vec2 texCoord; uniform sampler2D texture1; +uniform float time; void main() { diff --git a/shaders/ObjectGlTex/vert.glsl b/shaders/ObjectGlTex/vert.glsl index b90059c..64f4b92 100644 --- a/shaders/ObjectGlTex/vert.glsl +++ b/shaders/ObjectGlTex/vert.glsl @@ -1,4 +1,4 @@ -#version 330 core +#version 410 core layout (location = 0) in vec3 aPos; layout (location = 2) in vec2 aTexCoord; diff --git a/shaders/ObjectGlTexColor/frag.glsl b/shaders/ObjectGlTexColor/frag.glsl index 758da47..2f6e845 100644 --- a/shaders/ObjectGlTexColor/frag.glsl +++ b/shaders/ObjectGlTexColor/frag.glsl @@ -1,4 +1,4 @@ -#version 330 core +#version 410 core out vec4 FragColor; @@ -6,8 +6,14 @@ in vec4 color; in vec2 texCoord; uniform sampler2D texture1; +uniform float time; void main() { - FragColor = mix(texture(texture1, texCoord), color, 0.5); + vec4 tex = texture(texture1, texCoord); + if (tex.a == 0.0){ + FragColor = tex; + } else{ + FragColor = mix(tex, color, 0.5); + } } \ No newline at end of file diff --git a/shaders/ObjectGlTexColor/vert.glsl b/shaders/ObjectGlTexColor/vert.glsl index a591694..d1e7488 100644 --- a/shaders/ObjectGlTexColor/vert.glsl +++ b/shaders/ObjectGlTexColor/vert.glsl @@ -1,4 +1,4 @@ -#version 330 core +#version 410 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; diff --git a/shaders/PowerUpShader/FragEnergyWave.glsl b/shaders/PowerUpShader/FragEnergyWave.glsl new file mode 100644 index 0000000..a712d66 --- /dev/null +++ b/shaders/PowerUpShader/FragEnergyWave.glsl @@ -0,0 +1,21 @@ +#version 410 + +out vec4 FragColor; + +in vec4 color; +in vec4 position; +in vec2 texCoord; + +uniform sampler2D texture1; +uniform float time; + +void main() +{ // Pas fini c'est moche + vec4 colorPoweredUp = vec4(color.xyz * sin(position.y + time*40) * cos(position.x + time*40), 1.0); + vec4 tex = texture(texture1, texCoord); + if (tex.a == 0.0){ + FragColor = tex; + } else{ + FragColor = mix(tex, colorPoweredUp, 0.5); + } +} diff --git a/shaders/PowerUpShader/VertEnergyWave.glsl b/shaders/PowerUpShader/VertEnergyWave.glsl new file mode 100644 index 0000000..39a279a --- /dev/null +++ b/shaders/PowerUpShader/VertEnergyWave.glsl @@ -0,0 +1,21 @@ +#version 410 core + +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; + +out vec4 position; +out vec2 texCoord; +out vec4 color; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 transform; + +void main() +{ + gl_Position = projection * view * transform * vec4(aPos, 1.0); + color = vec4(aColor, 1.0f); + texCoord = aTexCoord; + position = vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 4a4957a..3c926f0 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -170,10 +170,12 @@ public class Engine { zangief.setTextureWrap(58,0,62,84, ObjectGl.STICK_TOP); engine.add_objectGl(zangief); zangief.translate(new Vector3f(-5000.0f,500.0f,10.0f)); + zangief.setColor(new Vector3f(0.5f, 0.0f, 0.0f)); +// zangief.setShader("shaders/PowerUpShader/VertEnergyWave.glsl","shaders/PowerUpShader/FragEnergyWave.glsl"); - ObjectGl smiley2 = new ObjectGl(0.0f,500.0f,500.0f, 1f, path2, null); - engine.add_objectGl(smiley2); - smiley2.translate(new Vector3f(0.0f,0.0f,5.0f)); +// ObjectGl smiley2 = new ObjectGl(0.0f,500.0f,500.0f, 1f, path2, null); +// engine.add_objectGl(smiley2); +// smiley2.translate(new Vector3f(0.0f,0.0f,5.0f)); long timer = System.currentTimeMillis(); long lastFrame; diff --git a/src/engine/object/ObjectGl.java b/src/engine/object/ObjectGl.java index 4d7f212..723cc2d 100644 --- a/src/engine/object/ObjectGl.java +++ b/src/engine/object/ObjectGl.java @@ -3,6 +3,8 @@ package engine.object; import engine.graphics.*; import engine.math.*; +import static org.lwjgl.glfw.GLFW.glfwGetTime; + /** * */ @@ -30,6 +32,8 @@ public class ObjectGl { private float height; private float scalingFactor; + public boolean useTime; + private Texture texture; /** @@ -57,15 +61,17 @@ public class ObjectGl { this.texture = new Texture(tex, 0); } - this.vertexArray = new VertexArray(Primitive.createRectangle(z, w, h), Primitive.rectangle_indices, colorBuffer, Primitive.stdTexWrap); - this.zPos = z; this.height = h; this.width = w; + + this.vertexArray = new VertexArray(Primitive.createRectangle(this.zPos, this.width, this.height), Primitive.rectangle_indices, colorBuffer, Primitive.stdTexWrap); + this.scalingFactor = size; this.transform = Matrix4f.identity(); this.scale(new Vector3f(size, size,1f)); this.stick_state = DEFAULT; + this.useTime = false; // use different shader for each set of option if (tex == null && color == null){ @@ -177,6 +183,29 @@ public class ObjectGl { this.setTextureWrap(result); } + /** + * Set a new shader to be used by this object + * @param vert path to glsl Vertex Shader + * @param frag path to glsl Fragment Shader + */ + public void setShader(String vert, String frag){ + this.shader = new Shader(vert, frag); + } + + /** + * Set a new Color for the object if the shader do not use the color attrib this will make no change. + * @param color Vector3f r,g,b format + */ + public void setColor(Vector3f color){ + float[] colorBuffer = new float[] { + color.x, color.y, color.z, + color.x, color.y, color.z, + color.x, color.y, color.z, + color.x, color.y, color.z + }; + this.vertexArray = new VertexArray(Primitive.createRectangle(this.zPos, this.width, this.height), Primitive.rectangle_indices, colorBuffer, Primitive.stdTexWrap); + } + private void setTextureWrap(float[] texture){ this.vertexArray.swapTextureBufferObject(texture); } @@ -189,9 +218,12 @@ public class ObjectGl { * Do shader binding, texture binding and vertexArray drawing */ public void render(){ + this.shader.enable(); if (this.texture != null) this.texture.bind(); + if (this.useTime) this.shader.setUniform1f("time", (float) glfwGetTime()); + this.shader.setUniformMat4f("projection", projection); this.shader.setUniformMat4f("view", view); this.shader.setUniformMat4f("transform", this.transform);