My code is based on the code of some of the tests.
Here's an excerpt from the "RenderToTextureTest" code:
sceneGraph.getRenderer().addRenderTarget( renderTarget, rp.getConfig() );
Where the "sceneGraph" reference is:
Xith3DEnvironment env = new Xith3DEnvironment( 0f, 0f, 5f,
0f, 0f, 0f,
0f, 1f, 0f,
this
);
BTW, I'm doing:
renderer.addRenderTarget(new TextureRenderTarget(root, colorAndAlphaTexture, backgroundColor, true), [b]renderPass.getConfig()[/b]);
And I took the care of registering the render target, canvas, etc. in the same order the examples do.
IMHO, the only thing I'm doing really differently is that I'm adding the scene elements at runtime. I have the following methods
public void updateScene(View view, List<Light> lightSources, BranchGroup newRoot) {
for (Light lightSource : lightSources) {
newRoot.addChild(lightSource);
}
// TODO: Check this!
ViewHelper.copy(view, getXith3DEnvironment().getView());
[b]setRoot(newRoot);[/b]
}
private void setRoot(BranchGroup newRoot) {
org.xith3d.render.Renderer renderer;
Colorf backgroundColor;
RenderPass renderPass;
if (root != null) {
getXith3DEnvironment().removeBranchGraph(root);
}
root = newRoot;
renderPass = getXith3DEnvironment().addPerspectiveBranch(root);
backgroundColor = new Colorf(0f, 0f, 0f, 0.1f);
colorAndAlphaTexture = TextureCreator.createTexture(TextureFormat.RGBA, screenWidth, screenHeight, backgroundColor);
depthTexture = TextureCreator.createTexture(TextureFormat.DEPTH, screenWidth, screenHeight);
renderer = getXith3DEnvironment().getRenderer();
renderer.addRenderTarget(new TextureRenderTarget(root, colorAndAlphaTexture, backgroundColor, true), renderPass.getConfig());
renderer.addRenderTarget(new TextureRenderTarget(root, depthTexture), renderPass.getConfig());
}
And he may be called concurrently with the "loopIteration". I created a simple mutex to guard the scene:
private void rebuildScene(View view, List<Light> lightSources, BranchGroup geometries) {
synchronized (renderer.getSceneLock()) {
renderer.updateScene(view, lightSources, geometries);
}
}
(...)
@Override
protected void loopIteration(long gameTime, long frameTime, TimingMode timingMode) {
super.prepareNextFrame(gameTime, frameTime, timingMode);
if (networkManager.startFrame()) {
synchronized (sceneLock) {
super.renderNextFrame(gameTime, frameTime, timingMode);
networkManager.sendColorAlphaAndDepthBuffers(readBytesFromTexture(colorAndAlphaTexture), readBytesFromTexture(depthTexture));
}
}
}
[code]
[/code]