Skip to content

Commit

Permalink
-Create, place and edit multiple navmeshs on world
Browse files Browse the repository at this point in the history
-Added prepass and render pass for every components
  • Loading branch information
PierreEVEN committed Nov 16, 2020
1 parent 7e49678 commit c0b72fe
Show file tree
Hide file tree
Showing 21 changed files with 294 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Coffee3D.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="imgui-binding-1.78-1.3.0-javadoc" level="project" />
<orderEntry type="library" name="lwjgl-assimp-javadoc" level="project" />
<orderEntry type="library" name="pngdecoder-0.13" level="project" />
<orderEntry type="library" name="joml-sources" level="project" />
<orderEntry type="library" name="joml-1.9.25-javadoc" level="project" />
</component>
</module>
Binary file modified gameContent/assets/world/defaultWorld.map
Binary file not shown.
2 changes: 1 addition & 1 deletion imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Collapsed=0
DockId=0x0000000A,1

[Window][statistics]
Pos=188,212
Pos=57,148
Size=628,651
Collapsed=0

Expand Down
2 changes: 1 addition & 1 deletion out/production/Coffee3D/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: coffee3D.MainGame
Main-Class: coffee3D.Main

2 changes: 1 addition & 1 deletion src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: coffee3D.MainGame
Main-Class: coffee3D.Main

10 changes: 10 additions & 0 deletions src/coffee3D/core/maths/MathLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ public static double GetSegmentDistanceToLine(Vector3f lineOrigin, Vector3f line

return distance;
}

public static void LinePlaneIntersection(Vector3f planePoint, Vector3f planeNormal, Vector3f lineDirection, Vector3f lineOrigin, Vector3f hitPoint) {
dir1.set(lineDirection).normalize();
if (planeNormal.dot(dir1) == 0) {
hitPoint.set(0);
return;
}
float t = (planeNormal.dot(planePoint) - planeNormal.dot(lineOrigin)) / planeNormal.dot(dir1);
hitPoint.set(lineDirection).mul(t).add(lineOrigin);
}
}
35 changes: 22 additions & 13 deletions src/coffee3D/core/navigation/NavMeshGrid.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
package coffee3D.core.navigation;

import coffee3D.core.io.log.Log;
import org.joml.Vector2i;
import org.joml.Vector3f;

import java.io.Serializable;
import java.util.ArrayList;

public class NavMeshGrid {
public class NavMeshGrid implements Serializable {

private static final long serialVersionUID = -4528707062414566942L;

private final NavmeshPoint[] _navMesh;
private final Vector3f _navmeshLocation;
private final Vector2i _size;
private final float _cellSize;


public NavMeshGrid(Vector3f navMeshLocation, NavmeshPoint[] navMesh, Vector2i navmeshSize, float cellSize) {
_navMesh = navMesh;
public NavMeshGrid(Vector3f navMeshLocation, Vector2i navmeshSize, float cellSize) {
_navMesh = new NavmeshPoint[navmeshSize.x * navmeshSize.y];
_navmeshLocation = navMeshLocation;
_size = navmeshSize;
_cellSize = cellSize;
if (navMesh.length != navmeshSize.x * navmeshSize.y) Log.Fail("invalid navmesh values");
for (int i = 0; i < _navMesh.length; ++i) {
_navMesh[i].location.set(i % _size.x, i / _size.y);
_navMesh[i] = new NavmeshPoint();
_navMesh[i].location.set(i % _size.x, i / _size.x);
_navMesh[i].isNavigable = false;
}
}

public Vector3f getNavmeshLocation() { return _navmeshLocation; }

public final NavmeshPoint[] getNavmesh() {
return _navMesh;
}

public float getCellSize() { return _cellSize; }
public Vector2i getGridSize() { return _size; }

public NavigationPath findPathToLocation(Vector3f from, Vector3f to) {
NavmeshPoint start = findClosestNavmeshPoint(from);
NavmeshPoint end = findClosestNavmeshPoint(to);
ArrayList<Vector3f> pathPoints = new ArrayList<>();


return new NavigationPath(null);
}

Expand All @@ -56,15 +65,15 @@ public NavmeshPoint findClosestNavmeshPoint(Vector3f worldPosition) {

public void worldToLocal(Vector3f world, Vector2i local) {
local.set(
(int) ((world.x - _navmeshLocation.x) * _cellSize),
(int) ((world.y - _navmeshLocation.y) * _cellSize)
(int) ((world.x - _navmeshLocation.x) / _cellSize + _cellSize / 2),
(int) ((world.y - _navmeshLocation.y) / _cellSize + _cellSize / 2)
);
}

public void localToWorld(Vector2i local, Vector3f world) {
world.set(
(local.x / _cellSize) + _navmeshLocation.x,
(local.y / _cellSize) + _navmeshLocation.y,
(local.x * _cellSize) + _navmeshLocation.x,
(local.y * _cellSize) + _navmeshLocation.y,
_navmeshLocation.z
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/coffee3D/core/navigation/NavigableActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
public class NavigableActor extends SceneComponent {


private static final long serialVersionUID = 2644030132802605634L;

public NavigableActor(Vector3f position, Quaternionf rotation, Vector3f scale) {
super(position, rotation, scale);
}
Expand Down
161 changes: 161 additions & 0 deletions src/coffee3D/core/navigation/NavmeshComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package coffee3D.core.navigation;

import coffee3D.core.maths.MathLibrary;
import coffee3D.core.renderer.RenderMode;
import coffee3D.core.renderer.RenderUtils;
import coffee3D.core.renderer.Window;
import coffee3D.core.renderer.debug.DebugRenderer;
import coffee3D.core.renderer.scene.RenderScene;
import coffee3D.core.renderer.scene.Scene;
import coffee3D.core.renderer.scene.SceneComponent;
import coffee3D.core.types.Color;
import coffee3D.core.types.SphereBound;
import coffee3D.core.types.TypeHelper;
import org.joml.Quaternionf;
import org.joml.Vector2i;
import org.joml.Vector3f;
import org.lwjgl.glfw.GLFW;

import static org.lwjgl.opengl.GL11.*;

public class NavmeshComponent extends SceneComponent {

private static final long serialVersionUID = -4773537143224102110L;

protected int _sizeX;
protected int _sizeY;
protected float _cellSize;
private NavMeshGrid _navmesh;
private transient SphereBound bounds;

public NavmeshComponent(Vector3f position, Quaternionf rotation, Vector3f scale) {
super(position, rotation, scale);
_sizeX = 100;
_sizeY = 40;
_cellSize = 1f;
newNavmesh();
}

@Override
protected void draw(Scene context) {}

@Override
public void postDraw(Scene context) {
super.draw(context);

if (_cellSize != _navmesh.getCellSize() ||
_sizeX != _navmesh.getGridSize().x ||
_sizeY != _navmesh.getGridSize().y
) newNavmesh();

_navmesh.getNavmeshLocation().set(getWorldPosition());
if (isSelected()) {
visualize((RenderScene) context);
edit((RenderScene) context);
}
}


@Override
public SphereBound getBound() {
if (bounds == null) {
bounds = new SphereBound();
}
bounds.radius = _cellSize * Math.max(_sizeX, _sizeY);
bounds.position.set(getWorldPosition());
return bounds;
}

private void newNavmesh() {
_navmesh = new NavMeshGrid(getWorldPosition(), new Vector2i(_sizeX, _sizeY), _cellSize);
}

private transient Vector3f _center;
private transient Vector3f _worldPosA;
private transient Vector3f _worldPosB;
private transient Vector3f _worldPosC;
private transient Vector3f _worldPosD;
private transient Color navigableColor;
private transient Color notNavigableColor;
private transient Color selectionColor;

private void visualize(RenderScene context) {

if (_center == null) _center = new Vector3f();
if (_worldPosA == null) _worldPosA = new Vector3f();
if (_worldPosB == null) _worldPosB = new Vector3f();
if (_worldPosC == null) _worldPosC = new Vector3f();
if (_worldPosD == null) _worldPosD = new Vector3f();
if (navigableColor == null) navigableColor = new Color(0, 1, 0, 0.8f);
if (notNavigableColor == null) notNavigableColor = new Color(1, 0, 0, 0.2f);
if (selectionColor == null) selectionColor = new Color(1, 1, 0, 0.5f);

if (RenderUtils.RENDER_MODE == RenderMode.Select) {
RenderUtils.getPickMaterialDrawList()[0].use(context);
RenderUtils.getPickMaterialDrawList()[0].getResource().setIntParameter("pickId", getComponentIndex() + 1);
RenderUtils.getPickMaterialDrawList()[0].getResource().setModelMatrix(TypeHelper.getMat4().identity());

_worldPosA.set(getWorldPosition());
_worldPosB.set(_worldPosA).add(_sizeX * _cellSize, 0, 0);
_worldPosC.set(_worldPosA).add(_sizeX * _cellSize, _sizeY * _cellSize, 0);
_worldPosD.set(_worldPosA).add(0, _sizeY * _cellSize, 0);

glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUADS);
{
glVertex3f(_worldPosA.x, _worldPosA.y, _worldPosA.z);
glVertex3f(_worldPosB.x, _worldPosB.y, _worldPosB.z);
glVertex3f(_worldPosC.x, _worldPosC.y, _worldPosC.z);
glVertex3f(_worldPosD.x, _worldPosD.y, _worldPosD.z);
}
glEnd();
}

if (RenderUtils.RENDER_MODE != RenderMode.Color) return;

DebugRenderer.DrawDebugBox(context, getWorldPosition(), _worldPosA.set(getWorldPosition()).add(_sizeX * _cellSize, _sizeY * _cellSize, 0), Color.BLUE);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float halfGridSize = _navmesh.getCellSize() / 2;
RenderUtils.getDebugMaterial().getResource().setModelMatrix(TypeHelper.getMat4().identity());

for (NavmeshPoint _points : _navmesh.getNavmesh()) {
_navmesh.localToWorld(_points.location, _center);
_worldPosA.set(_center).add(-halfGridSize, -halfGridSize, 0);
_worldPosB.set(_center).add(halfGridSize, -halfGridSize, 0);
_worldPosC.set(_center).add(halfGridSize, halfGridSize, 0);
_worldPosD.set(_center).add(-halfGridSize, halfGridSize, 0);

RenderUtils.getDebugMaterial().use(context);
RenderUtils.getDebugMaterial().setColor(_points.isNavigable ? navigableColor : notNavigableColor);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUADS);
{
glVertex3f(_worldPosA.x, _worldPosA.y, _worldPosA.z);
glVertex3f(_worldPosB.x, _worldPosB.y, _worldPosB.z);
glVertex3f(_worldPosC.x, _worldPosC.y, _worldPosC.z);
glVertex3f(_worldPosD.x, _worldPosD.y, _worldPosD.z);
}
glEnd();
}
glDisable(GL_BLEND);
}

private static final Vector3f Inter = new Vector3f();
private static final Vector3f up = new Vector3f(0, 0, 1);
private static final Vector3f camDir = new Vector3f(0);
private static final Vector2i local = new Vector2i(0);

private void edit(RenderScene scene) {

if (GLFW.glfwGetMouseButton(Window.GetPrimaryWindow().getGlfwWindowHandle(), GLFW.GLFW_MOUSE_BUTTON_1) == GLFW.GLFW_PRESS) {
scene.getCursorSceneDirection(camDir);
MathLibrary.LinePlaneIntersection(getWorldPosition(), up, camDir, scene.getCamera().getWorldPosition(), Inter);
_navmesh.worldToLocal(Inter, local);
if (_navmesh.getPoint(local) != null) {
_navmesh.getPoint(local).isNavigable = GLFW.glfwGetKey(Window.GetPrimaryWindow().getGlfwWindowHandle(), GLFW.GLFW_KEY_LEFT_SHIFT) != GLFW.GLFW_PRESS;
}
}
}
}
5 changes: 4 additions & 1 deletion src/coffee3D/core/navigation/NavmeshPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.joml.Vector2i;

public class NavmeshPoint {
import java.io.Serializable;

public class NavmeshPoint implements Serializable {
private static final long serialVersionUID = 6011416635037746495L;
public boolean isNavigable = false;
public final Vector2i location = new Vector2i();
}
14 changes: 0 additions & 14 deletions src/coffee3D/core/navigation/navmeshComponent.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/coffee3D/core/renderer/scene/DrawList.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ private void sortDrawList() {
});
}

public void preRender(RenderScene context) {
for (SceneComponent component : frustumDrawList) component.preDrawInternal(context);
}

public void render(RenderScene context) {
for (SceneComponent component : frustumDrawList) component.drawInternal(context);
}

public void postRender(RenderScene context) {
for (SceneComponent component : frustumDrawList) component.postDrawInternal(context);
}
}
6 changes: 4 additions & 2 deletions src/coffee3D/core/renderer/scene/HitResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ protected void update(RenderScene scene, Framebuffer pickBuffer) {
drawList.build(scene.getComponents(), viewMatrix, Scene.getProjection(pickBuffer.getWidth(), pickBuffer.getHeight(), scene.getCamera()));


RenderUtils.CheckGLErrors();
drawList.preRender(scene);
glClear(GL_DEPTH_BUFFER_BIT);
drawList.render(scene);
RenderUtils.CheckGLErrors();
glClear(GL_DEPTH_BUFFER_BIT);
drawList.postRender(scene);

glReadPixels(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, _hitColor);
glReadPixels(0, 0, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, _hitDepth);
Expand Down
9 changes: 8 additions & 1 deletion src/coffee3D/core/renderer/scene/RenderScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public boolean renderScene() {
glDisable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glFrontFace(GL_CW);
_drawList.preRender(this);
glClear(GL_DEPTH_BUFFER_BIT);
_drawList.render(this);
glClear(GL_DEPTH_BUFFER_BIT);
_drawList.postRender(this);
}
else if (_sceneSettings.hasStencilBuffer()) _stencilBuffer.use(this);

Expand Down Expand Up @@ -184,9 +188,12 @@ public void preDraw() {
skyBoxMesh.setMaterial(getSkyboxMaterial(), 0);
skyBoxMesh.drawInternal(this);
}
_drawList.preRender(this);
}

public void postDraw() {}
public void postDraw() {
_drawList.postRender(this);
}

public void resizeBuffers(int sizeX, int sizeY) {
if (_colorBuffer != null) _colorBuffer.resizeFramebuffer(sizeX, sizeY);
Expand Down
Loading

0 comments on commit c0b72fe

Please sign in to comment.