diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 5ec76a5..b0774ba 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -155,20 +155,17 @@ public class Engine { engine.init(); // Add objects to render - List path = new ArrayList<>(); - path.add("textures/zangief_sprite.png"); + String path = "textures/zangief_sprite.png"; - List path2 = new ArrayList<>(); - path2.add("textures/awesomeface.png"); + String path2 = "textures/awesomeface.png"; - ObjectGlTex zangief = new ObjectGlTex(0f,0f,0f,60f,80f,10f, path, Primitive.stdTexWrap); + ObjectGl zangief = new ObjectGl(0f,60f,80f,10f, path, null); engine.add_objectGl(zangief); // zangief.translate(new Vector3f(-600.0f,100,10.0f)); - ObjectGlTex smiley2 = new ObjectGlTex(-0.5f,0.5f,0.0f,500.0f,500.0f, 1f, path2, Primitive.stdTexWrap); + 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)); - float[] texWrap = Primitive.upperHalfTexWrap; long timer = System.currentTimeMillis(); long lastFrame; @@ -176,7 +173,6 @@ public class Engine { boolean nextFrame = false; while(engine.isRunning()){ - double time = glfwGetTime(); lastFrame = System.currentTimeMillis(); // Game logic should fit here @@ -216,9 +212,13 @@ public class Engine { // if (Input.isKeyDown(GLFW.GLFW_KEY_S))token.translate(new Vector3f ( 0.0f,speed * -5.0f, 0.0f)); // if (Input.isKeyDown(GLFW.GLFW_KEY_D))token.translate(new Vector3f (speed * 5.0f, 0.0f, 0.0f)); // } - private static void gamepadInput(ObjectGlTex token, int speed) { + private static void gamepadInput(ObjectGl token, int speed) { ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1); FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1); + + assert gamepadAxes != null; + assert gamepadButton != null; + String name = GLFW.glfwGetJoystickName(GLFW_JOYSTICK_1); //System.out.println("GamePad Name :" + name); // for (int i =0 ; i < gamepadAxes.capacity(); i++) { @@ -228,9 +228,8 @@ public class Engine { if (gamepadButton.get(0) ==1 ) { // appuie sur croix(PlayStation) A (Xbox) token.translate(new Vector3f ( 0.0f, speed * 5.0f, 0.0f)); - } - - if ( (gamepadAxes.get(2) < -0.1 || gamepadAxes.get(2) > 0.1) ) { // de droite à gauche //joystick gauche + } + if ( (gamepadAxes.get(2) < -0.1 || gamepadAxes.get(2) > 0.1) ) { // de droite à gauche //joystick gauche token.translate(new Vector3f (5*speed * gamepadAxes.get(2), 0.0f, 0.0f)); if ( gamepadAxes.get(2) < -0.1 ){ token.setTextureWrap(121,0,57,80); @@ -272,7 +271,7 @@ public class Engine { } - public static void input(ObjectGlTex token, int speed) { + public static void input(ObjectGl token, int speed) { if (Input.isKeyDown(GLFW.GLFW_KEY_W)) { token.scale(new Vector3f (1.01f, 1.01f, 0.0f)); } diff --git a/src/engine/math/Primitive.java b/src/engine/math/Primitive.java index 964ccf1..56f2f95 100644 --- a/src/engine/math/Primitive.java +++ b/src/engine/math/Primitive.java @@ -4,12 +4,12 @@ import engine.math.Vector3f; public class Primitive { - public static float[] createRectangle(float x, float y, float z, float w, float h){ + public static float[] createRectangle(float z, float w, float h){ return new float[] { - x , y , z, // Haut gauche - x + w, y , z, // Haut droit - x + w, y - h, z, // Bas droit - x , y - h, z // Bas gauche + 0 , 0 , z, // Haut gauche + 0 + w, 0 , z, // Haut droit + 0 + w, 0 - h, z, // Bas droit + 0 , 0 - h, z // Bas gauche }; } diff --git a/src/engine/object/ObjectGl.java b/src/engine/object/ObjectGl.java index f302cec..f22e1ca 100644 --- a/src/engine/object/ObjectGl.java +++ b/src/engine/object/ObjectGl.java @@ -1,11 +1,15 @@ package engine.object; +import engine.graphics.Texture; import engine.math.Primitive; import engine.graphics.VertexArray; import engine.graphics.Shader; import engine.math.Matrix4f; import engine.math.Vector3f; +import java.util.ArrayList; +import java.util.List; + /** * */ @@ -21,20 +25,54 @@ public class ObjectGl { public float zPos; public float scalingFactor; + private Texture texture; + public ObjectGl(){ } - public ObjectGl(float x, float y, float z, float h, float w, float size){ - this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, null, null); + /** + * Create a rectangle shape + * @param z depth of your model the larger it is the more it will be "close" to the camera + * @param h height of the rectangle + * @param w width of the rectangle + * @param size scaling factor of the rectangle, the model could not show up because this value is too small or too large, a good compromise is between 2 and 15 + * @param tex set to null if you don't want a tex on your model + * @param color set to null if you don't want a Color on your model + */ + public ObjectGl(float z, float h, float w, float size, String tex, Vector3f color){ + float[] colorBuffer = null; + // Check des options + if (color != null){ + 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.transform = Matrix4f.identity(); - this.scale(new Vector3f(size, size,1f)); + if (tex != null){ + this.texture = new Texture(tex, 0); + } - this.shader = new Shader("shaders/ObjectGl/vert.glsl","shaders/ObjectGl/frag.glsl"); + this.vertexArray = new VertexArray(Primitive.createRectangle(z, h, w), Primitive.rectangle_indices, colorBuffer, Primitive.stdTexWrap); this.scalingFactor = size; this.zPos = z; + this.transform = Matrix4f.identity(); + this.scale(new Vector3f(size, size,1f)); + + // use different shader for each set of option + if (tex == null && color == null){ + this.shader = new Shader("shaders/ObjectGl/vert.glsl","shaders/ObjectGl/frag.glsl"); + } else if (tex == null){ + this.shader = new Shader("shaders/ObjectGlColor/vert.glsl","shaders/ObjectGlColor/frag.glsl"); // TODO + } else if (color == null){ + this.shader = new Shader("shaders/ObjectGlTex/vert.glsl","shaders/ObjectGlTex/frag.glsl"); + } else { + this.shader = new Shader("shaders/ObjectGlTexColor/vert.glsl","shaders/ObjectGlTexColor/frag.glsl"); // TODO + } } public void resetTransform(){ @@ -48,9 +86,9 @@ public class ObjectGl { } /** - * Comme on foncitonne avec des sprite on part du principe que x et y sont scale de manière uniforme + * Comme on fonctionne avec des sprites on part du principe que x et y sont scale de manière uniforme * ou tout du moins que this.scalingFactor corresponds au scaling de x - * @param vec + * @param vec le vecteur de transformation */ public void scale(Vector3f vec){ this.scalingFactor *= vec.x; @@ -69,14 +107,42 @@ public class ObjectGl { this.transform = this.transform.multiply(Matrix4f.rotateZ(angle)); } + public void setTexture(String texPath){ + this.texture = new Texture(texPath, 0); + } + + public void setTextureWrap(float x, float y, float w, float h){ + this.vertexArray.swapVertexBufferObject(Primitive.createRectangle(this.zPos, w, h)); + int texWidth = this.texture.getWidth(); + int texHeight = this.texture.getHeight(); + x /= texWidth; + w /= texWidth; + y /= texHeight; + h /= texHeight; + float[] result = { + x , y , + x + w , y , + x + w , y + h , + x , y + h , + }; + this.setTextureWrap(result); + } + + public void setTextureWrap(float[] texture){ + this.vertexArray.swapTextureBufferObject(texture); + } + public void render(){ this.shader.enable(); + if (this.texture != null) this.texture.bind(); this.shader.setUniformMat4f("projection", projection); this.shader.setUniformMat4f("view", view); this.shader.setUniformMat4f("transform", this.transform); this.vertexArray.render(); + + if (this.texture != null) this.texture.unbind(); this.shader.disable(); } diff --git a/src/engine/object/ObjectGlColor.java b/src/engine/object/ObjectGlColor.java deleted file mode 100644 index c7fc3ee..0000000 --- a/src/engine/object/ObjectGlColor.java +++ /dev/null @@ -1,19 +0,0 @@ -package engine.object; - -import engine.math.Primitive; -import engine.graphics.Shader; -import engine.graphics.VertexArray; -import engine.math.Matrix4f; -import engine.math.Vector3f; - -public class ObjectGlColor extends ObjectGl{ - public ObjectGlColor(float x, float y, float z, float h, float w, float size, float[] color) { - super(); - this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, color, null); - this.transform = Matrix4f.identity(); - this.scale(new Vector3f(size, size,1)); - this.scalingFactor = size; - this.shader = new Shader("shaders/ObjectGlColor/vert.glsl","shaders/ObjectGlColor/frag.glsl"); - this.zPos = z; - } -} diff --git a/src/engine/object/ObjectGlTex.java b/src/engine/object/ObjectGlTex.java deleted file mode 100644 index a4f9d9a..0000000 --- a/src/engine/object/ObjectGlTex.java +++ /dev/null @@ -1,81 +0,0 @@ -package engine.object; - -import engine.math.Primitive; -import engine.graphics.*; -import engine.math.Matrix4f; -import engine.math.Vector3f; - -import java.util.ArrayList; -import java.util.List; - -public class ObjectGlTex extends ObjectGl{ - - protected List textures; - - public ObjectGlTex(){ - - } - - public ObjectGlTex(float x, float y, float z, float h, float w, float size, List texPath, float[] texCoord) { - super(); - this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, null, texCoord); - this.transform = Matrix4f.identity(); - this.scale(new Vector3f(size, size,1)); - this.scalingFactor = size; - this.shader = new Shader("shaders/ObjectGlTex/vert.glsl","shaders/ObjectGlTex/frag.glsl"); - this.zPos = z; - - this.setTexture(texPath); - } - - public void setTexture(List texPath){ - this.textures = new ArrayList<>(); - int count = 0; - for (String path : texPath){ - textures.add(new Texture(path, count)); - count++; - } - } - - public void setTextureWrap(float x, float y, float w, float h){ - this.vertexArray.swapVertexBufferObject(Primitive.createRectangle(0.0f,0.0f,this.zPos,w,h)); - int texWidth = this.textures.get(0).getWidth(); - int texHeight = this.textures.get(0).getHeight(); - x /= texWidth; - w /= texWidth; - y /= texHeight; - h /= texHeight; - float[] result = { - x , y , - x + w , y , - x + w , y + h , - x , y + h , - }; - this.setTextureWrap(result); - } - - public void setTextureWrap(float[] texture){ - this.vertexArray.swapTextureBufferObject(texture); - } - - @Override - public void render() { - this.shader.enable(); - - this.shader.setUniformMat4f("projection", projection); - this.shader.setUniformMat4f("view", view); - this.shader.setUniformMat4f("transform", this.transform); - - for (Texture t : textures){ - t.bind(); - } - - this.vertexArray.render(); - - for (Texture t : textures){ - t.unbind(); - } - - this.shader.disable(); - } -} diff --git a/src/engine/object/ObjectGlTexColor.java b/src/engine/object/ObjectGlTexColor.java deleted file mode 100644 index e1f8444..0000000 --- a/src/engine/object/ObjectGlTexColor.java +++ /dev/null @@ -1,23 +0,0 @@ -package engine.object; - -import engine.graphics.Shader; -import engine.graphics.VertexArray; -import engine.math.Primitive; -import engine.math.Matrix4f; -import engine.math.Vector3f; - -import java.util.List; - -public class ObjectGlTexColor extends ObjectGlTex{ - - public ObjectGlTexColor(float x, float y, float z, float h, float w, float size, List texPath, float[] texCoord, float[] color) { - super(); - this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, color, texCoord); - this.transform = Matrix4f.identity(); - this.scale(new Vector3f(size, size,1)); - this.scalingFactor = size; - this.shader = new Shader("shaders/ObjectGlTexColor/vert.glsl","shaders/ObjectGlTexColor/frag.glsl"); - this.zPos = z; - this.setTexture(texPath); - } -}