diff --git a/shaders/frag.frag b/shaders/frag.frag index 4a9e16a..01a4d15 100644 --- a/shaders/frag.frag +++ b/shaders/frag.frag @@ -1,10 +1,12 @@ #version 330 core - out vec4 FragColor; -in vec3 Color; +in vec3 ourColor; +in vec2 TexCoord; + +uniform sampler2D ourTexture; void main() { - FragColor = vec4(Color, 1.0); + FragColor = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0f); } \ No newline at end of file diff --git a/shaders/vert.vert b/shaders/vert.vert index f4fdd74..311ec24 100644 --- a/shaders/vert.vert +++ b/shaders/vert.vert @@ -1,12 +1,14 @@ #version 330 core - -layout (location = 0) in vec3 aPos; // la variable aPos a l'attribut de position 0 +layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; -out vec3 Color; +out vec3 ourColor; +out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); - Color = aColor; + ourColor = aColor; + TexCoord = aTexCoord; } \ No newline at end of file diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 57f53fa..1f3a370 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.triangle, Primitive.triangle_indices, Primitive.triangle_couleur); + this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices, Primitive.rectangle_color, Primitive.rectangle_texture); } diff --git a/src/engine/Primitive.java b/src/engine/Primitive.java index 7f1da5d..e4a44df 100644 --- a/src/engine/Primitive.java +++ b/src/engine/Primitive.java @@ -66,12 +66,18 @@ public class Primitive { -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[]{ - // positions // colors // texture coords - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left - -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, // top left + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f }; public static byte[] rectangle_indices = new byte[] { diff --git a/src/engine/Scene.java b/src/engine/Scene.java index eb6a8e3..5f791cf 100644 --- a/src/engine/Scene.java +++ b/src/engine/Scene.java @@ -1,13 +1,13 @@ package engine; -import static java.lang.Math.cos; -import static java.lang.Math.sin; -import static org.lwjgl.glfw.GLFW.glfwGetTime; +import static org.lwjgl.opengl.GL11.*; public class Scene { float[] vertices; float[] color; + float[] texture; + Texture texture_map; byte[] indices; VertexArray vertexArray; @@ -20,17 +20,21 @@ public class Scene { this.vertexArray = new VertexArray(this.vertices, this.indices); } - public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices, float[] color){ + public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices, float[] color, float[] texture){ this.vertices = vertices; this.indices = indices; this.color = color; this.shader = new Shader(vertPath, fragPath); - this.vertexArray = new VertexArray(this.vertices, this.indices, this.color); + this.texture_map = new Texture("textures/container.jpg"); + this.texture = texture; + this.vertexArray = new VertexArray(this.vertices, this.indices, this.color, this.texture); } public void render(){ this.shader.enable(); + this.texture_map.bind(); this.vertexArray.render(); + this.texture_map.unbind(); this.shader.disable(); } } diff --git a/src/engine/VertexArray.java b/src/engine/VertexArray.java index a338b25..9727ec3 100644 --- a/src/engine/VertexArray.java +++ b/src/engine/VertexArray.java @@ -6,7 +6,7 @@ import static org.lwjgl.opengl.GL30.*; public class VertexArray { - private int VAO ,VBO, EBO, CBO; + private int VAO ,VBO, EBO, CBO, TBO; private int count; public VertexArray(float[] vertices, byte[] indices){ @@ -49,6 +49,30 @@ public class VertexArray { } + public VertexArray(float[] vertices, byte[] indices, float[] color, float[] texture) { + count = indices.length; + // VERTEX ARRAY OBJECT + VAO = glGenVertexArrays(); + glBindVertexArray(VAO); + + glEnableVertexAttribArray(0); + + // VERTEX BUFFER OBJECT + createVertexBufferObject(vertices); + // COLOR BUFFER OBJECT + createColorBufferObject(color); + // TEXTURE BUFFER OBJECT + createTextureBufferObject(texture); + + EBO = glGenBuffers(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + } + private void createVertexBufferObject(float[] vertices){ VBO = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, VBO); @@ -65,6 +89,14 @@ public class VertexArray { glEnableVertexAttribArray(1); } + private void createTextureBufferObject(float[] texture){ + TBO = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, TBO); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(texture), GL_STATIC_DRAW); + glVertexAttribPointer(2, 2, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(2); + } + public void bind(){ glBindVertexArray(this.VAO); }