use of com.jme3.renderer.Camera in project jmonkeyengine by jMonkeyEngine.
the class DistanceLodCalculator method calculateLod.
public boolean calculateLod(TerrainPatch terrainPatch, List<Vector3f> locations, HashMap<String, UpdatedTerrainPatch> updates) {
if (locations == null || locations.isEmpty())
// no camera yet
return false;
float distance = getCenterLocation(terrainPatch).distance(locations.get(0));
if (turnOffLod) {
// set to full detail
int prevLOD = terrainPatch.getLod();
UpdatedTerrainPatch utp = updates.get(terrainPatch.getName());
if (utp == null) {
utp = new UpdatedTerrainPatch(terrainPatch);
updates.put(utp.getName(), utp);
}
utp.setNewLod(0);
utp.setPreviousLod(prevLOD);
//utp.setReIndexNeeded(true);
return true;
}
// go through each lod level to find the one we are in
for (int i = 0; i <= terrainPatch.getMaxLod(); i++) {
if (distance < getLodDistanceThreshold() * (i + 1) * terrainPatch.getWorldScaleCached().x || i == terrainPatch.getMaxLod()) {
boolean reIndexNeeded = false;
if (i != terrainPatch.getLod()) {
reIndexNeeded = true;
//System.out.println("lod change: "+lod+" > "+i+" dist: "+distance);
}
int prevLOD = terrainPatch.getLod();
UpdatedTerrainPatch utp = updates.get(terrainPatch.getName());
if (utp == null) {
//save in here, do not update actual variables
utp = new UpdatedTerrainPatch(terrainPatch);
updates.put(utp.getName(), utp);
}
utp.setNewLod(i);
utp.setPreviousLod(prevLOD);
return reIndexNeeded;
}
}
return false;
}
use of com.jme3.renderer.Camera in project jmonkeyengine by jMonkeyEngine.
the class PerspectiveLodCalculator method calculateLod.
public boolean calculateLod(TerrainPatch patch, List<Vector3f> locations, HashMap<String, UpdatedTerrainPatch> updates) {
if (turnOffLod) {
// set to full detail
int prevLOD = patch.getLod();
UpdatedTerrainPatch utp = updates.get(patch.getName());
if (utp == null) {
utp = new UpdatedTerrainPatch(patch);
updates.put(utp.getName(), utp);
}
utp.setNewLod(0);
utp.setPreviousLod(prevLOD);
//utp.setReIndexNeeded(true);
return true;
}
float[] lodEntropies = patch.getLodEntropies();
float cameraConstant = getCameraConstant(cam, pixelError);
Vector3f patchPos = getCenterLocation(patch);
// vector from camera to patch
//Vector3f toPatchDir = locations.get(0).subtract(patchPos).normalizeLocal();
//float facing = cam.getDirection().dot(toPatchDir);
float distance = patchPos.distance(locations.get(0));
// go through each lod level to find the one we are in
for (int i = 0; i <= patch.getMaxLod(); i++) {
if (distance < lodEntropies[i] * cameraConstant || i == patch.getMaxLod()) {
boolean reIndexNeeded = false;
if (i != patch.getLod()) {
reIndexNeeded = true;
// System.out.println("lod change: "+lod+" > "+i+" dist: "+distance);
}
int prevLOD = patch.getLod();
UpdatedTerrainPatch utp = updates.get(patch.getName());
if (utp == null) {
//save in here, do not update actual variables
utp = new UpdatedTerrainPatch(patch);
updates.put(utp.getName(), utp);
}
utp.setNewLod(i);
utp.setPreviousLod(prevLOD);
//utp.setReIndexNeeded(reIndexNeeded);
return reIndexNeeded;
}
}
return false;
}
use of com.jme3.renderer.Camera in project jmonkeyengine by jMonkeyEngine.
the class VRApplication method initCamera.
/**
* Creates the camera to use for rendering. Default values are perspective
* projection with 45° field of view, with near and far values 1 and 1000
* units respectively.
*/
private void initCamera() {
cam = new Camera(settings.getWidth(), settings.getHeight());
cam.setFrustumPerspective(45f, (float) cam.getWidth() / cam.getHeight(), 1f, 1000f);
cam.setLocation(new Vector3f(0f, 0f, 10f));
cam.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
renderManager = new RenderManager(renderer);
//Remy - 09/14/2010 setted the timer in the renderManager
renderManager.setTimer(timer);
viewPort = renderManager.createMainView("Default", cam);
viewPort.setClearFlags(true, true, true);
// Create a new cam for the gui
Camera guiCam = new Camera(settings.getWidth(), settings.getHeight());
guiViewPort = renderManager.createPostView("Gui Default", guiCam);
guiViewPort.setClearFlags(false, false, false);
}
use of com.jme3.renderer.Camera in project jmonkeyengine by jMonkeyEngine.
the class Camera method lookAt.
/**
* <code>lookAt</code> is a convenience method for auto-setting the frame
* based on a world position the user desires the camera to look at. It
* repoints the camera towards the given position using the difference
* between the position and the current camera location as a direction
* vector and the worldUpVector to compute up and left camera vectors.
*
* @param pos where to look at in terms of world coordinates
* @param worldUpVector a normalized vector indicating the up direction of the world.
* (typically {0, 1, 0} in jME.)
*/
public void lookAt(Vector3f pos, Vector3f worldUpVector) {
TempVars vars = TempVars.get();
Vector3f newDirection = vars.vect1;
Vector3f newUp = vars.vect2;
Vector3f newLeft = vars.vect3;
newDirection.set(pos).subtractLocal(location).normalizeLocal();
newUp.set(worldUpVector).normalizeLocal();
if (newUp.equals(Vector3f.ZERO)) {
newUp.set(Vector3f.UNIT_Y);
}
newLeft.set(newUp).crossLocal(newDirection).normalizeLocal();
if (newLeft.equals(Vector3f.ZERO)) {
if (newDirection.x != 0) {
newLeft.set(newDirection.y, -newDirection.x, 0f);
} else {
newLeft.set(0f, newDirection.z, -newDirection.y);
}
}
newUp.set(newDirection).crossLocal(newLeft).normalizeLocal();
this.rotation.fromAxes(newLeft, newUp, newDirection);
this.rotation.normalizeLocal();
vars.release();
onFrameChange();
}
use of com.jme3.renderer.Camera in project jmonkeyengine by jMonkeyEngine.
the class Camera method onFrameChange.
/**
* <code>onFrameChange</code> updates the view frame of the camera.
*/
public void onFrameChange() {
TempVars vars = TempVars.get();
Vector3f left = getLeft(vars.vect1);
Vector3f direction = getDirection(vars.vect2);
Vector3f up = getUp(vars.vect3);
float dirDotLocation = direction.dot(location);
// left plane
Vector3f leftPlaneNormal = worldPlane[LEFT_PLANE].getNormal();
leftPlaneNormal.x = left.x * coeffLeft[0];
leftPlaneNormal.y = left.y * coeffLeft[0];
leftPlaneNormal.z = left.z * coeffLeft[0];
leftPlaneNormal.addLocal(direction.x * coeffLeft[1], direction.y * coeffLeft[1], direction.z * coeffLeft[1]);
worldPlane[LEFT_PLANE].setConstant(location.dot(leftPlaneNormal));
// right plane
Vector3f rightPlaneNormal = worldPlane[RIGHT_PLANE].getNormal();
rightPlaneNormal.x = left.x * coeffRight[0];
rightPlaneNormal.y = left.y * coeffRight[0];
rightPlaneNormal.z = left.z * coeffRight[0];
rightPlaneNormal.addLocal(direction.x * coeffRight[1], direction.y * coeffRight[1], direction.z * coeffRight[1]);
worldPlane[RIGHT_PLANE].setConstant(location.dot(rightPlaneNormal));
// bottom plane
Vector3f bottomPlaneNormal = worldPlane[BOTTOM_PLANE].getNormal();
bottomPlaneNormal.x = up.x * coeffBottom[0];
bottomPlaneNormal.y = up.y * coeffBottom[0];
bottomPlaneNormal.z = up.z * coeffBottom[0];
bottomPlaneNormal.addLocal(direction.x * coeffBottom[1], direction.y * coeffBottom[1], direction.z * coeffBottom[1]);
worldPlane[BOTTOM_PLANE].setConstant(location.dot(bottomPlaneNormal));
// top plane
Vector3f topPlaneNormal = worldPlane[TOP_PLANE].getNormal();
topPlaneNormal.x = up.x * coeffTop[0];
topPlaneNormal.y = up.y * coeffTop[0];
topPlaneNormal.z = up.z * coeffTop[0];
topPlaneNormal.addLocal(direction.x * coeffTop[1], direction.y * coeffTop[1], direction.z * coeffTop[1]);
worldPlane[TOP_PLANE].setConstant(location.dot(topPlaneNormal));
if (isParallelProjection()) {
worldPlane[LEFT_PLANE].setConstant(worldPlane[LEFT_PLANE].getConstant() + frustumLeft);
worldPlane[RIGHT_PLANE].setConstant(worldPlane[RIGHT_PLANE].getConstant() - frustumRight);
worldPlane[TOP_PLANE].setConstant(worldPlane[TOP_PLANE].getConstant() - frustumTop);
worldPlane[BOTTOM_PLANE].setConstant(worldPlane[BOTTOM_PLANE].getConstant() + frustumBottom);
}
// far plane
worldPlane[FAR_PLANE].setNormal(left);
worldPlane[FAR_PLANE].setNormal(-direction.x, -direction.y, -direction.z);
worldPlane[FAR_PLANE].setConstant(-(dirDotLocation + frustumFar));
// near plane
worldPlane[NEAR_PLANE].setNormal(direction.x, direction.y, direction.z);
worldPlane[NEAR_PLANE].setConstant(dirDotLocation + frustumNear);
viewMatrix.fromFrame(location, direction, up, left);
vars.release();
// viewMatrix.transposeLocal();
updateViewProjection();
}
Aggregations