From 14a2ea2a7eb093ff3fb5322ace1a6f8767401f3d Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Sat, 3 Oct 2009 22:55:14 -0700
Subject: Texture: Relax/Pending creation of texture ID, no context must be
 current

---
 .../com/sun/opengl/util/texture/Texture.java       | 42 +++++++++++++---------
 1 file changed, 25 insertions(+), 17 deletions(-)

(limited to 'src/jogl/classes/com/sun/opengl/util/texture')

diff --git a/src/jogl/classes/com/sun/opengl/util/texture/Texture.java b/src/jogl/classes/com/sun/opengl/util/texture/Texture.java
index 87f045a6d..3a2799cf3 100755
--- a/src/jogl/classes/com/sun/opengl/util/texture/Texture.java
+++ b/src/jogl/classes/com/sun/opengl/util/texture/Texture.java
@@ -179,17 +179,14 @@ public class Texture {
     private static final boolean disableTexRect = Debug.isPropertyDefined("jogl.texture.notexrect", true, localACC);
 
     public Texture(TextureData data) throws GLException {
-        GL gl = GLContext.getCurrentGL();
-        texID = createTextureID(gl); 
-
+        texID = 0;
         updateImage(data);
     }
 
     // Constructor for use when creating e.g. cube maps, where there is
     // no initial texture data
     public Texture(int target) throws GLException {
-        GL gl = GLContext.getCurrentGL();
-        texID = createTextureID(gl); 
+        texID = 0;
         this.target = target;
     }
 
@@ -260,6 +257,7 @@ public class Texture {
      * OpenGL-related errors occurred
      */
     public void bind() throws GLException {
+        validateTexID(null, true);
         GLContext.getCurrentGL().glBindTexture(target, texID); 
     }
 
@@ -290,8 +288,10 @@ public class Texture {
      * @throws GLException if any OpenGL-related errors occurred
      */
     public void destroy(GL gl) throws GLException {
-        gl.glDeleteTextures(1, new int[] {texID}, 0);
-        texID = 0;
+        if(0<texID) {
+            gl.glDeleteTextures(1, new int[] {texID}, 0);
+            texID = 0;
+        }
     }
 
     /**
@@ -443,6 +443,7 @@ public class Texture {
      */
     public void updateImage(TextureData data, int target) throws GLException {
         GL gl = GLContext.getCurrentGL();
+        validateTexID(gl, true);
 
         imgWidth = data.getWidth();
         imgHeight = data.getHeight();
@@ -855,6 +856,7 @@ public class Texture {
      * handled automatically by the bind() and dispose() APIs.
      */
     public int getTextureObject() {
+        validateTexID(null, false);
         return texID;
     }
 
@@ -1071,16 +1073,22 @@ public class Texture {
         }
     }
 
-    /**
-     * Creates a new texture ID.
-     *
-     * @param gl the GL object associated with the current OpenGL context
-     * @return a new texture ID
-     */
-    private static int createTextureID(GL gl) {
-        int[] tmp = new int[1];
-        gl.glGenTextures(1, tmp, 0);
-        return tmp[0];
+    private void validateTexID(GL gl, boolean throwException) {
+        if( 0 < texID ) return;
+        if(null==gl) {
+            GLContext ctx = GLContext.getCurrent();
+            if(null!=ctx) {
+                gl = ctx.getGL();
+            } else if(throwException) {
+                throw new GLException("No context current, can't create texture ID");
+            }
+        }
+
+        if(null!=gl) {
+            int[] tmp = new int[1];
+            gl.glGenTextures(1, tmp, 0);
+            texID = tmp[0];
+        }
     }
 
     // Helper routines for disabling certain codepaths
-- 
cgit v1.2.3