protected Texture initializeTexture(DrawContext dc) { // The frame buffer can be used only during pre-rendering. if (!dc.isPreRenderMode()) { return(null); } // Bind actually binds the source texture only if the image source is available, otherwise it initiates image // source retrieval. If bind returns false, the image source is not yet available. if (this.sourceTexture == null || !this.sourceTexture.bind(dc)) { return(null); } // Ensure that the source texture size is available so that the FBO can be sized to match the source image. if (this.sourceTexture.getWidth(dc) < 1 || this.sourceTexture.getHeight(dc) < 1) { return(null); } int potSourceWidth = WWMath.powerOfTwoCeiling(this.sourceTexture.getWidth(dc)); int potSourceHeight = WWMath.powerOfTwoCeiling(this.sourceTexture.getHeight(dc)); this.width = Math.Min(potSourceWidth, dc.getView().getViewport().width); this.height = Math.Min(potSourceHeight, dc.getView().getViewport().height); if (!this.generateTexture(dc, this.width, this.height)) { return(null); } GL gl = dc.getGL(); TextureData td = new TextureData(gl.getGLProfile(), GL.GL_RGBA, this.width, this.height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, false, false, false, null, null); Texture t = TextureIO.newTexture(td); t.bind(gl); // must do this after generating texture because another texture is bound then gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, td.getInternalFormat(), 0, 0, td.getWidth(), td.getHeight(), td.getBorder()); dc.getTextureCache().put(this, t); return(t); }
/** * Creates a {@link Texture} from this instance's {@link TextureData} if the <code>TextureData</code> exists. * * @param dc the current draw context. * * @return the newly created texture, or null if this instance has no current <code>TextureData</code> or if texture * creation failed. */ protected Texture makeTextureFromTextureData(DrawContext dc) { if (dc == null) { String message = Logging.getMessage("nullValue.DrawContextIsNull"); Logging.logger().severe(message); throw new IllegalStateException(message); } if (this.getTextureData() == null) // texture not in cache yet texture data is null, can't initialize { String msg = Logging.getMessage("nullValue.TextureDataIsNull"); Logging.logger().severe(msg); throw new IllegalStateException(msg); } try { Texture texture = TextureIO.newTexture(this.getTextureData()); if (texture == null) { this.textureInitializationFailed = true; return(null); } this.width = texture.getWidth(); this.height = texture.getHeight(); this.texCoords = texture.getImageTexCoords(); this.setTextureParameters(dc, texture); // Cache the texture and release the texture data. dc.getTextureCache().put(this.getImageSource(), texture); this.setTextureData(null); return(texture); } catch (Exception e) { String name = this.isBufferedImageSource() ? "BufferedImage" : this.getImageSource().ToString(); String msg = Logging.getMessage("generic.ExceptionAttemptingToCreateTexture", name); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); return(null); } }
protected Texture initializeTexture(DrawContext dc) { // Bind actually binds the source texture only if the image source is available, otherwise it initiates image // source retrieval. If bind returns false, the image source is not yet available. if (this.sourceTexture == null || !this.sourceTexture.bind(dc)) { return(null); } // Ensure that the source texture size is available so that the FBO can be sized to match the source image. if (sourceTexture.getWidth(dc) < 1 || sourceTexture.getHeight(dc) < 1) { return(null); } // Limit FBO size to the max OGL size or 4k, whichever is smaller int maxSize = Math.Min(dc.getGLRuntimeCapabilities().getMaxTextureSize(), 4096); this.width = Math.Min(maxSize, sourceTexture.getWidth(dc)); this.height = Math.Min(maxSize, sourceTexture.getHeight(dc)); GL gl = dc.getGL(); int[] previousFbo = new int[1]; gl.glGetIntegerv(GL.GL_FRAMEBUFFER_BINDING, previousFbo, 0); int[] fbo = new int[1]; gl.glGenFramebuffers(1, fbo, 0); try { gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, fbo[0]); TextureData td = new TextureData(gl.getGLProfile(), GL.GL_RGBA, this.width, this.height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, false, false, true, Buffers.newDirectByteBuffer(this.width * this.height * 4), null); Texture t = TextureIO.newTexture(td); t.bind(gl); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, t.getTextureObject(gl), 0); int status = gl.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER); if (status == GL.GL_FRAMEBUFFER_COMPLETE) { this.generateTexture(dc, this.width, this.height); } else { String msg = Logging.getMessage("FBOTexture.TextureNotCreated"); throw new IllegalStateException(msg); } dc.getTextureCache().put(this, t); return(t); } finally { gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, previousFbo[0]); gl.glDeleteFramebuffers(1, fbo, 0); } }
protected Texture initializeTexture(DrawContext dc, Object imageSource) { if (dc == null) { String message = Logging.getMessage("nullValue.DrawContextIsNull"); Logging.logger().severe(message); throw new IllegalStateException(message); } if (this.textureInitializationFailed) { return(null); } Texture t; bool haveMipMapData; GL gl = dc.getGL(); if (imageSource is String) { String path = (String)imageSource; Object streamOrException = WWIO.getFileOrResourceAsStream(path, this.GetType()); if (streamOrException == null || streamOrException is Exception) { Logging.logger().log(java.util.logging.Level.SEVERE, "generic.ExceptionAttemptingToReadImageFile", streamOrException != null ? streamOrException : path); this.textureInitializationFailed = true; return(null); } try { TextureData td = OGLUtil.newTextureData(gl.getGLProfile(), (InputStream)streamOrException, this.useMipMaps); t = TextureIO.newTexture(td); haveMipMapData = td.getMipmapData() != null; } catch (Exception e) { String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile", imageSource); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); this.textureInitializationFailed = true; return(null); } } else if (imageSource is BufferedImage) { try { TextureData td = AWTTextureIO.newTextureData(gl.getGLProfile(), (BufferedImage)imageSource, this.useMipMaps); t = TextureIO.newTexture(td); haveMipMapData = td.getMipmapData() != null; } catch (Exception e) { String msg = Logging.getMessage("generic.IOExceptionDuringTextureInitialization"); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); this.textureInitializationFailed = true; return(null); } } else if (imageSource is URL) { try { InputStream stream = ((URL)imageSource).openStream(); if (stream == null) { Logging.logger().log(java.util.logging.Level.SEVERE, "generic.ExceptionAttemptingToReadImageFile", imageSource); this.textureInitializationFailed = true; return(null); } TextureData td = OGLUtil.newTextureData(gl.getGLProfile(), stream, this.useMipMaps); t = TextureIO.newTexture(td); haveMipMapData = td.getMipmapData() != null; } catch (Exception e) { String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile", imageSource); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); this.textureInitializationFailed = true; return(null); } } else { Logging.logger().log(java.util.logging.Level.SEVERE, "generic.UnrecognizedImageSourceType", imageSource.GetType().Name); this.textureInitializationFailed = true; return(null); } if (t == null) // In case JOGL TextureIO returned null { Logging.logger().log(java.util.logging.Level.SEVERE, "generic.TextureUnreadable", imageSource is String ? imageSource : imageSource.GetType().Name); this.textureInitializationFailed = true; return(null); } // Textures with the same path are assumed to be identical textures, so key the texture id off the // image source. dc.getTextureCache().put(imageSource, t); t.bind(gl); // Enable the appropriate mip-mapping texture filters if the caller has specified that mip-mapping should be // enabled, and the texture itself supports mip-mapping. bool useMipMapFilter = this.useMipMaps && (haveMipMapData || t.isUsingAutoMipmapGeneration()); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, useMipMapFilter ? GL.GL_LINEAR_MIPMAP_LINEAR : GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); if (this.isUseAnisotropy() && useMipMapFilter) { double maxAnisotropy = dc.getGLRuntimeCapabilities().getMaxTextureAnisotropy(); if (dc.getGLRuntimeCapabilities().isUseAnisotropicTextureFilter() && maxAnisotropy >= 2.0) { gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy); } } this.width = t.getWidth(); this.height = t.getHeight(); this.texCoords = t.getImageTexCoords(); return(t); }