Login  Register

JOGL Questions - Guidance on creating Scene class

Posted by glangho on Oct 29, 2015; 3:11pm
URL: https://forum.jogamp.org/JOGL-Questions-Guidance-on-creating-Scene-class-tp4035659.html

Hello,

I'm trying to build more complicated games, i.e., games with multiple "scenes". I ran into a few situations where I can't quite figure out the best way to proceed with JOGL. Was hoping someone would be kind enough to provide some advice. Also looking for answers to a few general questions I had.

1. I'm trying to create a Scene class. A new Scene would be created when rendering/updating certain objects becomes obsolete. For example, the game would start off in a TitleScene, transition into the WorldMapScene, then have the option of either a BattleScene or a MenuScene based on game logic / user input. Assuming I have the necessary management classes to save and recall Scenes on the heap, what's the best/cleanest way to handle this as far as JOGL goes?

As of now, I have a Scene abstract class which implements GLEventListener. In my Main class I initiate GLCanvas, Animator, and attach TitleScene using GLCanvas.addGLEventListener. Then in TitleScene I have a transition to add WorldMapScene under display() using drawable.addGLEventListener and TitleScene is removed as a listener. This effectively sets up TitleScene to be removed from the heap. Each Scene is responsible for initializing any OpenGL, shader, and textures needed, as well as displaying, reshaping, and disposing.

Is this the correct way to go? The other idea I had was to have my Main class implement GLEventListener, store the active scene on the heap and then pass down the GL object through the stack, i.e., calling scene.display(GL gl) under Main.display(GLAutoDrawable drawable). I had reservations of this approach. I'm unsure whether or not Scene would need it's own init, display, reshape, and dispose or only a draw / update methods and let the Main class worry about init, display, reshape, and dispose.

2. If I have a program that uses multiple textures and shaders, what's the best way to initialize them? In my current approach for my Scene class (see question #1) each Scene has it's own init() method so I'm initializing all of my OpenGL configurations, textures, and shaders in each Scene - even if the scene before is using the exact same code. This seems wasteful, but I was reading the JOGL User Guide (not sure how old this is) and it says JOGL may call init() more than once so maybe it's a moot point. This makes me think maybe using the second approach I mention in #1 is more feasible. I could then initialize everything once in Main that I would need (i.e., all shaders and textures) and pass them down either in the stack or as part of the Scene constructor.

3. I have an Event system with a type of event called RemoveVBO. I use it when an object is destroyed/killed. The RenderSystem has a method called removeVBO() which is executed when the RemoveVBO event is received. I use GLContext.getCurrent().getGL() and pass the GL object to GLArrayDataServer.destroy(GL gl). Is it safe to call GLContext.getCurrent().getGL() using either approach from question #1 in this use case? Is there any time calling GLContext to obtain the GL object not a good idea / practice?

4. Does the Texture class support Texture Arrays? I saw the CubeMap example and tried to adopt the same logic for GL_2D_TEXTURE_ARRAY but I got an invalid operation error from the GL_TEXTURE_2D_ARRAY. I was able to fit my textures in one 1024x1024 texture so I ended up not needing it, but wanted to know if it's supported.

            Texture texture = TextureIO.newTexture(GL2.GL_TEXTURE_2D_ARRAY);            
	    File file1 = new File("src/main/resources/images/spritesheet.png");
            File file2 = new File("src/main/resources/images/spritesheet_complete.png");
            TextureData data1 = TextureIO.newTextureData(GLContext.getCurrentGL().getGLProfile(), file1, false, IOUtil.getFileSuffix(file1));
            TextureData data2 = TextureIO.newTextureData(GLContext.getCurrentGL().getGLProfile(), file2, false, IOUtil.getFileSuffix(file2));
            texture.updateImage(gl, data1, 0);
            texture.updateImage(gl, data2, 1);
Thanks!