diff --git a/shaders/ObjectGlTex/frag.glsl b/shaders/ObjectGlTex/frag.glsl index 37f0ba7..b3d6cfd 100644 --- a/shaders/ObjectGlTex/frag.glsl +++ b/shaders/ObjectGlTex/frag.glsl @@ -8,5 +8,5 @@ uniform sampler2D texture1; void main() { - FragColor = texture(texture1, vec2(-texCoord.y, -texCoord.x)); + FragColor = texture(texture1, texCoord); } \ No newline at end of file diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 1f730df..2923794 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -166,6 +166,7 @@ public class Engine { ObjectGlTex smiley2 = new ObjectGlTex(-0.5f,0.5f,0.0f,500.0f,500.0f, path2, Primitive.stdTexWrap); engine.add_objectGl(smiley2); smiley2.translate(new Vector3f(0.0f,0.0f,5.0f)); + float[] texWrap = Primitive.upperHalfTexWrap; long timer = System.currentTimeMillis(); long lastFrame; @@ -173,11 +174,13 @@ public class Engine { boolean nextFrame = false; while(engine.isRunning()){ + double time = glfwGetTime(); lastFrame = System.currentTimeMillis(); // Game logic should fit here input(smiley, speed); // input(smiley2, speed); + smiley.setTextureWrap(0,0,200,200); //essential part v engine.update(); diff --git a/src/engine/graphics/Texture.java b/src/engine/graphics/Texture.java index 78a23a4..4f06db9 100644 --- a/src/engine/graphics/Texture.java +++ b/src/engine/graphics/Texture.java @@ -64,4 +64,12 @@ public class Texture { glBindTexture(GL_TEXTURE_2D, 0); } + public int getWidth(){ + return width; + } + + public int getHeight(){ + return height; + } + } diff --git a/src/engine/graphics/VertexArray.java b/src/engine/graphics/VertexArray.java index c37d437..c96feec 100644 --- a/src/engine/graphics/VertexArray.java +++ b/src/engine/graphics/VertexArray.java @@ -59,6 +59,16 @@ public class VertexArray { glEnableVertexAttribArray(2); } + public void swapVertexBufferObject(float[] vertices){ + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); + } + + public void swapTextureBufferObject(float [] texture){ + glBindBuffer(GL_ARRAY_BUFFER, TBO); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(texture), GL_STATIC_DRAW); + } + public void bind(){ glBindVertexArray(this.VAO); } diff --git a/src/engine/math/Primitive.java b/src/engine/math/Primitive.java index 5eaff03..964ccf1 100644 --- a/src/engine/math/Primitive.java +++ b/src/engine/math/Primitive.java @@ -6,20 +6,30 @@ public class Primitive { public static float[] createRectangle(float x, float y, float z, float w, float h){ return new float[] { - x , y , z, - x + w, y , z, - x + w, y - h, z, - x , y - h, z + x , y , z, // Haut gauche + x + w, y , z, // Haut droit + x + w, y - h, z, // Bas droit + x , y - h, z // Bas gauche }; } + /** + * Chaque point correspond à un vertex de la primite le reste est interpolé + */ public static float[] stdTexWrap = new float[] { - 1.0f, 1.0f, - 1.0f, 0.0f, 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, 0.0f, 1.0f }; + public static float[] upperHalfTexWrap = new float[] { + 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 0.5f, + 0.0f, 0.5f + }; + public static byte[] rectangle_indices = new byte[] { 0, 1, 3, 1, 2, 3 diff --git a/src/engine/object/ObjectGlTex.java b/src/engine/object/ObjectGlTex.java index a61b242..179fa73 100644 --- a/src/engine/object/ObjectGlTex.java +++ b/src/engine/object/ObjectGlTex.java @@ -5,6 +5,7 @@ import engine.graphics.Shader; import engine.graphics.Texture; import engine.graphics.VertexArray; import engine.math.Matrix4f; +import engine.math.Vector3f; import java.util.ArrayList; import java.util.List; @@ -35,6 +36,23 @@ public class ObjectGlTex extends ObjectGl{ } } + public void setTextureWrap(float x, float y, float w, float h){ + int texWidth = this.textures.get(0).getWidth(); + int texHeight = this.textures.get(0).getHeight(); + float[] result = { + x / texWidth, y / texHeight, + x + w / texWidth, y / texHeight, + x + w / texWidth, y + h / texHeight, + x / texWidth, y + h / texHeight, + }; + // TODO scaling object + this.setTextureWrap(result); + } + + public void setTextureWrap(float[] texture){ + this.vertexArray.swapTextureBufferObject(texture); + } + @Override public void render() { this.shader.enable();