Render to Texture

Is there a game state for this or a way to do this with game states?

Sorry, I realized now that its possible just to use a regular gamestate. What I should've asked (an should've asked in the General section) was how do you do something like simpleUpdate or simpleRender using a game state?

aeneas said:

What I should've asked (an should've asked in the General section) was how do you do something like simpleUpdate or simpleRender using a game state?


Basically you just put everything you otherwise put in your simpleUpdate and simpleRender to your GameState's update and render methods. Check the source of DebugGameState for example.

I'm still a bit confused. The only way that I could get the render to texture to work was to implement everything in the GameTaskQueue. This is not good code because now I do not know how to update the render. I am a total noob at this, please help!


package renderingtests;

import java.util.concurrent.Callable;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.TextureRenderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.system.GameSettings;
import com.jme.util.GameTaskQueueManager;
import com.jmex.game.StandardGame;
import com.jmex.game.state.DebugGameState;
import com.jmex.game.state.GameStateManager;

public class RenderingToTexture {

   public static void main(String[] args) throws Exception {
      // Create the game
      StandardGame game = new StandardGame("TestGame");

      // Change the settings
      // GameSettingsPanel.prompt(game.getSettings());
      GameSettings settings = game.getSettings();
      settings.setFramerate(72);
      settings.setFullscreen(true);
      settings.setDepth(32);
      settings.setHeight(1024);
      settings.setWidth(1280);

      // Start the game thread
      game.start(); // Start the game thread

      // Create a new gameState
      final DebugGameState gameState = new DebugGameState();
      GameStateManager.getInstance().attachChild(gameState);
      gameState.setActive(true);



      GameTaskQueueManager.getManager().update(new Callable<Object>() {
         public Object call() throws Exception {
            Box realBox = new Box("mybox", new Vector3f(), 4, 4, 4);
            gameState.getRootNode().attachChild(realBox);

            Box box1 = new Box("mybox", new Vector3f(5, 0, 0), 2, 2, 2);
            Box box2 = new Box("mybox", new Vector3f(-5, 0, 0), 2, 2, 2);

            Node fakeScene = new Node("Fake Node");
            fakeScene.setLocalTranslation(-7, 0, 0);
            fakeScene.attachChild(box1);
            fakeScene.attachChild(box2);

            // CloneImportExport cloner = new CloneImportExport();
            // cloner.saveClone(fakeScene);
            
            TextureRenderer tRenderer = DisplaySystem.getDisplaySystem().createTextureRenderer(512, 512,
                  TextureRenderer.RENDER_TEXTURE_2D);
            tRenderer.setBackgroundColor(new ColorRGBA(.667f, .667f, .851f, 1f));
            Texture fakeTex = new Texture();
              fakeTex.setWrap(Texture.WM_CLAMP_S_CLAMP_T);
              if ( tRenderer.isSupported() ) {
                  tRenderer.setupTexture(fakeTex);
                  tRenderer.getCamera().setLocation(new Vector3f(0, 0, 75f));
              } else {
                  System.out.println("Render to texture not supported!");
              }

            // Now add that texture to the "real" cube.
            TextureState ts = DisplaySystem.getDisplaySystem().getRenderer()
                  .createTextureState();
            ts.setEnabled(true);
            ts.setTexture(fakeTex, 0);

            gameState.getRootNode().setRenderState(ts);

            fakeScene.updateGeometricState(0.0f, true);
            fakeScene.updateRenderState();

            tRenderer.render(fakeScene, fakeTex);
            
            gameState.getRootNode().updateRenderState();
            
            return null;
         }
      });
   }
}



I guess I am suffering more from a lack of java knowledge -- how can you call objects which are outside of the anonymous class? Before using the GameTaskQueueManager, I was blowing up at

TextureRenderer tRenderer = DisplaySystem.getDisplaySystem().createTextureRenderer(512, 512, TextureRenderer.RENDER_TEXTURE_2D);

but did not know how to set up the variable external to the Callable object yet create the TextureRenderer inside it. Making it final does not allow me to set a new TextureRenderer to it.

you can do it this way, the TextureRenderer needs to be a class variable:


class MyGameState extends BasicGameState {
    private TextureRenderer tRenderer  = null;

    public MyGameState () {
        GameTaskQueueManager.getManager().update(new Callable<Object>() {
            public Object call() throws Exception {
                tRenderer  = DisplaySystem.getDisplaySystem().createTextureRenderer(512, 512, TextureRenderer.RENDER_TEXTURE_2D);
            }
        }
    }
  
    @Override
    public void render(final float tpf) {
        tRenderer.render(tpf);
    }
}