Hi,
I’ve got an interesting problem. I have a helper class that renders materials onto a single texture. To do this I’ve create a box, put the material on it and then I’m taking a snapshot from this box.
The class looks like the following
[java]
public class TextureService
{
RenderManager renderManager;
DummyGeomGenerator dumGen;
AssetManager assetManager;
private Camera cam;
Node scene;
Geometry box;
ViewPort port;
public void initComponents(RenderManager renderManager,
DummyGeomGenerator dumGen, AssetManager assetManager)
{
this.renderManager = renderManager;
this.dumGen = dumGen;
//create cam
cam = new Camera(128, 128);
cam.setLocation(new Vector3f(0, 0, 10f));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
cam.setFrustumFar(1000);
// cam.setParallelProjection(false);
//create viewport
port = new ViewPort(“toTexture”, cam);
port.setBackgroundColor(ColorRGBA.Green);
port.setClearDepth(true);
port.setClearColor(true);
port.setClearStencil(true);
//create scene
scene = new Node(“scene”);
box = dumGen.createBox(0.5f,0.5f,0.5f); //dumGen is just a little helper to build boxes easily
scene.attachChild(box);
scene.updateGeometricState();
port.attachScene(scene);
this.assetManager = assetManager;
}
/**
- Renders the given Material into a texture2D. To do this, the method
- projects the Material onto a box and takes a snapshot from one of it’s
- sides.
*/
public Texture2D renderToTexture(int size)
{
cam.resize(size, size, true);
cam.update();
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Blue);
box.setLocalTranslation(2,2,2);
box.setMaterial(mat);
scene.updateGeometricState();
Texture2D resultTex = new Texture2D(size, size, Format.RGB8);
FrameBuffer buff = new FrameBuffer(size, size, 0);
buff.setDepthBuffer(Format.Depth);
buff.setColorTexture(resultTex);
port.setOutputFrameBuffer(buff);
renderManager.renderViewPort(port, 0);
return resultTex;
}
[/java]
I want the following:
But I’ve got this:
The green plane is the geometry where I’m showing the rendering-result. As you can see the box is not at the correct position. It doesn’t matter if I move the camera somewhere else or the box. The result is everytime the same.
Now the tricky part: This only comes up when I render the viewport in the simpleInitialize-method. If I render the viewport while the game is initialized and running the result is correct.
2 or 3 months ago this methods where working properly but I don’t know what has changed up to now. I’m working with the latest svn-version.