This commit is contained in:
EvilMuffinHa 2020-07-18 22:27:01 -04:00
parent 5fe794304e
commit 7e2c4c7135
7 changed files with 146 additions and 18 deletions

View File

@ -3,7 +3,7 @@
<output-path>$PROJECT_DIR$/out/artifacts/Helium_Engine_jar</output-path> <output-path>$PROJECT_DIR$/out/artifacts/Helium_Engine_jar</output-path>
<root id="archive" name="Helium-Engine.jar"> <root id="archive" name="Helium-Engine.jar">
<element id="module-output" name="Helium-Engine" /> <element id="module-output" name="Helium-Engine" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/joml-1.9.24.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/snakeyaml-1.9.jar" path-in-jar="/" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl.jar" path-in-jar="/" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-egl.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-egl.jar" path-in-jar="/" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-lz4.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-lz4.jar" path-in-jar="/" />

View File

@ -136,9 +136,9 @@ public class Test implements Game {
// rendering the cube and plane // rendering the cube and plane
renderer.renderMesh(cubeObject, camera); renderer.renderObject3D(cubeObject, camera);
renderer.renderMesh(planeObject, camera); renderer.renderObject3D(planeObject, camera);
renderer.renderMesh(dragonObject, camera); renderer.renderObject3D(dragonObject, camera);
//swap buffers so the new one will appear //swap buffers so the new one will appear
display.reset(); display.reset();
@ -148,13 +148,13 @@ public class Test implements Game {
public void setup() throws Exception { public void setup() throws Exception {
//First, set up the display //First, set up the display
display = new Display(WIDTH, HEIGHT, windowName, 70, 0.1f, 1000f); display = new Display(WIDTH, HEIGHT, windowName);
display.create(); display.create();
// Open the shaders // Open the shaders
shader = new Shader(Shader.VERTEXSHADER, Shader.FRAGSHADER); shader = new Shader(0);
// Set up the renderer // Set up the renderer
renderer = new Renderer(display, shader); renderer = new Renderer(display, shader);

View File

@ -13,13 +13,20 @@ public class Renderer {
private Shader shader; private Shader shader;
private Display display; private Display display;
private Matrix4f projectionMatrix;
private static final float fov = 70;
private static final float near = 0.1f;
private static final float far = 1000f;
private Matrix4f orthoProjection;
public Renderer(Display display, Shader shader) { public Renderer(Display display, Shader shader) {
this.shader = shader; this.shader = shader;
this.display = display; this.display = display;
this.projectionMatrix = Matrix4f.perspective(fov, this.display.getAspectRatio(), near, far);
} }
public void renderMesh(GameObject object, Camera camera) { public void renderObject3D(GameObject object, Camera camera) {
// Renders the mesh by drawing it using triangles (least complicated) // Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject()); GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
@ -35,7 +42,7 @@ public class Renderer {
shader.bind(); shader.bind();
shader.setUniform("type", object.getMesh().isType()); shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", display.getProjectionMatrix()); shader.setUniform("projection", this.projectionMatrix);
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation())); shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale())); shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
@ -48,4 +55,66 @@ public class Renderer {
GL30.glBindVertexArray(0); GL30.glBindVertexArray(0);
} }
public void renderScene3D(Scene scene, Camera camera) {
for (GameObject object: scene.getObjects()) {
GL30.glBindVertexArray(object.getMesh().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());
shader.bind();
shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", this.projectionMatrix);
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
shader.unbind();
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}
}
public void renderObject2D(GameObject object, Camera camera, float top, float bottom, float left, float right) {
orthoProjection = Matrix4f.orthoProjection(right, left, bottom, top, near, far);
// Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(object.getMesh().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());
shader.bind();
shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", this.orthoProjection);
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
shader.unbind();
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}
} }

View File

@ -0,0 +1,40 @@
package org.hl.engine.graphics;
import org.hl.engine.objects.GameObject;
import java.util.ArrayList;
import java.util.Arrays;
public class Scene {
private ArrayList<GameObject> objects;
public Scene() {
objects = new ArrayList();
}
public ArrayList<GameObject> getObjects() {
return objects;
}
public void setObjects(ArrayList<GameObject> objects) {
this.objects = objects;
}
public void setObjects(GameObject[] objects) {
this.objects.addAll(Arrays.asList(objects));
}
public void addObject(GameObject object) {
this.objects.add(object);
}
public void removeObject(int index) {
this.objects.remove(index);
}
public GameObject getObject(int index) {
return this.objects.get(index);
}
}

View File

@ -17,8 +17,10 @@ public class Shader {
private int vertexID, fragmentID, programID; private int vertexID, fragmentID, programID;
public static final String VERTEXSHADER = "/resources/shaders/mainVertex.glsl"; public static final String VERTEX_SHADER_NO_LIGHT = "/resources/shaders/mainVertex.glsl";
public static final String FRAGSHADER = "/resources/shaders/mainFragment.glsl"; public static final String FRAG_SHADER_NO_LIGHT = "/resources/shaders/mainFragment.glsl";
public static final int NO_LIGHT = 0;
public Shader(String vertexPath, String fragmentPath) { public Shader(String vertexPath, String fragmentPath) {
vertexFile = FileUtils.loadAsString(vertexPath); vertexFile = FileUtils.loadAsString(vertexPath);
@ -26,6 +28,13 @@ public class Shader {
} }
public Shader(int type) {
if (type == 0) {
vertexFile = FileUtils.loadAsString(VERTEX_SHADER_NO_LIGHT);
fragmentFile = FileUtils.loadAsString(FRAG_SHADER_NO_LIGHT);
}
}
public void create() { public void create() {
// Creates the program // Creates the program

View File

@ -30,7 +30,6 @@ public class Display {
private int savedPosY; private int savedPosY;
private int savedWidth; private int savedWidth;
private int savedHeight; private int savedHeight;
private Matrix4f projection;
private boolean isLocked; private boolean isLocked;
private double[] cursorX = new double[1]; private double[] cursorX = new double[1];
private double[] cursorY = new double[1]; private double[] cursorY = new double[1];
@ -40,11 +39,14 @@ public class Display {
// Constructor to create the display // 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.width = width;
this.height = height; this.height = height;
this.windowName = windowName; this.windowName = windowName;
projection = Matrix4f.projection(fov, (float)this.width / (float) this.height, near, far); }
public float getAspectRatio() {
return (float)this.width / (float)this.height;
} }
// Change the window name // Change the window name
@ -74,10 +76,6 @@ public class Display {
return isFullscreen; return isFullscreen;
} }
public Matrix4f getProjectionMatrix() {
return projection;
}
// Makes the screen fullscreen or not based on the argument // Makes the screen fullscreen or not based on the argument
public void setFullscreen(boolean fullscreen) { public void setFullscreen(boolean fullscreen) {
isFullscreen = fullscreen; isFullscreen = fullscreen;
@ -124,6 +122,7 @@ public class Display {
public boolean isResized() { public boolean isResized() {
return isResized; return isResized;
} }

View File

@ -108,7 +108,7 @@ public class Matrix4f {
return result; return result;
} }
public static Matrix4f projection( float fov, float aspectRatio, float near, float far) { public static Matrix4f perspective( float fov, float aspectRatio, float near, float far) {
Matrix4f result = Matrix4f.identity(); Matrix4f result = Matrix4f.identity();
float tan = (float)Math.tan(Math.toRadians(fov / 2)); float tan = (float)Math.tan(Math.toRadians(fov / 2));
@ -124,6 +124,17 @@ public class Matrix4f {
return result; return result;
} }
public static Matrix4f orthoProjection(float right, float left, float bottom, float top, float near, float far) {
Matrix4f result = Matrix4f.identity();
result.set(0, 0, 2/(right - left));
result.set(1, 1, 2/(top - bottom));
result.set(2, 2, -2/(far - near));
result.set(0, 3, -(right + left) / (right - left));
result.set(1, 3, -(top + bottom) / (top - bottom));
result.set(2, 3, -(far + near) / (far - near));
return result;
}
public static Matrix4f view(Vector3f position, Vector3f rotation) { public static Matrix4f view(Vector3f position, Vector3f rotation) {
Vector3f negative = new Vector3f(-position.getX(), -position.getY(), -position.getZ()); Vector3f negative = new Vector3f(-position.getX(), -position.getY(), -position.getZ());