diff --git a/shaders/frag.frag b/shaders/frag.frag index 98b8780..4a9e16a 100644 --- a/shaders/frag.frag +++ b/shaders/frag.frag @@ -1,10 +1,10 @@ #version 330 core -uniform float time; - out vec4 FragColor; +in vec3 Color; + void main() { - FragColor = vec4(sin(time), sin(time/2), sin(time/4), 1.0f); + FragColor = vec4(Color, 1.0); } \ No newline at end of file diff --git a/shaders/vert.vert b/shaders/vert.vert index 4c407e2..f4fdd74 100644 --- a/shaders/vert.vert +++ b/shaders/vert.vert @@ -1,10 +1,12 @@ #version 330 core -uniform float time; +layout (location = 0) in vec3 aPos; // la variable aPos a l'attribut de position 0 +layout (location = 1) in vec3 aColor; -layout (location = 0) in vec3 aPos; +out vec3 Color; void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + gl_Position = vec4(aPos, 1.0); + Color = aColor; } \ No newline at end of file diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 57691d0..57f53fa 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); + this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.triangle, Primitive.triangle_indices, Primitive.triangle_couleur); } diff --git a/src/engine/Primitive.java b/src/engine/Primitive.java index cb6477d..7f1da5d 100644 --- a/src/engine/Primitive.java +++ b/src/engine/Primitive.java @@ -35,6 +35,12 @@ public class Primitive { -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 }; @@ -60,6 +66,14 @@ public class Primitive { -0.5f, 0.5f, 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 + }; + public static byte[] rectangle_indices = new byte[] { 0, 1, 3, 1, 2, 3 diff --git a/src/engine/Scene.java b/src/engine/Scene.java index 2608078..eb6a8e3 100644 --- a/src/engine/Scene.java +++ b/src/engine/Scene.java @@ -7,6 +7,7 @@ import static org.lwjgl.glfw.GLFW.glfwGetTime; public class Scene { float[] vertices; + float[] color; byte[] indices; VertexArray vertexArray; @@ -19,9 +20,16 @@ public class Scene { this.vertexArray = new VertexArray(this.vertices, this.indices); } + public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices, float[] color){ + 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); + } + public void render(){ this.shader.enable(); - this.shader.setUniform1f("time", (float)glfwGetTime()); this.vertexArray.render(); this.shader.disable(); } diff --git a/src/engine/Texture.java b/src/engine/Texture.java new file mode 100644 index 0000000..f3f8675 --- /dev/null +++ b/src/engine/Texture.java @@ -0,0 +1,61 @@ +package engine; + +import java.awt.image.BufferedImage; +import java.io.FileInputStream; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL30.*; + +public class Texture { + + private int width, height; + private int texture; + + public Texture(String path) { + texture = load(path); + } + + private int load(String path) { + int[] pixels = null; + try { + BufferedImage image = ImageIO.read(new FileInputStream(path)); + width = image.getWidth(); + height = image.getHeight(); + pixels = new int[width * height]; + image.getRGB(0, 0, width, height, pixels, 0, width); + } catch (IOException e) { + e.printStackTrace(); + } + + int[] data = new int[width * height]; + for (int i = 0; i < width * height; i++) { + int a = (pixels[i] & 0xff000000) >> 24; + int r = (pixels[i] & 0xff0000) >> 16; + int g = (pixels[i] & 0xff00) >> 8; + int b = (pixels[i] & 0xff); + + data[i] = a << 24 | b << 16 | g << 8 | r; + } + + int result = glGenTextures(); + glBindTexture(GL_TEXTURE_2D, result); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, BufferUtils.createIntBuffer(data)); + glGenerateMipmap(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + return result; + } + + public void bind() { + glBindTexture(GL_TEXTURE_2D, texture); + } + + public void unbind() { + glBindTexture(GL_TEXTURE_2D, 0); + } + +} diff --git a/src/engine/VertexArray.java b/src/engine/VertexArray.java index d14daaf..a338b25 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; + private int VAO ,VBO, EBO, CBO; private int count; public VertexArray(float[] vertices, byte[] indices){ @@ -16,11 +16,7 @@ public class VertexArray { glBindVertexArray(VAO); // VERTEX BUFFER OBJECT - VBO = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); - glEnableVertexAttribArray(0); + createVertexBufferObject(vertices); EBO = glGenBuffers(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); @@ -31,6 +27,44 @@ public class VertexArray { } + public VertexArray(float[] vertices, byte[] indices, float[] color) { + count = indices.length; + // VERTEX ARRAY OBJECT + VAO = glGenVertexArrays(); + glBindVertexArray(VAO); + + glEnableVertexAttribArray(0); + + // VERTEX BUFFER OBJECT + createVertexBufferObject(vertices); + // COLOR BUFFER OBJECT + createColorBufferObject(color); + + 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); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(0); + } + + private void createColorBufferObject(float[] color){ + CBO = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, CBO); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(color), GL_STATIC_DRAW); + glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(1); + } + public void bind(){ glBindVertexArray(this.VAO); } diff --git a/textures/container.jpg b/textures/container.jpg new file mode 100644 index 0000000..d07bee4 Binary files /dev/null and b/textures/container.jpg differ