gdx poi3d layer
This commit is contained in:
48
vtm-playground/src/org/oscim/test/gdx/poi3d/Gdx3DTest.java
Normal file
48
vtm-playground/src/org/oscim/test/gdx/poi3d/Gdx3DTest.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import org.oscim.gdx.GdxMap;
|
||||
import org.oscim.gdx.GdxMapApp;
|
||||
import org.oscim.layers.tile.vector.BuildingLayer;
|
||||
import org.oscim.layers.tile.vector.VectorTileLayer;
|
||||
import org.oscim.layers.tile.vector.labeling.LabelLayer;
|
||||
import org.oscim.renderer.MapRenderer;
|
||||
import org.oscim.theme.VtmThemes;
|
||||
import org.oscim.tiling.TileSource;
|
||||
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
|
||||
|
||||
public class Gdx3DTest extends GdxMap {
|
||||
|
||||
@Override
|
||||
public void createLayers() {
|
||||
MapRenderer.setBackgroundColor(0xff888888);
|
||||
|
||||
mMap.setMapPosition(53.1, 8.8, 1 << 15);
|
||||
|
||||
TileSource ts = new OSciMap4TileSource();
|
||||
// initDefaultLayers(ts, false, false, false);
|
||||
|
||||
VectorTileLayer mMapLayer = mMap.setBaseMap(ts);
|
||||
mMap.setTheme(VtmThemes.DEFAULT);
|
||||
// mMap.setTheme(VtmThemes.TRONRENDER);
|
||||
|
||||
mMap.layers().add(new BuildingLayer(mMap, mMapLayer));
|
||||
|
||||
// mMap.getLayers().add(new GenericLayer(mMap, new GridRenderer()));
|
||||
|
||||
// ts = new OSciMap4TileSource("http://opensciencemap.org/tiles/s3db");
|
||||
// VectorTileLayer tl = new VectorTileLayer(mMap, 16, 16, 20);
|
||||
// tl.setTileSource(ts);
|
||||
// tl.setRenderTheme(ThemeLoader.load(VtmThemes.DEFAULT));
|
||||
// mMap.getLayers().add(tl);
|
||||
// mMap.getLayers().add(new BuildingLayer(mMap, tl.getTileLayer()));
|
||||
|
||||
mMap.layers().add(new Poi3DLayer(mMap, mMapLayer));
|
||||
|
||||
mMap.layers().add(new LabelLayer(mMap, mMapLayer));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new Gdx3DTest(), null, 320);
|
||||
}
|
||||
}
|
||||
237
vtm-playground/src/org/oscim/test/gdx/poi3d/GdxModelLayer.java
Normal file
237
vtm-playground/src/org/oscim/test/gdx/poi3d/GdxModelLayer.java
Normal file
@@ -0,0 +1,237 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.event.Event;
|
||||
import org.oscim.layers.Layer;
|
||||
import org.oscim.map.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.graphics.g3d.Model;
|
||||
import com.badlogic.gdx.graphics.g3d.model.Node;
|
||||
|
||||
public class GdxModelLayer extends Layer implements Map.UpdateListener {
|
||||
|
||||
static final Logger log = LoggerFactory.getLogger(GdxModelLayer.class);
|
||||
|
||||
GdxRenderer3D g3d;
|
||||
|
||||
//VectorTileLayer mTileLayer;
|
||||
|
||||
public GdxModelLayer(Map map) {
|
||||
super(map);
|
||||
// tileLayer.addHook(new TileLoaderProcessHook() {
|
||||
//
|
||||
// @Override
|
||||
// public boolean process(MapTile tile, ElementLayers layers, MapElement element) {
|
||||
//
|
||||
// if (!element.tags.contains(TREE_TAG))
|
||||
// return false;
|
||||
//
|
||||
// Poi3DTileData td = get(tile);
|
||||
// PointF p = element.getPoint(0);
|
||||
// SymbolItem s = SymbolItem.pool.get();
|
||||
// s.x = p.x;
|
||||
// s.y = p.y;
|
||||
// td.symbols.push(s);
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
// });
|
||||
//mTileLayer = tileLayer;
|
||||
|
||||
mRenderer = g3d = new GdxRenderer3D(mMap);
|
||||
|
||||
// Material mat = new
|
||||
// Material(ColorAttribute.createDiffuse(Color.BLUE));
|
||||
// ModelBuilder modelBuilder = new ModelBuilder();
|
||||
// long attributes = Usage.Position | Usage.Normal |
|
||||
// Usage.TextureCoordinates;
|
||||
|
||||
// mModel = modelBuilder.createSphere(10f, 10f, 10f, 12, 12,
|
||||
// mat, attributes);
|
||||
|
||||
assets = new AssetManager();
|
||||
assets.load("data/g3d/test.g3db", Model.class);
|
||||
loading = true;
|
||||
}
|
||||
|
||||
// TileSet mTileSet = new TileSet();
|
||||
// TileSet mPrevTiles = new TileSet();
|
||||
//
|
||||
// LinkedHashMap<Tile, Array<SharedModel>> mTileMap =
|
||||
// new LinkedHashMap<Tile, Array<SharedModel>>();
|
||||
|
||||
boolean loading;
|
||||
Model mModel;
|
||||
AssetManager assets;
|
||||
|
||||
private void doneLoading() {
|
||||
Model model = assets.get("data/g3d/test.g3db", Model.class);
|
||||
for (int i = 0; i < model.nodes.size; i++) {
|
||||
Node node = model.nodes.get(i);
|
||||
log.debug("loader node " + node.id);
|
||||
|
||||
if (node.id.equals("test_root")) {
|
||||
node = node.getChild("Building", false, false);
|
||||
log.debug("loader node " + node.id);
|
||||
|
||||
node.rotation.setFromAxis(1, 0, 0, 90);
|
||||
mModel = model;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
//}
|
||||
}
|
||||
|
||||
loading = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapEvent(Event ev, MapPosition pos) {
|
||||
|
||||
// if (ev == Map.CLEAR_EVENT) {
|
||||
// mTileSet = new TileSet();
|
||||
// mPrevTiles = new TileSet();
|
||||
// mTileMap = new LinkedHashMap<Tile, Array<SharedModel>>();
|
||||
// synchronized (g3d) {
|
||||
// g3d.instances.clear();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
if (loading && assets.update()) {
|
||||
doneLoading();
|
||||
// Renderable renderable = new Renderable();
|
||||
// new SharedModel(mModel).getRenderable(renderable);
|
||||
// Shader shader = new DefaultShader(renderable, true, false,
|
||||
// false, false, 1, 0, 0, 0);
|
||||
|
||||
g3d.instances.add(new SharedModel(mModel));
|
||||
|
||||
}
|
||||
if (loading)
|
||||
return;
|
||||
|
||||
int x = 17185 << 1;
|
||||
int y = 10662 << 1;
|
||||
int z = 16;
|
||||
double scale = 1 / (1 << z);
|
||||
|
||||
g3d.cam.setMapPosition(x * scale - pos.x, y * scale - pos.y, scale / pos.scale);
|
||||
|
||||
//
|
||||
// // log.debug("update");
|
||||
//
|
||||
// mTileLayer.tileRenderer().getVisibleTiles(mTileSet);
|
||||
//
|
||||
// if (mTileSet.cnt == 0) {
|
||||
// mTileSet.releaseTiles();
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// boolean changed = false;
|
||||
//
|
||||
// Array<SharedModel> added = new Array<SharedModel>();
|
||||
// Array<SharedModel> removed = new Array<SharedModel>();
|
||||
|
||||
// for (int i = 0; i < mTileSet.cnt; i++) {
|
||||
// MapTile t = mTileSet.tiles[i];
|
||||
// if (mPrevTiles.contains(t))
|
||||
// continue;
|
||||
//
|
||||
// Array<SharedModel> instances = new Array<SharedModel>();
|
||||
//
|
||||
// Poi3DTileData ld = (Poi3DTileData) t.getData(POI_DATA);
|
||||
// if (ld == null)
|
||||
// continue;
|
||||
//
|
||||
// for (SymbolItem it : ld.symbols) {
|
||||
//
|
||||
// SharedModel inst = new SharedModel(mModel);
|
||||
// inst.userData = it;
|
||||
// // float r = 0.5f + 0.5f * (float) Math.random();
|
||||
// // float g = 0.5f + 0.5f * (float) Math.random();
|
||||
// // float b = 0.5f + 0.5f * (float) Math.random();
|
||||
//
|
||||
// // inst.transform.setTranslation(new Vector3(it.x, it.y,
|
||||
// // 10));
|
||||
// // inst.materials.get(0).set(ColorAttribute.createDiffuse(r,
|
||||
// // g, b, 0.8f));
|
||||
// instances.add(inst);
|
||||
// added.add(inst);
|
||||
// }
|
||||
//
|
||||
// if (instances.size == 0)
|
||||
// continue;
|
||||
//
|
||||
// log.debug("add " + t + " " + instances.size);
|
||||
//
|
||||
// changed = true;
|
||||
//
|
||||
// mTileMap.put(t, instances);
|
||||
// }
|
||||
//
|
||||
// for (int i = 0; i < mPrevTiles.cnt; i++) {
|
||||
// MapTile t = mPrevTiles.tiles[i];
|
||||
// if (mTileSet.contains(t))
|
||||
// continue;
|
||||
//
|
||||
// Array<SharedModel> instances = mTileMap.get(t);
|
||||
// if (instances == null)
|
||||
// continue;
|
||||
//
|
||||
// changed = true;
|
||||
//
|
||||
// removed.addAll(instances);
|
||||
// mTileMap.remove(t);
|
||||
// log.debug("remove " + t);
|
||||
// }
|
||||
//
|
||||
// mPrevTiles.releaseTiles();
|
||||
//
|
||||
// int zoom = mTileSet.tiles[0].zoomLevel;
|
||||
//
|
||||
// TileSet tmp = mPrevTiles;
|
||||
// mPrevTiles = mTileSet;
|
||||
// mTileSet = tmp;
|
||||
//
|
||||
// if (!changed)
|
||||
// return;
|
||||
//
|
||||
// // scale aka tree height
|
||||
// float scale = (float) (1f / (1 << (17 - zoom))) * 8;
|
||||
//
|
||||
// double tileX = (pos.x * (Tile.SIZE << zoom));
|
||||
// double tileY = (pos.y * (Tile.SIZE << zoom));
|
||||
//
|
||||
// synchronized (g3d) {
|
||||
//
|
||||
// for (Entry<Tile, Array<SharedModel>> e : mTileMap.entrySet()) {
|
||||
// Tile t = e.getKey();
|
||||
//
|
||||
// float dx = (float) (t.tileX * Tile.SIZE - tileX);
|
||||
// float dy = (float) (t.tileY * Tile.SIZE - tileY);
|
||||
//
|
||||
// for (SharedModel inst : e.getValue()) {
|
||||
// SymbolItem it = (SymbolItem) inst.userData;
|
||||
//
|
||||
// // variable height
|
||||
// float s = scale + (it.x * it.y) % 3;
|
||||
// float r = (it.x * it.y) % 360;
|
||||
//
|
||||
// inst.transform.idt();
|
||||
// inst.transform.scale(s, s, s);
|
||||
// inst.transform.translate((dx + it.x) / s, (dy + it.y) / s, 0);
|
||||
// inst.transform.rotate(0, 0, 1, r);
|
||||
//
|
||||
// // inst.transform.setToTranslationAndScaling((dx +
|
||||
// // it.x), (dy + it.y),
|
||||
// // 0, s, s, s);
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.map.Map;
|
||||
import org.oscim.map.Viewport;
|
||||
import org.oscim.renderer.GLState;
|
||||
import org.oscim.renderer.GLViewport;
|
||||
import org.oscim.renderer.LayerRenderer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.Environment;
|
||||
import com.badlogic.gdx.graphics.g3d.Model;
|
||||
import com.badlogic.gdx.graphics.g3d.ModelBatch;
|
||||
import com.badlogic.gdx.graphics.g3d.Renderable;
|
||||
import com.badlogic.gdx.graphics.g3d.Shader;
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
|
||||
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
public class GdxModelRenderer extends LayerRenderer {
|
||||
static final Logger log = LoggerFactory.getLogger(GdxModelRenderer.class);
|
||||
|
||||
ModelBatch modelBatch;
|
||||
public MapCamera cam;
|
||||
Map mMap;
|
||||
|
||||
boolean loading;
|
||||
|
||||
public Environment lights;
|
||||
|
||||
public Array<SharedModel> instances = new Array<SharedModel>();
|
||||
|
||||
public Shader shader;
|
||||
public RenderContext renderContext;
|
||||
public Model model;
|
||||
private ModelBatch mBatch = new ModelBatch();
|
||||
|
||||
public GdxModelRenderer(Map map) {
|
||||
mMap = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setup() {
|
||||
|
||||
modelBatch = new ModelBatch(new DefaultShaderProvider());
|
||||
|
||||
lights = new Environment();
|
||||
lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 1.0f, 1.0f, 1.f));
|
||||
lights.add(new DirectionalLight().set(0.3f, 0.3f, 0.3f, 0, 1, -0.2f));
|
||||
|
||||
cam = new MapCamera(mMap);
|
||||
|
||||
renderContext =
|
||||
new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.WEIGHTED, 1));
|
||||
|
||||
// shader = new DefaultShader(renderable.material,
|
||||
// renderable.mesh.getVertexAttributes(), true, false, 1, 0, 0, 0);
|
||||
// shader.init();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized void update(GLViewport v) {
|
||||
// if (loading && assets.update())
|
||||
// doneLoading();
|
||||
|
||||
if (!isReady()) {
|
||||
cam.setPosition(v.pos);
|
||||
setReady(true);
|
||||
}
|
||||
|
||||
// if (changed) {
|
||||
// cam.update(position, matrices);
|
||||
// }
|
||||
}
|
||||
|
||||
Vector3 tempVector = new Vector3();
|
||||
float[] mBox = new float[8];
|
||||
|
||||
Renderable r = new Renderable();
|
||||
|
||||
@Override
|
||||
protected void render(GLViewport v) {
|
||||
if (instances.size == 0)
|
||||
return;
|
||||
|
||||
// GLUtils.checkGlError(">" + TAG);
|
||||
|
||||
GL.glDepthMask(true);
|
||||
|
||||
if (v.pos.zoomLevel < 16)
|
||||
GL.glClear(GL20.GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
// set state that is expected after modelBatch.end();
|
||||
// modelBatch keeps track of its own state
|
||||
GLState.enableVertexArrays(-1, -1);
|
||||
GLState.bindTex2D(-1);
|
||||
GLState.useProgram(-1);
|
||||
GLState.test(false, false);
|
||||
GLState.blend(false);
|
||||
|
||||
cam.update(v);
|
||||
|
||||
Viewport p = mMap.viewport();
|
||||
p.getMapExtents(mBox, 10);
|
||||
float scale = (float) (cam.mMapPosition.scale / v.pos.scale);
|
||||
|
||||
float dx = (float) (cam.mMapPosition.x - v.pos.x)
|
||||
* (Tile.SIZE << cam.mMapPosition.zoomLevel);
|
||||
float dy = (float) (cam.mMapPosition.y - v.pos.y)
|
||||
* (Tile.SIZE << cam.mMapPosition.zoomLevel);
|
||||
|
||||
for (int i = 0; i < 8; i += 2) {
|
||||
mBox[i] *= scale;
|
||||
mBox[i] -= dx;
|
||||
mBox[i + 1] *= scale;
|
||||
mBox[i + 1] -= dy;
|
||||
}
|
||||
|
||||
//int w = mMap.getWidth() / 2;
|
||||
//int h = mMap.getHeight() / 2;
|
||||
//float sqRadius = (w * w + h * h) / scale;
|
||||
|
||||
synchronized (this) {
|
||||
if (instances.size == 0)
|
||||
return;
|
||||
|
||||
//renderContext.begin();
|
||||
|
||||
// if (shader == null) {
|
||||
// r = instances.get(0).getRenderable(r);
|
||||
// DefaultShader.Config c = new DefaultShader.Config();
|
||||
// c.numBones = 0;
|
||||
// c.numDirectionalLights = 1;
|
||||
// r.environment = lights;
|
||||
//
|
||||
// shader = new DefaultShader(r, c);
|
||||
// shader.init();
|
||||
// }
|
||||
mBatch.begin(cam);
|
||||
//shader.begin(cam, renderContext);
|
||||
|
||||
for (SharedModel instance : instances) {
|
||||
instance.transform.getTranslation(tempVector);
|
||||
//instance.getRenderables(renderables, pool);
|
||||
// if (tempVector.x * tempVector.x + tempVector.y * tempVector.y > sqRadius)
|
||||
// continue;
|
||||
// tempVector.scl(0.8f, 0.8f, 1);
|
||||
// if (!GeometryUtils.pointInPoly(tempVector.x, tempVector.y, mBox, 8, 0))
|
||||
// continue;
|
||||
|
||||
mBatch.render(instance);
|
||||
|
||||
//shader.render(r);
|
||||
}
|
||||
mBatch.end();
|
||||
|
||||
//shader.end();
|
||||
//renderContext.end();
|
||||
}
|
||||
|
||||
GL.glDepthMask(false);
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void dispose () {
|
||||
// modelBatch.dispose();
|
||||
// assets.dispose();
|
||||
// assets = null;
|
||||
// axesModel.dispose();
|
||||
// axesModel = null;
|
||||
// }
|
||||
}
|
||||
198
vtm-playground/src/org/oscim/test/gdx/poi3d/GdxRenderer3D.java
Normal file
198
vtm-playground/src/org/oscim/test/gdx/poi3d/GdxRenderer3D.java
Normal file
@@ -0,0 +1,198 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.map.Map;
|
||||
import org.oscim.map.Viewport;
|
||||
import org.oscim.renderer.GLState;
|
||||
import org.oscim.renderer.GLViewport;
|
||||
import org.oscim.renderer.LayerRenderer;
|
||||
import org.oscim.utils.geom.GeometryUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.Environment;
|
||||
import com.badlogic.gdx.graphics.g3d.Model;
|
||||
import com.badlogic.gdx.graphics.g3d.ModelBatch;
|
||||
import com.badlogic.gdx.graphics.g3d.Renderable;
|
||||
import com.badlogic.gdx.graphics.g3d.Shader;
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
|
||||
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
|
||||
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
public class GdxRenderer3D extends LayerRenderer {
|
||||
static final Logger log = LoggerFactory.getLogger(GdxRenderer3D.class);
|
||||
|
||||
ModelBatch modelBatch;
|
||||
public MapCamera cam;
|
||||
Map mMap;
|
||||
|
||||
boolean loading;
|
||||
|
||||
public Environment lights;
|
||||
|
||||
public Array<SharedModel> instances = new Array<SharedModel>();
|
||||
|
||||
public Shader shader;
|
||||
public RenderContext renderContext;
|
||||
public Model model;
|
||||
|
||||
public GdxRenderer3D(Map map) {
|
||||
mMap = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setup() {
|
||||
|
||||
modelBatch = new ModelBatch(new DefaultShaderProvider());
|
||||
|
||||
lights = new Environment();
|
||||
lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 1.0f, 1.0f, 1.f));
|
||||
lights.add(new DirectionalLight().set(0.3f, 0.3f, 0.3f, 0, 1, -0.2f));
|
||||
|
||||
cam = new MapCamera(mMap);
|
||||
|
||||
renderContext =
|
||||
new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.WEIGHTED, 1));
|
||||
|
||||
// shader = new DefaultShader(renderable.material,
|
||||
// renderable.mesh.getVertexAttributes(), true, false, 1, 0, 0, 0);
|
||||
// shader.init();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized void update(GLViewport v) {
|
||||
// if (loading && assets.update())
|
||||
// doneLoading();
|
||||
|
||||
if (!isReady()) {
|
||||
cam.setPosition(v.pos);
|
||||
setReady(true);
|
||||
}
|
||||
|
||||
// if (changed) {
|
||||
// cam.update(position, matrices);
|
||||
// }
|
||||
}
|
||||
|
||||
Vector3 tempVector = new Vector3();
|
||||
float[] mBox = new float[8];
|
||||
|
||||
Renderable r = new Renderable();
|
||||
|
||||
@Override
|
||||
protected void render(GLViewport v) {
|
||||
if (instances.size == 0)
|
||||
return;
|
||||
|
||||
// GLUtils.checkGlError(">" + TAG);
|
||||
|
||||
GL.glDepthMask(true);
|
||||
|
||||
// if (position.zoomLevel < 17)
|
||||
// GL.glClear(GL20.GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
// set state that is expected after modelBatch.end();
|
||||
// modelBatch keeps track of its own state
|
||||
GLState.enableVertexArrays(-1, -1);
|
||||
GLState.bindTex2D(-1);
|
||||
GLState.useProgram(-1);
|
||||
GLState.test(false, false);
|
||||
GLState.blend(false);
|
||||
|
||||
cam.update(v);
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
int cnt = 0;
|
||||
int rnd = 0;
|
||||
|
||||
Viewport p = mMap.viewport();
|
||||
p.getMapExtents(mBox, 10);
|
||||
float scale = (float) (cam.mMapPosition.scale / v.pos.scale);
|
||||
|
||||
float dx = (float) (cam.mMapPosition.x - v.pos.x)
|
||||
* (Tile.SIZE << cam.mMapPosition.zoomLevel);
|
||||
float dy = (float) (cam.mMapPosition.y - v.pos.y)
|
||||
* (Tile.SIZE << cam.mMapPosition.zoomLevel);
|
||||
|
||||
for (int i = 0; i < 8; i += 2) {
|
||||
mBox[i] *= scale;
|
||||
mBox[i] -= dx;
|
||||
mBox[i + 1] *= scale;
|
||||
mBox[i + 1] -= dy;
|
||||
}
|
||||
|
||||
int w = mMap.getWidth() / 2;
|
||||
int h = mMap.getHeight() / 2;
|
||||
|
||||
float sqRadius = (w * w + h * h) / scale;
|
||||
|
||||
synchronized (this) {
|
||||
if (instances.size == 0)
|
||||
return;
|
||||
|
||||
cnt = instances.size;
|
||||
|
||||
renderContext.begin();
|
||||
|
||||
if (shader == null) {
|
||||
r = instances.get(0).getRenderable(r);
|
||||
DefaultShader.Config c = new DefaultShader.Config();
|
||||
c.numBones = 0;
|
||||
c.numDirectionalLights = 1;
|
||||
r.environment = lights;
|
||||
// shader = new DefaultShader(r, true, false, false, false, 1,
|
||||
// 0, 0, 0);
|
||||
shader = new DefaultShader(r, c);
|
||||
shader.init();
|
||||
}
|
||||
|
||||
shader.begin(cam, renderContext);
|
||||
|
||||
for (SharedModel instance : instances) {
|
||||
instance.transform.getTranslation(tempVector);
|
||||
|
||||
if (tempVector.x * tempVector.x + tempVector.y * tempVector.y > sqRadius)
|
||||
continue;
|
||||
|
||||
tempVector.scl(0.8f, 0.8f, 1);
|
||||
|
||||
if (!GeometryUtils.pointInPoly(tempVector.x, tempVector.y, mBox, 8, 0))
|
||||
continue;
|
||||
|
||||
instance.getRenderable(r);
|
||||
// r.lights = lights;
|
||||
// r.environment = lights;
|
||||
shader.render(r);
|
||||
|
||||
rnd++;
|
||||
}
|
||||
|
||||
shader.end();
|
||||
renderContext.end();
|
||||
}
|
||||
log.debug(">>> " + (System.currentTimeMillis() - time) + " " + cnt + "/" + rnd);
|
||||
|
||||
GL.glDepthMask(false);
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void dispose () {
|
||||
// modelBatch.dispose();
|
||||
// assets.dispose();
|
||||
// assets = null;
|
||||
// axesModel.dispose();
|
||||
// axesModel = null;
|
||||
// }
|
||||
}
|
||||
164
vtm-playground/src/org/oscim/test/gdx/poi3d/GdxRenderer3D2.java
Normal file
164
vtm-playground/src/org/oscim/test/gdx/poi3d/GdxRenderer3D2.java
Normal file
@@ -0,0 +1,164 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.map.Map;
|
||||
import org.oscim.map.Viewport;
|
||||
import org.oscim.renderer.GLState;
|
||||
import org.oscim.renderer.GLViewport;
|
||||
import org.oscim.renderer.LayerRenderer;
|
||||
import org.oscim.utils.geom.GeometryUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.Environment;
|
||||
import com.badlogic.gdx.graphics.g3d.ModelBatch;
|
||||
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
|
||||
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
public class GdxRenderer3D2 extends LayerRenderer {
|
||||
static final Logger log = LoggerFactory.getLogger(GdxRenderer3D2.class);
|
||||
|
||||
ModelBatch modelBatch;
|
||||
public MapCamera cam;
|
||||
Map mMap;
|
||||
|
||||
boolean loading;
|
||||
|
||||
public Environment lights;
|
||||
|
||||
public Array<SharedModel> instances = new Array<SharedModel>();
|
||||
|
||||
public GdxRenderer3D2(Map map) {
|
||||
mMap = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setup() {
|
||||
|
||||
// if (assets == null)
|
||||
// assets = new AssetManager();
|
||||
|
||||
// assets.load("data/g3d/invaders.g3dj", Model.class);
|
||||
// loading = true;
|
||||
|
||||
modelBatch = new ModelBatch(new DefaultShaderProvider());
|
||||
|
||||
lights = new Environment();
|
||||
// lights.ambientLight.set(1.0f, 1.0f, 1.0f, 1f);
|
||||
// lights.ambientLight.set(215 / 255f,
|
||||
// 240 / 255f,
|
||||
// 51 / 255f, 1f);
|
||||
|
||||
lights.add(new DirectionalLight().set(0.9f, 0.9f, 0.9f, 0, 1, -0.2f));
|
||||
|
||||
cam = new MapCamera(mMap);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized void update(GLViewport v) {
|
||||
// if (loading && assets.update())
|
||||
// doneLoading();
|
||||
|
||||
if (!isReady()) {
|
||||
cam.setPosition(v.pos);
|
||||
setReady(true);
|
||||
}
|
||||
|
||||
// if (changed) {
|
||||
// cam.update(position, matrices);
|
||||
// }
|
||||
}
|
||||
|
||||
Vector3 tempVector = new Vector3();
|
||||
float[] mBox = new float[8];
|
||||
|
||||
@Override
|
||||
protected void render(GLViewport v) {
|
||||
if (instances.size == 0)
|
||||
return;
|
||||
|
||||
// GLUtils.checkGlError(">" + TAG);
|
||||
|
||||
GL.glDepthMask(true);
|
||||
|
||||
if (v.pos.zoomLevel < 17)
|
||||
GL.glClear(GL20.GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
// set state that is expected after modelBatch.end();
|
||||
// modelBatch keeps track of its own state
|
||||
GLState.enableVertexArrays(-1, -1);
|
||||
GLState.bindTex2D(-1);
|
||||
GLState.useProgram(-1);
|
||||
GLState.test(false, false);
|
||||
GLState.blend(false);
|
||||
|
||||
// GL.glCullFace(GL20.GL_BACK);
|
||||
// GL.glFrontFace(GL20.GL_CW);
|
||||
|
||||
cam.update(v);
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
int cnt = 0;
|
||||
int rnd = 0;
|
||||
|
||||
Viewport p = mMap.viewport();
|
||||
p.getMapExtents(mBox, 10);
|
||||
float scale = (float) (cam.mMapPosition.scale / v.pos.scale);
|
||||
|
||||
float dx =
|
||||
(float) (cam.mMapPosition.x - v.pos.x)
|
||||
* (Tile.SIZE << cam.mMapPosition.zoomLevel);
|
||||
float dy =
|
||||
(float) (cam.mMapPosition.y - v.pos.y)
|
||||
* (Tile.SIZE << cam.mMapPosition.zoomLevel);
|
||||
|
||||
for (int i = 0; i < 8; i += 2) {
|
||||
mBox[i] *= scale;
|
||||
mBox[i] -= dx;
|
||||
mBox[i + 1] *= scale;
|
||||
mBox[i + 1] -= dy;
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
modelBatch.begin(cam);
|
||||
cnt = instances.size;
|
||||
|
||||
for (SharedModel instance : instances) {
|
||||
instance.transform.getTranslation(tempVector);
|
||||
tempVector.scl(0.9f, 0.9f, 1);
|
||||
if (!GeometryUtils.pointInPoly(tempVector.x, tempVector.y, mBox, 8, 0))
|
||||
continue;
|
||||
|
||||
modelBatch.render(instance);
|
||||
rnd++;
|
||||
}
|
||||
modelBatch.end();
|
||||
}
|
||||
log.debug(">>> " + (System.currentTimeMillis() - time) + " " + cnt + "/" + rnd);
|
||||
|
||||
// GLUtils.checkGlError("<" + TAG);
|
||||
|
||||
GL.glDepthMask(false);
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// GLState.bindTex2D(-1);
|
||||
// GLState.useProgram(-1);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void dispose () {
|
||||
// modelBatch.dispose();
|
||||
// assets.dispose();
|
||||
// assets = null;
|
||||
// axesModel.dispose();
|
||||
// axesModel = null;
|
||||
// }
|
||||
}
|
||||
70
vtm-playground/src/org/oscim/test/gdx/poi3d/MapCamera.java
Normal file
70
vtm-playground/src/org/oscim/test/gdx/poi3d/MapCamera.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.map.Map;
|
||||
import org.oscim.renderer.GLViewport;
|
||||
|
||||
import com.badlogic.gdx.graphics.Camera;
|
||||
import com.badlogic.gdx.math.Matrix4;
|
||||
|
||||
public class MapCamera extends Camera {
|
||||
|
||||
private final Map mMap;
|
||||
|
||||
public MapCamera(Map map) {
|
||||
mMap = map;
|
||||
|
||||
this.near = 1;
|
||||
this.far = 8;
|
||||
}
|
||||
|
||||
MapPosition mMapPosition = new MapPosition();
|
||||
|
||||
public void setPosition(MapPosition pos) {
|
||||
mMapPosition.copy(pos);
|
||||
|
||||
this.viewportWidth = mMap.getWidth();
|
||||
this.viewportHeight = mMap.getHeight();
|
||||
}
|
||||
|
||||
public void setMapPosition(double x, double y, double scale) {
|
||||
mMapPosition.setScale(scale);
|
||||
mMapPosition.x = x;
|
||||
mMapPosition.y = y;
|
||||
}
|
||||
|
||||
public void update(GLViewport v) {
|
||||
double scale = (v.pos.scale * Tile.SIZE);
|
||||
|
||||
float x = (float) ((mMapPosition.x - v.pos.x) * scale);
|
||||
float y = (float) ((mMapPosition.y - v.pos.y) * scale);
|
||||
float z = (float) (v.pos.scale / mMapPosition.scale);
|
||||
|
||||
v.proj.get(projection.getValues());
|
||||
v.mvp.setTransScale(x, y, z);
|
||||
v.mvp.setValue(10, z);
|
||||
v.mvp.multiplyLhs(v.view);
|
||||
v.mvp.get(view.getValues());
|
||||
|
||||
combined.set(projection);
|
||||
|
||||
Matrix4.mul(combined.val, view.val);
|
||||
|
||||
//if (updateFrustum) {
|
||||
invProjectionView.set(combined);
|
||||
Matrix4.inv(invProjectionView.val);
|
||||
frustum.update(invProjectionView);
|
||||
//}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(boolean updateFrustum) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
258
vtm-playground/src/org/oscim/test/gdx/poi3d/Poi3DLayer.java
Normal file
258
vtm-playground/src/org/oscim/test/gdx/poi3d/Poi3DLayer.java
Normal file
@@ -0,0 +1,258 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.oscim.core.MapElement;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.PointF;
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.event.Event;
|
||||
import org.oscim.layers.Layer;
|
||||
import org.oscim.layers.tile.MapTile;
|
||||
import org.oscim.layers.tile.MapTile.TileData;
|
||||
import org.oscim.layers.tile.TileSet;
|
||||
import org.oscim.layers.tile.vector.VectorTileLayer;
|
||||
import org.oscim.layers.tile.vector.VectorTileLayer.TileLoaderProcessHook;
|
||||
import org.oscim.map.Map;
|
||||
import org.oscim.renderer.elements.ElementLayers;
|
||||
import org.oscim.renderer.elements.SymbolItem;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.graphics.g3d.Model;
|
||||
import com.badlogic.gdx.graphics.g3d.model.Node;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
public class Poi3DLayer extends Layer implements Map.UpdateListener {
|
||||
|
||||
static final Logger log = LoggerFactory.getLogger(Poi3DLayer.class);
|
||||
|
||||
static class Poi3DTileData extends TileData {
|
||||
public final List<SymbolItem> symbols = new List<SymbolItem>();
|
||||
|
||||
@Override
|
||||
protected void dispose() {
|
||||
SymbolItem.pool.releaseAll(symbols.clear());
|
||||
}
|
||||
}
|
||||
|
||||
final static String POI_DATA = Poi3DLayer.class.getSimpleName();
|
||||
final static Tag TREE_TAG = new Tag("natural", "tree");
|
||||
|
||||
private Poi3DTileData get(MapTile tile) {
|
||||
Poi3DTileData ld = (Poi3DTileData) tile.getData(POI_DATA);
|
||||
if (ld == null) {
|
||||
ld = new Poi3DTileData();
|
||||
tile.addData(POI_DATA, ld);
|
||||
}
|
||||
return ld;
|
||||
}
|
||||
|
||||
GdxRenderer3D g3d;
|
||||
VectorTileLayer mTileLayer;
|
||||
|
||||
public Poi3DLayer(Map map, VectorTileLayer tileLayer) {
|
||||
super(map);
|
||||
tileLayer.addHook(new TileLoaderProcessHook() {
|
||||
|
||||
@Override
|
||||
public boolean process(MapTile tile, ElementLayers layers, MapElement element) {
|
||||
|
||||
if (!element.tags.contains(TREE_TAG))
|
||||
return false;
|
||||
|
||||
Poi3DTileData td = get(tile);
|
||||
PointF p = element.getPoint(0);
|
||||
SymbolItem s = SymbolItem.pool.get();
|
||||
s.x = p.x;
|
||||
s.y = p.y;
|
||||
td.symbols.push(s);
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
mTileLayer = tileLayer;
|
||||
|
||||
mRenderer = g3d = new GdxRenderer3D(mMap);
|
||||
|
||||
// Material mat = new
|
||||
// Material(ColorAttribute.createDiffuse(Color.BLUE));
|
||||
// ModelBuilder modelBuilder = new ModelBuilder();
|
||||
// long attributes = Usage.Position | Usage.Normal |
|
||||
// Usage.TextureCoordinates;
|
||||
|
||||
// mModel = modelBuilder.createSphere(10f, 10f, 10f, 12, 12,
|
||||
// mat, attributes);
|
||||
|
||||
assets = new AssetManager();
|
||||
assets.load("data/g3d/treeA.g3dj", Model.class);
|
||||
loading = true;
|
||||
}
|
||||
|
||||
TileSet mTileSet = new TileSet();
|
||||
TileSet mPrevTiles = new TileSet();
|
||||
|
||||
LinkedHashMap<Tile, Array<SharedModel>> mTileMap =
|
||||
new LinkedHashMap<Tile, Array<SharedModel>>();
|
||||
|
||||
boolean loading;
|
||||
Model mModel;
|
||||
AssetManager assets;
|
||||
|
||||
private void doneLoading() {
|
||||
Model model = assets.get("data/g3d/treeA.g3dj", Model.class);
|
||||
for (int i = 0; i < model.nodes.size; i++) {
|
||||
Node node = model.nodes.get(i);
|
||||
|
||||
if (node.id.equals("treeA_root")) {
|
||||
node.rotation.setFromAxis(1, 0, 0, 90);
|
||||
mModel = model;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
loading = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapEvent(Event ev, MapPosition pos) {
|
||||
|
||||
if (ev == Map.CLEAR_EVENT) {
|
||||
mTileSet = new TileSet();
|
||||
mPrevTiles = new TileSet();
|
||||
mTileMap = new LinkedHashMap<Tile, Array<SharedModel>>();
|
||||
synchronized (g3d) {
|
||||
g3d.instances.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (loading && assets.update()) {
|
||||
doneLoading();
|
||||
// Renderable renderable = new Renderable();
|
||||
// new SharedModel(mModel).getRenderable(renderable);
|
||||
// Shader shader = new DefaultShader(renderable, true, false,
|
||||
// false, false, 1, 0, 0, 0);
|
||||
}
|
||||
if (loading)
|
||||
return;
|
||||
|
||||
// log.debug("update");
|
||||
|
||||
mTileLayer.tileRenderer().getVisibleTiles(mTileSet);
|
||||
|
||||
if (mTileSet.cnt == 0) {
|
||||
mTileSet.releaseTiles();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
Array<SharedModel> added = new Array<SharedModel>();
|
||||
Array<SharedModel> removed = new Array<SharedModel>();
|
||||
|
||||
for (int i = 0; i < mTileSet.cnt; i++) {
|
||||
MapTile t = mTileSet.tiles[i];
|
||||
if (mPrevTiles.contains(t))
|
||||
continue;
|
||||
|
||||
Array<SharedModel> instances = new Array<SharedModel>();
|
||||
|
||||
Poi3DTileData ld = (Poi3DTileData) t.getData(POI_DATA);
|
||||
if (ld == null)
|
||||
continue;
|
||||
|
||||
for (SymbolItem it : ld.symbols) {
|
||||
|
||||
SharedModel inst = new SharedModel(mModel);
|
||||
inst.userData = it;
|
||||
// float r = 0.5f + 0.5f * (float) Math.random();
|
||||
// float g = 0.5f + 0.5f * (float) Math.random();
|
||||
// float b = 0.5f + 0.5f * (float) Math.random();
|
||||
|
||||
// inst.transform.setTranslation(new Vector3(it.x, it.y,
|
||||
// 10));
|
||||
// inst.materials.get(0).set(ColorAttribute.createDiffuse(r,
|
||||
// g, b, 0.8f));
|
||||
instances.add(inst);
|
||||
added.add(inst);
|
||||
}
|
||||
|
||||
if (instances.size == 0)
|
||||
continue;
|
||||
|
||||
log.debug("add " + t + " " + instances.size);
|
||||
|
||||
changed = true;
|
||||
|
||||
mTileMap.put(t, instances);
|
||||
}
|
||||
|
||||
for (int i = 0; i < mPrevTiles.cnt; i++) {
|
||||
MapTile t = mPrevTiles.tiles[i];
|
||||
if (mTileSet.contains(t))
|
||||
continue;
|
||||
|
||||
Array<SharedModel> instances = mTileMap.get(t);
|
||||
if (instances == null)
|
||||
continue;
|
||||
|
||||
changed = true;
|
||||
|
||||
removed.addAll(instances);
|
||||
mTileMap.remove(t);
|
||||
log.debug("remove " + t);
|
||||
}
|
||||
|
||||
mPrevTiles.releaseTiles();
|
||||
|
||||
int zoom = mTileSet.tiles[0].zoomLevel;
|
||||
|
||||
TileSet tmp = mPrevTiles;
|
||||
mPrevTiles = mTileSet;
|
||||
mTileSet = tmp;
|
||||
|
||||
if (!changed)
|
||||
return;
|
||||
|
||||
// scale aka tree height
|
||||
float scale = (float) (1f / (1 << (17 - zoom))) * 8;
|
||||
|
||||
double tileX = (pos.x * (Tile.SIZE << zoom));
|
||||
double tileY = (pos.y * (Tile.SIZE << zoom));
|
||||
|
||||
synchronized (g3d) {
|
||||
|
||||
for (Entry<Tile, Array<SharedModel>> e : mTileMap.entrySet()) {
|
||||
Tile t = e.getKey();
|
||||
|
||||
float dx = (float) (t.tileX * Tile.SIZE - tileX);
|
||||
float dy = (float) (t.tileY * Tile.SIZE - tileY);
|
||||
|
||||
for (SharedModel inst : e.getValue()) {
|
||||
SymbolItem it = (SymbolItem) inst.userData;
|
||||
|
||||
// variable height
|
||||
float s = scale + (it.x * it.y) % 3;
|
||||
float r = (it.x * it.y) % 360;
|
||||
|
||||
inst.transform.idt();
|
||||
inst.transform.scale(s, s, s);
|
||||
inst.transform.translate((dx + it.x) / s, (dy + it.y) / s, 0);
|
||||
inst.transform.rotate(0, 0, 1, r);
|
||||
|
||||
// inst.transform.setToTranslationAndScaling((dx +
|
||||
// it.x), (dy + it.y),
|
||||
// 0, s, s, s);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
g3d.instances.removeAll(removed, true);
|
||||
g3d.instances.addAll(added);
|
||||
g3d.cam.setMapPosition(pos.x, pos.y, 1 << zoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
461
vtm-playground/src/org/oscim/test/gdx/poi3d/SharedModel.java
Normal file
461
vtm-playground/src/org/oscim/test/gdx/poi3d/SharedModel.java
Normal file
@@ -0,0 +1,461 @@
|
||||
package org.oscim.test.gdx.poi3d;
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.Material;
|
||||
import com.badlogic.gdx.graphics.g3d.Model;
|
||||
import com.badlogic.gdx.graphics.g3d.ModelBatch;
|
||||
import com.badlogic.gdx.graphics.g3d.Renderable;
|
||||
import com.badlogic.gdx.graphics.g3d.RenderableProvider;
|
||||
import com.badlogic.gdx.graphics.g3d.model.Animation;
|
||||
import com.badlogic.gdx.graphics.g3d.model.MeshPart;
|
||||
import com.badlogic.gdx.graphics.g3d.model.Node;
|
||||
import com.badlogic.gdx.graphics.g3d.model.NodeAnimation;
|
||||
import com.badlogic.gdx.graphics.g3d.model.NodeKeyframe;
|
||||
import com.badlogic.gdx.graphics.g3d.model.NodePart;
|
||||
import com.badlogic.gdx.math.Matrix4;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.math.collision.BoundingBox;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.ArrayMap;
|
||||
import com.badlogic.gdx.utils.ObjectMap;
|
||||
import com.badlogic.gdx.utils.Pool;
|
||||
|
||||
/** An instance of a {@link Model}, allows to specify global transform and modify the materials, as it
|
||||
* has a copy of the model's materials. Multiple instances can be created from the same Model,
|
||||
* all sharing the meshes and textures of the Model. The Model owns the meshes and textures, to
|
||||
* dispose of these, the Model has to be disposed. Therefor, the Model must outlive all its ModelInstances</p>
|
||||
*
|
||||
* The ModelInstance creates a full copy of all materials, nodes and animations.
|
||||
* @author badlogic, xoppa */
|
||||
public class SharedModel implements RenderableProvider {
|
||||
/** the materials of the model, used by nodes that have a graphical representation FIXME not sure if superfluous, allows modification of materials without having to traverse the nodes **/
|
||||
public final Array<Material> materials = new Array<Material>();
|
||||
/** root nodes of the model **/
|
||||
public final Array<Node> nodes = new Array<Node>();
|
||||
/** animations of the model, modifying node transformations **/
|
||||
public final Array<Animation> animations = new Array<Animation>();
|
||||
/** the {@link Model} this instances derives from **/
|
||||
public final Model model;
|
||||
/** the world transform **/
|
||||
public Matrix4 transform;
|
||||
/** user definable value, which is passed to the shader. */
|
||||
public Object userData;
|
||||
|
||||
/** Constructs a new ModelInstance with all nodes and materials of the given model.
|
||||
* @param model The {@link Model} to create an instance of. */
|
||||
public SharedModel(final Model model) {
|
||||
this(model, (String[])null);
|
||||
}
|
||||
|
||||
/** @param model The source {@link Model}
|
||||
* @param nodeId The ID of the root {@link Node} of the {@link Model} for the instance to contain
|
||||
* @param mergeTransform True to apply the source node transform to the instance transform, resetting the node transform. */
|
||||
public SharedModel(final Model model, final String nodeId, boolean mergeTransform) {
|
||||
this(model, null, nodeId, false, false, mergeTransform);
|
||||
}
|
||||
|
||||
/** @param model The source {@link Model}
|
||||
* @param transform The {@link Matrix4} instance for this ModelInstance to reference or null to create a new matrix.
|
||||
* @param nodeId The ID of the root {@link Node} of the {@link Model} for the instance to contain
|
||||
* @param mergeTransform True to apply the source node transform to the instance transform, resetting the node transform. */
|
||||
public SharedModel(final Model model, final Matrix4 transform, final String nodeId, boolean mergeTransform) {
|
||||
this(model, transform, nodeId, false, false, mergeTransform);
|
||||
}
|
||||
|
||||
/** Recursively searches the mode for the specified node.
|
||||
* @param model The source {@link Model}
|
||||
* @param nodeId The ID of the {@link Node} within the {@link Model} for the instance to contain
|
||||
* @param parentTransform True to apply the parent's node transform to the instance (only applicable if recursive is true).
|
||||
* @param mergeTransform True to apply the source node transform to the instance transform, resetting the node transform. */
|
||||
public SharedModel(final Model model, final String nodeId, boolean parentTransform, boolean mergeTransform) {
|
||||
this(model, null, nodeId, true, parentTransform, mergeTransform);
|
||||
}
|
||||
|
||||
/** Recursively searches the mode for the specified node.
|
||||
* @param model The source {@link Model}
|
||||
* @param transform The {@link Matrix4} instance for this ModelInstance to reference or null to create a new matrix.
|
||||
* @param nodeId The ID of the {@link Node} within the {@link Model} for the instance to contain
|
||||
* @param parentTransform True to apply the parent's node transform to the instance (only applicable if recursive is true).
|
||||
* @param mergeTransform True to apply the source node transform to the instance transform, resetting the node transform. */
|
||||
public SharedModel(final Model model, final Matrix4 transform, final String nodeId, boolean parentTransform, boolean mergeTransform) {
|
||||
this(model, transform, nodeId, true, parentTransform, mergeTransform);
|
||||
}
|
||||
|
||||
/** @param model The source {@link Model}
|
||||
* @param nodeId The ID of the {@link Node} within the {@link Model} for the instance to contain
|
||||
* @param recursive True to recursively search the Model's node tree, false to only search for a root node
|
||||
* @param parentTransform True to apply the parent's node transform to the instance (only applicable if recursive is true).
|
||||
* @param mergeTransform True to apply the source node transform to the instance transform, resetting the node transform. */
|
||||
public SharedModel(final Model model, final String nodeId, boolean recursive, boolean parentTransform, boolean mergeTransform) {
|
||||
this(model, null, nodeId, recursive, parentTransform, mergeTransform);
|
||||
}
|
||||
|
||||
/** @param model The source {@link Model}
|
||||
* @param transform The {@link Matrix4} instance for this ModelInstance to reference or null to create a new matrix.
|
||||
* @param nodeId The ID of the {@link Node} within the {@link Model} for the instance to contain
|
||||
* @param recursive True to recursively search the Model's node tree, false to only search for a root node
|
||||
* @param parentTransform True to apply the parent's node transform to the instance (only applicable if recursive is true).
|
||||
* @param mergeTransform True to apply the source node transform to the instance transform, resetting the node transform. */
|
||||
public SharedModel(final Model model, final Matrix4 transform, final String nodeId, boolean recursive, boolean parentTransform, boolean mergeTransform) {
|
||||
this.model = model;
|
||||
this.transform = transform == null ? new Matrix4() : transform;
|
||||
nodePartBones.clear();
|
||||
Node copy, node = model.getNode(nodeId, recursive);
|
||||
this.nodes.add(copy = copyNode(null, node));
|
||||
if (mergeTransform) {
|
||||
this.transform.mul(parentTransform ? node.globalTransform : node.localTransform);
|
||||
copy.translation.set(0,0,0);
|
||||
copy.rotation.idt();
|
||||
copy.scale.set(1,1,1);
|
||||
} else if (parentTransform && copy.parent != null)
|
||||
this.transform.mul(node.parent.globalTransform);
|
||||
setBones();
|
||||
copyAnimations(model.animations);
|
||||
calculateTransforms();
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance with only the specified nodes and materials of the given model. */
|
||||
public SharedModel(final Model model, final String... rootNodeIds) {
|
||||
this(model, null, rootNodeIds);
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance with only the specified nodes and materials of the given model. */
|
||||
public SharedModel(final Model model, final Matrix4 transform, final String... rootNodeIds) {
|
||||
this.model = model;
|
||||
this.transform = transform == null ? new Matrix4() : transform;
|
||||
if (rootNodeIds == null)
|
||||
copyNodes(model.nodes);
|
||||
else
|
||||
copyNodes(model.nodes, rootNodeIds);
|
||||
copyAnimations(model.animations);
|
||||
calculateTransforms();
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance with only the specified nodes and materials of the given model. */
|
||||
public SharedModel(final Model model, final Array<String> rootNodeIds) {
|
||||
this(model, null, rootNodeIds);
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance with only the specified nodes and materials of the given model. */
|
||||
public SharedModel(final Model model, final Matrix4 transform, final Array<String> rootNodeIds) {
|
||||
this.model = model;
|
||||
this.transform = transform == null ? new Matrix4() : transform;
|
||||
copyNodes(model.nodes, rootNodeIds);
|
||||
copyAnimations(model.animations);
|
||||
calculateTransforms();
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance at the specified position. */
|
||||
public SharedModel(final Model model, Vector3 position) {
|
||||
this(model);
|
||||
this.transform.setToTranslation(position);
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance at the specified position. */
|
||||
public SharedModel(final Model model, float x, float y, float z) {
|
||||
this(model);
|
||||
this.transform.setToTranslation(x, y, z);
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance with the specified transform. */
|
||||
public SharedModel(final Model model, Matrix4 transform) {
|
||||
this(model, transform, (String[])null);
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance which is an copy of the specified ModelInstance. */
|
||||
public SharedModel(SharedModel copyFrom) {
|
||||
this(copyFrom, copyFrom.transform.cpy());
|
||||
}
|
||||
|
||||
/** Constructs a new ModelInstance which is an copy of the specified ModelInstance. */
|
||||
public SharedModel(SharedModel copyFrom, final Matrix4 transform) {
|
||||
this.model = copyFrom.model;
|
||||
this.transform = transform == null ? new Matrix4() : transform;
|
||||
copyNodes(copyFrom.nodes);
|
||||
copyAnimations(copyFrom.animations);
|
||||
calculateTransforms();
|
||||
}
|
||||
|
||||
/** @return A newly created ModelInstance which is a copy of this ModelInstance */
|
||||
public SharedModel copy() {
|
||||
return new SharedModel(this);
|
||||
}
|
||||
|
||||
private ObjectMap<NodePart, ArrayMap<Node, Matrix4>> nodePartBones = new ObjectMap<NodePart, ArrayMap<Node, Matrix4>>();
|
||||
private void copyNodes (Array<Node> nodes) {
|
||||
nodePartBones.clear();
|
||||
for(int i = 0, n = nodes.size; i<n; ++i) {
|
||||
final Node node = nodes.get(i);
|
||||
this.nodes.add(copyNode(null, node));
|
||||
}
|
||||
setBones();
|
||||
}
|
||||
|
||||
private void copyNodes (Array<Node> nodes, final String... nodeIds) {
|
||||
nodePartBones.clear();
|
||||
for(int i = 0, n = nodes.size; i<n; ++i) {
|
||||
final Node node = nodes.get(i);
|
||||
for (final String nodeId : nodeIds) {
|
||||
if (nodeId.equals(node.id)) {
|
||||
this.nodes.add(copyNode(null, node));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
setBones();
|
||||
}
|
||||
|
||||
private void copyNodes (Array<Node> nodes, final Array<String> nodeIds) {
|
||||
nodePartBones.clear();
|
||||
for(int i = 0, n = nodes.size; i<n; ++i) {
|
||||
final Node node = nodes.get(i);
|
||||
for (final String nodeId : nodeIds) {
|
||||
if (nodeId.equals(node.id)) {
|
||||
this.nodes.add(copyNode(null, node));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
setBones();
|
||||
}
|
||||
|
||||
private void setBones() {
|
||||
for (ObjectMap.Entry<NodePart,ArrayMap<Node, Matrix4>> e : nodePartBones.entries()) {
|
||||
if (e.key.invBoneBindTransforms == null)
|
||||
e.key.invBoneBindTransforms = new ArrayMap<Node, Matrix4>(true, e.value.size, Node.class, Matrix4.class);
|
||||
e.key.invBoneBindTransforms.clear();
|
||||
|
||||
for (final ObjectMap.Entry<Node, Matrix4> b : e.value.entries())
|
||||
e.key.invBoneBindTransforms.put(getNode(b.key.id), b.value); // Share the inv bind matrix with the model
|
||||
|
||||
e.key.bones = new Matrix4[e.value.size];
|
||||
for (int i = 0; i < e.key.bones.length; i++)
|
||||
e.key.bones[i] = new Matrix4();
|
||||
}
|
||||
}
|
||||
|
||||
private Node copyNode(Node parent, Node node) {
|
||||
Node copy = new Node();
|
||||
copy.id = node.id;
|
||||
//copy.boneId = node.boneId;
|
||||
copy.parent = parent;
|
||||
copy.translation.set(node.translation);
|
||||
copy.rotation.set(node.rotation);
|
||||
copy.scale.set(node.scale);
|
||||
copy.localTransform.set(node.localTransform);
|
||||
copy.globalTransform.set(node.globalTransform);
|
||||
for(NodePart nodePart: node.parts) {
|
||||
copy.parts.add(copyNodePart(nodePart));
|
||||
}
|
||||
for(Node child: node.children) {
|
||||
copy.children.add(copyNode(copy, child));
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
private NodePart copyNodePart (NodePart nodePart) {
|
||||
NodePart copy = new NodePart();
|
||||
copy.meshPart = new MeshPart();
|
||||
copy.meshPart.id = nodePart.meshPart.id;
|
||||
copy.meshPart.indexOffset = nodePart.meshPart.indexOffset;
|
||||
copy.meshPart.numVertices = nodePart.meshPart.numVertices;
|
||||
copy.meshPart.primitiveType = nodePart.meshPart.primitiveType;
|
||||
copy.meshPart.mesh = nodePart.meshPart.mesh;
|
||||
|
||||
if (nodePart.invBoneBindTransforms != null)
|
||||
nodePartBones.put(copy, nodePart.invBoneBindTransforms);
|
||||
|
||||
// final int index = materials.indexOf(nodePart.material, false);
|
||||
// if (index < 0)
|
||||
// materials.add(copy.material = nodePart.material.copy());
|
||||
// else
|
||||
// copy.material = materials.get(index);
|
||||
//
|
||||
copy.material = nodePart.material;
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
private void copyAnimations (final Iterable<Animation> source) {
|
||||
for (final Animation anim : source) {
|
||||
Animation animation = new Animation();
|
||||
animation.id = anim.id;
|
||||
animation.duration = anim.duration;
|
||||
for (final NodeAnimation nanim : anim.nodeAnimations) {
|
||||
final Node node = getNode(nanim.node.id);
|
||||
if (node == null)
|
||||
continue;
|
||||
NodeAnimation nodeAnim = new NodeAnimation();
|
||||
nodeAnim.node = node;
|
||||
for (final NodeKeyframe kf : nanim.keyframes) {
|
||||
NodeKeyframe keyframe = new NodeKeyframe();
|
||||
keyframe.keytime = kf.keytime;
|
||||
keyframe.rotation.set(kf.rotation);
|
||||
keyframe.scale.set(kf.scale);
|
||||
keyframe.translation.set(kf.translation);
|
||||
nodeAnim.keyframes.add(keyframe);
|
||||
}
|
||||
if (nodeAnim.keyframes.size > 0)
|
||||
animation.nodeAnimations.add(nodeAnim);
|
||||
}
|
||||
if (animation.nodeAnimations.size > 0)
|
||||
animations.add(animation);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Traverses the Node hierarchy and collects {@link Renderable} instances for every
|
||||
* node with a graphical representation. Renderables are obtained from the provided
|
||||
* pool. The resulting array can be rendered via a {@link ModelBatch}.
|
||||
*
|
||||
* @param renderables the output array
|
||||
* @param pool the pool to obtain Renderables from
|
||||
*/
|
||||
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
|
||||
for(Node node: nodes) {
|
||||
getRenderables(node, renderables, pool);
|
||||
}
|
||||
}
|
||||
|
||||
/** @return The renderable of the first node's first part. */
|
||||
public Renderable getRenderable(final Renderable out) {
|
||||
return getRenderable(out, nodes.get(0));
|
||||
}
|
||||
|
||||
/** @return The renderable of the node's first part. */
|
||||
public Renderable getRenderable(final Renderable out, final Node node) {
|
||||
return getRenderable(out, node, node.parts.get(0));
|
||||
}
|
||||
|
||||
public Renderable getRenderable(final Renderable out, final Node node, final NodePart nodePart) {
|
||||
nodePart.setRenderable(out);
|
||||
if (nodePart.bones == null && transform != null)
|
||||
out.worldTransform.set(transform).mul(node.globalTransform);
|
||||
else if (transform != null)
|
||||
out.worldTransform.set(transform);
|
||||
else
|
||||
out.worldTransform.idt();
|
||||
out.userData = userData;
|
||||
return out;
|
||||
}
|
||||
|
||||
protected void getRenderables(Node node, Array<Renderable> renderables, Pool<Renderable> pool) {
|
||||
if(node.parts.size > 0) {
|
||||
for(NodePart nodePart: node.parts) {
|
||||
renderables.add(getRenderable(pool.obtain(), node, nodePart));
|
||||
}
|
||||
}
|
||||
|
||||
for(Node child: node.children) {
|
||||
getRenderables(child, renderables, pool);
|
||||
}
|
||||
}
|
||||
|
||||
/** Calculates the local and world transform of all {@link Node} instances in this model, recursively.
|
||||
* First each {@link Node#localTransform} transform is calculated based on the translation, rotation and
|
||||
* scale of each Node. Then each {@link Node#calculateWorldTransform()}
|
||||
* is calculated, based on the parent's world transform and the local transform of each Node.
|
||||
* Finally, the animation bone matrices are updated accordingly.</p>
|
||||
*
|
||||
* This method can be used to recalculate all transforms if any of the Node's local properties (translation, rotation, scale)
|
||||
* was modified.
|
||||
*/
|
||||
public void calculateTransforms() {
|
||||
final int n = nodes.size;
|
||||
for(int i = 0; i < n; i++) {
|
||||
nodes.get(i).calculateTransforms(true);
|
||||
}
|
||||
for(int i = 0; i < n; i++) {
|
||||
nodes.get(i).calculateBoneTransforms(true);
|
||||
}
|
||||
}
|
||||
|
||||
/** Calculate the bounding box of this model instance.
|
||||
* This is a potential slow operation, it is advised to cache the result.
|
||||
* @param out the {@link BoundingBox} that will be set with the bounds.
|
||||
* @return the out parameter for chaining */
|
||||
public BoundingBox calculateBoundingBox(final BoundingBox out) {
|
||||
out.inf();
|
||||
return extendBoundingBox(out);
|
||||
}
|
||||
|
||||
/** Extends the bounding box with the bounds of this model instance.
|
||||
* This is a potential slow operation, it is advised to cache the result.
|
||||
* @param out the {@link BoundingBox} that will be extended with the bounds.
|
||||
* @return the out parameter for chaining */
|
||||
public BoundingBox extendBoundingBox(final BoundingBox out) {
|
||||
final int n = nodes.size;
|
||||
for(int i = 0; i < n; i++)
|
||||
nodes.get(i).extendBoundingBox(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** @param id The ID of the animation to fetch (case sensitive).
|
||||
* @return The {@link Animation} with the specified id, or null if not available. */
|
||||
public Animation getAnimation(final String id) {
|
||||
return getAnimation(id, true);
|
||||
}
|
||||
|
||||
/** @param id The ID of the animation to fetch.
|
||||
* @param ignoreCase whether to use case sensitivity when comparing the animation id.
|
||||
* @return The {@link Animation} with the specified id, or null if not available. */
|
||||
public Animation getAnimation(final String id, boolean ignoreCase) {
|
||||
final int n = animations.size;
|
||||
Animation animation;
|
||||
if (ignoreCase) {
|
||||
for (int i = 0; i < n; i++)
|
||||
if ((animation = animations.get(i)).id.equalsIgnoreCase(id))
|
||||
return animation;
|
||||
} else {
|
||||
for (int i = 0; i < n; i++)
|
||||
if ((animation = animations.get(i)).id.equals(id))
|
||||
return animation;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// /** @param id The ID of the material to fetch.
|
||||
// * @return The {@link Material} with the specified id, or null if not available. */
|
||||
// public Material getMaterial(final String id) {
|
||||
// return getMaterial(id, true);
|
||||
// }
|
||||
//
|
||||
// /** @param id The ID of the material to fetch.
|
||||
// * @param ignoreCase whether to use case sensitivity when comparing the material id.
|
||||
// * @return The {@link Material} with the specified id, or null if not available. */
|
||||
// public Material getMaterial(final String id, boolean ignoreCase) {
|
||||
// final int n = materials.size;
|
||||
// Material material;
|
||||
// if (ignoreCase) {
|
||||
// for (int i = 0; i < n; i++)
|
||||
// if ((material = materials.get(i)).id.equalsIgnoreCase(id))
|
||||
// return material;
|
||||
// } else {
|
||||
// for (int i = 0; i < n; i++)
|
||||
// if ((material = materials.get(i)).id.equals(id))
|
||||
// return material;
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
/** @param id The ID of the node to fetch.
|
||||
* @return The {@link Node} with the specified id, or null if not found. */
|
||||
public Node getNode(final String id) {
|
||||
return getNode(id, true);
|
||||
}
|
||||
|
||||
/** @param id The ID of the node to fetch.
|
||||
* @param recursive false to fetch a root node only, true to search the entire node tree for the specified node.
|
||||
* @return The {@link Node} with the specified id, or null if not found. */
|
||||
public Node getNode(final String id, boolean recursive) {
|
||||
return getNode(id, recursive, false);
|
||||
}
|
||||
|
||||
/** @param id The ID of the node to fetch.
|
||||
* @param recursive false to fetch a root node only, true to search the entire node tree for the specified node.
|
||||
* @param ignoreCase whether to use case sensitivity when comparing the node id.
|
||||
* @return The {@link Node} with the specified id, or null if not found. */
|
||||
public Node getNode(final String id, boolean recursive, boolean ignoreCase) {
|
||||
return Node.getNode(nodes, id, recursive, ignoreCase);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user