diff --git a/shaders/frag.frag b/shaders/ObjectGlTexColor/frag.glsl similarity index 56% rename from shaders/frag.frag rename to shaders/ObjectGlTexColor/frag.glsl index 9642779..17a5870 100644 --- a/shaders/frag.frag +++ b/shaders/ObjectGlTexColor/frag.glsl @@ -10,5 +10,5 @@ uniform sampler2D texture2; void main() { - FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(TexCoord.x, -TexCoord.y)), position.x * position.y); + FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(TexCoord.x, -TexCoord.y)), position.x * position.y); } \ No newline at end of file diff --git a/shaders/vert.vert b/shaders/ObjectGlTexColor/vert.glsl similarity index 100% rename from shaders/vert.vert rename to shaders/ObjectGlTexColor/vert.glsl diff --git a/src/Main.java b/src/Main.java index d4c19c8..c769c8c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,8 +7,8 @@ * */ -import launcher.Launcher; import engine.Engine; +import launcher.Launcher; public class Main { diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 05bf3f9..36a0998 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -8,10 +8,15 @@ */ package engine; +import engine.object.ObjectGl; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; + import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.*; @@ -20,28 +25,35 @@ public class Engine { private long window; - private Scene scene; - private Scene scene2; + private List objectsGl; private boolean running; - public Engine(){ + /** + * Create the engine and initial attributes use .init() to start the engine + */ + public Engine() { this.running = false; + this.objectsGl = new ArrayList(); } + /** + * Start the engine + * Create the window + */ private void init() { - if(!glfwInit()){ - // TODO Erreur d'initialisation - } + glfwInit(); + this.running = true; + glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //On utilise la version 3.3 d'openGL glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //Compatible MAC glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Le core profile est l'interface 'avancé' d'openGL - int width = 800; - int height = 600; + int width = 1280; + int height = 720; this.window = glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL); assert this.window != NULL; @@ -49,6 +61,7 @@ public class Engine { GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); assert vidmode != null; + // On met la fenêtre au centre de l'écran principale glfwSetWindowPos(this.window, (vidmode.width() - width)/2, (vidmode.height() - height)/2); glfwSetKeyCallback(window, new Input()); @@ -64,38 +77,54 @@ public class Engine { glEnable(GL_DEPTH_TEST); // Z-Buffer glClearColor(0.2f, 0.3f, 0.3f, 1.0f); System.out.println("OpenGL: " + glGetString(GL_VERSION)); - - this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices, Primitive.rectangle_color, Primitive.rectangle_texture); - this.scene2 = new Scene("shaders/vert.glsl", "shaders/frag.frag", Primitive.triangle, Primitive.triangle_indices); - } + /** + * + */ private void update(){ glfwPollEvents(); } + /** + * + */ private void render(){ + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //SWAP avec le precedent - scene.render(); - scene2.render(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + for (ObjectGl objectGl : objectsGl) { + objectGl.render(); + } + int error = glGetError(); if (error != GL_NO_ERROR) System.out.println(error); glfwSwapBuffers(window); //Envoie le buffer vers le moniteur } + /** + * TODO la boucle de rendu doit être inscrite dans la boucle de gameplay: cette methode doit disparaitre + */ public void run(){ - init(); - while(running){ - update(); - render(); - if(glfwWindowShouldClose(window)) running = false; + while(running){ + update(); + render(); + if(glfwWindowShouldClose(window)) running = false; } } - /* - Est appelé à chaque modification de la taille de la fenêtre, et modifie la taille de la zone de rendu - pour quelle corresponde à la taille de la fenêtre + public void add_objectGl(ObjectGl obj){ + objectsGl.add(obj); + } + + public void remove_objectGl(ObjectGl obj){ + objectsGl.remove(obj); + } + + /** + * Est appelé à chaque modification de la taille de la fenêtre, et modifie la taille de la zone de rendu + * pour quelle corresponde à la taille de la fenêtre */ private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){ @Override @@ -106,6 +135,12 @@ public class Engine { public static void main(String[] args) { Engine engine = new Engine(); + engine.init(); + // Add objects to render + ObjectGl cube = new ObjectGl(-0.2f,-0.2f,0.0f,1.0f,1.0f); + engine.add_objectGl(cube); +// engine.remove_objectGl(cube); + engine.run(); } diff --git a/src/engine/Primitive.java b/src/engine/Primitive.java deleted file mode 100644 index 9f7ec19..0000000 --- a/src/engine/Primitive.java +++ /dev/null @@ -1,99 +0,0 @@ -package engine; - -import engine.math.Vector3f; - -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[] createRectangle(float x, float y, float z, float w, float h){ - return new float[] { - x , y , z, - x + h, y , z, - x + h, y + w, z, - x , y + w, z - }; - } - - public static float[] triangle = new float[] { - 0.0f, 0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - -0.5f, -0.5f, 0.0f, - }; - - public static float[] triangle_couleur = new float[] { - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f - }; - - public static byte[] triangle_indices = new byte[]{ - 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 - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left - }; - - public static float[] rectangle_color = new float[]{ - 1.0f, 0.0f, 0.0f, // top right - 0.0f, 1.0f, 0.0f, // bottom right - 0.0f, 0.0f, 1.0f, // bottom left - 1.0f, 1.0f, 0.0f, // top left - }; - - public static float[] rectangle_texture = new float[]{ - 2.0f, 2.0f, - 2.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 2.0f - }; - - public static byte[] rectangle_indices = new byte[] { - 0, 1, 3, - 1, 2, 3 - }; - -} diff --git a/src/engine/math/Primitive.java b/src/engine/math/Primitive.java new file mode 100644 index 0000000..5eaff03 --- /dev/null +++ b/src/engine/math/Primitive.java @@ -0,0 +1,28 @@ +package engine.math; + +import engine.math.Vector3f; + +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 + }; + } + + public static float[] stdTexWrap = new float[] { + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f + }; + + public static byte[] rectangle_indices = new byte[] { + 0, 1, 3, + 1, 2, 3 + }; + +} diff --git a/src/engine/object/ObjectGl.java b/src/engine/object/ObjectGl.java index fefa176..6563ad7 100644 --- a/src/engine/object/ObjectGl.java +++ b/src/engine/object/ObjectGl.java @@ -1,6 +1,6 @@ package engine.object; -import engine.Primitive; +import engine.math.Primitive; import engine.graphics.VertexArray; import engine.graphics.Shader; import engine.math.Matrix4f; @@ -15,6 +15,10 @@ public class ObjectGl { protected Shader shader; protected Matrix4f transform; + public ObjectGl(){ + + } + public ObjectGl(float x, float y, float z, float h, float w){ this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, null, null); this.transform = Matrix4f.identity(); diff --git a/src/engine/object/ObjectGlColor.java b/src/engine/object/ObjectGlColor.java index ee4042d..d3759a3 100644 --- a/src/engine/object/ObjectGlColor.java +++ b/src/engine/object/ObjectGlColor.java @@ -1,11 +1,16 @@ package engine.object; +import engine.math.Primitive; import engine.graphics.Shader; +import engine.graphics.VertexArray; +import engine.math.Matrix4f; public class ObjectGlColor extends ObjectGl{ - public ObjectGlColor(float x, float y, float h, float w, float[] color) { - super(x, y, h, w); + public ObjectGlColor(float x, float y, float z, float h, float w, float[] color) { + super(); + this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, color, null); + this.transform = Matrix4f.identity(); this.shader = new Shader("shaders/ObjectGlColor/vert.glsl","shaders/ObjectGlColor/frag.glsl"); } diff --git a/src/engine/object/ObjectGlTex.java b/src/engine/object/ObjectGlTex.java index 0fea018..d636b10 100644 --- a/src/engine/object/ObjectGlTex.java +++ b/src/engine/object/ObjectGlTex.java @@ -1,12 +1,28 @@ package engine.object; +import engine.math.Primitive; import engine.graphics.Shader; +import engine.graphics.Texture; +import engine.graphics.VertexArray; +import engine.math.Matrix4f; + +import java.util.List; public class ObjectGlTex extends ObjectGl{ - public ObjectGlTex(float x, float y, float h, float w, float[] texCoord) { - super(x, y, h, w); + List textures; + + public ObjectGlTex(){ + + } + + public ObjectGlTex(float x, float y, float z, float h, float w, 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.shader = new Shader("shaders/ObjectGlTex/vert.glsl","shaders/ObjectGlTex/frag.glsl"); + + // TODO tex } @Override diff --git a/src/engine/object/ObjectGlTexColor.java b/src/engine/object/ObjectGlTexColor.java new file mode 100644 index 0000000..3c1641c --- /dev/null +++ b/src/engine/object/ObjectGlTexColor.java @@ -0,0 +1,20 @@ +package engine.object; + +import engine.math.Primitive; +import engine.graphics.Shader; +import engine.graphics.VertexArray; +import engine.math.Matrix4f; + +import java.util.List; + +public class ObjectGlTexColor extends ObjectGlTex{ + + public ObjectGlTexColor(float x, float y, float z, float h, float w, 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.shader = new Shader("shaders/ObjectGlTexColor/vert.glsl","shaders/ObjectGlTexColor/frag.glsl"); + + // TODO Create texture + } +} diff --git a/textures/zangief_sprite.png b/textures/zangief_sprite.png new file mode 100644 index 0000000..862b9fb Binary files /dev/null and b/textures/zangief_sprite.png differ