Revert "Revert "adding textures (has errors)""

This reverts commit 8c94a73d70.

removing textureloader
This commit is contained in:
EvilMuffinHa 2020-05-10 10:52:12 -04:00
parent 8c94a73d70
commit 6f99ed0ebe
12 changed files with 99 additions and 26 deletions

View File

@ -1,10 +1,7 @@
import org.hl.engine.graphics.Mesh; import org.hl.engine.graphics.*;
import org.hl.engine.graphics.Renderer;
import org.hl.engine.graphics.Shader;
import org.hl.engine.graphics.Vertex;
import org.hl.engine.io.Display; import org.hl.engine.io.Display;
import org.hl.engine.io.Input; import org.hl.engine.io.Input;
import org.hl.engine.math.Vector3f; import org.hl.engine.math.lalg.*;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
@ -18,16 +15,16 @@ public class Test {
public Shader shader; public Shader shader;
public Mesh mesh = new Mesh(new Vertex[] { 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 Vector2f(0, 0)),
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 Vector2f(0, 1)),
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 Vector2f(1, 1)),
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 Vector2f(1, 0)),
}, new int[] { }, new int[] {
0, 1, 2, 0, 1, 2,
0, 2, 3 0, 2, 3
}); }, new Material("/textures/beautiful.png"));
public void run() { public void run() {
init(); init();

View File

@ -14,13 +14,16 @@ public class Material {
private int width, height; private int width, height;
private int textureID; private int textureID;
private String path;
public Material(String path) { public Material(String path) {
this.image = TextureLoader.loadImage(path); //The path is inside the jar file this.path = path;
} }
public void create() { 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.width = this.image.getWidth();
this.height = this.image.getHeight(); this.height = this.image.getHeight();
this.textureID = TextureLoader.loadTexture(image); this.textureID = TextureLoader.loadTexture(image);

View File

@ -12,12 +12,14 @@ import java.nio.IntBuffer;
public class Mesh { public class Mesh {
private Vertex[] vertices; private Vertex[] vertices;
private int[] indices; 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 // 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.vertices = vertices;
this.indices = indices; this.indices = indices;
this.material = material;
} }
// Destroy the mesh // Destroy the mesh
@ -25,6 +27,8 @@ public class Mesh {
GL15.glDeleteBuffers(positionBufferObject); GL15.glDeleteBuffers(positionBufferObject);
GL15.glDeleteBuffers(indicesBufferObject); GL15.glDeleteBuffers(indicesBufferObject);
GL15.glDeleteBuffers(colorBufferObject); GL15.glDeleteBuffers(colorBufferObject);
GL15.glDeleteBuffers(textureBufferObject);
material.destroy();
GL30.glDeleteVertexArrays(vertexArrayObject); GL30.glDeleteVertexArrays(vertexArrayObject);
} }
@ -55,8 +59,19 @@ public class Mesh {
return colorBufferObject; return colorBufferObject;
} }
public int getTextureBufferObject() {
return textureBufferObject;
}
public Material getMaterial() {
return material;
}
public void create() { public void create() {
// Create the material
material.create();
// Creates the mesh by formatting the vertices and indices and inputting them to OpenGL // Creates the mesh by formatting the vertices and indices and inputting them to OpenGL
vertexArrayObject = GL30.glGenVertexArrays(); vertexArrayObject = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vertexArrayObject); GL30.glBindVertexArray(vertexArrayObject);
@ -88,6 +103,16 @@ public class Mesh {
colorBufferObject = storeData(colorBuffer, 1, 3); 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); IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip(); indicesBuffer.put(indices).flip();

View File

@ -1,6 +1,7 @@
package org.hl.engine.graphics; package org.hl.engine.graphics;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
@ -19,8 +20,10 @@ public class Renderer {
GL30.glBindVertexArray(mesh.getVertexArrayObject()); GL30.glBindVertexArray(mesh.getVertexArrayObject());
GL30.glEnableVertexAttribArray(0); GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1); GL30.glEnableVertexAttribArray(1);
GL30.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIndicesBufferObject()); 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(); shader.bind();
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0); GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
@ -28,6 +31,7 @@ public class Renderer {
shader.unbind(); shader.unbind();
GL30.glDisableVertexAttribArray(0); GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1); GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0); GL30.glBindVertexArray(0);
} }

View File

@ -1,6 +1,7 @@
package org.hl.engine.graphics; 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 { public class Vertex {
@ -8,16 +9,22 @@ public class Vertex {
private Vector3f position; private Vector3f position;
private Vector3f color; private Vector3f color;
private Vector2f textureCoordinates;
public Vertex (Vector3f position, Vector3f color) { public Vertex (Vector3f position, Vector3f color, Vector2f textureCoordinates) {
this.position = position; this.position = position;
this.color = color; this.color = color;
this.textureCoordinates = textureCoordinates;
} }
public Vector3f getPosition() { public Vector3f getPosition() {
return position; return position;
} }
public Vector2f getTextureCoordinates() {
return textureCoordinates;
}
public Vector3f getColor() { public Vector3f getColor() {
return color; return color;
} }

View File

@ -1,5 +1,5 @@
package org.hl.engine.io; 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.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback; import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
@ -102,8 +102,8 @@ public class Display {
System.exit(1); System.exit(1);
} }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package org.hl.engine.math; package org.hl.engine.math.lalg;
public class Vector3f { public class Vector3f {
private float x; private float x;

View File

@ -1,6 +1,5 @@
package org.hl.engine.utils; package org.hl.engine.utils;
// Original TextureLoader by Krythic (replaces SlickUtils texture loader)
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;

View File

@ -1,9 +1,12 @@
#version 330 core #version 460 core
in vec3 passColor; in vec3 passColor;
in vec3 passTextureCoord;
out vec4 outColor; out vec4 outColor;
uniform sampler2D tex;
void main() { void main() {
outColor = vec4(passColor, 1.0); outColor = texture(tex, passTextureCoord);
} }

View File

@ -1,11 +1,14 @@
#version 330 core #version 460 core
layout(location = 0) in vec3 position; in vec3 position;
layout(location = 1) in vec3 color; in vec3 color;
in vec2 textureCoordinates;
out vec3 passColor; out vec3 passColor;
out vec2 passTextureCoord;
void main() { void main() {
gl_Position = vec4(position, 1.0); gl_Position = vec4(position, 1.0);
passColor = color; passColor = color;
passTextureCoord = textureCoordinates;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB