new branch for texture

This commit is contained in:
EvilMuffinHa 2020-05-10 10:24:00 -04:00
parent b2dbf16329
commit 0c6ccbbb42
12 changed files with 23 additions and 257 deletions

View File

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

View File

@ -1,47 +0,0 @@
package org.hl.engine.graphics;
import org.hl.engine.utils.TextureLoader;
import org.lwjgl.opengl.GL11;
import org.lwjgl.openvr.Texture;
import java.awt.image.BufferedImage;
public class Material {
private Texture texture;
private BufferedImage image;
private int width, height;
private int textureID;
private String path;
public Material(String path) {
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);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getTextureID() {
return textureID;
}
public void destroy() {
GL11.glDeleteTextures(textureID);
}
}

View File

@ -12,14 +12,12 @@ import java.nio.IntBuffer;
public class Mesh {
private Vertex[] vertices;
private int[] indices;
private Material material;
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject;
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject;
// A group of vertices combined based on the indexes
public Mesh(Vertex[] vertices, int[] indices, Material material) {
public Mesh(Vertex[] vertices, int[] indices) {
this.vertices = vertices;
this.indices = indices;
this.material = material;
}
// Destroy the mesh
@ -27,8 +25,6 @@ public class Mesh {
GL15.glDeleteBuffers(positionBufferObject);
GL15.glDeleteBuffers(indicesBufferObject);
GL15.glDeleteBuffers(colorBufferObject);
GL15.glDeleteBuffers(textureBufferObject);
material.destroy();
GL30.glDeleteVertexArrays(vertexArrayObject);
}
@ -59,19 +55,8 @@ 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);
@ -103,19 +88,6 @@ public class Mesh {
colorBufferObject = storeData(colorBuffer, 1, 3);
// Putting texture into the buffer so renderer and shader can read it
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(textureData).flip();
textureBufferObject = storeData(textureBuffer, 2, 2);
IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();

View File

@ -1,7 +1,6 @@
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;
@ -20,10 +19,8 @@ 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);
@ -31,7 +28,6 @@ public class Renderer {
shader.unbind();
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}

View File

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

View File

@ -1,5 +1,5 @@
package org.hl.engine.io;
import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.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, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

View File

@ -1,32 +0,0 @@
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,44 +0,0 @@
package org.hl.engine.math.lalg;
public class Vector3f {
private float x;
private float y;
private float z;
// Just a vector if you know what I mean
public Vector3f (float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setVector(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
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 float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
}

View File

@ -1,69 +0,0 @@
package org.hl.engine.utils;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL12;
import static org.lwjgl.opengl.GL11.*;
public class TextureLoader {
private static final int BYTES_PER_PIXEL = 4;//3 for RGB, 4 for RGBA
public static int loadTexture(BufferedImage image){
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
for(int y = 0; y < image.getHeight(); y++){
for(int x = 0; x < image.getWidth(); x++){
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF)); // Blue component
buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
}
}
buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
// You now have a ByteBuffer filled with the color data of each pixel.
// Now just create a texture ID and bind it. Then you can load it using
// whatever OpenGL method you want, for example:
int textureID = glGenTextures(); //Generate texture ID
glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
//Setup wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
//Setup texture scaling filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//Send texel data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//Return the texture ID so we can bind it later again
return textureID;
}
public static BufferedImage loadImage(String loc)
{
try {
return ImageIO.read(TextureLoader.class.getResource(loc));
} catch (IOException e) {
//Error Handling Here
System.err.println("Error with loading texture. ");
System.exit(1);
}
return null;
}
}

View File

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

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB