use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method getMeshNormal.
protected Vector3f getMeshNormal(int x, int z) {
int quad = findQuadrant(x, z);
int split = (size + 1) >> 1;
if (children != null) {
for (int i = children.size(); --i >= 0; ) {
Spatial spat = children.get(i);
int col = x;
int row = z;
boolean match = false;
// get the childs quadrant
int childQuadrant = 0;
if (spat instanceof TerrainQuad) {
childQuadrant = ((TerrainQuad) spat).getQuadrant();
} else if (spat instanceof TerrainPatch) {
childQuadrant = ((TerrainPatch) spat).getQuadrant();
}
if (childQuadrant == 1 && (quad & 1) != 0) {
match = true;
} else if (childQuadrant == 2 && (quad & 2) != 0) {
row = z - split + 1;
match = true;
} else if (childQuadrant == 3 && (quad & 4) != 0) {
col = x - split + 1;
match = true;
} else if (childQuadrant == 4 && (quad & 8) != 0) {
col = x - split + 1;
row = z - split + 1;
match = true;
}
if (match) {
if (spat instanceof TerrainQuad) {
return ((TerrainQuad) spat).getMeshNormal(col, row);
} else if (spat instanceof TerrainPatch) {
return ((TerrainPatch) spat).getMeshNormal(col, row);
}
}
}
}
return null;
}
use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method getHeightMap.
public float[] getHeightMap() {
float[] hm = null;
int length = ((size - 1) / 2) + 1;
int area = size * size;
hm = new float[area];
if (getChildren() != null && !getChildren().isEmpty()) {
float[] ul = null, ur = null, bl = null, br = null;
// get the child heightmaps
if (getChild(0) instanceof TerrainPatch) {
for (Spatial s : getChildren()) {
if (((TerrainPatch) s).getQuadrant() == 1)
ul = ((TerrainPatch) s).getHeightMap();
else if (((TerrainPatch) s).getQuadrant() == 2)
bl = ((TerrainPatch) s).getHeightMap();
else if (((TerrainPatch) s).getQuadrant() == 3)
ur = ((TerrainPatch) s).getHeightMap();
else if (((TerrainPatch) s).getQuadrant() == 4)
br = ((TerrainPatch) s).getHeightMap();
}
} else {
ul = getQuad(1).getHeightMap();
bl = getQuad(2).getHeightMap();
ur = getQuad(3).getHeightMap();
br = getQuad(4).getHeightMap();
}
// first upper blocks
for (int y = 0; y < length; y++) {
// rows
for (int x1 = 0; x1 < length; x1++) {
int row = y * size;
hm[row + x1] = ul[y * length + x1];
}
for (int x2 = 1; x2 < length; x2++) {
int row = y * size + length;
hm[row + x2 - 1] = ur[y * length + x2];
}
}
// second lower blocks
int rowOffset = size * length;
for (int y = 1; y < length; y++) {
// rows
for (int x1 = 0; x1 < length; x1++) {
int row = (y - 1) * size;
hm[rowOffset + row + x1] = bl[y * length + x1];
}
for (int x2 = 1; x2 < length; x2++) {
int row = (y - 1) * size + length;
hm[rowOffset + row + x2 - 1] = br[y * length + x2];
}
}
}
return hm;
}
use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method setHeight.
protected void setHeight(List<LocationHeight> locations, boolean overrideHeight) {
if (children == null)
return;
List<LocationHeight> quadLH1 = new ArrayList<LocationHeight>();
List<LocationHeight> quadLH2 = new ArrayList<LocationHeight>();
List<LocationHeight> quadLH3 = new ArrayList<LocationHeight>();
List<LocationHeight> quadLH4 = new ArrayList<LocationHeight>();
Spatial quad1 = null;
Spatial quad2 = null;
Spatial quad3 = null;
Spatial quad4 = null;
// get the child quadrants
for (int i = children.size(); --i >= 0; ) {
Spatial spat = children.get(i);
int childQuadrant = 0;
if (spat instanceof TerrainQuad) {
childQuadrant = ((TerrainQuad) spat).getQuadrant();
} else if (spat instanceof TerrainPatch) {
childQuadrant = ((TerrainPatch) spat).getQuadrant();
}
if (childQuadrant == 1)
quad1 = spat;
else if (childQuadrant == 2)
quad2 = spat;
else if (childQuadrant == 3)
quad3 = spat;
else if (childQuadrant == 4)
quad4 = spat;
}
int split = (size + 1) >> 1;
// distribute each locationHeight into the quadrant it intersects
for (LocationHeight lh : locations) {
int quad = findQuadrant(lh.x, lh.z);
int col = lh.x;
int row = lh.z;
if ((quad & 1) != 0) {
quadLH1.add(lh);
}
if ((quad & 2) != 0) {
row = lh.z - split + 1;
quadLH2.add(new LocationHeight(lh.x, row, lh.h));
}
if ((quad & 4) != 0) {
col = lh.x - split + 1;
quadLH3.add(new LocationHeight(col, lh.z, lh.h));
}
if ((quad & 8) != 0) {
col = lh.x - split + 1;
row = lh.z - split + 1;
quadLH4.add(new LocationHeight(col, row, lh.h));
}
}
// send the locations to the children
if (!quadLH1.isEmpty()) {
if (quad1 instanceof TerrainQuad)
((TerrainQuad) quad1).setHeight(quadLH1, overrideHeight);
else if (quad1 instanceof TerrainPatch)
((TerrainPatch) quad1).setHeight(quadLH1, overrideHeight);
}
if (!quadLH2.isEmpty()) {
if (quad2 instanceof TerrainQuad)
((TerrainQuad) quad2).setHeight(quadLH2, overrideHeight);
else if (quad2 instanceof TerrainPatch)
((TerrainPatch) quad2).setHeight(quadLH2, overrideHeight);
}
if (!quadLH3.isEmpty()) {
if (quad3 instanceof TerrainQuad)
((TerrainQuad) quad3).setHeight(quadLH3, overrideHeight);
else if (quad3 instanceof TerrainPatch)
((TerrainPatch) quad3).setHeight(quadLH3, overrideHeight);
}
if (!quadLH4.isEmpty()) {
if (quad4 instanceof TerrainQuad)
((TerrainQuad) quad4).setHeight(quadLH4, overrideHeight);
else if (quad4 instanceof TerrainPatch)
((TerrainPatch) quad4).setHeight(quadLH4, overrideHeight);
}
}
use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.
the class RenderManager method renderViewPort.
/**
* Renders the {@link ViewPort}.
* <p>
* If the ViewPort is {@link ViewPort#isEnabled() disabled}, this method
* returns immediately. Otherwise, the ViewPort is rendered by
* the following process:<br>
* <ul>
* <li>All {@link SceneProcessor scene processors} that are attached
* to the ViewPort are {@link SceneProcessor#initialize(com.jme3.renderer.RenderManager, com.jme3.renderer.ViewPort) initialized}.
* </li>
* <li>The SceneProcessors' {@link SceneProcessor#preFrame(float) } method
* is called.</li>
* <li>The ViewPort's {@link ViewPort#getOutputFrameBuffer() output framebuffer}
* is set on the Renderer</li>
* <li>The camera is set on the renderer, including its view port parameters.
* (see {@link #setCamera(com.jme3.renderer.Camera, boolean) })</li>
* <li>Any buffers that the ViewPort requests to be cleared are cleared
* and the {@link ViewPort#getBackgroundColor() background color} is set</li>
* <li>Every scene that is attached to the ViewPort is flattened into
* the ViewPort's render queue
* (see {@link #renderViewPortQueues(com.jme3.renderer.ViewPort, boolean) })
* </li>
* <li>The SceneProcessors' {@link SceneProcessor#postQueue(com.jme3.renderer.queue.RenderQueue) }
* method is called.</li>
* <li>The render queue is sorted and then flushed, sending
* rendering commands to the underlying Renderer implementation.
* (see {@link #flushQueue(com.jme3.renderer.ViewPort) })</li>
* <li>The SceneProcessors' {@link SceneProcessor#postFrame(com.jme3.texture.FrameBuffer) }
* method is called.</li>
* <li>The translucent queue of the ViewPort is sorted and then flushed
* (see {@link #renderTranslucentQueue(com.jme3.renderer.ViewPort) })</li>
* <li>If any objects remained in the render queue, they are removed
* from the queue. This is generally objects added to the
* {@link RenderQueue#renderShadowQueue(com.jme3.renderer.queue.RenderQueue.ShadowMode, com.jme3.renderer.RenderManager, com.jme3.renderer.Camera, boolean)
* shadow queue}
* which were not rendered because of a missing shadow renderer.</li>
* </ul>
*
* @param vp View port to render
* @param tpf Time per frame value
*/
public void renderViewPort(ViewPort vp, float tpf) {
if (!vp.isEnabled()) {
return;
}
if (prof != null)
prof.vpStep(VpStep.BeginRender, vp, null);
SafeArrayList<SceneProcessor> processors = vp.getProcessors();
if (processors.isEmpty()) {
processors = null;
}
if (processors != null) {
if (prof != null)
prof.vpStep(VpStep.PreFrame, vp, null);
for (SceneProcessor proc : processors.getArray()) {
if (!proc.isInitialized()) {
proc.initialize(this, vp);
}
proc.setProfiler(this.prof);
if (prof != null)
prof.spStep(SpStep.ProcPreFrame, proc.getClass().getSimpleName());
proc.preFrame(tpf);
}
}
renderer.setFrameBuffer(vp.getOutputFrameBuffer());
setCamera(vp.getCamera(), false);
if (vp.isClearDepth() || vp.isClearColor() || vp.isClearStencil()) {
if (vp.isClearColor()) {
renderer.setBackgroundColor(vp.getBackgroundColor());
}
renderer.clearBuffers(vp.isClearColor(), vp.isClearDepth(), vp.isClearStencil());
}
if (prof != null)
prof.vpStep(VpStep.RenderScene, vp, null);
List<Spatial> scenes = vp.getScenes();
for (int i = scenes.size() - 1; i >= 0; i--) {
renderScene(scenes.get(i), vp);
}
if (processors != null) {
if (prof != null)
prof.vpStep(VpStep.PostQueue, vp, null);
for (SceneProcessor proc : processors.getArray()) {
if (prof != null)
prof.spStep(SpStep.ProcPostQueue, proc.getClass().getSimpleName());
proc.postQueue(vp.getQueue());
}
}
if (prof != null)
prof.vpStep(VpStep.FlushQueue, vp, null);
flushQueue(vp);
if (processors != null) {
if (prof != null)
prof.vpStep(VpStep.PostFrame, vp, null);
for (SceneProcessor proc : processors.getArray()) {
if (prof != null)
prof.spStep(SpStep.ProcPostFrame, proc.getClass().getSimpleName());
proc.postFrame(vp.getOutputFrameBuffer());
}
if (prof != null)
prof.vpStep(VpStep.ProcEndRender, vp, null);
}
//renders the translucent objects queue after processors have been rendered
renderTranslucentQueue(vp);
// clear any remaining spatials that were not rendered.
clearQueue(vp);
if (prof != null)
prof.vpStep(VpStep.EndRender, vp, null);
}
use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.
the class AssetLinkNode method attachLinkedChildren.
/**
* Loads the linked children AssetKeys from the AssetManager and attaches them to the Node<br>
* If they are already attached, they will be reloaded.
* @param manager
*/
public void attachLinkedChildren(AssetManager manager) {
detachLinkedChildren();
for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext(); ) {
ModelKey assetKey = it.next();
Spatial curChild = assetChildren.get(assetKey);
if (curChild != null) {
curChild.removeFromParent();
}
Spatial child = manager.loadAsset(assetKey);
attachChild(child);
assetChildren.put(assetKey, child);
}
}
Aggregations