Mousepicker with offset in selection
This commit is contained in:
parent
fb3d4e3685
commit
8b8497b53b
@ -138,14 +138,14 @@ public class Engine {
|
|||||||
MousePicker mousePicker = new MousePicker(camera, renderer.getProjectionMatrix(), terrain);
|
MousePicker mousePicker = new MousePicker(camera, renderer.getProjectionMatrix(), terrain);
|
||||||
input(camera, renderer, mousePicker);
|
input(camera, renderer, mousePicker);
|
||||||
while ( !glfwWindowShouldClose(window) ) {
|
while ( !glfwWindowShouldClose(window) ) {
|
||||||
DoubleBuffer posX = BufferUtils.createDoubleBuffer(1);
|
|
||||||
DoubleBuffer posY = BufferUtils.createDoubleBuffer(1);
|
|
||||||
glfwGetCursorPos(window, posX, posY);
|
|
||||||
mousePicker.update((float) posX.get(0), (float) posY.get(0));
|
|
||||||
renderer.processTerrain(terrain);
|
renderer.processTerrain(terrain);
|
||||||
|
|
||||||
//renderer.processEntity(hexagonEntity);
|
//renderer.processEntity(hexagonEntity);
|
||||||
renderer.render(light, camera);
|
renderer.render(light, camera);
|
||||||
|
DoubleBuffer posX = BufferUtils.createDoubleBuffer(1);
|
||||||
|
DoubleBuffer posY = BufferUtils.createDoubleBuffer(1);
|
||||||
|
glfwGetCursorPos(window, posX, posY);
|
||||||
|
mousePicker.update((float) posX.get(0), (float) posY.get(0));
|
||||||
glfwSwapBuffers(window); // swap the color buffers
|
glfwSwapBuffers(window); // swap the color buffers
|
||||||
// Poll for window events. The key callback above will only be
|
// Poll for window events. The key callback above will only be
|
||||||
// invoked during this call.
|
// invoked during this call.
|
||||||
@ -187,7 +187,7 @@ public class Engine {
|
|||||||
glfwGetCursorPos(window, posX, posY);
|
glfwGetCursorPos(window, posX, posY);
|
||||||
System.out.println(posX.get(0) + "|" + posY.get(0));
|
System.out.println(posX.get(0) + "|" + posY.get(0));
|
||||||
mousePicker.update((float) posX.get(0), (float) posY.get(0));
|
mousePicker.update((float) posX.get(0), (float) posY.get(0));
|
||||||
System.out.println(mousePicker.getCurrentRay());
|
mousePicker.calcIntersectingTerrainTile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -4,7 +4,7 @@ import utils.vectors.Vector3f;
|
|||||||
|
|
||||||
public class Camera {
|
public class Camera {
|
||||||
|
|
||||||
private Vector3f position = new Vector3f(0,37,-1);
|
private Vector3f position = new Vector3f(0, 37,0);
|
||||||
private float pitch = 90;
|
private float pitch = 90;
|
||||||
private float yaw = 180;
|
private float yaw = 180;
|
||||||
private float roll;
|
private float roll;
|
||||||
|
@ -85,4 +85,12 @@ public class TerrainTile {
|
|||||||
public void setColor(Vector3f color) {
|
public void setColor(Vector3f color) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getRow() {
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColumn() {
|
||||||
|
return column;
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,9 +10,6 @@ import utils.vectors.*;
|
|||||||
|
|
||||||
public class MousePicker {
|
public class MousePicker {
|
||||||
|
|
||||||
private static final int RECURSION_COUNT = 200;
|
|
||||||
private static final float RAY_RANGE = 600;
|
|
||||||
|
|
||||||
private Vector3f currentRay = new Vector3f();
|
private Vector3f currentRay = new Vector3f();
|
||||||
|
|
||||||
private Matrix4f projectionMatrix;
|
private Matrix4f projectionMatrix;
|
||||||
@ -42,13 +39,21 @@ public class MousePicker {
|
|||||||
viewMatrix = MatrixGraphicUtils.createViewMatrix(camera);
|
viewMatrix = MatrixGraphicUtils.createViewMatrix(camera);
|
||||||
currentRay = calculateMouseRay(mouseX, mouseY);
|
currentRay = calculateMouseRay(mouseX, mouseY);
|
||||||
currentTerrainPoint = calcCurrentTerrainIntersection();
|
currentTerrainPoint = calcCurrentTerrainIntersection();
|
||||||
|
currentTerrainTile = calcIntersectingTerrainTile();
|
||||||
|
if(currentTerrainTile != null) {
|
||||||
|
currentTerrainTile.setColor(new Vector3f(1f, 1f, 1f));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector3f calculateMouseRay(float mouseX, float mouseY) {
|
private Vector3f calculateMouseRay(float mouseX, float mouseY) {
|
||||||
|
|
||||||
Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
|
Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
|
||||||
Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
|
Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
|
||||||
|
|
||||||
|
// Transform the clip coordinates into eye coordinates using the projection matrix
|
||||||
Vector4f eyeCoords = toEyeCoords(clipCoords);
|
Vector4f eyeCoords = toEyeCoords(clipCoords);
|
||||||
|
|
||||||
|
// Transform the eye coordinates into world coordinates using the view matrix
|
||||||
return toWorldCoords(eyeCoords);
|
return toWorldCoords(eyeCoords);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +61,6 @@ public class MousePicker {
|
|||||||
Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
|
Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
|
||||||
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
|
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
|
||||||
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
|
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
|
||||||
mouseRay.normalise();
|
|
||||||
return mouseRay;
|
return mouseRay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,10 +80,33 @@ public class MousePicker {
|
|||||||
|
|
||||||
private Vector3f calcCurrentTerrainIntersection() {
|
private Vector3f calcCurrentTerrainIntersection() {
|
||||||
Vector3f camPos = camera.getPosition();
|
Vector3f camPos = camera.getPosition();
|
||||||
return null;
|
float rayFactor = -camPos.y / currentRay.y;
|
||||||
|
|
||||||
|
float terrainX = camPos.x + rayFactor * currentRay.x;
|
||||||
|
float terrainY = camPos.y + rayFactor * currentRay.y;
|
||||||
|
float terrainZ = camPos.z + rayFactor * currentRay.z;
|
||||||
|
|
||||||
|
return new Vector3f(terrainX, terrainY, terrainZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
//**********************************************************
|
//**********************************************************
|
||||||
|
public TerrainTile calcIntersectingTerrainTile() {
|
||||||
|
|
||||||
|
for(TerrainTile terrainTile : terrain.getTerrainTiles()) {
|
||||||
|
float distanceX = (float) Math.pow(currentTerrainPoint.x - terrainTile.getPosition().x, 2);
|
||||||
|
float distanceZ = (float) Math.pow(-currentTerrainPoint.z - terrainTile.getPosition().z, 2);
|
||||||
|
|
||||||
|
float radius = (float) Math.pow(1, 2);
|
||||||
|
|
||||||
|
if(distanceX + distanceZ < radius) {
|
||||||
|
if(!terrainTile.getColor().equals(new Vector3f(1f, 1f, 1f))) {
|
||||||
|
System.out.println("Center: " + terrainTile.getPosition());
|
||||||
|
System.out.println("TerrainPoint: " + currentTerrainPoint);
|
||||||
|
}
|
||||||
|
return terrainTile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user