Barf initializing camera

I'm getting a null pointer when I try to create a Camera.



    DisplaySystem display = DisplaySystem.getDisplaySystem();
    int width = display.getWidth();
    int height = display.getHeight();
    camera = display.getRenderer().createCamera(width, height);



here's the exception


java.lang.NullPointerException
   at org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:1958)
   at com.jme.scene.state.lwjgl.records.RendererRecord.switchMode(RendererRecord.java:25)
   at com.jme.renderer.lwjgl.LWJGLCamera.doFrustumChange(LWJGLCamera.java:172)
   at com.jme.renderer.lwjgl.LWJGLCamera.apply(LWJGLCamera.java:134)
   at com.jme.renderer.lwjgl.LWJGLCamera.<init>(LWJGLCamera.java:75)
   at com.jme.renderer.lwjgl.LWJGLRenderer.createCamera(LWJGLRenderer.java:240)



I've created a SimpleGame using GameType.GRAPHICAL in the constructor and my game state extends GameState.

My width and height are coming in as 640x480.

Anybody seen this before?

Edit: I just noticed I said SimpleGame above.  I'm using StandardGame.  Bet it makes more sense now.

any exceptions happening in

at org.lwjgl.opengl.GL11

are most propably because you dont run the code in the OpenGL thread when using StandardGame or anything multithreaded. at least thats what always happens to me  :expressionless:



try running your code like this:



      GameTaskQueueManager.getManager().update(new Callable<Object>() {
        public Object call() throws Exception {
          CODE HERE;
          return null;
        } // call
      }); // Callable<Object>



if you dont use StandardGame  ... well i dont know if this helps or even works  :wink:

so long,
Andy

But SimeplGame already creates a Camera for you, so you can just call DisplaySystem.getDisplaySystem().getRenderer().getCamera() to tget the camera.

Core-Dump said:

But SimeplGame already creates a Camera for you, so you can just call DisplaySystem.getDisplaySystem().getRenderer().getCamera() to tget the camera.

or just this.cam :)  (in the class extending SimpleGame)
dhdd said:

any exceptions happening in

at org.lwjgl.opengl.GL11

are most propably because you dont run the code in the OpenGL thread when using StandardGame or anything multithreaded. at least thats what always happens to me  :|

true - execute your code in simpleInitGame to prevent this error (but be aware that you don't even need to create a camera, usually)

I've got quite a few cameras that are managed pretty deep in my game objects (it's a racing game).  I'll try what andy said.

if you want to get a reference to a camera you made do:



    Future<Camera> future = GameTaskQueueManager.getManager().update(new Callable<Camera>() {

      public Camera call() throws Exception {
        return CREATE CAMERA HERE
      } // call()
    }); // Callable

    try {
      Camera c = future.get();
..



its a Callabale instead of a Runnable with which you can have return values by using Java Generics.

so long,
Andy

This worked, thanks.



    Future<Object> future = GameTaskQueueManager.getManager().update(new Callable<Object>() {
      public Object call() throws Exception {
        DisplaySystem display = DisplaySystem.getDisplaySystem();
        int width = display.getWidth();
        int height = display.getHeight();
        camera = display.getRenderer().createCamera(width, height);
        //TODO parameterize the frustrum
        camera.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 5500);
        return null;
      }
    });
    future.get();   



Returning the camera out of there is cleaner, doing that next.

Also, don't forget to do DisplaySystem.getDisplaySystem().getRenderer().setCamera(camera) either.  :mrgreen:

I'm going to add a blurb to darkfrog's tutorial about this, since there's no mention of Camera issues.  He's using DebugGameState so that example doesn't need to care.  I'll also be shamelessly stealing your quote about org.lwjgl.opengl.GL11.  That's good stuff to know too.

Edit:  wiki updated.

Incidentally, I'm wondering about whether my approach to this is desirable…



I've got several cameras at various places in my scene graph pointing at different things.  When the user switches between them, I just set the display system renderer camera to the new one and then update that in the game loop.



Is it better to do it that way with several cameras or just have one and move it around?  It seemed easier to me to have multiple ones ready to go so I don't have to unattach one, reattach it somewhere else, and point it toward my stuff every time the user changes views.  I'm not touching anything but the active camera in my game loop so I'm not maintaining all of them at once (I make sure to update the new one when switching).



opinions?

you are looking for the CameraNode.



use CameraNodes at the various points in your scene and when the user switches camera positions, you attach the ONE CAMERA you have to the next CameraNode and so on. after you attached the Camera to the CameraNode call CameraNode.updateGeometricState(…) to be shure it updates the Camera. You can handle the CameraNode similarly than the Camera. As an example you can set the rotation by passing three vectors (up, left and direction), just like you would set the camera itself.



look in the wiki UserGuide, good luck & have fun.



so long,

Andy

dhdd said:

you are looking for the CameraNode.

use CameraNodes at the various points in your scene and when the user switches camera positions, you attach the ONE CAMERA you have to the next CameraNode and so on. after you attached the Camera to the CameraNode call CameraNode.updateGeometricState(...) to be shure it updates the Camera. You can handle the CameraNode similarly than the Camera. As an example you can set the rotation by passing three vectors (up, left and direction), just like you would set the camera itself.

look in the wiki UserGuide, good luck & have fun.

so long,
Andy


Yeah got that part.  All of my cameras are attached to CameraNodes.  It was more of a philosophical/optimization question.  Multiple Cameras attached to CameraNodes and just set them on the renderer, or multiple CameraNodes and a single camera that migrates between them.

All I can come up with is that I'm wasting the unused camera resources, but my switching code is (maybe marginally) simpler.

I've got my cameras split into 4 groups, some are fixed and always point to the same place, some are fixed and pan to follow the boat, some are attached to the boat and point in a constant direction, and some are free floating with their positions and directions controlled by the user.

I've got a key that switches between groups, and then +/- switches cameras within the selected group.  When they switch, I just set the renderer camera to the selected one and call update.  In the game loop, I call update on it.

I could just keep one camera and attach it to the selected camera node when they switch, so it's probably a wash.  I was just wondering how other people are doing it.