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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex