adding textures (to texture branch, split due to errors)
This commit is contained in:
@@ -10,6 +10,8 @@ public class Material {
|
||||
|
||||
private Texture texture;
|
||||
|
||||
private String path;
|
||||
|
||||
private BufferedImage image;
|
||||
|
||||
private int width, height;
|
||||
@@ -17,10 +19,12 @@ public class Material {
|
||||
|
||||
public Material(String path) {
|
||||
|
||||
this.image = TextureLoader.loadImage(path); //The path is inside the jar file
|
||||
this.path = path;
|
||||
|
||||
}
|
||||
public void 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);
|
||||
|
||||
@@ -12,12 +12,14 @@ import java.nio.IntBuffer;
|
||||
public class Mesh {
|
||||
private Vertex[] vertices;
|
||||
private int[] indices;
|
||||
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject;
|
||||
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject;
|
||||
private Material material;
|
||||
|
||||
// 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,8 +27,12 @@ public class Mesh {
|
||||
GL15.glDeleteBuffers(positionBufferObject);
|
||||
GL15.glDeleteBuffers(indicesBufferObject);
|
||||
GL15.glDeleteBuffers(colorBufferObject);
|
||||
GL30.glDeleteBuffers(textureBufferObject);
|
||||
|
||||
GL30.glDeleteVertexArrays(vertexArrayObject);
|
||||
|
||||
material.destroy();
|
||||
|
||||
}
|
||||
|
||||
// getters for the mesh
|
||||
@@ -55,8 +61,18 @@ public class Mesh {
|
||||
return colorBufferObject;
|
||||
}
|
||||
|
||||
public int getTextureBufferObject() {
|
||||
return textureBufferObject;
|
||||
}
|
||||
|
||||
public Material getMaterial() {
|
||||
return material;
|
||||
}
|
||||
|
||||
public void create() {
|
||||
|
||||
material.create();
|
||||
|
||||
// Creates the mesh by formatting the vertices and indices and inputting them to OpenGL
|
||||
vertexArrayObject = GL30.glGenVertexArrays();
|
||||
GL30.glBindVertexArray(vertexArrayObject);
|
||||
@@ -88,6 +104,18 @@ public class Mesh {
|
||||
|
||||
colorBufferObject = storeData(colorBuffer, 1, 3);
|
||||
|
||||
// Putting the texture into the buffer so renderer and shader can read it
|
||||
|
||||
FloatBuffer textureBuffer = MemoryUtil.memAllocFloat(vertices.length * 2);
|
||||
float[] textureData = new float[vertices.length * 2];
|
||||
for (int i = 0; i < vertices.length; i ++ ) {
|
||||
textureData[i * 2] = vertices[i].getTextureCoords().getX();
|
||||
textureData[i * 2 + 1] = vertices[i].getTextureCoords().getY();
|
||||
}
|
||||
textureBuffer.put(textureData).flip();
|
||||
|
||||
textureBufferObject = storeData(textureBuffer, 2, 2);
|
||||
|
||||
IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
|
||||
indicesBuffer.put(indices).flip();
|
||||
|
||||
|
||||
@@ -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,13 @@ 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 +34,7 @@ public class Renderer {
|
||||
shader.unbind();
|
||||
GL30.glDisableVertexAttribArray(0);
|
||||
GL30.glDisableVertexAttribArray(1);
|
||||
GL30.glDisableVertexAttribArray(2);
|
||||
GL30.glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.hl.engine.graphics;
|
||||
|
||||
import org.hl.engine.math.Vector3f;
|
||||
import org.hl.engine.math.lalg.*;
|
||||
|
||||
public class Vertex {
|
||||
|
||||
@@ -8,8 +8,9 @@ public class Vertex {
|
||||
|
||||
private Vector3f position;
|
||||
private Vector3f color;
|
||||
private Vector2f textureCoords;
|
||||
|
||||
public Vertex (Vector3f position, Vector3f color) {
|
||||
public Vertex (Vector3f position, Vector3f color, Vector2f texture) {
|
||||
this.position = position;
|
||||
this.color = color;
|
||||
}
|
||||
@@ -21,4 +22,8 @@ public class Vertex {
|
||||
public Vector3f getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public Vector2f getTextureCoords() {
|
||||
return textureCoords;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.hl.engine.math.lalg;
|
||||
|
||||
public class Vector2f {
|
||||
private float x;
|
||||
private float y;
|
||||
|
||||
// Just a vector if you know what I mean
|
||||
public Vector2f (float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void setVector(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;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.hl.engine.math;
|
||||
package org.hl.engine.math.lalg;
|
||||
|
||||
public class Vector3f {
|
||||
private float x;
|
||||
Reference in New Issue
Block a user