Add BoundingBox interface to Camera
This commit is contained in:
@@ -4,10 +4,11 @@ import com.bartlomiejpluta.base.api.context.Context;
|
|||||||
import com.bartlomiejpluta.base.api.move.Movable;
|
import com.bartlomiejpluta.base.api.move.Movable;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||||
|
import com.bartlomiejpluta.base.internal.render.BoundingBox;
|
||||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||||
import org.joml.Matrix4fc;
|
import org.joml.Matrix4fc;
|
||||||
|
|
||||||
public interface Camera extends Placeable {
|
public interface Camera extends Placeable, BoundingBox {
|
||||||
Matrix4fc computeViewModelMatrix(Matrix4fc modelMatrix);
|
Matrix4fc computeViewModelMatrix(Matrix4fc modelMatrix);
|
||||||
|
|
||||||
boolean insideFrustum(float x, float y, float radius);
|
boolean insideFrustum(float x, float y, float radius);
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.bartlomiejpluta.base.internal.render;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public interface BoundingBox {
|
||||||
|
float getMinX();
|
||||||
|
|
||||||
|
float getMaxX();
|
||||||
|
|
||||||
|
float getMinY();
|
||||||
|
|
||||||
|
float getMaxY();
|
||||||
|
|
||||||
|
default boolean containsBox(float minX, float maxX, float minY, float maxY) {
|
||||||
|
return !(this.getMaxX() < minX ||
|
||||||
|
this.getMinX() > maxX ||
|
||||||
|
this.getMaxY() < minY ||
|
||||||
|
this.getMinY() > maxY);
|
||||||
|
}
|
||||||
|
|
||||||
|
default boolean containsBox(BoundingBox box) {
|
||||||
|
return !(this.getMaxX() < box.getMinX() ||
|
||||||
|
this.getMinX() > box.getMaxX() ||
|
||||||
|
this.getMaxY() < box.getMinY() ||
|
||||||
|
this.getMinY() > box.getMaxY());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import com.bartlomiejpluta.base.api.screen.Screen;
|
|||||||
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
||||||
import com.bartlomiejpluta.base.engine.world.object.Model;
|
import com.bartlomiejpluta.base.engine.world.object.Model;
|
||||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||||
|
import lombok.Getter;
|
||||||
import org.joml.FrustumIntersection;
|
import org.joml.FrustumIntersection;
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.joml.Matrix4fc;
|
import org.joml.Matrix4fc;
|
||||||
@@ -16,6 +17,18 @@ public class DefaultCamera extends Model implements Camera {
|
|||||||
private final Matrix4f projectionViewMatrix = new Matrix4f();
|
private final Matrix4f projectionViewMatrix = new Matrix4f();
|
||||||
private final FrustumIntersection frustum = new FrustumIntersection();
|
private final FrustumIntersection frustum = new FrustumIntersection();
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private float minX = 0f;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private float maxX = 0f;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private float minY = 0f;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private float maxY = 0f;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Matrix4fc computeViewModelMatrix(Matrix4fc modelMatrix) {
|
public Matrix4fc computeViewModelMatrix(Matrix4fc modelMatrix) {
|
||||||
return new Matrix4f(viewMatrix).mul(modelMatrix);
|
return new Matrix4f(viewMatrix).mul(modelMatrix);
|
||||||
@@ -35,10 +48,13 @@ public class DefaultCamera extends Model implements Camera {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(Screen screen, ShaderManager shaderManager) {
|
public void render(Screen screen, ShaderManager shaderManager) {
|
||||||
|
var screenWidth = screen.getWidth();
|
||||||
|
var screenHeight = screen.getHeight();
|
||||||
|
|
||||||
// Update matrices
|
// Update matrices
|
||||||
projectionMatrix
|
projectionMatrix
|
||||||
.identity()
|
.identity()
|
||||||
.setOrtho2D(0, screen.getWidth(), screen.getHeight(), 0);
|
.setOrtho2D(0, screenWidth, screenHeight, 0);
|
||||||
|
|
||||||
viewMatrix
|
viewMatrix
|
||||||
.identity()
|
.identity()
|
||||||
@@ -51,6 +67,11 @@ public class DefaultCamera extends Model implements Camera {
|
|||||||
|
|
||||||
frustum.set(projectionViewMatrix);
|
frustum.set(projectionViewMatrix);
|
||||||
|
|
||||||
|
this.minX = position.x;
|
||||||
|
this.maxX = position.x + screenWidth / scaleX;
|
||||||
|
this.minY = position.y;
|
||||||
|
this.maxY = position.y + screenHeight / scaleY;
|
||||||
|
|
||||||
shaderManager.setUniform(UniformName.UNI_PROJECTION_MATRIX, projectionMatrix);
|
shaderManager.setUniform(UniformName.UNI_PROJECTION_MATRIX, projectionMatrix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user