From 6f99ed0ebe49494997a3c57f5e17ff149366bb53 Mon Sep 17 00:00:00 2001 From: EvilMuffinHa Date: Sun, 10 May 2020 10:52:12 -0400 Subject: [PATCH] Revert "Revert "adding textures (has errors)"" This reverts commit 8c94a73d70a4af17077eca764ae6965b9730b4c5. removing textureloader --- src/Test.java | 17 ++++------ src/org/hl/engine/graphics/Material.java | 5 ++- src/org/hl/engine/graphics/Mesh.java | 29 ++++++++++++++-- src/org/hl/engine/graphics/Renderer.java | 6 +++- src/org/hl/engine/graphics/Vertex.java | 11 ++++-- src/org/hl/engine/io/Display.java | 6 ++-- src/org/hl/engine/math/lalg/Vector2f.java | 32 ++++++++++++++++++ .../hl/engine/math/{ => lalg}/Vector3f.java | 2 +- src/org/hl/engine/utils/TextureLoader.java | 1 - src/resources/shaders/mainFragment.glsl | 7 ++-- src/resources/shaders/mainVertex.glsl | 9 +++-- src/resources/textures/beautiful.png | Bin 0 -> 5785 bytes 12 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 src/org/hl/engine/math/lalg/Vector2f.java rename src/org/hl/engine/math/{ => lalg}/Vector3f.java (95%) create mode 100644 src/resources/textures/beautiful.png diff --git a/src/Test.java b/src/Test.java index 1c153f3..757a0fe 100644 --- a/src/Test.java +++ b/src/Test.java @@ -1,10 +1,7 @@ -import org.hl.engine.graphics.Mesh; -import org.hl.engine.graphics.Renderer; -import org.hl.engine.graphics.Shader; -import org.hl.engine.graphics.Vertex; +import org.hl.engine.graphics.*; import org.hl.engine.io.Display; import org.hl.engine.io.Input; -import org.hl.engine.math.Vector3f; +import org.hl.engine.math.lalg.*; import org.lwjgl.glfw.GLFW; @@ -18,16 +15,16 @@ public class Test { public Shader shader; public Mesh mesh = new Mesh(new Vertex[] { - new Vertex(new Vector3f(-0.5F, 0.5F, 0.0F), new Vector3f(0, 0, 1.0F)), - new Vertex(new Vector3f(-0.5F, -0.5F, 0.0F), new Vector3f(0, 0, 1.0F)), - new Vertex(new Vector3f(0.5F, -0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F)), - new Vertex(new Vector3f(0.5F, 0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F) ), + new Vertex(new Vector3f(-0.5F, 0.5F, 0.0F), new Vector3f(0, 0, 1.0F), new Vector2f(0, 0)), + new Vertex(new Vector3f(-0.5F, -0.5F, 0.0F), new Vector3f(0, 0, 1.0F), new Vector2f(0, 1)), + new Vertex(new Vector3f(0.5F, -0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F), new Vector2f(1, 1)), + new Vertex(new Vector3f(0.5F, 0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F), new Vector2f(1, 0)), }, new int[] { 0, 1, 2, 0, 2, 3 - }); + }, new Material("/textures/beautiful.png")); public void run() { init(); diff --git a/src/org/hl/engine/graphics/Material.java b/src/org/hl/engine/graphics/Material.java index becaef4..3cfbd8b 100644 --- a/src/org/hl/engine/graphics/Material.java +++ b/src/org/hl/engine/graphics/Material.java @@ -14,13 +14,16 @@ public class Material { private int width, height; private int textureID; + private String path; public Material(String path) { - this.image = TextureLoader.loadImage(path); //The path is inside the jar file + this.path = path; } public void create() { + // Loading image on create + this.image = TextureLoader.loadImage(path); //The path is inside the jar file this.width = this.image.getWidth(); this.height = this.image.getHeight(); this.textureID = TextureLoader.loadTexture(image); diff --git a/src/org/hl/engine/graphics/Mesh.java b/src/org/hl/engine/graphics/Mesh.java index b6d6194..f59c735 100644 --- a/src/org/hl/engine/graphics/Mesh.java +++ b/src/org/hl/engine/graphics/Mesh.java @@ -12,12 +12,14 @@ import java.nio.IntBuffer; public class Mesh { private Vertex[] vertices; private int[] indices; - private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject; + private Material material; + private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject; // A group of vertices combined based on the indexes - public Mesh(Vertex[] vertices, int[] indices) { + public Mesh(Vertex[] vertices, int[] indices, Material material) { this.vertices = vertices; this.indices = indices; + this.material = material; } // Destroy the mesh @@ -25,6 +27,8 @@ public class Mesh { GL15.glDeleteBuffers(positionBufferObject); GL15.glDeleteBuffers(indicesBufferObject); GL15.glDeleteBuffers(colorBufferObject); + GL15.glDeleteBuffers(textureBufferObject); + material.destroy(); GL30.glDeleteVertexArrays(vertexArrayObject); } @@ -55,8 +59,19 @@ public class Mesh { return colorBufferObject; } + public int getTextureBufferObject() { + return textureBufferObject; + } + + public Material getMaterial() { + return material; + } + public void create() { + // Create the material + material.create(); + // Creates the mesh by formatting the vertices and indices and inputting them to OpenGL vertexArrayObject = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vertexArrayObject); @@ -88,6 +103,16 @@ public class Mesh { colorBufferObject = storeData(colorBuffer, 1, 3); + FloatBuffer textureBuffer = MemoryUtil.memAllocFloat(vertices.length * 2); + float[] textureData = new float[vertices.length * 3]; + for (int i = 0; i < vertices.length; i ++ ) { + textureData[i * 2] = vertices[i].getTextureCoordinates().getX(); + textureData[i * 2 + 1] = vertices[i].getTextureCoordinates().getY(); + } + textureBuffer.put(colorData).flip(); + + textureBufferObject = storeData(textureBuffer, 2, 2); + IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); diff --git a/src/org/hl/engine/graphics/Renderer.java b/src/org/hl/engine/graphics/Renderer.java index 17f7c53..b0e7e31 100644 --- a/src/org/hl/engine/graphics/Renderer.java +++ b/src/org/hl/engine/graphics/Renderer.java @@ -1,6 +1,7 @@ package org.hl.engine.graphics; import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL30; @@ -19,8 +20,10 @@ public class Renderer { GL30.glBindVertexArray(mesh.getVertexArrayObject()); GL30.glEnableVertexAttribArray(0); GL30.glEnableVertexAttribArray(1); + GL30.glEnableVertexAttribArray(2); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIndicesBufferObject()); - + GL13.glActiveTexture(GL13.GL_TEXTURE0); + GL13.glBindTexture(GL11.GL_TEXTURE_2D, mesh.getMaterial().getTextureID()); shader.bind(); GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0); @@ -28,6 +31,7 @@ public class Renderer { shader.unbind(); GL30.glDisableVertexAttribArray(0); GL30.glDisableVertexAttribArray(1); + GL30.glDisableVertexAttribArray(2); GL30.glBindVertexArray(0); } diff --git a/src/org/hl/engine/graphics/Vertex.java b/src/org/hl/engine/graphics/Vertex.java index 5e2df5b..3d81c3c 100644 --- a/src/org/hl/engine/graphics/Vertex.java +++ b/src/org/hl/engine/graphics/Vertex.java @@ -1,6 +1,7 @@ package org.hl.engine.graphics; -import org.hl.engine.math.Vector3f; +import org.hl.engine.math.lalg.Vector2f; +import org.hl.engine.math.lalg.Vector3f; public class Vertex { @@ -8,16 +9,22 @@ public class Vertex { private Vector3f position; private Vector3f color; + private Vector2f textureCoordinates; - public Vertex (Vector3f position, Vector3f color) { + public Vertex (Vector3f position, Vector3f color, Vector2f textureCoordinates) { this.position = position; this.color = color; + this.textureCoordinates = textureCoordinates; } public Vector3f getPosition() { return position; } + public Vector2f getTextureCoordinates() { + return textureCoordinates; + } + public Vector3f getColor() { return color; } diff --git a/src/org/hl/engine/io/Display.java b/src/org/hl/engine/io/Display.java index 0c0c5ec..d9126f7 100644 --- a/src/org/hl/engine/io/Display.java +++ b/src/org/hl/engine/io/Display.java @@ -1,5 +1,5 @@ package org.hl.engine.io; -import org.hl.engine.math.Vector3f; +import org.hl.engine.math.lalg.Vector3f; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.glfw.GLFWWindowSizeCallback; import org.lwjgl.opengl.GL; @@ -102,8 +102,8 @@ public class Display { System.exit(1); } - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); diff --git a/src/org/hl/engine/math/lalg/Vector2f.java b/src/org/hl/engine/math/lalg/Vector2f.java new file mode 100644 index 0000000..7b385cd --- /dev/null +++ b/src/org/hl/engine/math/lalg/Vector2f.java @@ -0,0 +1,32 @@ +package org.hl.engine.math.lalg; + +public class Vector2f { + private float x, y; + + // Just a vector if you know what I mean + public Vector2f (float x, float y) { + this.x = x; + this.y = y; + } + + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + public void setVector(float x, float y) { + this.x = x; + this.y = y; + } +} diff --git a/src/org/hl/engine/math/Vector3f.java b/src/org/hl/engine/math/lalg/Vector3f.java similarity index 95% rename from src/org/hl/engine/math/Vector3f.java rename to src/org/hl/engine/math/lalg/Vector3f.java index fa1da5e..b2a87f0 100644 --- a/src/org/hl/engine/math/Vector3f.java +++ b/src/org/hl/engine/math/lalg/Vector3f.java @@ -1,4 +1,4 @@ -package org.hl.engine.math; +package org.hl.engine.math.lalg; public class Vector3f { private float x; diff --git a/src/org/hl/engine/utils/TextureLoader.java b/src/org/hl/engine/utils/TextureLoader.java index 7f3e0b7..32ce01f 100644 --- a/src/org/hl/engine/utils/TextureLoader.java +++ b/src/org/hl/engine/utils/TextureLoader.java @@ -1,6 +1,5 @@ package org.hl.engine.utils; -// Original TextureLoader by Krythic (replaces SlickUtils texture loader) import java.awt.image.BufferedImage; import java.io.IOException; diff --git a/src/resources/shaders/mainFragment.glsl b/src/resources/shaders/mainFragment.glsl index 85d4098..647b0dc 100644 --- a/src/resources/shaders/mainFragment.glsl +++ b/src/resources/shaders/mainFragment.glsl @@ -1,9 +1,12 @@ -#version 330 core +#version 460 core in vec3 passColor; +in vec3 passTextureCoord; out vec4 outColor; +uniform sampler2D tex; + void main() { - outColor = vec4(passColor, 1.0); + outColor = texture(tex, passTextureCoord); } diff --git a/src/resources/shaders/mainVertex.glsl b/src/resources/shaders/mainVertex.glsl index 9ae1698..78ba706 100644 --- a/src/resources/shaders/mainVertex.glsl +++ b/src/resources/shaders/mainVertex.glsl @@ -1,11 +1,14 @@ -#version 330 core +#version 460 core -layout(location = 0) in vec3 position; -layout(location = 1) in vec3 color; +in vec3 position; +in vec3 color; +in vec2 textureCoordinates; out vec3 passColor; +out vec2 passTextureCoord; void main() { gl_Position = vec4(position, 1.0); passColor = color; + passTextureCoord = textureCoordinates; } \ No newline at end of file diff --git a/src/resources/textures/beautiful.png b/src/resources/textures/beautiful.png new file mode 100644 index 0000000000000000000000000000000000000000..ffe963ef4dd2c0a504e694b733c8f1f274fe07cb GIT binary patch literal 5785 zcmdsbXH*m2_jN+(5JC|UPy@J!kK;&zYHf@4B(quNi?^1z7I4Ar$(k7G-VC-~pZ0in`da`K(Nr>FB+|&Y!!V;*%-xYiL^M2H+AvPqupnIco>Jh; z3jZH6HiNyTvT zmw@)i4y}i#T1qem;y^PD0TE_!VY{vRr$74yR=M(s2t)p^(3}YQ{m{5$GW?rasGBB`eo=$`Okx$h}q$uF&-L4 zj^7-a|8($-{m$TcMimQ8nY?i5d`k6)DC`hv(SX$?8Z|_R4w7G6w8Yj&JGg{ zKI832m8h}JU;GL}N3YNAbxc8g{grq?vS>{EA4lp+>t1f?&(p?DGg;OwW3LPNuxZ{$ zRxw9%lyUPxO%g281w;pB32l~Lb_S{P~pfIqJAFm0`+E92_TCp zN^9CmSSbZuIw~tp|FRXA&k>paBT$R}2g#+-lH&;+#_gV}L7G1U_gToTU;}6eTYOkw zUf(Vs=}1UizsApiWzmTzMgws=Ot()VU48q>A3YT>0iTN8D@rwLTDG_?KuqpTJ(P?t5WhhhYkMfeztkJft4Y?OZ!vHE!%BG#=ZJZ{JG zmFVNZ#UGBSOEkTXZi<)XRG4Mhk#X`37tpE~pCb!3+0qOWaw(k!>6d?wqJ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