Viewport rendering

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.

You are not supposed to call render yourself. Theres previews for that, look at TestRenderToTexture…

My case is a little different. I only need the renderToTexture-method when I explicitly call it. And in some cases I need it in the initializing-routine (before the update-rendering-loop starts).



In the engine-loop the method is working properly but there’s this strange behaviour when I use it in the initialization-method.

Yes. An update loop is there to maintain a certain sequence of things happening. Thats why you cant just call some part of that loop at some time. Look at the TestRenderToTexture and TestRenderToMemory tests, you are not required to render the preView to the texture/image each time, you can just check a bool or w/e. Still you have to do it in its appropriate calls (update/render).

hm, I think I got that.



My idea was that the TextureService works like a factory that creates a 2dtexture from a material. I texturized several scene-objects with this factory when I’m starting the game and sometimes while the game is running.



So you say I can’t use this in the initialization?