Skip to content

Commit

Permalink
More render mode changes
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotjarnit committed Jan 17, 2024
1 parent 6a8c5ec commit d5fca52
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/main/java/dev/elliotjarnit/elliotengine/ElliotEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ public enum Options {

public enum AdvancedOptions {
MAX_CLIPPING_VERTEXES,
WIRE_FRAME,
}

private final HashMap<Options, String> options = new HashMap<Options, String>() {{
Expand All @@ -227,6 +226,5 @@ public enum AdvancedOptions {
private final HashMap<AdvancedOptions, String> advancedOptions = new HashMap<AdvancedOptions, String>() {{
// Default advanced options
put(AdvancedOptions.MAX_CLIPPING_VERTEXES, "20");
put(AdvancedOptions.WIRE_FRAME, "false");
}};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,47 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

public class RenderingEngine extends JPanel {
private EScene scene;
private EOverlay overlay;
private final ElliotEngine engine;
private double lastFrameTime = System.nanoTime();
private int fps = 0;
public enum ProjectionMode {
PERSPECTIVE,
ORTHOGRAPHIC
}
private ProjectionMode projectionMode = ProjectionMode.PERSPECTIVE;
public enum RenderMode {
WIREFRAME,
SOLID,
BOTH
}
private RenderMode renderMode = RenderMode.SOLID;

public RenderingEngine(ElliotEngine engine) {
super();
this.engine = engine;
}

public void setProjectionMode(ProjectionMode mode) {
this.projectionMode = mode;
}

public ProjectionMode getProjectionMode() {
return this.projectionMode;
}

public void setRenderMode(RenderMode mode) {
this.renderMode = mode;
}

public RenderMode getRenderMode() {
return this.renderMode;
}

// JPanel built-in paint function
// Start of rendering frame
@Override
Expand Down Expand Up @@ -98,9 +126,12 @@ public void renderScene(EScene scene, Graphics2D g2d) {
// Don't render face if point is missing
if (facePoints[0] == null || facePoints[1] == null || facePoints[2] == null) continue;

if (this.engine.getOption(ElliotEngine.AdvancedOptions.WIRE_FRAME).equals("false")) {
if (renderMode == RenderMode.SOLID) {
this.renderFaceInWorld(img, zBuffer, facePoints[0], facePoints[1], facePoints[2], face.getColor());
} else if (renderMode == RenderMode.WIREFRAME) {
this.renderFaceInWorldWireframe(g2d, facePoints[0], facePoints[1], facePoints[2]);
} else if (renderMode == RenderMode.BOTH) {
this.renderFaceInWorld(img, zBuffer, facePoints[0], facePoints[1], facePoints[2], face.getColor());
} else {
this.renderFaceInWorldWireframe(g2d, facePoints[0], facePoints[1], facePoints[2]);
}
}
Expand All @@ -118,7 +149,14 @@ public Vector3 worldToCameraSpace(Vector3 worldSpace, EScene scene) {
}

public Vector3 cameraToScreenSpace(Vector3 cameraSpace, EScene scene) {
Vector3 screenSpace = scene.getCamera().getPerspectiveProjectionMatrix((double) getWidth() / (double) getHeight()).transform(cameraSpace);
Vector3 screenSpace;
if (projectionMode == ProjectionMode.PERSPECTIVE) {
screenSpace = scene.getCamera().getPerspectiveProjectionMatrix((double) getWidth() / (double) getHeight()).transform(cameraSpace);
} else if (projectionMode == ProjectionMode.ORTHOGRAPHIC) {
screenSpace = scene.getCamera().getOrthographicProjectionMatrix((double) getWidth() / (double) getHeight()).transform(cameraSpace);
} else {
screenSpace = new Vector3();
}

screenSpace.x = (screenSpace.x + 1.0) * 0.5 * getWidth();
screenSpace.y = (1.0 - screenSpace.y) * 0.5 * getHeight();
Expand Down Expand Up @@ -196,28 +234,15 @@ public int getFPS() {
}

public EObject getObjectAtPoint(Vector2 point) {
for (EObject object : this.scene.getObjects()) {
for (EFace face : object.getFaces()) {
Vector3[] facePoints = new Vector3[3];
for (int i = 0; i < 3; i++) {
Vector3 objectSpace = face.getVertices()[i];
Vector3 worldSpace = this.objectToWorldSpace(objectSpace, object);
Vector3 cameraSpace = this.worldToCameraSpace(worldSpace, this.scene);
Vector3 screenSpace = this.cameraToScreenSpace(cameraSpace, this.scene);
facePoints[i] = screenSpace;
}

// Don't render face if point is missing
if (facePoints[0] == null || facePoints[1] == null || facePoints[2] == null) continue;

if (this.pointInTriangle(new Vector3(point.x, point.y, 0), facePoints[0], facePoints[1], facePoints[2])) {
return object;
}
}
}
return null;
}

public EObject getObjectLookingAt() {
// Calcuate middle of screen
Vector2 middle = new Vector2(this.getWidth() / 2, this.getHeight() / 2);
return this.getObjectAtPoint(middle);
}

private boolean pointInTriangle(Vector3 p, Vector3 A, Vector3 B, Vector3 C){
return sameSide(A,B,C,p) && sameSide(B,C,A,p) && sameSide(C,A,B,p);
}
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/dev/elliotjarnit/elliotengine/Objects/ECamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public void moveForward(double distance) {
this.addOrigin(change);
}

public void moveBackward(double distance) {
Matrix4 rotationMatrix = this.getRotationMatrix();
Vector3 forwardVector = new Vector3(rotationMatrix.get(0, 2), rotationMatrix.get(1, 2), rotationMatrix.get(2, 2));
Vector3 change = forwardVector.mul(-distance);
change.y = 0;
this.addOrigin(change);
}

public void moveRight(double distance) {
Matrix4 rotationMatrix = this.getRotationMatrix();
Vector3 forwardVector = new Vector3(rotationMatrix.get(0, 2), rotationMatrix.get(1, 2), rotationMatrix.get(2, 2));
Expand All @@ -82,6 +90,33 @@ public void moveRight(double distance) {
this.addOrigin(change);
}

public void moveLeft(double distance) {
Matrix4 rotationMatrix = this.getRotationMatrix();
Vector3 forwardVector = new Vector3(rotationMatrix.get(0, 2), rotationMatrix.get(1, 2), rotationMatrix.get(2, 2));
Vector3 rightVector = forwardVector.cross(new Vector3(0, 1, 0));
Vector3 change = rightVector.mul(-distance);
change.y = 0;
this.addOrigin(change);
}

public void moveUp(double distance) {
Matrix4 rotationMatrix = this.getRotationMatrix();
Vector3 upVector = new Vector3(rotationMatrix.get(0, 1), rotationMatrix.get(1, 1), rotationMatrix.get(2, 1));
Vector3 change = upVector.mul(distance);
change.x = 0;
change.z = 0;
this.addOrigin(change);
}

public void moveDown(double distance) {
Matrix4 rotationMatrix = this.getRotationMatrix();
Vector3 upVector = new Vector3(rotationMatrix.get(0, 1), rotationMatrix.get(1, 1), rotationMatrix.get(2, 1));
Vector3 change = upVector.mul(-distance);
change.x = 0;
change.z = 0;
this.addOrigin(change);
}

public Matrix4 getPerspectiveProjectionMatrix(double aspectRatio) {
double far = this.getRenderDistance();
double near = this.getNearDistance();
Expand All @@ -99,6 +134,23 @@ public Matrix4 getPerspectiveProjectionMatrix(double aspectRatio) {
});
}

public Matrix4 getOrthographicProjectionMatrix(double aspectRatio) {
double far = this.getRenderDistance();
double near = this.getNearDistance();

double t = Math.tan(this.fov * 0.5 * Math.PI / 180) * near;
double r = aspectRatio * t;
double l = -r;
double b = -t;

return new Matrix4(new double[] {
2 / (r - l), 0, 0, -(r + l) / (r - l),
0, 2 / (t - b), 0, -(t + b) / (t - b),
0, 0, -2 / (far - near), -(far + near) / (far - near),
0, 0, 0, 1
});
}

public Matrix4 getCameraRotationMatrix() {
Vector2 rotation = new Vector2(this.getRotationRadians());

Expand Down
36 changes: 34 additions & 2 deletions src/test/java/BasicCubeDraw/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.elliotjarnit.elliotengine.ElliotEngine;
import dev.elliotjarnit.elliotengine.Graphics.EColor;
import dev.elliotjarnit.elliotengine.Graphics.RenderingEngine;
import dev.elliotjarnit.elliotengine.Objects.*;
import dev.elliotjarnit.elliotengine.Utils.Matrix4;
import dev.elliotjarnit.elliotengine.Utils.Vector2;
Expand Down Expand Up @@ -31,6 +32,7 @@ public void optionSetup() {
@Override
public void setup() {
this.inputManager.takeoverMouse();
this.renderer.setProjectionMode(RenderingEngine.ProjectionMode.PERSPECTIVE);

EScene mainScene = new EScene(false);
playerCamera = new ECamera(new Vector3(0.1, 2, -10), 60.0);
Expand Down Expand Up @@ -64,16 +66,46 @@ public void loop() {
}

if (this.inputManager.isKeyDown(InputManager.Key.S)) {
playerCamera.moveForward(-0.5);
playerCamera.moveBackward(0.5);
}
if (this.inputManager.isKeyDown(InputManager.Key.A)) {
playerCamera.moveRight(-0.5);
playerCamera.moveLeft(0.5);
}

if (this.inputManager.isKeyDown(InputManager.Key.D)) {
playerCamera.moveRight(0.5);
}

if (this.inputManager.isKeyDown(InputManager.Key.SPACE)) {
playerCamera.moveUp(0.5);
}

if (this.inputManager.isKeyDown(InputManager.Key.SHIFT)) {
playerCamera.moveDown(0.5);
}

if (this.inputManager.isKeyDown(InputManager.Key.R)) {
if (this.renderer.getProjectionMode() == RenderingEngine.ProjectionMode.PERSPECTIVE) {
this.renderer.setProjectionMode(RenderingEngine.ProjectionMode.ORTHOGRAPHIC);
} else {
this.renderer.setProjectionMode(RenderingEngine.ProjectionMode.PERSPECTIVE);
}
}

if (this.inputManager.isKeyDown(InputManager.Key.T)) {
if (this.renderer.getRenderMode() == RenderingEngine.RenderMode.WIREFRAME) {
this.renderer.setRenderMode(RenderingEngine.RenderMode.SOLID);
} else {
this.renderer.setRenderMode(RenderingEngine.RenderMode.WIREFRAME);
}
}

if (this.inputManager.isKeyDown(InputManager.Key.ESCAPE)) {
this.stop();
}



if (this.inputManager.isMouseDown(InputManager.MouseButton.LEFT)) {
System.out.println("Left mouse button down");
Vector2 mousePos = this.inputManager.getMousePos();
Expand Down

0 comments on commit d5fca52

Please sign in to comment.