From 3dc9a591b74733ccde3fffcaf0b306d8bcf61b0a Mon Sep 17 00:00:00 2001 From: "bre..ns" Date: Sat, 31 Mar 2012 06:18:10 +0000 Subject: [PATCH] whoops, I blame jesus for not adding this class git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9270 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../geomipmap/TerrainGridLodControl.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGridLodControl.java diff --git a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGridLodControl.java b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGridLodControl.java new file mode 100644 index 000000000..cf881f687 --- /dev/null +++ b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGridLodControl.java @@ -0,0 +1,50 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.jme3.terrain.geomipmap; + +import com.jme3.math.Vector3f; +import com.jme3.renderer.Camera; +import com.jme3.terrain.Terrain; +import com.jme3.terrain.geomipmap.lodcalc.LodCalculator; +import java.util.List; + +/** + * Updates grid offsets and cell positions. + * As well as updating LOD. + * + * @author sploreg + */ +public class TerrainGridLodControl extends TerrainLodControl { + + public TerrainGridLodControl(Terrain terrain, Camera camera) { + super(terrain, camera); + } + + @Override + protected void updateLOD(List locations, LodCalculator lodCalculator) { + TerrainGrid terrainGrid = (TerrainGrid)getSpatial(); + + // for now, only the first camera is handled. + // to accept more, there are two ways: + // 1: every camera has an associated grid, then the location is not enough to identify which camera location has changed + // 2: grids are associated with locations, and no incremental update is done, we load new grids for new locations, and unload those that are not needed anymore + Vector3f cam = locations.isEmpty() ? Vector3f.ZERO.clone() : locations.get(0); + Vector3f camCell = terrainGrid.getCamCell(cam); // get the grid index value of where the camera is (ie. 2,1) + if (terrainGrid.cellsLoaded > 1) { // Check if cells are updated before updating gridoffset. + terrainGrid.gridOffset[0] = Math.round(camCell.x * (terrainGrid.size / 2)); + terrainGrid.gridOffset[1] = Math.round(camCell.z * (terrainGrid.size / 2)); + terrainGrid.cellsLoaded = 0; + } + if (camCell.x != terrainGrid.currentCamCell.x || camCell.z != terrainGrid.currentCamCell.z || !terrainGrid.runOnce) { + // if the camera has moved into a new cell, load new terrain into the visible 4 center quads + terrainGrid.updateChildren(camCell); + for (TerrainGridListener l : terrainGrid.listeners) { + l.gridMoved(camCell); + } + } + terrainGrid.runOnce = true; + super.updateLOD(locations, lodCalculator); + } +}