28package com.jogamp.opengl.demos.graph.ui;
31import java.io.IOException;
33import com.jogamp.opengl.FPSCounter;
34import com.jogamp.opengl.GL;
35import com.jogamp.opengl.GL2ES2;
36import com.jogamp.opengl.GLAnimatorControl;
37import com.jogamp.opengl.GLAutoDrawable;
38import com.jogamp.opengl.GLCapabilities;
39import com.jogamp.opengl.GLEventListener;
40import com.jogamp.opengl.GLException;
41import com.jogamp.opengl.GLPipelineFactory;
42import com.jogamp.opengl.GLRunnable;
43import com.jogamp.opengl.demos.graph.MSAATool;
44import com.jogamp.opengl.demos.util.CommandlineOptions;
45import com.jogamp.common.util.InterruptSource;
46import com.jogamp.graph.curve.Region;
47import com.jogamp.graph.curve.opengl.RegionRenderer;
48import com.jogamp.graph.curve.opengl.TextRegionUtil;
49import com.jogamp.graph.font.Font;
50import com.jogamp.graph.font.FontFactory;
51import com.jogamp.graph.font.FontSet;
52import com.jogamp.graph.ui.Shape;
53import com.jogamp.graph.ui.shapes.Button;
54import com.jogamp.graph.ui.shapes.CrossHair;
55import com.jogamp.math.FloatUtil;
56import com.jogamp.math.Recti;
57import com.jogamp.math.Vec3f;
58import com.jogamp.math.Vec4f;
59import com.jogamp.math.geom.AABBox;
60import com.jogamp.math.geom.plane.AffineTransform;
61import com.jogamp.math.util.PMVMatrix4f;
62import com.jogamp.newt.Window;
63import com.jogamp.newt.event.KeyAdapter;
64import com.jogamp.newt.event.KeyEvent;
65import com.jogamp.newt.event.KeyListener;
66import com.jogamp.newt.event.MouseEvent;
67import com.jogamp.newt.event.MouseListener;
68import com.jogamp.newt.event.WindowAdapter;
69import com.jogamp.newt.event.WindowEvent;
70import com.jogamp.newt.opengl.GLWindow;
71import com.jogamp.opengl.util.Animator;
72import com.jogamp.opengl.util.GLReadBufferUtil;
86 static final boolean DEBUG =
false;
87 static final boolean TRACE =
false;
91 public static void main(
final String[] args)
throws IOException {
93 final int[] idx = { 0 };
94 for (idx[0] = 0; idx[0] < args.length; ++idx[0]) {
95 if( options.
parse(args, idx) ) {
97 }
else if(args[idx[0]].equals(
"-font")) {
102 System.err.println(options);
109 System.out.println(
"Requested: " + reqCaps);
122 animator.
add(window);
126 public void keyPressed(
final KeyEvent arg0) {
129 new InterruptSource.Thread( () -> { window.
destroy(); } ).start();
143 private final Font font;
145 private final int renderModes;
147 private final boolean debug;
148 private final boolean trace;
150 private final Button button;
153 private KeyAction keyAction;
154 private MouseAction mouseAction;
158 private final float[] position =
new float[] {0,0,0};
160 private static final float xTran = 0f;
161 private static final float yTran = 0f;
162 private static final float zTran = -1/5f;
163 private static final float zNear = 0.1f;
164 private static final float zFar = 7000.0f;
166 boolean ignoreInput =
false;
171 public UIShapeDemo01(
final Font font,
final int renderModes,
final boolean debug,
final boolean trace) {
173 this.renderModes = renderModes;
181 final float sz1_w = 1/8f;
182 final float sz2 = 1/20f;
183 button =
new Button(renderModes, font,
"Click me!", sz1_w, sz1_w/2f);
191 System.err.println(button);
192 crossHair =
new CrossHair(renderModes, sz2, sz2, 1/1000f);
202 autoDrawable = drawable;
220 public void reshape(
final GLAutoDrawable drawable,
final int xstart,
final int ystart,
final int width,
final int height) {
231 if( drawable instanceof
Window ) {
232 ((
Window)drawable).setTitle(
UIShapeDemo01.class.getSimpleName()+
": "+drawable.getSurfaceWidth()+
" x "+drawable.getSurfaceHeight());
240 shape.
draw(gl, renderer);
242 System.err.println(
"draw.0: "+shape);
244 System.err.println(
"draw.1: surfaceSize "+winSize[0]+
" x "+winSize[1]);
246 System.err.println(
"draw.2: winCoord "+winPos[0]+
" x "+winPos[1]);
260 renderer.
enable(gl,
true);
261 drawShape(gl, renderer, button);
262 drawShape(gl, renderer, crossHair);
264 final String text =
"Hello Origin.";
265 final float full_width_o;
267 final float orthoDist = -zTran;
275 System.err.printf(
"winToObjCoord: win [%f, %f, %f] -> obj [%s]%n", glWinX, glWinY, winZ, objCoord0);
282 System.err.printf(
"winToObjCoord: win [%f, %f, %f] -> obj [%s]%n", glWinX, glWinY, winZ, objCoord1);
285 full_width_o = objCoord1.x() - objCoord0.x();
288 final float full_width_s = full_width_o / txt_box_em.
getWidth();
289 final float txt_scale = full_width_s/2f;
291 pmv.
scaleMv(txt_scale, txt_scale, 1f);
296 System.err.println(
"XXX: full_width: "+full_width_o+
" / "+txt_box_em.
getWidth()+
" -> "+full_width_s);
297 System.err.println(
"XXX: txt_box_em "+txt_box_em);
298 System.err.println(
"XXX: txt_box_e2 "+txt_box_em2);
299 System.err.println(
"XXX: txt_box_rg "+txt_box_r);
304 renderer.
enable(gl,
false);
306 static boolean once =
true;
321 if (
null == keyAction ) {
325 if (
null == mouseAction ) {
332 if (
null == keyAction ) {
335 if (
null == mouseAction ) {
344 final String sw = String.format(
"-%03dx%03d-Z%04d-T%04d-%s", drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), (
int)Math.abs(zTran), 0, objName);
346 final String filename = dir + tech + sw +
".png";
347 if(screenshot.
readPixels(drawable.getGL(),
false)) {
348 screenshot.
write(
new File(filename));
352 int screenshot_num = 0;
381 System.err.println(
"\n\nMouse: "+e);
390 final int glWinX = e.
getX();
391 final int glWinY = viewport.height() - e.
getY() - 1;
397 System.err.println(
"\n\nButton: "+button);
399 if(
null != objPos ) {
400 System.err.println(
"Button: Click: Win "+glWinX+
"/"+glWinY+
" -> Obj "+objPos);
403 final int[] surfaceSize = button.
getSurfaceSize(pmv, viewport,
new int[2]);
404 if(
null != surfaceSize ) {
405 System.err.println(
"Button: Size: Pixel "+surfaceSize[0]+
" x "+surfaceSize[1]);
415 System.err.println(
"\n\nCrossHair: "+crossHair);
416 final int[] objWinPos = crossHair.
shapeToWinCoord(pmv, viewport, objPosC,
new int[2]);
417 System.err.println(
"CrossHair: Obj: Obj "+objPosC+
" -> Win "+objWinPos[0]+
"/"+objWinPos[1]);
420 System.err.println(
"CrossHair: Obj: Win "+objWinPos[0]+
"/"+objWinPos[1]+
" -> Obj "+objPos2);
423 if(
null != winObjPos ) {
428 System.err.println(
"CrossHair: Move.1: Win "+glWinX+
"/"+glWinY+
" -> Obj "+winObjPos+
" -> diff "+diff);
429 crossHair.
move(diff.x(), diff.y(), 0f);
431 System.err.println(
"CrossHair: Move.0: Win "+glWinX+
"/"+glWinY+
" -> Obj "+winObjPos+
" -> diff "+diff);
435 final int[] surfaceSize = crossHair.
getSurfaceSize(pmv, viewport,
new int[2]);
436 System.err.println(
"CrossHair: Size: Pixel "+surfaceSize[0]+
" x "+surfaceSize[1]);
471 button.
move(0f, 0f, -zTran/10f);
474 button.
move(0f, 0f, zTran/10f);
490 System.err.println(
"Button Spacing: " + button.
getSpacing());
494 System.err.println(
"Button Spacing: " + button.
getSpacing());
498 System.err.println(
"Button Corner: " + button.
getCorner());
502 System.err.println(
"Button Corner: " + button.
getCorner());
511 if(
null != autoDrawable) {
515 final GL gl = drawable.
getGL();
519 case 0: i = 1;
break;
520 case 1: i = -1;
break;
521 case -1: i = 0;
break;
522 default: i = 1;
break;
533 System.err.println(
"Swap Interval: "+_i+
" -> "+i+
" -> "+gl.
getSwapInterval());
540 if(
null != autoDrawable) {
546 printScreen(drawable,
"./",
"demo-"+type,
"snap"+screenshot_num,
false);
550 }
catch (
final IOException e) {
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
static String getRenderModeString(final int renderModes)
Returns a unique technical description string for renderModes as follows:
static final int VBAA_RENDERING_BIT
Rendering-Mode bit for Region.
final void enable(final GL2ES2 gl, final boolean enable)
Enabling or disabling the RenderState's current shader program.
final PMVMatrix4f getMatrix()
Borrow the current PMVMatrix4f.
final int setSampleCount(final int v)
Sets pass2 AA sample count clipped to the range [Region#MIN_AA_SAMPLE_COUNT..Region#MAX_AA_SAMPLE_COU...
static final GLCallback defaultBlendDisable
Default GL#GL_BLEND disable GLCallback, simply turning-off the GL#GL_BLEND state and turning-on depth...
final int setAAQuality(final int v)
Sets pass2 AA-quality rendering value clipped to the range [Region#MIN_AA_QUALITY....
static final GLCallback defaultBlendEnable
Default GL#GL_BLEND enable GLCallback, turning-off depth writing via GL#glDepthMask(boolean) if Rende...
final void init(final GL2ES2 gl)
Initialize shader and bindings for GPU based rendering bound to the given GL object's GLContext if no...
static RegionRenderer create()
Create a hardware accelerated RegionRenderer including its RenderState composition.
final void reshapePerspective(final float angle_rad, final int width, final int height, final float near, final float far)
Perspective projection, method also calls reshapeNotify(int, int, int, int).
final Recti getViewport(final Recti target)
Copies the current Rect4i viewport in given target and returns it for chaining.
final void destroy(final GL2ES2 gl)
Deletes all ShaderPrograms and nullifies its references including RenderState#destroy(GL2ES2).
Text Type Rendering Utility Class adding the Font.Glyphs OutlineShape to a GLRegion.
AABBox drawString3D(final GL2ES2 gl, final RegionRenderer renderer, final Font font, final CharSequence str, final Vec4f rgbaColor)
Render the string in 3D space w.r.t.
The optional property jogamp.graph.font.ctor allows user to specify the FontConstructor implementatio...
static final FontSet get(final int font)
static final int UBUNTU
Ubuntu is the default font family, {@value}.
Generic Shape, potentially using a Graph via GraphShape or other means of representing content.
Shape setColor(final float r, final float g, final float b, final float a)
Set base color.
final Shape move(final float dtx, final float dty, final float dtz)
Move about scaled distance.
void draw(final GL2ES2 gl, final RegionRenderer renderer)
Renders the shape.
final Vec3f winToShapeCoord(final PMVMatrix4f pmv, final Recti viewport, final int glWinX, final int glWinY, final Vec3f objPos)
Map given gl-window-coordinates to object coordinates relative to this shape and its z-coordinate.
final Vec3f getPosition()
Returns position Vec3f reference, i.e.
final AABBox getBounds()
Returns the unscaled bounding AABBox for this shape, borrowing internal instance.
final int[] getSurfaceSize(final PMVMatrix4f pmv, final Recti viewport, final int[] surfaceSize)
Retrieve surface (view) size in pixels of this shape.
final void destroy(final GL2ES2 gl, final RegionRenderer renderer)
Destroys all data.
final int[] shapeToWinCoord(final PMVMatrix4f pmv, final Recti viewport, final Vec3f objPos, final int[] glWinPos)
Map given object coordinate relative to this shape to window coordinates.
final void applyMatToMv(final PMVMatrix4f pmv)
Applies the internal Matrix4f to the given modelview matrix, i.e.
final Shape setVisible(final boolean v)
Enable (default) or disable this shape's visibility.
A GraphUI Crosshair GraphShape.
Basic Float math utility functions.
static float getOrthoWinZ(final float orthoZ, final float zNear, final float zFar)
Returns orthogonal distance (1f/zNear-1f/orthoZ) / (1f/zNear-1f/zFar);.
static final float QUARTER_PI
The value PI/4, i.e.
static boolean isZero(final float a, final float epsilon)
Returns true if value is zero, i.e.
Rectangle with x, y, width and height integer components.
3D Vector based upon three float components.
Vec3f minus(final Vec3f arg)
Returns this - arg; creates new vector.
4D Vector based upon four float components.
Axis Aligned Bounding Box.
final Vec3f getCenter()
Returns computed center of this AABBox of getLow() and getHigh().
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
final PMVMatrix4f translateMv(final float x, final float y, final float z)
Translate the modelview matrix.
final PMVMatrix4f scaleMv(final float x, final float y, final float z)
Scale the modelview matrix.
final PMVMatrix4f loadMvIdentity()
Load the modelview matrix with the values of the given Matrix4f.
final PMVMatrix4f popMv()
Pop the modelview matrix from its stack.
final boolean mapWinToObj(final float winx, final float winy, final float winz, final Recti viewport, final Vec3f objPos)
Map window coordinates to object coordinates.
final PMVMatrix4f pushMv()
Push the modelview matrix to its stack, while preserving its values.
static final short VK_RIGHT
Constant for the cursor- or numerical-pad right arrow key.
static final short VK_F4
Constant for the F4 function key.
static final short VK_2
See VK_0.
static final short VK_ESCAPE
Constant for the ESCAPE function key.
static final short VK_UP
Constant for the cursor- or numerical-pad up arrow key.
static final short VK_LEFT
Constant for the cursor- or numerical-pad left arrow key.
final short getKeySymbol()
Returns the virtual key symbol reflecting the current keyboard layout.
static final short VK_DOWN
Constant for the cursor- or numerical pad down arrow key.
static final short VK_4
See VK_0.
static final short VK_9
See VK_0.
static final short VK_5
See VK_0.
static final short VK_0
VK_0 thru VK_9 are the same as UTF16/ASCII '0' thru '9' [0x30 - 0x39].
static final short VK_S
See VK_A.
static final short VK_Q
See VK_A.
static final short VK_V
See VK_A.
static final short VK_1
See VK_0.
final short getKeyCode()
Returns the virtual key code using a fixed mapping to the US keyboard layout.
static final short VK_7
See VK_0.
static final short VK_6
See VK_0.
Pointer event of type PointerType.
final int getY()
See details for multiple-pointer events.
final int getX()
See details for multiple-pointer events.
NEWT Window events are provided for notification purposes ONLY.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
final void addMouseListener(final MouseListener l)
Appends the given MouseListener to the end of the list.
final void setTitle(final String title)
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
final void removeKeyListener(final KeyListener l)
final void removeMouseListener(final MouseListener l)
Removes the given MouseListener from the list.
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Specifies a set of OpenGL capabilities.
abstract GL setGL(GL gl)
Sets the GL pipeline object for this GLContext.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Factory for pipelining GL instances.
static final GL create(final String pipelineClazzBaseName, final Class<?> reqInterface, final GL downstream, final Object[] additionalArgs)
Creates a pipelined GL instance using the given downstream downstream and optional arguments addition...
void keyPressed(final KeyEvent arg0)
A key has been pressed, excluding auto-repeat modifier keys.
void keyReleased(final KeyEvent arg0)
A key has been released, excluding auto-repeat modifier keys.
void mouseClicked(final MouseEvent e)
void mouseMoved(final MouseEvent e)
void mouseDragged(final MouseEvent e)
void mouseEntered(final MouseEvent e)
Only generated for PointerType#Mouse.
void mouseReleased(final MouseEvent e)
void mouseWheelMoved(final MouseEvent e)
Traditional event name originally produced by a mouse pointer type.
void mousePressed(final MouseEvent e)
void mouseExited(final MouseEvent e)
Only generated for PointerType#Mouse.
Basic UIShape and Type Rendering demo.
void setIgnoreInput(final boolean v)
void attachInputListenerTo(final GLWindow window)
Attach the input listener to the window.
static void main(final String[] args)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
final float[] getPosition()
void reshape(final GLAutoDrawable drawable, final int xstart, final int ystart, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
final AffineTransform tempT2
void printScreen(final GLAutoDrawable drawable, final String dir, final String tech, final String objName, final boolean exportAlpha)
UIShapeDemo01(final Font font, final int renderModes, final boolean debug, final boolean trace)
final RegionRenderer getRegionRenderer()
void detachFrom(final GLWindow window)
final AffineTransform tempT1
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
GLCapabilities getGLCaps()
int graphAASamples
Sample count for Graph Region AA render-modes: Region#VBAA_RENDERING_BIT or Region#MSAA_RENDERING_BIT...
int graphAAQuality
Pass2 AA-quality rendering for Graph Region AA render-modes: VBAA_RENDERING_BIT.
void parse(final String[] args)
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
final synchronized boolean stop()
Stops this animator.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
void write(final File dest)
Write the TextureData filled by readPixels(GLAutoDrawable, boolean) to file.
void dispose(final GL gl)
boolean readPixels(final GL gl, final boolean mustFlipVertically)
Read the drawable's pixels to TextureData and Texture, if requested at construction.
static final int FAMILY_LIGHT
Font family LIGHT, {@value}.
Font get(int family, int stylebits)
static final int STYLE_SERIF
SERIF style/family bit flag.
Interface wrapper for font implementation.
AABBox getGlyphShapeBounds(final AffineTransform transform, final CharSequence string)
Returns accurate bounding box by taking each glyph's font em-sized OutlineShape into account.
String getFullFamilyName()
Shall return the family and subfamily name, separated a dash.
AABBox getGlyphBounds(final CharSequence string)
Try using getGlyphBounds(CharSequence, AffineTransform, AffineTransform) to reuse AffineTransform ins...
Specifying NEWT's Window functionality:
Listener for MouseEvents.
void resetFPSCounter()
Reset all performance counter (startTime, currentTime, frame number)
An animator control interface, which implementation may drive a com.jogamp.opengl....
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
boolean invoke(boolean wait, GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
GLAnimatorControl getAnimator()
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GLEventListener removeGLEventListener(GLEventListener listener)
Removes the given listener from this drawable queue.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
GLContext getContext()
Returns the GLContext associated which this GL object.
int getSwapInterval()
Return the current swap interval.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...