diff options
author | Kenneth Russel <[email protected]> | 2006-01-06 22:41:39 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2006-01-06 22:41:39 +0000 |
commit | c7c06d846e217bff8deac1914cd18cf8b3b6d6c4 (patch) | |
tree | 2972a554b1489d0e0930588b513c9a2ae0aa1db5 | |
parent | 605053cfa6696ebf5850f62480e37f960a91d127 (diff) |
Changed how Texture returns texture coordinates for both the entire
image as well as a sub-image. Added TextureCoords class. Updated
TestTexture demo.
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@520 232f8b59-042b-4e1e-8c03-345bb8c30851
-rwxr-xr-x | src/classes/com/sun/opengl/utils/Texture.java | 123 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/TextureCoords.java | 79 |
2 files changed, 117 insertions, 85 deletions
diff --git a/src/classes/com/sun/opengl/utils/Texture.java b/src/classes/com/sun/opengl/utils/Texture.java index 6c3fbd1c3..a21facb65 100755 --- a/src/classes/com/sun/opengl/utils/Texture.java +++ b/src/classes/com/sun/opengl/utils/Texture.java @@ -36,8 +36,6 @@ package com.sun.opengl.utils; -import java.awt.geom.*; - import javax.media.opengl.*; import com.sun.opengl.impl.*; @@ -70,13 +68,9 @@ public class Texture { the texture coords. */ private boolean mustFlipVertically; - /** The texture coordinate corresponding to the lower left corner - of the texture when properly oriented. */ - private Point2D lowerLeftTexCoord = new Point2D.Float(); - - /** The texture coordinate corresponding to the upper right corner - of the texture when properly oriented. */ - private Point2D upperRightTexCoord = new Point2D.Float(); + /** The texture coordinates corresponding to the entire texture + image. */ + private TextureCoords coords; private static final boolean DEBUG = Debug.debug("Texture"); @@ -238,89 +232,50 @@ public class Texture { } /** - * Returns the texture coordinate corresponding to the lower-left - * point of this texture when oriented properly. If the TextureData - * indicated that the texture coordinates must be flipped - * vertically, the returned Point will take that into account. - * - * @return the texture coordinate of the lower-left point of the - * texture + * Returns the set of texture coordinates corresponding to the + * entire image. If the TextureData indicated that the texture + * coordinates must be flipped vertically, the returned + * TextureCoords will take that into account. + * + * @return the texture coordinates corresponding to the entire image */ - public Point2D getImageLowerLeftTexCoord() { - return (Point2D) lowerLeftTexCoord.clone(); + public TextureCoords getImageTexCoords() { + return coords; } /** - * Returns the texture coordinate corresponding to the upper-right - * point of this texture when oriented properly. If the TextureData - * indicated that the texture coordinates must be flipped - * vertically, the returned Point will take that into account. - * - * @return the texture coordinates of the upper-right point of the - * texture + * Returns the set of texture coordinates corresponding to the + * specified sub-image. The (x1, y1) and (x2, y2) points are + * specified in terms of pixels starting from the lower-left of the + * image. (x1, y1) should specify the lower-left corner of the + * sub-image and (x2, y2) the upper-right corner of the sub-image. + * If the TextureData indicated that the texture coordinates must be + * flipped vertically, the returned TextureCoords will take that + * into account; this should not be handled by the end user in the + * specification of the y1 and y2 coordinates. + * + * @return the texture coordinates corresponding to the specified sub-image */ - public Point2D getImageUpperRightTexCoord() { - return (Point2D) upperRightTexCoord.clone(); - } - - - /** - * Returns the texture coordinate corresponding to the lower-left - * point of the specified sub-image of this texture when oriented - * properly. If the TextureData indicated that the texture - * coordinates must be flipped vertically, the returned Point will - * take that into account. - * - * @return the texture coordinate of the lower-left point of the - * texture - */ - public Point2D getSubImageLowerLeftTexCoord(int x1, int y1, int x2, int y2) { + public TextureCoords getSubImageTexCoords(int x1, int y1, int x2, int y2) { if (target == GL.GL_TEXTURE_RECTANGLE_ARB) { if (mustFlipVertically) { - return new Point2D.Float(x1, texHeight - y1); + return new TextureCoords(x1, texHeight - y1, x2, texHeight - y2); } else { - return new Point2D.Float(x1, y1); + return new TextureCoords(x1, y1, x2, y2); } } else { - float tx = (float)x1 / (float)texWidth; - float ty = (float)y1 / (float)texHeight; - + float tx1 = (float)x1 / (float)texWidth; + float ty1 = (float)y1 / (float)texHeight; + float tx2 = (float)x2 / (float)texWidth; + float ty2 = (float)y2 / (float)texHeight; if (mustFlipVertically) { - return new Point2D.Float(tx, 1.0f - ty); + return new TextureCoords(tx1, 1.0f - ty1, tx2, 1.0f - ty2); } else { - return new Point2D.Float(tx, ty); + return new TextureCoords(tx1, ty1, tx2, ty2); } } } - /** - * Returns the texture coordinate corresponding to the upper-right - * point of the specified sub-image of this texture when oriented - * properly. If the TextureData indicated that the texture - * coordinates must be flipped vertically, the returned Point will - * take that into account. - * - * @return the texture coordinate of the upper-right point of the - * texture - */ - public Point2D getSubImageUpperRightTexCoord(int x1, int y1, int x2, int y2) { - if (target == GL.GL_TEXTURE_RECTANGLE_ARB) { - if (mustFlipVertically) { - return new Point2D.Float(x2, texHeight - y2); - } else { - return new Point2D.Float(x2, y2); - } - } else { - float tx = (float)x2 / (float)texWidth; - float ty = (float)y2 / (float)texHeight; - - if (mustFlipVertically) { - return new Point2D.Float(tx, 1.0f - ty); - } else { - return new Point2D.Float(tx, ty); - } - } - } /** * Updates a subregion of the content area of this texture using the @@ -408,20 +363,18 @@ public class Texture { imgHeight = height; if (target == GL.GL_TEXTURE_RECTANGLE_ARB) { if (mustFlipVertically) { - lowerLeftTexCoord = new Point2D.Float(0, imgHeight); - upperRightTexCoord = new Point2D.Float(imgWidth, 0); + coords = new TextureCoords(0, imgHeight, imgWidth, 0); } else { - lowerLeftTexCoord = new Point2D.Float(0, 0); - upperRightTexCoord = new Point2D.Float(imgWidth, imgHeight); + coords = new TextureCoords(0, 0, imgWidth, imgHeight); } } else { if (mustFlipVertically) { - lowerLeftTexCoord = new Point2D.Float(0, (float) imgHeight / (float) texHeight); - upperRightTexCoord = new Point2D.Float((float) imgWidth / (float) texWidth, 0); + coords = new TextureCoords(0, (float) imgHeight / (float) texHeight, + (float) imgWidth / (float) texWidth, 0); } else { - lowerLeftTexCoord = new Point2D.Float(0, 0); - upperRightTexCoord = new Point2D.Float((float) imgWidth / (float) texWidth, - (float) imgHeight / (float) texHeight); + coords = new TextureCoords(0, 0, + (float) imgWidth / (float) texWidth, + (float) imgHeight / (float) texHeight); } } } diff --git a/src/classes/com/sun/opengl/utils/TextureCoords.java b/src/classes/com/sun/opengl/utils/TextureCoords.java new file mode 100755 index 000000000..ec3760026 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/TextureCoords.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ + +package com.sun.opengl.utils; + +/** Specifies texture coordinates for a rectangular area of a + texture. Note that some textures are inherently flipped vertically + from OpenGL's standard coordinate system. This class takes care of + this vertical flip so that the "bottom" and "top" coordinates may + sometimes be reversed. From the point of view of code rendering + textured polygons, it can always map the bottom and left texture + coordinates from the TextureCoords to the lower left point of the + textured polygon and achieve correct results. */ + +public class TextureCoords { + // These represent the lower-left point + private float left; + private float bottom; + // These represent the upper-right point + private float right; + private float top; + + public TextureCoords(float left, float bottom, + float right, float top) { + this.left = left; + this.bottom = bottom; + this.right = right; + this.top = top; + } + + /** Returns the leftmost (x) texture coordinate of this + rectangle. */ + public float left() { return left; } + + /** Returns the rightmost (x) texture coordinate of this + rectangle. */ + public float right() { return right; } + + /** Returns the bottommost (y) texture coordinate of this + rectangle. */ + public float bottom() { return bottom; } + + /** Returns the topmost (y) texture coordinate of this + rectangle. */ + public float top() { return top; } +} |