public class Texture extends Object
Due to many confusions w/ texture usage, following list described the order and semantics of texture unit selection, binding and enabling.
gl.glActiveTexture(GL.GL_TEXTURE0 + textureUnit)
, 0
is default.textureId
-> active textureUnit
's textureTarget
via gl.glBindTexture(textureTarget, textureId)
textureUnit
's textureTarget
via glEnable(textureTarget)
.
textureUnit
in your shader program, enable shader program.Non-power-of-two restrictions
When creating an OpenGL texture object, the Texture class will
attempt to use non-power-of-two textures (NPOT) if available, see GLBase.isNPOTTextureAvailable()
.
Further more,
GL_ARB_texture_rectangle
(RECT) will be attempted on OSX w/ ATI drivers.
If NPOT is not available or RECT not chosen, the Texture class will simply upload a non-pow2-sized
image into a standard pow2-sized texture (without any special
scaling).
Since the choice of extension (or whether one is used at
all) depends on the user's machine configuration, developers are
recommended to use getImageTexCoords()
and getSubImageTexCoords(int, int, int, int)
, as those methods will calculate the
appropriate texture coordinates for the situation.
One caveat in this approach is that certain texture wrap modes
(e.g. GL_REPEAT
) are not legal when the GL_ARB_texture_rectangle
extension is in use. Another issue to be aware of is that in the
default pow2 scenario, if the original image does not have pow2
dimensions, then wrapping may not work as one might expect since
the image does not extend to the edges of the pow2 texture. If
texture wrapping is important, it is recommended to use only
pow2-sized images with the Texture class.
Performance Tips
For best performance, try to avoid calling enable(com.jogamp.opengl.GL)
/
bind(com.jogamp.opengl.GL)
/ disable(com.jogamp.opengl.GL)
any more than necessary. For
example, applications using many Texture objects in the same scene
may want to reduce the number of calls to both enable(com.jogamp.opengl.GL)
and
disable(com.jogamp.opengl.GL)
. To do this it is necessary to call getTarget()
to make sure the OpenGL texture target is the same for
all of the Texture objects in use; non-power-of-two textures using
the GL_ARB_texture_rectangle extension use a different target than
power-of-two textures using the GL_TEXTURE_2D target. Note that
when switching between textures it is necessary to call bind(com.jogamp.opengl.GL)
, but when drawing many triangles all using the same texture,
for best performance only one call to bind(com.jogamp.opengl.GL)
should be made.
User may also utilize multiple texture units,
see order of texture commands above.
Alpha premultiplication and blending
Disclaimer: Consider performing alpha premultiplication in shader code, if really desired! Otherwise use RGBA.
The Texture class does not convert RGBA image data into premultiplied data when storing it into an OpenGL texture.
The mathematically correct way to perform blending in OpenGL with the SrcOver "source over destination" mode, or any other Porter-Duff rule, is to use premultiplied color components, which means the R/G/ B color components must have been multiplied by the alpha value. If using premultiplied color components it is important to use the correct blending function; for example, the SrcOver rule is expressed as:
gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);Also, when using a texture function like
GL_MODULATE
where
the current color plays a role, it is important to remember to make
sure that the color is specified in a premultiplied form, for
example:
float a = ...; float r = r * a; float g = g * a; float b = b * a; gl.glColor4f(r, g, b, a);For reference, here is a list of the Porter-Duff compositing rules and the associated OpenGL blend functions (source and destination factors) to use in the face of premultiplied alpha:
Rule | Source | Dest |
Clear | GL_ZERO | GL_ZERO |
Src | GL_ONE | GL_ZERO |
SrcOver | GL_ONE | GL_ONE_MINUS_SRC_ALPHA |
DstOver | GL_ONE_MINUS_DST_ALPHA | GL_ONE |
SrcIn | GL_DST_ALPHA | GL_ZERO |
DstIn | GL_ZERO | GL_SRC_ALPHA |
SrcOut | GL_ONE_MINUS_DST_ALPHA | GL_ZERO |
DstOut | GL_ZERO | GL_ONE_MINUS_SRC_ALPHA |
Dst | GL_ZERO | GL_ONE |
SrcAtop | GL_DST_ALPHA | GL_ONE_MINUS_SRC_ALPHA |
DstAtop | GL_ONE_MINUS_DST_ALPHA | GL_SRC_ALPHA |
AlphaXor | GL_ONE_MINUS_DST_ALPHA | GL_ONE_MINUS_SRC_ALPHA |
Constructor and Description |
---|
Texture(GL gl,
TextureData data) |
Texture(int target)
Constructor for use when creating e.g.
|
Texture(int textureID,
int target,
int texWidth,
int texHeight,
int imgWidth,
int imgHeight,
boolean mustFlipVertically)
Constructor to wrap an OpenGL texture ID from an external library and allows
some of the base methods from the Texture class, such as
binding and querying of texture coordinates, to be used with
it.
|
Modifier and Type | Method and Description |
---|---|
void |
bind(GL gl)
Binds this texture to the given GL context.
|
void |
destroy(GL gl)
Destroys the native resources used by this texture object.
|
void |
disable(GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the
given GL state.
|
void |
enable(GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the
given GL context's state.
|
float |
getAspectRatio()
Returns the original aspect ratio of the image, defined as (image
width) / (image height), before any scaling that might have
occurred as a result of using the GLU mipmap routines.
|
int |
getEstimatedMemorySize()
Returns an estimate of the amount of texture memory in bytes
this Texture consumes.
|
int |
getHeight()
Returns the height of the allocated OpenGL texture in pixels.
|
int |
getImageHeight()
Returns the height of the image contained within this texture.
|
TextureCoords |
getImageTexCoords()
Returns the set of texture coordinates corresponding to the
entire image.
|
int |
getImageWidth()
Returns the width of the image contained within this texture.
|
boolean |
getMustFlipVertically()
Indicates whether this texture's texture coordinates must be
flipped vertically in order to properly display the texture.
|
TextureCoords |
getSubImageTexCoords(int x1,
int y1,
int x2,
int y2)
Returns the set of texture coordinates corresponding to the
specified sub-image.
|
int |
getTarget()
Returns the OpenGL "target" of this texture.
|
int |
getTextureObject()
Returns the underlying OpenGL texture object for this texture,
maybe
0 if not yet generated. |
int |
getTextureObject(GL gl)
Returns the underlying OpenGL texture object for this texture
and generates it if not done yet.
|
int |
getWidth()
Returns the width of the allocated OpenGL texture in pixels.
|
boolean |
isUsingAutoMipmapGeneration()
Indicates whether this Texture is using automatic mipmap
generation (via the OpenGL texture parameter
GL_GENERATE_MIPMAP).
|
void |
setMustFlipVertically(boolean v)
Change whether the TextureData requires a vertical flip of
the texture coords.
|
void |
setTexParameterf(GL gl,
int parameterName,
float value)
Sets the OpenGL floating-point texture parameter for the
texture's target.
|
void |
setTexParameterfv(GL gl,
int parameterName,
float[] params,
int params_offset)
Sets the OpenGL multi-floating-point texture parameter for the
texture's target.
|
void |
setTexParameterfv(GL gl,
int parameterName,
FloatBuffer params)
Sets the OpenGL multi-floating-point texture parameter for the
texture's target.
|
void |
setTexParameteri(GL gl,
int parameterName,
int value)
Sets the OpenGL integer texture parameter for the texture's
target.
|
void |
setTexParameteriv(GL gl,
int parameterName,
int[] params,
int params_offset)
Sets the OpenGL multi-integer texture parameter for the texture's
target.
|
void |
setTexParameteriv(GL gl,
int parameterName,
IntBuffer params)
Sets the OpenGL multi-integer texture parameter for the texture's
target.
|
String |
toString() |
void |
updateImage(GL gl,
TextureData data)
Updates the entire content area incl.
|
void |
updateImage(GL gl,
TextureData data,
int targetOverride)
Updates the content area incl.
|
void |
updateSubImage(GL gl,
TextureData data,
int mipmapLevel,
int x,
int y)
Updates a subregion of the content area of this texture using the
given data.
|
void |
updateSubImage(GL gl,
TextureData data,
int mipmapLevel,
int dstx,
int dsty,
int srcx,
int srcy,
int width,
int height)
Updates a subregion of the content area of this texture using the
specified sub-region of the given data.
|
public Texture(GL gl, TextureData data) throws GLException
GLException
public Texture(int target)
target
- the OpenGL texture target, eg GL.GL_TEXTURE_2D,
GL2.GL_TEXTURE_RECTANGLEpublic Texture(int textureID, int target, int texWidth, int texHeight, int imgWidth, int imgHeight, boolean mustFlipVertically)
textureID
- the OpenGL texture object to wraptarget
- the OpenGL texture target, eg GL.GL_TEXTURE_2D,
GL2.GL_TEXTURE_RECTANGLEtexWidth
- the width of the texture in pixelstexHeight
- the height of the texture in pixelsimgWidth
- the width of the image within the texture in
pixels (if the content is a sub-rectangle in the upper
left corner); otherwise, pass in texWidthimgHeight
- the height of the image within the texture in
pixels (if the content is a sub-rectangle in the upper
left corner); otherwise, pass in texHeightmustFlipVertically
- indicates whether the texture
coordinates must be flipped vertically
in order to properly display the
texturepublic void enable(GL gl) throws GLException
gl.glEnable(texture.getTarget());
Call is ignored if the GL
object's context
is using a core profile, see GLBase.isGLcore()
,
or if getTarget()
is GLES2.GL_TEXTURE_EXTERNAL_OES
.
See the performance tips above for hints on how to maximize performance when using many Texture objects.
gl
- the current GL objectGLException
- if no OpenGL context was current or if any
OpenGL-related errors occurredpublic void disable(GL gl) throws GLException
gl.glDisable(texture.getTarget());
Call is ignored if the GL
object's context
is using a core profile, see GLBase.isGLcore()
,
or if getTarget()
is GLES2.GL_TEXTURE_EXTERNAL_OES
.
See the performance tips above for hints on how to maximize performance when using many Texture objects.
gl
- the current GL objectGLException
- if no OpenGL context was current or if any
OpenGL-related errors occurredpublic void bind(GL gl) throws GLException
gl.glBindTexture(texture.getTarget(), texture.getTextureObject());See the performance tips above for hints on how to maximize performance when using many Texture objects.
gl
- the current GL contextGLException
- if no OpenGL context was current or if any
OpenGL-related errors occurredpublic void destroy(GL gl) throws GLException
GLException
- if any OpenGL-related errors occurredpublic int getTarget()
GL.GL_TEXTURE_2D
,
GL2.GL_TEXTURE_RECTANGLE_ARB
public int getWidth()
public int getHeight()
public int getImageWidth()
getWidth()
. It is
recommended that applications call getImageTexCoords()
and
getSubImageTexCoords(int, int, int, int)
rather than using this API
directly.public int getImageHeight()
getHeight()
. It is
recommended that applications call getImageTexCoords()
and
getSubImageTexCoords(int, int, int, int)
rather than using this API
directly.public float getAspectRatio()
public TextureCoords getImageTexCoords()
public TextureCoords getSubImageTexCoords(int x1, int y1, int x2, int y2)
public void updateImage(GL gl, TextureData data) throws GLException
TextureCoords
of this texture using the data in the given image.GLException
- if any OpenGL-related errors occurredpublic boolean getMustFlipVertically()
getImageTexCoords
and getSubImageTexCoords
, but applications may generate or otherwise
produce texture coordinates which must be corrected.public void setMustFlipVertically(boolean v)
No-op if no change, otherwise generates new TextureCoords
.
public void updateImage(GL gl, TextureData data, int targetOverride) throws GLException
TextureCoords
of the specified target of this texture
using the data in the given image. In general this is intended
for construction of cube maps.GLException
- if any OpenGL-related errors occurredpublic void updateSubImage(GL gl, TextureData data, int mipmapLevel, int x, int y) throws GLException
isUsingAutoMipmapGeneration
),
updates to the base (level 0) mipmap will cause the lower-level
mipmaps to be regenerated, and updates to other mipmap levels
will be ignored. Otherwise, if automatic mipmap generation is not
in use, only updates the specified mipmap level and does not
re-generate mipmaps if they were originally produced or loaded.data
- the image data to be uploaded to this texturemipmapLevel
- the mipmap level of the texture to set. If
this is non-zero and the TextureData contains mipmap data, the
appropriate mipmap level will be selected.x
- the x offset (in pixels) relative to the lower-left corner
of this texturey
- the y offset (in pixels) relative to the lower-left corner
of this textureGLException
- if any OpenGL-related errors occurredpublic void updateSubImage(GL gl, TextureData data, int mipmapLevel, int dstx, int dsty, int srcx, int srcy, int width, int height) throws GLException
isUsingAutoMipmapGeneration
), updates to the base (level 0)
mipmap will cause the lower-level mipmaps to be regenerated, and
updates to other mipmap levels will be ignored. Otherwise, if
automatic mipmap generation is not in use, only updates the
specified mipmap level and does not re-generate mipmaps if they
were originally produced or loaded. This method is only supported
for uncompressed TextureData sources.data
- the image data to be uploaded to this texturemipmapLevel
- the mipmap level of the texture to set. If
this is non-zero and the TextureData contains mipmap data, the
appropriate mipmap level will be selected.dstx
- the x offset (in pixels) relative to the lower-left corner
of this texture where the update will be applieddsty
- the y offset (in pixels) relative to the lower-left corner
of this texture where the update will be appliedsrcx
- the x offset (in pixels) relative to the lower-left corner
of the supplied TextureData from which to fetch the update rectanglesrcy
- the y offset (in pixels) relative to the lower-left corner
of the supplied TextureData from which to fetch the update rectanglewidth
- the width (in pixels) of the rectangle to be updatedheight
- the height (in pixels) of the rectangle to be updatedGLException
- if no OpenGL context was current or if any
OpenGL-related errors occurredpublic void setTexParameterf(GL gl, int parameterName, float value)
GLException
- if no OpenGL context was current or if any
OpenGL-related errors occurredpublic void setTexParameterfv(GL gl, int parameterName, FloatBuffer params)
GLException
- if any OpenGL-related errors occurredpublic void setTexParameterfv(GL gl, int parameterName, float[] params, int params_offset)
GLException
- if any OpenGL-related errors occurredpublic void setTexParameteri(GL gl, int parameterName, int value)
GLException
- if any OpenGL-related errors occurredpublic void setTexParameteriv(GL gl, int parameterName, IntBuffer params)
GLException
- if any OpenGL-related errors occurredpublic void setTexParameteriv(GL gl, int parameterName, int[] params, int params_offset)
GLException
- if any OpenGL-related errors occurredpublic int getTextureObject(GL gl)
Most applications will not need to access this, since it is handled automatically by the bind(GL) and destroy(GL) APIs.
gl
- required to be valid and current in case the texture object has not been generated yet,
otherwise it may be null
.getTextureObject()
public int getTextureObject()
0
if not yet generated.
Most applications will not need to access this, since it is handled automatically by the bind(GL) and destroy(GL) APIs.
getTextureObject(GL)
public int getEstimatedMemorySize()
public boolean isUsingAutoMipmapGeneration()
Copyright 2010 JogAmp Community.