JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Rectangle.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.graph.ui.shapes;
29
30import com.jogamp.graph.curve.OutlineShape;
31import com.jogamp.graph.curve.Region;
32import com.jogamp.graph.curve.opengl.GLRegion;
33import com.jogamp.graph.ui.GraphShape;
34import com.jogamp.graph.ui.Shape;
35import com.jogamp.math.FloatUtil;
36import com.jogamp.math.geom.AABBox;
37import com.jogamp.opengl.GL2ES2;
38import com.jogamp.opengl.GLProfile;
39import com.jogamp.opengl.util.texture.TextureSequence;
40
41/**
42 * A GraphUI rectangle {@link GraphShape}
43 * <p>
44 * GraphUI is GPU based and resolution independent.
45 * </p>
46 */
47public class Rectangle extends GraphShape {
48 private float minX, minY, zPos;
49 private float width;
50 private float height;
51 private float lineWidth;
52
53 /**
54 * Create a rectangular Graph based {@link GLRegion} UI {@link Shape}.
55 *
56 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
57 * @param minX
58 * @param minY
59 * @param width
60 * @param height
61 * @param lineWidth line thickness, use zero for filled rectangle
62 * @param zPos
63 */
64 public Rectangle(final int renderModes, final float minX, final float minY, final float width, final float height, final float lineWidth, final float zPos) {
65 super( renderModes );
66 this.minX = minX;
67 this.minY = minY;
68 this.zPos = zPos;
69 this.width = width;
70 this.height = height;
71 this.lineWidth = lineWidth;
72 }
73
74 /**
75 * Create a rectangular Graph based {@link GLRegion} UI {@link Shape}.
76 *
77 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
78 * @param abox
79 * @param lineWidth line thickness, use zero for filled rectangle
80 */
81 public Rectangle(final int renderModes, final AABBox abox, final float lineWidth) {
82 this( renderModes, abox.getMinX(), abox.getMinY(), abox.getWidth(), abox.getHeight(), lineWidth, abox.getCenter().z());
83 }
84
85 /**
86 * Create a rectangular Graph based {@link GLRegion} UI {@link Shape}.
87 *
88 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
89 * @param minX
90 * @param minY
91 * @param width
92 * @param height
93 * @param lineWidth line thickness, use zero for filled rectangle
94 */
95 public Rectangle(final int renderModes, final float minX, final float minY, final float width, final float height, final float lineWidth) {
96 this( renderModes, minX, minY, width, height, lineWidth, 0);
97 }
98
99 /**
100 * Create a rectangular Graph based {@link GLRegion} UI {@link Shape}.
101 *
102 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
103 * @param width
104 * @param height
105 * @param lineWidth line thickness, use zero for filled rectangle
106 */
107 public Rectangle(final int renderModes, final float width, final float height, final float lineWidth) {
108 this( renderModes, 0, 0, width, height, lineWidth, 0);
109 }
110
111 public final float getX() { return minX; }
112 public final float getY() { return minY; }
113 public final float getZ() { return zPos; }
114 public final float getWidth() { return width; }
115 public final float getHeight() { return height; }
116 public final float getLineWidth() { return lineWidth; }
117
118 public void setPosition(final float minX, final float minY, final float zPos) {
119 this.minX = minX;
120 this.minY = minY;
121 this.zPos = zPos;
123 }
124 public void setDimension(final float width, final float height, final float lineWidth) {
125 if( this.width != width || this.height != height || this.lineWidth != lineWidth ) {
126 this.width = width;
127 this.height = height;
128 this.lineWidth = lineWidth;
130 }
131 }
132 public void setBounds(final AABBox abox, final float lineWidth) {
133 setPosition(abox.getMinX(), abox.getMinY(), abox.getCenter().z());
134 setDimension(abox.getWidth(), abox.getHeight(), lineWidth);
135 }
136
137 @Override
138 protected void addShapeToRegion(final GLProfile glp, final GL2ES2 gl) {
139 final OutlineShape shape = new OutlineShape();
140 final float x1 = minX;
141 final float y1 = minY;
142 final float x2 = minX + getWidth();
143 final float y2 = minY + getHeight();
144 final float z = zPos;
145 {
146 // Outer OutlineShape as Winding.CCW.
147 shape.moveTo(x1, y1, z);
148 shape.lineTo(x2, y1, z);
149 shape.lineTo(x2, y2, z);
150 shape.lineTo(x1, y2, z);
151 shape.lineTo(x1, y1, z);
152 shape.closeLastOutline(true);
153 }
154 if( !FloatUtil.isZero(lineWidth) ) {
155 shape.addEmptyOutline();
156 // Inner OutlineShape as Winding.CW.
157 // final float dxy0 = getWidth() < getHeight() ? getWidth() : getHeight();
158 final float dxy = lineWidth; // dxy0 * getDebugBox();
159 shape.moveTo(x1+dxy, y1+dxy, z);
160 shape.lineTo(x1+dxy, y2-dxy, z);
161 shape.lineTo(x2-dxy, y2-dxy, z);
162 shape.lineTo(x2-dxy, y1+dxy, z);
163 shape.lineTo(x1+dxy, y1+dxy, z);
164 shape.closeLastOutline(true);
165 }
166 shape.setIsQuadraticNurbs();
168
169 resetGLRegion(glp, gl, null, shape);
170 region.addOutlineShape(shape, null, rgbaColor);
171 box.resize(shape.getBounds());
173 }
174
175 @Override
176 public String getSubString() {
177 return super.getSubString()+", dim "+getWidth() + " x " + getHeight();
178 }
179}
A Generic shape objects which is defined by a list of Outlines.
final void setIsQuadraticNurbs()
Claim this outline's vertices are all OutlineShape.VerticesState#QUADRATIC_NURBS, hence no cubic tran...
final void moveTo(final float x, final float y, final float z)
Start a new position for the next line segment at given point x/y (P1).
final void lineTo(final float x, final float y, final float z)
Add a line segment, intersecting the last point and the given point x/y (P1).
final void setSharpness(final float s)
Sets sharpness, defaults to DEFAULT_SHARPNESS.
final void closeLastOutline(final boolean closeTail)
Closes the last outline in the shape.
final void addEmptyOutline()
Add a new empty Outline to the end of this shape's outline list.
final void addOutlineShape(final OutlineShape shape, final AffineTransform t, final Vec4f rgbaColor)
Add the given OutlineShape to this region with the given optional AffineTransform.
Definition: Region.java:616
Graph based GLRegion Shape.
Definition: GraphShape.java:55
final void resetGLRegion(final GLProfile glp, final GL2ES2 gl, final TextureSequence colorTexSeq, int vertexCount, int indexCount)
Reset the GLRegion and reserving its buffers to have a free capacity for vertexCount and indexCount e...
final void markShapeDirty()
Marks the shape dirty, causing next draw() to recreate the Graph shape and reset the region.
Definition: Shape.java:688
final Vec4f rgbaColor
Default base-color w/o color channel, will be modulated w/ pressed- and toggle color.
Definition: Shape.java:261
final Shape setRotationPivot(final float px, final float py, final float pz)
Set unscaled rotation origin, aka pivot.
Definition: Shape.java:620
A GraphUI rectangle GraphShape.
Definition: Rectangle.java:47
Rectangle(final int renderModes, final float minX, final float minY, final float width, final float height, final float lineWidth)
Create a rectangular Graph based GLRegion UI Shape.
Definition: Rectangle.java:95
void addShapeToRegion(final GLProfile glp, final GL2ES2 gl)
Definition: Rectangle.java:138
Rectangle(final int renderModes, final float minX, final float minY, final float width, final float height, final float lineWidth, final float zPos)
Create a rectangular Graph based GLRegion UI Shape.
Definition: Rectangle.java:64
Rectangle(final int renderModes, final float width, final float height, final float lineWidth)
Create a rectangular Graph based GLRegion UI Shape.
Definition: Rectangle.java:107
void setBounds(final AABBox abox, final float lineWidth)
Definition: Rectangle.java:132
void setDimension(final float width, final float height, final float lineWidth)
Definition: Rectangle.java:124
void setPosition(final float minX, final float minY, final float zPos)
Definition: Rectangle.java:118
Rectangle(final int renderModes, final AABBox abox, final float lineWidth)
Create a rectangular Graph based GLRegion UI Shape.
Definition: Rectangle.java:81
Basic Float math utility functions.
Definition: FloatUtil.java:83
static boolean isZero(final float a, final float epsilon)
Returns true if value is zero, i.e.
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final float getWidth()
Definition: AABBox.java:879
final float getHeight()
Definition: AABBox.java:883
final AABBox resize(final AABBox newBox)
Resize the AABBox to encapsulate another AABox.
Definition: AABBox.java:274
final Vec3f getCenter()
Returns computed center of this AABBox of getLow() and getHigh().
Definition: AABBox.java:737
Specifies the the OpenGL profile.
Definition: GLProfile.java:77