Question about managing multiple Gamestates in StandardGame

Hello there. I searched the forums but can't find anything addressing my problem. You see, I have Three files in my project. One, core.java is my main file, the second, skyCam.java generates a camera object and a blue test sphere. The third, TerraTest, generates some test terrain for use in the next stage of development. And that's where I encounter my problem.



The code is as fallows.



core.java

package GoKart_CORE;

import com.jmex.game.StandardGame;
import com.jmex.game.state.GameStateManager;
import com.jmex.editors.swing.settings.GameSettingsPanel;

/** Core is the controlling class (Or "Core" ) for the Go Kart Racing Engine.
 *
 * @author Jarred D. Leverton
 */
public class core {
   public static void main(String[] args) throws Exception {
      // Instantiate StandardGame, show the settings screen, and start the game.
      StandardGame game = new StandardGame("Go Kart Racing Engine");
      GameSettingsPanel.prompt(game.getSettings());
      game.start();
      
      //Game State Management
      skyCam playcam = new skyCam();
      terraTest terra = new terraTest();
      
      playcam.setActive(true);
      terra.setActive(false);
      
      GameStateManager.getInstance().attachChild(playcam);
      GameStateManager.getInstance().attachChild(terra);
      
   }
}



skyCam.java

package GoKart_CORE;

import com.jme.bounding.BoundingSphere;
import com.jme.light.DirectionalLight;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.GameState;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.LightState;

/** Skycam creates the players camera and the associated skybox.
 *
 * @author Jarred D. Leverton*/
public class skyCam extends GameState {
   Node rootNode;
   
   int playerNum = 1;
   
   private Camera camera1, camera2, camera3, camera4;
   
   private Sphere mySphere = new Sphere("t3h sf34r", new Vector3f(5,5,5), 25, 25, 5);
   
   //Get the screen dimensions
   DisplaySystem display = DisplaySystem.getDisplaySystem();
   int width = display.getWidth();
   int height = display.getHeight();
      
   //initialize the Camera and Skybox.
   public skyCam() {
      
      testSphere();

      //create the cameras
      if (playerNum == 1){
         
         player1();
           //Tell the camera its viewport size
           camera1.setViewPort(0f, 1f, 0f, 1f);

      }
      else{
         if(playerNum == 2){
         player1();
         camera1.setViewPort(0f, 1f, 0.5f, 1f);
         player2();
         camera2.setViewPort(0f, 1f, 0f, 0.5f);
         }
         else{
            if(playerNum == 3){
               player1();
               camera1.setViewPort(0f, 0.5f, 0.5f, 1f);
               player2();
               camera2.setViewPort(0.5f, 1f, 0.5f, 1f);
               player3();
               camera3.setViewPort(0f, 0.5f, 0f, 0.5f);
            }
            else{
               if(playerNum == 4){
                  player1();
                  camera1.setViewPort(0f, 0.5f, 0.5f, 1f);
                  player2();
                  camera2.setViewPort(0.5f, 1f, 0.5f, 1f);
                  player3();
                  camera3.setViewPort(0f, 0.5f, 0f, 0.5f);
                  player4();
                  camera4.setViewPort(0.5f, 1f, 0f, 0.5f);
               }
               else{
                  System.exit(1);
               }
            }
         }
      }
      
   }
   @Override
   public void render(float tpf) {
      
      Renderer r = display.getRenderer();
      
      display.getRenderer().renderQueue();
      
      // flush previous content
      display.getRenderer().renderQueue();
      
       //render the cameras
      if (playerNum == 1){
         display.getRenderer().setCamera(camera1);
         renderCam();
         }
      else{
         if(playerNum == 2){
            display.getRenderer().setCamera(camera1);
            renderCam();
            display.getRenderer().setCamera(camera2);
            renderCam();
            }
         else{
            if(playerNum == 3){
               display.getRenderer().setCamera(camera1);
               renderCam();
               display.getRenderer().setCamera(camera2);
               renderCam();
               display.getRenderer().setCamera(camera3);
               renderCam();
               }
            else{
               if(playerNum == 4){
                  display.getRenderer().setCamera(camera1);
                  renderCam();
                  display.getRenderer().setCamera(camera2);
                  renderCam();
                  display.getRenderer().setCamera(camera3);
                  renderCam();
                  display.getRenderer().setCamera(camera4);
                  renderCam();
                  }
               else{System.exit(1);}}}}
       
        // Draw the rootNode and all its children.
        r.draw( rootNode );
   }
   
   public void update(float tpf) {
      
   }
   
   public void cleanup() {

   }
   
   public void testSphere() {
      //Create our Sphere
      rootNode = new Node("t3h n0d3z");
        mySphere.setModelBound(new BoundingSphere());
        mySphere.setDefaultColor(ColorRGBA.blue);
        mySphere.updateRenderState();
        mySphere.updateModelBound();
        rootNode.attachChild(mySphere);
 
        LightState lights = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
        DirectionalLight myLight = new DirectionalLight();
        myLight.setDirection(new Vector3f(-1f, .5f, 0f));
        myLight.setAmbient(ColorRGBA.white);
        myLight.setDiffuse(ColorRGBA.blue);
        myLight.setEnabled(true);
        lights.attach(myLight);
        rootNode.setRenderState(lights);
   }
   
   public void player1(){
      camera1 = display.getRenderer().createCamera(width, height);
      camera1.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
      // Place the camera in its position
      camera1.setLocation(new Vector3f(8, 50, 8));
        // Tell the camera what it should be looking at and which way is up
      camera1.lookAt(mySphere.getLocalTranslation(), new Vector3f(0, 1, 0));
   }
   
   public void player2(){
      camera2 = display.getRenderer().createCamera(width, height);
      camera2.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
      // Place the camera in its position
        camera2.setLocation(new Vector3f(8, 50, 8));
      // Tell the camera what it should be looking at and which way is up
        camera2.lookAt(mySphere.getLocalTranslation(), new Vector3f(0, 1, 0));
   }
   
   public void player3(){
      camera3 = display.getRenderer().createCamera(width, height);
      camera3.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
      // Place the camera in its position
        camera3.setLocation(new Vector3f(8, 50, 8));
      // Tell the camera what it should be looking at and which way is up
        camera3.lookAt(mySphere.getLocalTranslation(), new Vector3f(0, 1, 0));
   }
   
   public void player4(){
      camera4 = display.getRenderer().createCamera(width, height);
      camera4.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
      // Place the camera in its position
        camera4.setLocation(new Vector3f(8, 50, 8));
      // Tell the camera what it should be looking at and which way is up
        camera4.lookAt(mySphere.getLocalTranslation(), new Vector3f(0, 1, 0));
   }
   
   public void renderCam(){
       // render camera
       display.getRenderer().getCamera().apply();
       display.getRenderer().getCamera().update();
       // draw and flush
       display.getRenderer().draw(rootNode);
       display.getRenderer().renderQueue();   
   }
}



terraTest.java

package GoKart_CORE;

import java.util.concurrent.Callable;

import javax.swing.ImageIcon;

import jmetest.terrain.TestTerrain;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameState;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.MidPointHeightMap;
import com.jmex.terrain.util.ProceduralTextureGenerator;

public class terraTest extends GameState {
   private Node scene;
   TerrainBlock tb;
   DisplaySystem display = DisplaySystem.getDisplaySystem();
   
   public terraTest() {
      MidPointHeightMap heightMap = new MidPointHeightMap(64, 1f);
        // Scale the data
        Vector3f terrainScale = new Vector3f(4, 0.0575f, 4);
        // create a terrainblock
         tb = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale,heightMap.getHeightMap(), new Vector3f(0, 0, 0));
 
        tb.setModelBound(new BoundingBox());
        tb.updateModelBound();
 
        /**generate a terrain texture with 2 textures */
        ProceduralTextureGenerator pt = new ProceduralTextureGenerator(heightMap);
        pt.addTexture(new ImageIcon(TestTerrain.class.getClassLoader().getResource("jmetest/data/texture/grassb.png")), -128, 0, 128);
        pt.addTexture(new ImageIcon(TestTerrain.class.getClassLoader().getResource("jmetest/data/texture/dirt.jpg")), 0, 128, 255);
        pt.addTexture(new ImageIcon(TestTerrain.class.getClassLoader().getResource("jmetest/data/texture/highest.jpg")), 128, 255,384);pt.createTexture(32);
 
        /** assign the texture to the terrain */
        TextureState ts = display.getRenderer().createTextureState();
        Texture t1 = TextureManager.loadTexture(pt.getImageIcon().getImage(),Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);
        ts.setTexture(t1, 0);
 
        tb.setRenderState(ts);
        
        tb.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        GameTaskQueueManager.getManager().update(new Callable<Object>() {
           public Object call() throws Exception {
              scene.attachChild(tb);
              return null;
            }
        });
   }

   @Override
   public void cleanup() {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void render(float tpf) {
      display.getRenderer().clearBuffers();
      display.getRenderer().draw(scene);
   }

   @Override
   public void update(float tpf) {
      // TODO Auto-generated method stub
      
   }
}



With terraTest removed from the Game State Manager in core.java, the Camera generation code runs fine, and will generate 1, 2, 3, or 4 cameras based on the playerNum variable. The problem seems to stem from "scene.attachChild(tb);" according to my console. When I first implemented the code I got a black screen, with an error pointing to that line. Now, as you can see, I have it wrapped so it's inserted in to the openGL thread, and that caused the skyCam code to render propperly again. but it's still generating an error, and I can't see any terrain.

My goal is to generate a camera (and later a skybox) with Skycam.java, and be able to see the testTerrain generated by TerraTest.java. I know this has to be a simple failure to grasp the mechanics of rendering and node management with Standard Game, but I can't seem to figure out my own mistake. Any help would be greatly appreciated.


      playcam.setActive(true);
terra.setActive(false);

In core.java you have not made the terrain active! Try calling 'setActive(true)' and see what happens.

I forgot to note, that. False is just its current state. When Terra is false, the skycam portion will work properly, but when Terra is true, I just get a black screen.

Just a bump: Still can't solve my problem.

Dont know what problem it is, just a hint:

You'll have to draw your terrain "playerNum" times like you do in the skycam "class". Try to move the terrain things in the other gamestate and if it works there, you can start extracting it again.



regards