java.lang.Object
de.grogra.imp.awt.ViewComponentAdapter
de.grogra.glsl.GLDisplay
- All Implemented Interfaces:
ViewComponent,RenderState,ComponentWrapper,Selectable,Disposable,ImageObserver,Runnable,EventListener,javax.media.opengl.GLEventListener
- Direct Known Subclasses:
GLSLDisplay
public class GLDisplay
extends ViewComponentAdapter
implements javax.media.opengl.GLEventListener, RenderState, ImageObserver, Selectable
This class is responsible for drawing all visible objects in the GroIMP3D
editor. It uses OpenGL to provide hardware accelerated drawing.
To access OpenGL, the library JOGL has to be installed. JOGL is available
from: https://jogl.dev.java.net/
Download the jogl.jar (contains the Java API for JOGL) and the
jogl-natives-*.jar (contains the native library for your system).
When starting GroIMP, make sure that the jogl.jar is in your classpath
and the libraries (.dll or .so) from the jogl-natives-*.jar are in your
search path for system libraries.
Making thos files available can be done by calling GroIMP like this:
\code
(linux version)
javaw -cp core.jar:jogl.jar -Djava.library.path=/usr/lib/jogl de.grogra.pf.boot.Main
(windows version)
javaw -cp core.jar;jogl.jar -Djava.library.path=c:\java\jogl de.grogra.pf.boot.Main
\endcode
OpenGL acts as a state machine. To provide a consistent state for every
drawing function, some rules were created. There are state variables that
are expected to have a certain value when a drawing function is entered.
Those state variables are:
- MatrixMode set to GL_MODELVIEW
- ShadeModel set to GL_SMOOTH
- GL_DEPTH_TEST enabled
- GL_LIGHTING enabled
- GL_TEXTURE_2D disabled
- GL_CULL_FACE enabled
- GL_COLOR_MATERIAL enabled
- ColorMaterial is set to GL_FRONT_AND_BACK and GL_AMBIENT_AND_DIFFUSE
- GL_NORMALIZE is enabled
- PolygonMode is set to GL_FRONT_AND_BACK and GL_LINE
- GL_ALPHA_TEST enabled
- AlphaFunc set to GL_GREATER and 0.1f
- GL_LIGHT_MODEL_TWO_SIDE is set to 0
If a drawing function changes some of those state variables, it has to
reset them to their previous value upon return.
Other state variables are not expected to have a defined value when a
drawing function is called. Those state variables are:
- all texture related state for TEXTURE UNIT 0
- the contents of the matrices MODELVIEW and TEXTURE
All state variables not mentioned above should be reset to their default
value as defined by OpenGL when they were changed.
This class also provides an implementation of LOD (level of detail). For
now lod is only applied to the sphere and frustum primitives. While the
sphere uses the lod to select the appropriate display list, the frustum
uses it to calculate the number of generated segments when drawing.
- Author:
- nmi
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThis class is used to traverse the graph and obtain all visible objects and the lights for visualisation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected intDisplay list number for spheres.protected intDisplay list number for supershapes.protected final Point3fprotected final Color3fprotected final int[]protected static final intmaximum number of levels of detailprotected floatprotected floatprotected floatprotected intprotected floatprotected booleanprotected booleanprotected booleanprotected booleanprotected booleanprotected PolygonizationCachefinal Poolstatic final Typeprotected final Point3fprotected booleanIf the GL context supports the ARB extension GL_ARB_vertex_buffer_object this flag is set to true in the initialization function.protected GLDisplay.GLVisitorFields inherited from class de.grogra.imp.awt.ViewComponentAdapter
DISPOSED, DISPOSING, RENDER_OVERLAY, RENDER_OVERLAY_FINISHED, RENDERED_IMAGE, RENDERING, RENDERING_OVERLAY, REPAINT_MASKFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields inherited from interface de.grogra.imp3d.RenderState
CURRENT_HIGHLIGHTFields inherited from interface de.grogra.imp.ViewComponent
ALL, CHANGED, MIN_USER_FLAG, SCENE, SELECTION, TOOLS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddisplay(javax.media.opengl.GLAutoDrawable d) voiddisplayChanged(javax.media.opengl.GLAutoDrawable d, boolean modeChanged, boolean deviceChanged) voiddispose()protected voidvoiddrawBox(float halfWidth, float halfLength, float height, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a box from -axis to +axis using the transformation matrix t.protected voiddrawDisplaySize(javax.media.opengl.GL gl, Camera c) voiddrawFrustum(float length, float baseRadius, float topRadius, boolean baseClosed, boolean topClosed, float scaleV, Shader s, int highlight, boolean asWireframe, Matrix4d t) Can be used to draw cylinders and the like.voiddrawFrustumIrregular(float length, int sectorCount, float[] baseRadii, float[] topRadii, boolean baseClosed, boolean topclosed, float scaleV, Shader s, int highlight, boolean asWireframe, Matrix4d t) voiddrawLamella(float halfWidth, float halfLength, float height, float a, float b, Shader s, int highlight, boolean asWireframe, Matrix4d t) voidConnect the two points with a line and apply the transformation matrix.voiddrawParallelogram(float length, Vector3f axis, float scaleU, float scaleV, Shader s, int highlight, boolean asWireframe, Matrix4d t) voidDraw an x/y plane.voidDraw a single point with the specified pixel size and color.voiddrawPointCloud(float[] locations, float pointSize, Tuple3f color, int highlight, Matrix4d t) Draw a set of points.voiddrawPolygons(Polygonizable pz, Object obj, boolean asNode, Shader s, int highlight, boolean asWireframe, Matrix4d t) voiddrawPrismRectangular(float y, float xPos, float xNeg, float zPos, float zNeg, int highlight, boolean asWireframe, Matrix4d t) voiddrawRectangle(int x, int y, int w, int h, Tuple3f color) Draw a rectangle from (x/y) to (x+w/y+h) with the specified color.voiddrawSphere(float radius, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a sphere with the specified radius around the origin (0/0/0).voiddrawSphereSegmentSolid(float radius, float theta1, float theta2, float phi, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a sphere segment with the specified radius, theta1, theta2, and phi, around the origin (0/0/0).voiddrawString(int x, int y, String text, Font font, Tuple3f color) Draw a string with the selected font and color at location (x/y).voiddrawSupershape(float a, float b, float m1, float n11, float n12, float n13, float m2, float n21, float n22, float n23, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a supershape around the origin (0/0/0).voiddrawTextBlock(String caption, Font font, float depth, Shader s, int highlight, boolean asWireframe, Matrix4d t) intdrawWithDisplayList(int index, javax.media.opengl.GL gl, de.grogra.glsl.DisplayListRenderable dlr, int levels, float lod) floatestimateScaleAt(Tuple3f point) floatestimateScaleAt(Tuple3f point, Matrix4d t) Returns the size of an object of size 1.0 (in object coordinates) on the screen (in pixels).voidfillRectangle(int x, int y, int w, int h, Tuple3f color) Draw a filled rectangle from (x/y) to (x+w/y+h) with the specified color.intprotected floatCalculate level of detail.protected ImageObserverReturns an observer which receives the information about the rendered image from aRenderer.getPool()protected Shaderprotected Matrix4dbooleangetWindowPos(Tuple3f origin, Tuple2f out) Computes the window coordinates in pixels of a location in the current object coordinates.booleanimageUpdate(Image img, int infoflags, int x, int y, int width, int height) This class implements the ImageObserver interface to be notified about changes of the background image.voidinit(javax.media.opengl.GLAutoDrawable d) Initialise opengl state and set default values.protected voidinitRender(int flags) Performs initialization tasks in preparation for rendering.voidinitView(View view, EventListener listener) Initializes this component.protected voidinvokeRender(int flags) Invoked to perform rendering.static booleanisExtensionSupported(javax.media.opengl.GL gl, String extension) voidmakeSnapshot(ObjectConsumer<? super RenderedImage> callback) Instructs the view component to create a snapshot.protected voidoptionValueChanged(String name, Object value) protected voidrender(int flags) Render the scene graph.voidreshape(javax.media.opengl.GLAutoDrawable d, int x, int y, int w, int h) Methods inherited from class de.grogra.imp.awt.ViewComponentAdapter
checkRepaint, checkRepaintWrapException, disposeRenderer, disposeView, getColor, getColor, getDisplayResolution, getFactory, getFontMetrics, getGlobalLOD, getIntColor, getInterruptedException, getOption, getRenderGraphState, getView, initFactory, installListeners, invokeRenderSync, isRenderingOverlay, render, render, renderUninterruptibly, repaint, run, toSelection, uninstallListeners, updateResolutionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.grogra.imp3d.RenderState
getFontMetrics, getRenderGraphStateMethods inherited from interface de.grogra.pf.ui.edit.Selectable
toSelection
-
Field Details
-
optionShowPoints
protected boolean optionShowPoints -
optionShowGrid
protected boolean optionShowGrid -
optionRenderAsWireframe
protected boolean optionRenderAsWireframe -
optionGridDimension
protected int optionGridDimension -
optionGridSpacing
protected float optionGridSpacing -
optionGridColorR
protected float optionGridColorR -
optionGridColorG
protected float optionGridColorG -
optionGridColorB
protected float optionGridColorB -
optionShowAxes
protected boolean optionShowAxes -
optionShowDisplaySize
protected boolean optionShowDisplaySize -
supportsVBO
protected boolean supportsVBOIf the GL context supports the ARB extension GL_ARB_vertex_buffer_object this flag is set to true in the initialization function. When this flag is true, rendering of surfaces (i.e. NURBS) uses the faster vertex buffer objects. -
LOD_LEVELS
protected static final int LOD_LEVELSmaximum number of levels of detail- See Also:
-
pool
-
visitor
-
polyCache
-
curHighlight
protected int curHighlight -
dlSphere
protected int dlSphereDisplay list number for spheres. There will be display lists generated for numbers dlSphere to (dlSphere + LOD_LEVELS - 1). A value of 0 for dlSphere means, that no display list is available. -
dlSupershape
protected int dlSupershapeDisplay list number for supershapes. There will be display lists generated for numbers dlSupershape to (dlSupershape + LOD_LEVELS - 1). A value of 0 for dlSupershape means, that no display list is available. -
RENDER_MODE
-
lineWidth
protected final int[] lineWidth -
gridColor
-
startPoint
-
endPoint
-
-
Constructor Details
-
GLDisplay
public GLDisplay()
-
-
Method Details
-
getObserverForRenderer
Description copied from class:ViewComponentAdapterReturns an observer which receives the information about the rendered image from aRenderer. The returned observer has to manage the drawing of the (partially) rendered image on the view component.- Specified by:
getObserverForRendererin classViewComponentAdapter- Returns:
- an observer receiving the image, or
nullif this is not supported by this component
-
imageUpdate
This class implements the ImageObserver interface to be notified about changes of the background image.- Specified by:
imageUpdatein interfaceImageObserver
-
makeSnapshot
Description copied from interface:ViewComponentInstructs the view component to create a snapshot. The created snapshot has to be delivered to the providedcallbackas an instance ofRenderedImage. This may happen asynchronously, i.e., in an arbitrary thread.- Specified by:
makeSnapshotin interfaceViewComponent- Parameters:
callback- callback which asynchronously receives the snapshot
-
initRender
protected void initRender(int flags) Description copied from class:ViewComponentAdapterPerforms initialization tasks in preparation for rendering. This method is invoked byViewComponentAdapter.run()in thisViewComponent's own thread.- Specified by:
initRenderin classViewComponentAdapter- Parameters:
flags- combination of bit masks
-
invokeRender
protected void invokeRender(int flags) Description copied from class:ViewComponentAdapterInvoked to perform rendering. This method is invoked byViewComponentAdapter.run()in thisViewComponent's own thread. Its sole task is to invokeViewComponentAdapter.invokeRenderSync(int)in the rendering thread (which may be thisViewComponent's thread, the AWT-thread, or another thread, depending on the implementation) in a write-protected context. The invocation has to be synchronously, i.e., if it is in another thread, the current thread has to wait untilViewComponentAdapter.invokeRenderSync(int)has completed.- Specified by:
invokeRenderin classViewComponentAdapter- Parameters:
flags- the flags to pass toViewComponentAdapter.invokeRenderSync(int)
-
display
public void display(javax.media.opengl.GLAutoDrawable d) - Specified by:
displayin interfacejavax.media.opengl.GLEventListener
-
dispose
public void dispose()- Specified by:
disposein interfaceDisposable- Overrides:
disposein classViewComponentAdapter
-
getComponent
- Specified by:
getComponentin interfaceComponentWrapper
-
getView3D
-
initView
Description copied from interface:ViewComponentInitializes this component. This method sets theviewwithin which this view component is used to display the graph. It also sets an event listener. this has to be informed of mouse and keys events within the view component by implementations of this method.- Specified by:
initViewin interfaceViewComponent- Overrides:
initViewin classViewComponentAdapter- Parameters:
view- the containing viewlistener- mouse and key events will be reported to this listener
-
render
Render the scene graph. This function will traverse the scene graph and render all objects of it. These steps are performed:
- get GL
- if GL changed, delete textures
- clear the frame buffer
- draw background image (i.e. from povray)
- cleaar depth buffer
- set projection matrix
- check if the scene graph changed since last render, than remember that and perform an update of the light sources
- walk graph first time, visit every object, update lights if necessary
- if lights have changed, draw the scene again
- walk graph second time, visit highlighted objects
- disable lighting and depth buffer
- walk graph third time, visit tools (i.e. arrows to move objects)
- enable lighting and depth buffer
- Specified by:
renderin classViewComponentAdapter- Parameters:
flags- combination of bit masks- Throws:
InterruptedException- if the rendering has been interrupted
-
init
public void init(javax.media.opengl.GLAutoDrawable d) Initialise opengl state and set default values. All functions can assume those values being set upon call (i.e. MatrixMode as GL_MODELVIEW). If a function changes any of those values, it has to restore its state before calling another function or exiting.- Specified by:
initin interfacejavax.media.opengl.GLEventListener
-
reshape
public void reshape(javax.media.opengl.GLAutoDrawable d, int x, int y, int w, int h) - Specified by:
reshapein interfacejavax.media.opengl.GLEventListener
-
displayChanged
public void displayChanged(javax.media.opengl.GLAutoDrawable d, boolean modeChanged, boolean deviceChanged) - Specified by:
displayChangedin interfacejavax.media.opengl.GLEventListener
-
getPool
- Specified by:
getPoolin interfaceRenderState
-
getCurrentShader
- Specified by:
getCurrentShaderin interfaceRenderState
-
getCurrentHighlight
public int getCurrentHighlight()- Specified by:
getCurrentHighlightin interfaceRenderState
-
estimateScaleAt
- Specified by:
estimateScaleAtin interfaceRenderState
-
estimateScaleAt
Returns the size of an object of size 1.0 (in object coordinates) on the screen (in pixels).- Parameters:
point-t-- Returns:
-
getLOD
Calculate level of detail. The returned value is 0.0f for lowest detail and 1.0f for highest detail. This lod depends on global lod (i.e. decreases if the frame rate drops) and the scale (estimated size of a line of length 1.0 in pixels). The lod is calculated as: lod = globalLod * (scale - s_min) / (s_max - s_min) where s_min is the size of a line of length 1.0 in pixels for the smallest lod and s_max similar for the biggest lod, globalLod is a value from 0.0 to 1.0 Smallest scale is 5 pixels, biggest scale is 100 pixels- Returns:
-
getWindowPos
Description copied from interface:RenderStateComputes the window coordinates in pixels of a location in the current object coordinates.- Specified by:
getWindowPosin interfaceRenderState- Parameters:
origin- a location in local object coordinatesout- the computed window coordinates are placed in here- Returns:
trueiff the window coordinates are valid (i.e., the location is in the clipping region)
-
getShader
-
getTransformation
-
drawBox
public void drawBox(float halfWidth, float halfLength, float height, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a box from -axis to +axis using the transformation matrix t.- Specified by:
drawBoxin interfaceRenderState- Parameters:
halfWidth-halfLength-height-s-highlight-t-axis- defines size of box
-
drawLamella
public void drawLamella(float halfWidth, float halfLength, float height, float a, float b, Shader s, int highlight, boolean asWireframe, Matrix4d t) - Specified by:
drawLamellain interfaceRenderState- Parameters:
halfWidth-halfLength-height-a- amplitudeb- frequencys-highlight-t-
-
drawPoint
Draw a single point with the specified pixel size and color. Although OpenGL provides a function to draw points, the point is represented by a sphere, because some graphics cards/drivers seem to have problems setting the size for the point.- Specified by:
drawPointin interfaceRenderState- Parameters:
pixelSize- size of point on screen in pixels
-
drawPointCloud
public void drawPointCloud(float[] locations, float pointSize, Tuple3f color, int highlight, Matrix4d t) Description copied from interface:RenderStateDraw a set of points. The array locations contains a sequence of points given as triples of floats for x, y and z position of each point. If the same reference for the array location is passed in the implementation might assume that the contents of the array are the same as well. This is necessary for GLDisplay, for instance, to ensure a performant implementation. The class PointCloud ensures that this is the case.- Specified by:
drawPointCloudin interfaceRenderState- Parameters:
locations- array containing a sequence (x,y,z) of pointspointSize- size of the point on screencolor- color of the pointhighlight-t- transformation of the point cloud
-
drawLine
Connect the two points with a line and apply the transformation matrix.- Specified by:
drawLinein interfaceRenderState
-
drawParallelogram
public void drawParallelogram(float length, Vector3f axis, float scaleU, float scaleV, Shader s, int highlight, boolean asWireframe, Matrix4d t) - Specified by:
drawParallelogramin interfaceRenderState
-
drawPlane
Draw an x/y plane.- Specified by:
drawPlanein interfaceRenderState
-
drawSphere
Draw a sphere with the specified radius around the origin (0/0/0).- Specified by:
drawSpherein interfaceRenderState
-
drawSphereSegmentSolid
public void drawSphereSegmentSolid(float radius, float theta1, float theta2, float phi, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a sphere segment with the specified radius, theta1, theta2, and phi, around the origin (0/0/0).- Specified by:
drawSphereSegmentSolidin interfaceRenderState
-
drawTextBlock
public void drawTextBlock(String caption, Font font, float depth, Shader s, int highlight, boolean asWireframe, Matrix4d t) - Specified by:
drawTextBlockin interfaceRenderState
-
drawSupershape
public void drawSupershape(float a, float b, float m1, float n11, float n12, float n13, float m2, float n21, float n22, float n23, Shader s, int highlight, boolean asWireframe, Matrix4d t) Draw a supershape around the origin (0/0/0). An implementation of Johan Gielis's Superformula which was published in the American Journal of Botany 90(3): 333–338. 2003. INVITED SPECIAL PAPER A GENERIC GEOMETRIC TRANSFORMATION THAT UNIFIES A WIDE RANGE OF NATURAL AND ABSTRACT SHAPES- Specified by:
drawSupershapein interfaceRenderState- Parameters:
a- , b length of curveshighlight-t- transformation of the point cloudm- , n shape parametersshader-
-
drawFrustum
public void drawFrustum(float length, float baseRadius, float topRadius, boolean baseClosed, boolean topClosed, float scaleV, Shader s, int highlight, boolean asWireframe, Matrix4d t) Can be used to draw cylinders and the like. The frustum has a cirle at the top and bottom, which are connected with a surface. The center of the bottom circle is (0/0/0), the center of the top circle is axis. Both circles are parallel to the x/y plane.- Specified by:
drawFrustumin interfaceRenderState- Parameters:
length-baseRadius-topRadius-baseClosed-topClosed-scaleV-s-highlight-t-
-
drawWithDisplayList
public int drawWithDisplayList(int index, javax.media.opengl.GL gl, de.grogra.glsl.DisplayListRenderable dlr, int levels, float lod) -
drawPolygons
public void drawPolygons(Polygonizable pz, Object obj, boolean asNode, Shader s, int highlight, boolean asWireframe, Matrix4d t) - Specified by:
drawPolygonsin interfaceRenderState- Parameters:
pz-obj-asNode-s-highlight-t-
-
drawRectangle
Draw a rectangle from (x/y) to (x+w/y+h) with the specified color.- Specified by:
drawRectanglein interfaceRenderState
-
fillRectangle
Draw a filled rectangle from (x/y) to (x+w/y+h) with the specified color.- Specified by:
fillRectanglein interfaceRenderState
-
drawString
Draw a string with the selected font and color at location (x/y).- Specified by:
drawStringin interfaceRenderState
-
optionValueChanged
- Overrides:
optionValueChangedin classViewComponentAdapter
-
isExtensionSupported
-
drawAxes
-
drawDisplaySize
-
drawFrustumIrregular
public void drawFrustumIrregular(float length, int sectorCount, float[] baseRadii, float[] topRadii, boolean baseClosed, boolean topclosed, float scaleV, Shader s, int highlight, boolean asWireframe, Matrix4d t) - Specified by:
drawFrustumIrregularin interfaceRenderState
-
drawPrismRectangular
public void drawPrismRectangular(float y, float xPos, float xNeg, float zPos, float zNeg, int highlight, boolean asWireframe, Matrix4d t) - Specified by:
drawPrismRectangularin interfaceRenderState- Parameters:
y-xPos-xNeg-zPos-zNeg-highlight-t-
-