beta just for testing

This commit is contained in:
EvilMuffinHa 2020-05-26 14:08:27 -04:00
parent 1285842b55
commit 47887a4abb
22 changed files with 39 additions and 806 deletions

View File

@ -8,6 +8,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lwjgl-release-3.2" level="project" />
<orderEntry type="library" name="commons-io-2" level="project" />
<orderEntry type="library" name="joml-1.9.24" level="project" />
</component>
</module>

View File

@ -1,15 +0,0 @@
<component name="libraryTable">
<library name="commons-io-2">
<CLASSES>
<root url="file://$USER_HOME$/Downloads/commons-io-2.6" />
</CLASSES>
<JAVADOC>
<root url="file://$USER_HOME$/Downloads/commons-io-2.6/docs" />
</JAVADOC>
<SOURCES>
<root url="file://$USER_HOME$/Downloads/commons-io-2.6" />
</SOURCES>
<jarDirectory url="file://$USER_HOME$/Downloads/commons-io-2.6" recursive="false" />
<jarDirectory url="file://$USER_HOME$/Downloads/commons-io-2.6" recursive="false" type="SOURCES" />
</library>
</component>

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="joml-1.9.24">
<CLASSES>
<root url="jar://$USER_HOME$/Downloads/joml-1.9.24.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -1,4 +1,4 @@
# Helium-Engine
Java game engine using LWJGL
Java game org.hl.engine.engine using LWJGL
To run add the `-XstartOnFirstThread` to your VM options (only for Mac)

View File

@ -1,6 +0,0 @@
abstract public class Game {
abstract public void setup() throws Exception;
abstract public void loop() throws Exception;
abstract public void close() throws Exception;
}

View File

@ -1,101 +0,0 @@
import org.hl.engine.graphics.*;
import org.hl.engine.io.Display;
import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.objects.GameObject;
import org.lwjgl.glfw.GLFW;
public class Test extends Game {
// Defining original parts of the game
public final static int WIDTH = 1280, HEIGHT = 760;
public final String windowName = "Game!";
public Display display;
public Input i;
public Renderer renderer;
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 int[] {
0, 1, 2,
0, 2, 3
}, new Material(new Texture("resources/textures/b.png")));
public GameObject testObject = new GameObject(mesh, new Vector3f(0, 0, -1), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public void run() throws Exception {
setup();
i = new Input(display);
while (!(display.shouldClose()) && !i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
loop();
}
close();
}
public void loop(){
//First updating
int frames = display.update();
//testObject.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
i.reset();
// Now Render!
// rendering the mesh
renderer.renderMesh(testObject);
//swap buffers so the new one will appear
display.swapBuffers();
}
public void setup() throws Exception {
//First, set up the display
display = new Display(WIDTH, HEIGHT, windowName, 70, 0.1f, 1000f);
display.create();
// Open the shaders
shader = new Shader(Shader.VERTEXSHADER, Shader.FRAGSHADER);
// Set up the renderer
renderer = new Renderer(display, shader);
// Changing the background color
display.setBackgroundColor(0F, 0F, 0F);
// Creating / displaying the mesh
mesh.create();
// Creating the shader
shader.create();
}
public void close() {
// Removing everything
display.destroy();
mesh.destroy();
shader.destroy();
}
public static void main(String[] args) throws Exception {
//Running
new Test().run();
}
}

View File

@ -1,107 +0,0 @@
package org.hl.engine.graphics;
import org.hl.engine.math.lalg.Vector4f;
public class Material {
private static final Vector4f DEFAULT_COLOR = new Vector4f(1.0f, 1.0f, 1.0f, 1.0f);
private Vector4f ambientColor;
private Vector4f diffuseColor;
private Vector4f specularColor;
private float reflectance;
private Texture texture;
private Texture normalMap;
public Material() {
this.ambientColor = DEFAULT_COLOR;
this.diffuseColor = DEFAULT_COLOR;
this.specularColor = DEFAULT_COLOR;
this.texture = null;
this.reflectance = 0;
}
public Material(Vector4f color, float reflectance) {
this(color, color, color, null, reflectance);
}
public Material(Texture texture) {
this(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, texture, 0);
}
public Material(Texture texture, float reflectance) {
this(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, texture, reflectance);
}
public Material(Vector4f ambientColor, Vector4f diffuseColor, Vector4f specularColor, Texture texture, float reflectance) {
this.ambientColor = ambientColor;
this.diffuseColor = diffuseColor;
this.specularColor = specularColor;
this.texture = texture;
this.reflectance = reflectance;
}
public Vector4f getAmbientColor() {
return ambientColor;
}
public void setAmbientColor(Vector4f ambientColor) {
this.ambientColor = ambientColor;
}
public Vector4f getDiffuseColor() {
return diffuseColor;
}
public void setDiffuseColor(Vector4f diffuseColor) {
this.diffuseColor = diffuseColor;
}
public Vector4f getSpecularColor() {
return specularColor;
}
public void setSpecularColor(Vector4f specularColor) {
this.specularColor = specularColor;
}
public float getReflectance() {
return reflectance;
}
public void setReflectance(float reflectance) {
this.reflectance = reflectance;
}
public boolean isTextured() {
return this.texture != null;
}
public Texture getTexture() {
return texture;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
public boolean hasNormalMap() {
return this.normalMap != null;
}
public Texture getNormalMap() {
return normalMap;
}
public void setNormalMap(Texture normalMap) {
this.normalMap = normalMap;
}
public void create() {
texture.create();
}
}

36
src/org/hl/engine/graphics/Mesh.java Normal file → Executable file
View File

@ -10,31 +10,23 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer;
public class Mesh {
private Vertex[] vertices;
private int[] indices;
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject;
private Material material;
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
public void destroy () {
GL15.glDeleteBuffers(positionBufferObject);
GL15.glDeleteBuffers(indicesBufferObject);
GL15.glDeleteBuffers(colorBufferObject);
GL30.glDeleteBuffers(textureBufferObject);
GL30.glDeleteVertexArrays(vertexArrayObject);
material.getTexture().destroy();
}
// getters for the mesh
@ -55,7 +47,6 @@ public class Mesh {
return positionBufferObject;
}
public int getIndicesBufferObject() {
return indicesBufferObject;
}
@ -64,19 +55,8 @@ 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);
@ -108,18 +88,6 @@ 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();

26
src/org/hl/engine/graphics/Renderer.java Normal file → Executable file
View File

@ -1,47 +1,33 @@
package org.hl.engine.graphics;
import org.hl.engine.io.Display;
import org.hl.engine.math.lalg.Matrix4f;
import org.hl.engine.objects.GameObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;
public class Renderer {
private Shader shader;
private Display display;
public Renderer(Display display, Shader shader) {
public Renderer(Shader shader) {
this.shader = shader;
this.display = display;
}
public void renderMesh(GameObject object) {
public void renderMesh(Mesh mesh) {
// Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
GL30.glBindVertexArray(mesh.getVertexArrayObject());
GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1);
GL30.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, object.getMesh().getIndicesBufferObject());
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMesh().getMaterial().getTexture().getId());
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIndicesBufferObject());
shader.bind();
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
shader.setUniform("projection", display.getProjectionMatrix());
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
shader.unbind();
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}

51
src/org/hl/engine/graphics/Shader.java Normal file → Executable file
View File

@ -1,15 +1,8 @@
package org.hl.engine.graphics;
import org.hl.engine.math.lalg.Matrix4f;
import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.lalg.Vector4f;
import org.hl.engine.utils.FileUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.system.MemoryUtil;
import java.nio.FloatBuffer;
public class Shader {
private String vertexFile;
@ -17,9 +10,6 @@ public class Shader {
private int vertexID, fragmentID, programID;
public static final String VERTEXSHADER = "/resources/shaders/mainVertex.glsl";
public static final String FRAGSHADER = "/resources/shaders/mainFragment.glsl";
public Shader(String vertexPath, String fragmentPath) {
vertexFile = FileUtils.loadAsString(vertexPath);
fragmentFile = FileUtils.loadAsString(fragmentPath);
@ -76,40 +66,9 @@ public class Shader {
return;
}
}
GL20.glDeleteShader(vertexID);
GL20.glDeleteShader(fragmentID);
public int getUniformLocation(String name) {
return GL20.glGetUniformLocation(programID, name);
}
public void setUniform(String name, float value) {
GL20.glUniform1f(getUniformLocation(name), value);
}
public void setUniform(String name, int value) {
GL20.glUniform1i(getUniformLocation(name), value);
}
public void setUniform(String name, boolean value) {
GL20.glUniform1i(getUniformLocation(name), value ? 1 : 0);
}
public void setUniform(String name, Vector2f value) {
GL20.glUniform2f(getUniformLocation(name), value.getX(), value.getY());
}
public void setUniform(String name, Vector3f value) {
GL20.glUniform3f(getUniformLocation(name), value.getX(), value.getY(), value.getZ());
}
public void setUniform(String name, Vector4f value) {
GL20.glUniform4f(getUniformLocation(name), value.getX(), value.getY(), value.getZ(), value.getA());
}
public void setUniform(String name, Matrix4f value) {
FloatBuffer matrix = MemoryUtil.memAllocFloat(Matrix4f.SIZE * Matrix4f.SIZE);
matrix.put(value.convertTo1D()).flip();
GL20.glUniformMatrix4fv(getUniformLocation(name), true, matrix);
}
// Bind so we can use the shader
@ -124,12 +83,6 @@ public class Shader {
// Destroy the program
public void destroy() {
GL20.glDetachShader(programID, vertexID);
GL20.glDetachShader(programID, fragmentID);
GL20.glDeleteShader(vertexID);
GL20.glDeleteShader(fragmentID);
GL20.glDeleteProgram(programID);
}
}

View File

@ -1,133 +0,0 @@
package org.hl.engine.graphics;
import static org.lwjgl.opengl.GL46.*;
import static org.lwjgl.stb.STBImage.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.system.MemoryStack;
public class Texture {
private int id;
private int width;
private int height;
private int type;
private String fileName;
private ByteBuffer imageBuffer;
private int pixelFormat;
public Texture(int width, int height, int pixelFormat) {
this.type = 0;
this.width = width;
this.height = height;
this.pixelFormat = pixelFormat;
}
public Texture(String fileName) {
this.type = 1;
this.fileName = fileName;
}
public Texture(ByteBuffer imageBuffer) {
type = 2;
this.imageBuffer = imageBuffer;
}
public void create() {
if (this.type == 0) {
this.id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, this.id);
glTexImage2D(GL_TEXTURE_2D, 0, this.pixelFormat, this.width, this.height, 0, this.pixelFormat, GL_FLOAT, (ByteBuffer) null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
} else if (this.type == 1) {
ByteBuffer buf;
try(MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer w = stack.mallocInt(1);
IntBuffer h = stack.mallocInt(1);
IntBuffer channels = stack.mallocInt(1);
buf = stbi_load(this.fileName, w, h, channels, 4);
if(buf == null) {
System.err.println("Image file [" + this.fileName + "] not loaded: " + stbi_failure_reason());
System.exit(1);
}
this.width = w.get();
this.height = h.get();
}
this.id = createTexture(buf);
stbi_image_free(buf);
} else {
ByteBuffer buf;
try(MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer w = stack.mallocInt(1);
IntBuffer h = stack.mallocInt(1);
IntBuffer channels = stack.mallocInt(1);
buf = stbi_load_from_memory(this.imageBuffer, w, h, channels, 4);
if(buf == null) {
System.err.println("Image file not loaded: " + stbi_failure_reason());
System.exit(1);
}
this.width = w.get();
this.height = h.get();
}
this.id = createTexture(buf);
stbi_image_free(buf);
}
}
private int createTexture(ByteBuffer buf) {
int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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, buf);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getId() {
return id;
}
public void destroy() {
glDeleteTextures(id);
}
}

15
src/org/hl/engine/graphics/Vertex.java Normal file → Executable file
View File

@ -1,8 +1,6 @@
package org.hl.engine.graphics;
import org.hl.engine.math.lalg.*;
import org.hl.engine.math.Vector3f;
public class Vertex {
@ -10,26 +8,17 @@ public class Vertex {
private Vector3f position;
private Vector3f color;
private Vector2f textureCoords;
public Vertex (Vector3f position, Vector3f color, Vector2f textureCoords) {
public Vertex (Vector3f position, Vector3f color) {
this.position = position;
this.color = color;
this.textureCoords = textureCoords;
}
public Vector3f getPosition() {
return position;
}
public Vector3f getColor() {
return color;
}
public Vector2f getTextureCoords() {
return textureCoords;
}
}

34
src/org/hl/engine/io/Display.java Normal file → Executable file
View File

@ -1,8 +1,5 @@
package org.hl.engine.io;
import org.hl.engine.math.lalg.Matrix4f;
import org.hl.engine.math.lalg.Vector3f;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.hl.engine.math.Vector3f;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
@ -11,7 +8,6 @@ import org.lwjgl.opengl.GL11;
import static org.lwjgl.glfw.GLFW.*;
public class Display {
private int width, height;
private String windowName;
private long window;
@ -30,16 +26,15 @@ public class Display {
private int savedPosY;
private int savedWidth;
private int savedHeight;
private Matrix4f projection;
// Constructor to create the display
public Display (int width, int height, String windowName, float fov, float near, float far) {
public Display (int width, int height, String windowName) {
this.width = width;
this.height = height;
this.windowName = windowName;
projection = Matrix4f.projection(fov, (float)this.width / (float) this.height, near, far);
}
// Change the window name
@ -69,10 +64,6 @@ public class Display {
return isFullscreen;
}
public Matrix4f getProjectionMatrix() {
return projection;
}
// Makes the screen fullscreen or not based on the argument
public void setFullscreen(boolean fullscreen) {
isFullscreen = fullscreen;
@ -103,19 +94,16 @@ public class Display {
// Creates the window (should go in the init() function of your Main program)
public void create() throws Exception {
GLFWErrorCallback.createPrint(System.err).set();
public void create() {
// initializing glfw
if (!glfwInit()) {
//System.err.println("Failed to initialize GLFW! ");
//System.exit(1);
throw new Exception("Failed to initialize GLFW! ");
System.err.println("Failed to initialize GLFW! ");
System.exit(1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
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);
@ -123,9 +111,8 @@ public class Display {
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(this.width, this.height, this.windowName, isFullscreen ? glfwGetPrimaryMonitor():0, 0);
if (window == 0) {
//System.err.println("Failed to create window! ");
//System.exit(1);
throw new Exception("Failed to create window! ");
System.err.println("Failed to create window! ");
System.exit(1);
}
// Setting size of window
@ -201,7 +188,6 @@ public class Display {
// Completely DESTROYS the window
public void destroy() {
resizeCallback.free();
glfwSetErrorCallback(null).free();
glfwDestroyWindow(window);
glfwTerminate();
}

0
src/org/hl/engine/io/Input.java Normal file → Executable file
View File

View File

@ -1,5 +1,4 @@
package org.hl.engine.math.lalg;
package org.hl.engine.math;
public class Vector3f {
private float x;
@ -19,12 +18,6 @@ public class Vector3f {
this.z = z;
}
public void add(float x, float y, float z) {
this.x += x;
this.y += y;
this.z += z;
}
public float getX() {
return x;
}
@ -49,4 +42,3 @@ public class Vector3f {
this.z = z;
}
}

View File

@ -1,139 +0,0 @@
package org.hl.engine.math.lalg;
public class Matrix4f {
public static final int SIZE = 4;
private float[][] elements = new float[SIZE][SIZE];
public Matrix4f() {
}
public Matrix4f(float[][] values) {
this.elements = values;
}
public static Matrix4f identity() {
float[][] identityArray = {
{1 , 0 , 0 , 0},
{0 , 1 , 0 , 0},
{0 , 0 , 1 , 0},
{0 , 0 , 0 , 1}
};
Matrix4f identity = new Matrix4f(identityArray);
return identity;
}
public static Matrix4f translate(Vector3f translate) {
Matrix4f result = Matrix4f.identity();
result.set(3, 0, translate.getX());
result.set(3, 1, translate.getY());
result.set(3, 2, translate.getZ());
return result;
}
public static Matrix4f rotate(float angle, Vector3f axis) {
float cos = (float)Math.cos(Math.toDegrees(angle));
float sin = (float)Math.sin(Math.toDegrees(angle));
float C = 1 - cos;
float x = axis.getX();
float y = axis.getY();
float z = axis.getZ();
float[][] rotArray = {
{cos + x*x*C , x*y*C - z*sin , x*z*C + y*sin , 0},
{y*z*C + z*sin , cos + y*y*C , y*z*C - x*sin , 0},
{z*x*C-y*sin , z*y*C + x*sin , cos + z*z*C , 0},
{0 , 0 , 0 , 1}
};
Matrix4f result = new Matrix4f(rotArray);
return result;
}
public static Matrix4f scale(Vector3f scaleVec) {
Matrix4f result = Matrix4f.identity();
result.set(0, 0, scaleVec.getX());
result.set(1, 1, scaleVec.getY());
result.set(2, 2, scaleVec.getZ());
return result;
}
public static Matrix4f projection( float fov, float aspectRatio, float near, float far) {
Matrix4f result = Matrix4f.identity();
float tan = (float)Math.tan(Math.toRadians(fov / 2));
float range = far - near;
result.set(0, 0, 1.0f / (aspectRatio * tan));
result.set(1, 1, 1.0f / tan);
result.set(2, 2, -((far + near) / range));
result.set(2, 3, -1.0f);
result.set(3, 2, -(2.0f*far*near/range));
result.set(3, 3, 0f);
return result;
}
public static Matrix4f multiply(Matrix4f first, Matrix4f second) {
Matrix4f result = Matrix4f.identity();
for (int i = 0; i < SIZE; i ++ ) {
for (int j = 0; j < SIZE; j ++) {
result.set(i, j,
first.get(i, 0) * second.get(0, j) +
first.get(i, 1) * second.get(1, j) +
first.get(i, 2) * second.get(2, j) +
first.get(i, 3) * second.get(3, j)
);
}
}
return result;
}
public float get(int x, int y) {
return elements[x][y];
}
public void set(int x, int y, float value) {
elements[x][y] = value;
}
public float[][] getAll() {
return elements;
}
public static Matrix4f transform(Vector3f position, Vector3f rotation, Vector3f scale) {
Matrix4f result = identity();
Matrix4f translationMatrix = Matrix4f.translate(position);
Matrix4f rotationXMatrix = Matrix4f.rotate(rotation.getX(), new Vector3f(1, 0, 0));
Matrix4f rotationYMatrix = Matrix4f.rotate(rotation.getY(), new Vector3f(0, 1, 0));
Matrix4f rotationZMatrix = Matrix4f.rotate(rotation.getZ(), new Vector3f(0, 0, 1));
Matrix4f scaleMatrix = Matrix4f.scale(scale);
Matrix4f rotMat = Matrix4f.multiply(rotationXMatrix, Matrix4f.multiply(rotationYMatrix, rotationZMatrix));
return Matrix4f.multiply(translationMatrix, Matrix4f.multiply(rotMat, scaleMatrix));
}
public float[] convertTo1D() {
float[] returnedArray = new float[SIZE*SIZE];
int sizeOfRow = elements[0].length;
for (int i = 0; i < elements.length; i ++) {
for (int j = 0; j < sizeOfRow; j ++) {
returnedArray[i*SIZE + j] = elements[i][j];
}
}
return returnedArray;
}
}

View File

@ -1,39 +0,0 @@
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 void add(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;
}
}

View File

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

View File

@ -1,36 +0,0 @@
package org.hl.engine.objects;
import org.hl.engine.graphics.Mesh;
import org.hl.engine.math.lalg.Vector3f;
public class GameObject {
private Vector3f position, rotation, scale;
private Mesh mesh;
public GameObject(Mesh mesh, Vector3f position, Vector3f rotation, Vector3f scale) {
this.position = position;
this.rotation = rotation;
this.scale = scale;
this.mesh = mesh;
}
public void update() {
position.add(0, 0, -0.1F);
}
public Vector3f getPosition() {
return position;
}
public Vector3f getRotation() {
return rotation;
}
public Vector3f getScale() {
return scale;
}
public Mesh getMesh() {
return mesh;
}
}

0
src/org/hl/engine/utils/FileUtils.java Normal file → Executable file
View File

7
src/resources/shaders/mainFragment.glsl Normal file → Executable file
View File

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

15
src/resources/shaders/mainVertex.glsl Normal file → Executable file
View File

@ -1,20 +1,11 @@
#version 410 core
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 2) in vec2 textureCoord;
uniform mat4 model;
uniform mat4 projection;
layout(location = 0) out vec3 passColor;
layout(location = 1) out vec2 passTextureCoord;
out vec3 passColor;
void main() {
gl_Position = projection * model * vec4(position, 1.0);
gl_Position = vec4(position, 1.0);
passColor = color;
passTextureCoord = textureCoord;
}