📄 textureretained.java
字号:
/* * $RCSfile: TextureRetained.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Use is subject to license terms. * * $Revision: 1.16 $ * $Date: 2007/04/20 22:53:31 $ * $State: Exp $ */package javax.media.j3d;import java.util.*;import javax.vecmath.*;import java.awt.image.DataBufferByte;import java.awt.image.RenderedImage;/** * The Texture object is a component object of an Appearance object * that defines the texture properties used when texture mapping is * enabled. Texture object is an abstract class and all texture * objects must be created as either a Texture2D object or a * Texture3D object. */abstract class TextureRetained extends NodeComponentRetained { // A list of pre-defined bits to indicate which component // in this Texture object changed. static final int ENABLE_CHANGED = 0x001; static final int COLOR_CHANGED = 0x002; static final int IMAGE_CHANGED = 0x004; static final int STATE_CHANGED = 0x008; static final int UPDATE_IMAGE = 0x010; static final int IMAGES_CHANGED = 0x020; static final int BASE_LEVEL_CHANGED = 0x040; static final int MAX_LEVEL_CHANGED = 0x080; static final int MIN_LOD_CHANGED = 0x100; static final int MAX_LOD_CHANGED = 0x200; static final int LOD_OFFSET_CHANGED = 0x400; // constants for min and mag filter static final int MIN_FILTER = 0; static final int MAG_FILTER = 1; // Boundary width int boundaryWidth = 0; // Boundary modes (wrap, clamp, clamp_to_edge, clamp_to_boundary) int boundaryModeS = Texture.WRAP; int boundaryModeT = Texture.WRAP; // Filter modes int minFilter = Texture.BASE_LEVEL_POINT; int magFilter = Texture.BASE_LEVEL_POINT; // Integer flag that contains bitset to indicate // which field changed. int isDirty = 0xffff; // Texture boundary color Color4f boundaryColor = new Color4f(0.0f, 0.0f, 0.0f, 0.0f); // Texture Object Id used by native code. int objectId = -1; int mipmapMode = Texture.BASE_LEVEL; // Type of mip-mapping int format = Texture.RGB; // Texture format int width = 1; // Width in pixels (2**n) int height = 1; // Height in pixels (2**m) // true if width or height is non power of two private boolean widthOrHeightIsNPOT = false; // Array of images (one for each mipmap level) ImageComponentRetained images[][]; // maximum number of levels needed for the mipmapMode of this texture int maxLevels = 0; // maximum number of mipmap levels that can be defined for this texture private int maxMipMapLevels = 0; int numFaces = 1; // For CubeMap, it is 6 int baseLevel = 0; int maximumLevel = 0; float minimumLod = -1000.0f; float maximumLod = 1000.0f; Point3f lodOffset = null; private boolean useAsRaster = false; // true if used by Raster or Background. // Texture mapping enable switch // This enable is derived from the user specified enable // and whether the buf image in the imagecomp is null boolean enable = true; // User specified enable boolean userSpecifiedEnable = true; // true if alpha channel need update during rendering boolean isAlphaNeedUpdate = false; // sharpen texture info int numSharpenTextureFuncPts = 0; float sharpenTextureFuncPts[] = null; // array of pairs of floats // first value for LOD // second value for the fcn value // filter4 info float filter4FuncPts[] = null; // anisotropic filter info int anisotropicFilterMode = Texture.ANISOTROPIC_NONE; float anisotropicFilterDegree = 1.0f; // Each bit corresponds to a unique renderer if shared context // or a unique canvas otherwise. // This mask specifies which renderer/canvas has loaded the // texture images. 0 means no renderer/canvas has loaded the texture. // 1 at the particular bit means that renderer/canvas has loaded the // texture. 0 means otherwise. int resourceCreationMask = 0x0; // Each bit corresponds to a unique renderer if shared context // or a unique canvas otherwise // This mask specifies if texture images are up-to-date. // 0 at a particular bit means texture images are not up-to-date. // 1 means otherwise. If it specifies 0, then it needs to go // through the imageUpdateInfo to update the images accordingly. // int resourceUpdatedMask = 0x0; // Each bit corresponds to a unique renderer if shared context // or a unique canvas otherwise // This mask specifies if texture lod info is up-to-date. // 0 at a particular bit means texture lod info is not up-to-date. // 1 means otherwise. // int resourceLodUpdatedMask = 0x0; // Each bit corresponds to a unique renderer if shared context // or a unique canvas otherwise // This mask specifies if texture is in the resource reload list // 0 at a particular bit means texture is not in reload list // 1 means otherwise. // int resourceInReloadList = 0x0; // image update info ArrayList imageUpdateInfo[][]; int imageUpdatePruneMask[]; // Issue 357 - we need a separate reference counter per RenderBin, since // each RenderBin keeps an independent list of Texture objects to be freed. // Since this is accessed infrequently, we will use a HashMap for the // textureBin reference counter private HashMap<RenderBin,Integer> textureBinRefCount = new HashMap<RenderBin,Integer>(); // This is used for D3D only to check whether texture need to // resend down private int texTimestamp = 0; // need to synchronize access from multiple rendering threads Object resourceLock = new Object(); private static boolean isPowerOfTwo(int val) { return ((val & (val - 1)) == 0); } void initialize(int format, int width, int widLevels, int height, int heiLevels, int mipmapMode, int boundaryWidth) { this.mipmapMode = mipmapMode; this.format = format; this.width = width; this.height = height; this.boundaryWidth = boundaryWidth; if(!isPowerOfTwo(width) || !isPowerOfTwo(height)) { this.widthOrHeightIsNPOT = true; } // determine the maximum number of mipmap levels that can be // defined from the specified dimension if (widLevels > heiLevels) { maxMipMapLevels = widLevels + 1; } else { maxMipMapLevels = heiLevels + 1; } // determine the maximum number of mipmap levels that will be // needed with the current mipmapMode if (mipmapMode != Texture.BASE_LEVEL) { baseLevel = 0; maximumLevel = maxMipMapLevels - 1; maxLevels = maxMipMapLevels; } else { baseLevel = 0; maximumLevel = 0; maxLevels = 1; } images = new ImageComponentRetained[numFaces][maxLevels]; for (int j = 0; j < numFaces; j++) { for (int i = 0; i < maxLevels; i++) { images[j][i] = null; } } } final int getFormat() { return this.format; } final int getWidth() { return this.width; } final int getHeight() { return this.height; } final int numMipMapLevels() { return (maximumLevel - baseLevel + 1); } /** * Sets the boundary mode for the S coordinate in this texture object. * @param boundaryModeS the boundary mode for the S coordinate, * one of: CLAMP or WRAP. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final void initBoundaryModeS(int boundaryModeS) { this.boundaryModeS = boundaryModeS; } /** * Retrieves the boundary mode for the S coordinate. * @return the current boundary mode for the S coordinate. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final int getBoundaryModeS() { return boundaryModeS; } /** * Sets the boundary mode for the T coordinate in this texture object. * @param boundaryModeT the boundary mode for the T coordinate, * one of: CLAMP or WRAP. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final void initBoundaryModeT(int boundaryModeT) { this.boundaryModeT = boundaryModeT; } /** * Retrieves the boundary mode for the T coordinate. * @return the current boundary mode for the T coordinate. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final int getBoundaryModeT() { return boundaryModeT; } /** * Retrieves the boundary width. * @return the boundary width of this texture. */ final int getBoundaryWidth() { return boundaryWidth; } /** * Sets the minification filter function. This * function is used when the pixel being rendered maps to an area * greater than one texel. * @param minFilter the minification filter, one of: * FASTEST, NICEST, BASE_LEVEL_POINT, BASE_LEVEL_LINEAR, * MULTI_LEVEL_POINT, MULTI_LEVEL_LINEAR. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final void initMinFilter(int minFilter) { this.minFilter = minFilter; } /** * Retrieves the minification filter. * @return the current minification filter function. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final int getMinFilter() { return minFilter; } /** * Sets the magnification filter function. This * function is used when the pixel being rendered maps to an area * less than or equal to one texel. * @param magFilter the magnification filter, one of: * FASTEST, NICEST, BASE_LEVEL_POINT, or BASE_LEVEL_LINEAR. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final void initMagFilter(int magFilter) { this.magFilter = magFilter; } /** * Retrieves the magnification filter. * @return the current magnification filter function. * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. */ final int getMagFilter() { return magFilter; } /** * Sets a specified mipmap level. * @param level mipmap level to set: 0 is the base level * @param image pixel array object containing the texture image * @exception RestrictedAccessException if the method is called * when this object is part of live or compiled scene graph. * @exception IllegalArgumentException if an ImageComponent3D * is used in a Texture2D or ImageComponent2D in Texture3D * power of 2 OR invalid format/mipmapMode is specified. */ void initImage(int level, ImageComponent image) { // Issue 172 : call checkImageSize even for non-live setImage calls checkImageSize(level, image); if (this.images == null) { throw new IllegalArgumentException(J3dI18N.getString("TextureRetained0")); } if (this.source instanceof Texture2D) { if (image instanceof ImageComponent3D) throw new IllegalArgumentException(J3dI18N.getString("Texture8")); } else { if (image instanceof ImageComponent2D) throw new IllegalArgumentException(J3dI18N.getString("Texture14")); } if (this.source.isLive()) { if (this.images[0][level] != null) { this.images[0][level].clearLive(refCount); } if (image != null) { ((ImageComponentRetained)image.retained).setLive(inBackgroundGroup, refCount); } } if (image != null) { this.images[0][level] = (ImageComponentRetained)image.retained; } else { this.images[0][level] = null; } } final void checkImageSize(int level, ImageComponent image) { if (image != null) { int imgWidth = ((ImageComponentRetained)image.retained).width; int imgHeight = ((ImageComponentRetained)image.retained).height; int wdh = width; int hgt = height; for (int i = 0; i < level; i++) { wdh >>= 1; hgt >>= 1; } if (wdh < 1) wdh = 1; if (hgt < 1) hgt = 1; if ((wdh != (imgWidth - 2*boundaryWidth)) || (hgt != (imgHeight - 2*boundaryWidth))) { throw new IllegalArgumentException( J3dI18N.getString("TextureRetained1")); } } } final void checkSizes(ImageComponentRetained images[]) { // Issue 172 : this method is now redundant // Assertion check that the image at each level is the correct size // This shouldn't be needed since we already should have checked the // size at each level, and verified that all levels are set. if (images != null) { int hgt = height; int wdh = width; for (int level = 0; level < images.length; level++) { int imgWidth = images[level].width; int imgHeight = images[level].height; assert (wdh == (imgWidth - 2*boundaryWidth)) && (hgt == (imgHeight - 2*boundaryWidth)); wdh /= 2; hgt /= 2; if (wdh < 1) wdh = 1; if (hgt < 1) hgt = 1; } } } final void setImage(int level, ImageComponent image) { initImage(level, image); Object arg[] = new Object[3]; arg[0] = new Integer(level); arg[1] = image; arg[2] = new Integer(0); sendMessage(IMAGE_CHANGED, arg); // If the user has set enable to true, then if the image is null // turn off texture enable if (userSpecifiedEnable) { enable = userSpecifiedEnable; if (image != null && level >= baseLevel && level <= maximumLevel) { ImageComponentRetained img= (ImageComponentRetained)image.retained; if (img.isByReference()) { if (img.getRefImage(0) == null) { enable = false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -