Skip to main content

Simple Java OpenGL Textures Manager



I'm trying to make a simple textures manager for a 2D tile-based project.





What I have is a class that has a hashmap of strings (texture name) and textures. If the name doesn't exist in the hashmap, it uses TextureIO.newTexture(..) to create it and store it into the hashmap.





This is the error I get when I try to load a texture:







Exception in thread "Timer-0" javax.media.opengl.GLException: java.lang.IllegalArgumentException: Illegally formatted version identifier: "null"

at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:271)

at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:410)

at javax.media.opengl.GLCanvas.display(GLCanvas.java:244)

at com.sun.opengl.util.Animator.display(Animator.java:144)

at com.sun.opengl.util.FPSAnimator$1.run(FPSAnimator.java:95)

at java.util.TimerThread.mainLoop(Unknown Source)

at java.util.TimerThread.run(Unknown Source)

Caused by: java.lang.IllegalArgumentException: Illegally formatted version identifier: "null"

at com.sun.opengl.impl.FunctionAvailabilityCache$Version.<init>(FunctionAvailabilityCache.java:354)

at com.sun.opengl.impl.FunctionAvailabilityCache.initAvailableExtensions(FunctionAvailabilityCache.java:133)

at com.sun.opengl.impl.FunctionAvailabilityCache.isExtensionAvailable(FunctionAvailabilityCache.java:104)

at com.sun.opengl.impl.GLContextImpl.isExtensionAvailable(GLContextImpl.java:351)

at com.sun.opengl.impl.GLImpl.isExtensionAvailable(GLImpl.java:30493)

at com.sun.opengl.util.texture.Texture.updateImage(Texture.java:416)

at com.sun.opengl.util.texture.Texture.updateImage(Texture.java:381)

at com.sun.opengl.util.texture.Texture.<init>(Texture.java:182)

at com.sun.opengl.util.texture.TextureIO.newTexture(TextureIO.java:445)

at com.sun.opengl.util.texture.TextureIO.newTexture(TextureIO.java:465)

at dqs.manager.TextureManager.loadTexture(TextureManager.java:31)

at dqs.world.World.draw(World.java:59)

at dqs.scene.GameScene.display(GameScene.java:58)

at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:78)

at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:435)

at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:194)

at javax.media.opengl.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:452)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.awt.EventQueue.access$000(Unknown Source)

at java.awt.EventQueue$1.run(Unknown Source)

at java.awt.EventQueue$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: java.lang.NullPointerException

at com.sun.opengl.impl.FunctionAvailabilityCache$Version.<init>(FunctionAvailabilityCache.java:309)

... 30 more

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at com.sun.opengl.impl.mipmap.Mipmap.closestFit(Mipmap.java:252)

at com.sun.opengl.impl.mipmap.Mipmap.gluBuild2DMipmaps(Mipmap.java:726)

at javax.media.opengl.glu.GLU.gluBuild2DMipmapsJava(GLU.java:1525)

at javax.media.opengl.glu.GLU.gluBuild2DMipmaps(GLU.java:1581)

at com.sun.opengl.util.texture.Texture.updateImage(Texture.java:523)

at com.sun.opengl.util.texture.Texture.updateImage(Texture.java:381)

at com.sun.opengl.util.texture.Texture.<init>(Texture.java:182)

at com.sun.opengl.util.texture.TextureIO.newTexture(TextureIO.java:445)

at com.sun.opengl.util.texture.TextureIO.newTexture(TextureIO.java:465)

at dqs.manager.TextureManager.loadTexture(TextureManager.java:31)

at dqs.world.World.draw(World.java:59)

at dqs.scene.GameScene.display(GameScene.java:58)

at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:78)

at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:435)

at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:194)

at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:412)

at javax.media.opengl.GLCanvas.display(GLCanvas.java:244)

at javax.media.opengl.GLCanvas.paint(GLCanvas.java:277)

at sun.awt.RepaintArea.paintComponent(Unknown Source)

at sun.awt.RepaintArea.paint(Unknown Source)

at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.awt.EventQueue.access$000(Unknown Source)

at java.awt.EventQueue$1.run(Unknown Source)

at java.awt.EventQueue$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)

at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue$2.run(Unknown Source)

at java.awt.EventQueue$2.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)







The error occurs the moment I call loadTexture from within my TextureManager.





Here is the TextureManager code:







public class TextureManager {



private static HashMap<String, Texture> textureMap;



public TextureManager() {



textureMap = new HashMap<String, Texture>();



}



public static void loadTexture(String name) {



if(textureMap.containsKey(name)) return;



try {



textureMap.put(name, TextureIO.newTexture(new File("textures/" + name + ".png"), true));

}

catch (GLException e) {



e.printStackTrace();

}

catch (IOException e) {



e.printStackTrace();

}

}



public static void bindTexture(String name) {



textureMap.get(name).bind();

}

}







A TextureManager object has already been created earlier in the code via:







new TextureManager();







This is the code that I called it using:







TextureManager.loadTexture("tree");







The path textures/tree.png exists too.





I did something similar to this in an older project, but instead of using a hashmap, I hard coded a variable in the TextureManager for every texture I used (not many). I suspect It's because the thread lacks a GL context because my old TextureManager passed GL in via the constructor, but never used it in the code. The draw function that called the loadTexture function also passed in a GL variable.


Comments

  1. Ah I figured out what was causing the problem. I was binding the texture within the glBegin and glEnd calls.

    Thanks for everyone that had a look at my question, though unfortunately I didn't include the code that would have provided the solution in my question. Apologies for that.

    Hopefully this is still able to help someone else in the future!

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.