Userguide.html 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Jogl - User's Guide</TITLE>
  5. </HEAD>
  6. <BODY>
  7. <H1>Jogl - User's Guide</H1>
  8. <P>
  9. <UL>
  10. <LI> Overview
  11. <LI> Developing with JOGL
  12. <UL>
  13. <LI> Building the source tree
  14. <LI> Local installation for development
  15. <LI> Java Web Start integration
  16. <LI> Applet support
  17. </UL>
  18. <LI> GLDrawable and GLContext
  19. <LI> Creating a GLAutoDrawable
  20. <LI> Writing a GLEventListener
  21. <LI> Using the Composable Pipeline
  22. <LI> Heavyweight and Lightweight Issues
  23. <LI> Multithreading Issues
  24. <LI> Pbuffers
  25. <LI> GLU
  26. <LI> More Resources
  27. <LI> Platform notes
  28. <UL>
  29. <LI> All Platforms
  30. <LI> Windows
  31. <LI> Linux
  32. <LI> Solaris, Linux (X11 platforms)
  33. <LI> Macintosh OS X
  34. </UL>
  35. <LI> Version History
  36. </UL>
  37. <H2> Overview </H2>
  38. <P>
  39. Jogl is a Java programming language binding for the OpenGL 3D graphics
  40. API. It supports integration with the Java platform's AWT and Swing
  41. widget sets while providing a minimal and easy-to-use API that handles
  42. many of the issues associated with building multithreaded OpenGL
  43. applications. Jogl provides access to the latest OpenGL routines
  44. (OpenGL 2.0 with vendor extensions) as well as platform-independent
  45. access to hardware-accelerated offscreen rendering ("pbuffers"). Jogl
  46. also provides some of the most popular features introduced by other
  47. Java bindings for OpenGL like GL4Java, LWJGL and Magician, including a
  48. composable pipeline model which can provide faster debugging for
  49. Java-based OpenGL applications than the analogous C program.
  50. </P>
  51. <P>
  52. Jogl was designed for the most recent versions of the Java platform
  53. and for this reason supports only J2SE 1.4 and later. It also only
  54. supports truecolor (15 bits per pixel and higher) rendering; it does
  55. not support color-indexed modes. It was designed with New I/O (NIO) in
  56. mind and uses NIO internally in the implementation. The Jogl binding
  57. is itself written almost completely in the Java programming language.
  58. There are roughly 150 lines of handwritten C code in the entire Jogl
  59. source base (100 of which work around bugs in older OpenGL drivers on
  60. Windows); the rest of the native code is autogenerated during the
  61. build process by a new tool called <a
  62. href="http://gluegen.dev.java.net/">GlueGen</a>, the source code of
  63. which is available from its own java.net project.
  64. </P>
  65. <P>
  66. The JOGL source tree in its current form is an experimental workspace
  67. for the <a href="http://jcp.org/en/jsr/detail?id=231">JSR-231</a> Java
  68. Bindings for OpenGL JSR. JOGL is not the official reference
  69. implementation, but an evolving workspace. Snapshots of the JOGL
  70. source tree are run through the JSR-231 Technology Compatibility Kit
  71. (TCK) to become the official reference implementation (RI). As of this
  72. writing the JSR has not been finalized, so the first official RI of
  73. the JSR has not yet been produced.
  74. </P>
  75. <H2> Developing with JOGL </H2>
  76. <H3> Building the source tree </H3>
  77. <P>
  78. Most developers using JOGL will download the most current <a
  79. href="https://jogl.dev.java.net/servlets/ProjectDocumentList">release
  80. build</a>. Separate instructions are available on how to <a
  81. href="https://jogl.dev.java.net/unbranded-source/browse/*checkout*/jogl/doc/HowToBuild.html?rev=HEAD&amp;content-type=text/html">build
  82. the source tree</a>.
  83. </P>
  84. <H3> Local installation for development </H3>
  85. <P>
  86. The JOGL distribution for developers comes in the form of a zip
  87. archive which contains the Java classes to call OpenGL from Java, as
  88. well as the associated JNI native libraries. JOGL depends on some
  89. run-time support classes and native code provided by the GlueGen
  90. project; these classes and native code are also provided in the zip
  91. bundles.
  92. </P>
  93. <P>
  94. If you are developing a new application which uses JOGL, download the
  95. zip archive for your platform (for example.,
  96. jogl-[version]-windows-i586.zip) and unzip it. Modify your CLASSPATH
  97. environment variable to include the full paths to jogl.jar and
  98. gluegen-rt.jar; for example,
  99. ".;C:\Some\Other\Package\foo.jar;C:\Users\myhome\jogl-[version]-windows-i586\lib\jogl.jar;C:\Users\myhome\jogl-[version]-windows-i586\lib\gluegen-rt.jar".
  100. (If you did not previously set the CLASSPATH environment variable, you
  101. may want to make sure that ".", the current directory, is on your new
  102. CLASSPATH.) Modify your PATH environment variable (Windows),
  103. LD_LIBRARY_PATH environment variable (Solaris and Linux), or
  104. DYLD_LIBRARY_PATH environment variable (Mac OS X) to contain the full
  105. path to the "lib" directory; for example, on Windows, add
  106. "C:\Users\myhome\jogl-[version]-windows-i586\lib" to your PATH using
  107. the System control panel, Advanced tab, Environment Variables
  108. button. At this point your Java installation should be able to see the
  109. JOGL class files. Users of IDEs such as NetBeans and Eclipse should
  110. consult the IDE's documentation to see how to add jar files and native
  111. libraries to their current project.
  112. </P>
  113. <P>
  114. Dropping the JOGL jar and native library into the extension directory
  115. of the JRE is <b>strongly discouraged</b>. Doing so can cause
  116. conflicts with third-party applications launched via Java Web Start,
  117. and causes confusion later when upgrading the distribution.
  118. </P>
  119. <P>
  120. If you are on the Linux platform, please see the Linux-specific
  121. platform notes, below, with information on incompatibility between the
  122. JPackage Java RPMs and JOGL.
  123. </P>
  124. <H3> Java Web Start integration </H3>
  125. <P>
  126. The recommended distribution vehicle for applications using JOGL is
  127. Java Web Start. JOGL-based applications do not even need to be signed;
  128. all that is necessary is to reference the JOGL extension JNLP file.
  129. Because the JOGL jar files are signed, an unsigned application can
  130. reference the signed JOGL library and continue to run inside the
  131. sandbox.
  132. </P>
  133. <P>
  134. To reference JOGL within your application's JNLP file, simply place
  135. the following line in the <code>&lt;resources&gt;</code> section:
  136. <PRE>
  137. &lt;extension name="jogl" href="http://download.java.net/media/jogl/builds/archive/jsr-231-webstart-current/jogl.jnlp" /&gt;
  138. </PRE>
  139. This JNLP file points to the current JSR-231 unofficial development
  140. build. For reference, the extension JNLP file for the most recent
  141. official JSR-231 build is available at
  142. <PRE>
  143. &lt;extension name="jogl" href="http://download.java.net/media/jogl/builds/archive/jsr-231-1.1.0/webstart/jogl.jnlp" /&gt;
  144. </PRE>
  145. Note that before JOGL transitioned to the JSR-231 APIs, there were
  146. releases of the library in the <code>net.java.games.jogl</code>
  147. namespace under version numbers "1.0", "1.1", and "1.1.1". All of
  148. these releases have been superseded by JSR-231. Please update your
  149. applications.
  150. </P>
  151. <H3> Applet support </H3>
  152. <P>
  153. Lilian Chamontin, in conjunction with several other members of the
  154. JOGL community, has contributed a JOGL applet installer. This
  155. installer uses some clever tricks to allow deployment of unsigned
  156. applets which use JOGL into existing web browsers and JREs as far back
  157. as 1.4.2, which is the earliest version of Java supported by JOGL.
  158. </P>
  159. <P>
  160. The JOGLAppletInstaller is distributed inside jogl.jar as a utility
  161. class in com.sun.opengl.util. It requires that the developer host a
  162. local, signed copy of jogl.jar and all of the jogl-natives jars; the
  163. certificates must be the same on all of these jars. Note that in the
  164. release builds of JOGL all of these jars are signed by Sun
  165. Microsystems, so the developer can deploy applets without needing any
  166. certificates.
  167. </P>
  168. <P>
  169. The JOGLAppletInstaller javadoc describes the basic steps for
  170. deployment of an applet utilizing JOGL. Please refer to this
  171. documentation for more information. A live example of deploying an
  172. unsigned JOGL applet will be added to this documentation shortly once
  173. the first signed build of the JOGLAppletInstaller has been shipped.
  174. </P>
  175. <H2> GLDrawable and GLContext </H2>
  176. <P>
  177. The JSR-231 APIs specify interfaces two low-level OpenGL abstractions:
  178. drawables and contexts. An OpenGL drawable is effectively a surface
  179. upon which OpenGL rendering will be performed. In order to perform
  180. rendering, an OpenGL rendering context is needed. Contexts and
  181. drawables typically go hand-in-hand. More than one context may be
  182. created for a particular drawable. In the JSR-231 abstractions, a
  183. context is always associated with exactly one drawable.
  184. </P>
  185. <P>
  186. Most end users will not need to use these abstractions directly.
  187. However, when sharing textures, display lists and other OpenGL objects
  188. between widgets, the concrete identifier for the "namespace" for these
  189. objects is the GLContext.
  190. </P>
  191. <H2> Creating a GLAutoDrawable </H2>
  192. <P>
  193. Jogl provides two basic widgets into which OpenGL rendering can be
  194. performed. The GLCanvas is a heavyweight AWT widget which supports
  195. hardware acceleration and which is intended to be the primary widget
  196. used by applications. The GLJPanel is a fully Swing-compatible
  197. lightweight widget which supports hardware acceleration but which is
  198. not as fast as the GLCanvas because it typically reads back the frame
  199. buffer in order to draw it using Java2D. The GLJPanel is intended to
  200. provide 100% correct Swing integration in the circumstances where a
  201. GLCanvas can not be used. See <a href =
  202. "http://java.sun.com/products/jfc/tsc/articles/mixing/">this
  203. article</a> on <a href = "http://java.sun.com/products/jfc/tsc/">The
  204. Swing Connection</a> for more information about mixing lightweight and
  205. heavyweight widgets. See also the section on "Heavyweight and
  206. Lightweight Issues" below. Recent work in the Mustang release of the
  207. JDK has sped up the GLJPanel significantly when the Java2D OpenGL
  208. pipeline is enabled; see <a
  209. href="http://www.javagaming.org/forums/index.php?topic=10813.0">this
  210. forum discussion</a> for more details.
  211. </P>
  212. <P>
  213. Both the GLCanvas and GLJPanel implement a common interface called
  214. GLAutoDrawable so applications can switch between them with minimal
  215. code changes. The GLAutoDrawable interface provides
  216. <UL>
  217. <LI> access to the GL object for calling OpenGL routines
  218. <LI> a callback mechanism (GLEventListener) for performing OpenGL
  219. rendering
  220. <LI> a <CODE>display()</CODE> method for forcing OpenGL rendering to
  221. be performed synchronously
  222. <LI> AWT- and Swing-independent abstractions for getting and setting
  223. the size of the widget and adding and removing event listeners
  224. </UL>
  225. </P>
  226. <P>
  227. When creating GLCanvas and GLJPanel instances, the user may request a
  228. certain set of OpenGL parameters in the form of a GLCapabilities
  229. object, customize the format selection algorithm by specifying a
  230. GLCapabilitiesChooser, share textures and display lists with other
  231. GLDrawables, and specify the display device on which the
  232. GLAutoDrawable will be created (GLCanvas only).
  233. </P>
  234. <P>
  235. A GLCapabilities object specifies the OpenGL parameters for a
  236. newly-created widget, such as the color, alpha,, z-buffer and
  237. accumulation buffer bit depths and whether the widget is
  238. double-buffered. The default capabilities are loosely specified but
  239. provide for truecolor RGB, a reasonably large depth buffer,
  240. double-buffered, with no alpha, stencil, or accumulation buffers.
  241. </P>
  242. <P>
  243. An application can override the default pixel format selection
  244. algorithm by providing a GLCapabilitiesChooser to the GLCanvas or
  245. GLJPanel constructor. (Not all platforms support the
  246. GLCapabilitiesChooser mechanism, however; it may be ignored, in
  247. particular on Mac OS X where pixel format selection is very different
  248. than on other platforms.) The chooseCapabilities method will be called
  249. with all of the available pixel formats as an array of GLCapabilities
  250. objects, as well as the index indicating the window system's
  251. recommended choice; it should return an integer index into this
  252. array. The DefaultGLCapabilitiesChooser uses the window system's
  253. recommendation when it is available, and otherwise attempts to use a
  254. platform-independent selection algorithm.
  255. </P>
  256. <P>
  257. The GLJPanel can be made non-opaque according to Swing's rendering
  258. model, so it can act as an overlay to other Swing or Java2D drawing.
  259. In order to enable this, set up your GLCapabilities object with a
  260. non-zero alpha depth (a common value is 8 bits) and call
  261. setOpaque(false) on the GLJPanel once it has been created. Java2D
  262. rendering underneath it will then show through areas where OpenGL has
  263. produced an alpha value less than 1.0. See the JGears and JRefract
  264. demos for examples of how to use this functionality.
  265. </P>
  266. <H2> Writing a GLEventListener </H2>
  267. <P>
  268. Applications implement the GLEventListener interface to perform OpenGL
  269. drawing via callbacks. When the methods of the GLEventListener are
  270. called, the underlying OpenGL context associated with the drawable is
  271. already current. The listener fetches the GL object out of the
  272. GLAutoDrawable and begins to perform rendering.
  273. </P>
  274. <P>
  275. The <CODE>init()</CODE> method is called when a new OpenGL context is
  276. created for the given GLAutoDrawable. Any display lists or textures
  277. used during the application's normal rendering loop can be safely
  278. initialized in <CODE>init()</CODE>. It is important to note that
  279. because the underlying AWT window may be destroyed and recreated while
  280. using the same GLCanvas and GLEventListener, the GLEventListener's
  281. <CODE>init()</CODE> method may be called more than once during the
  282. lifetime of the application. The init() method should therefore be
  283. kept as short as possible and only contain the OpenGL initialization
  284. required for the <CODE>display()</CODE> method to run properly. It is
  285. the responsibility of the application to keep track of how its various
  286. OpenGL contexts share display lists, textures and other OpenGL objects
  287. so they can be either be reinitialized or so that reinitialization can
  288. be skipped when the <CODE>init()</CODE> callback is called.
  289. </P>
  290. <P>
  291. Note also that the GLEventListener should be added to the
  292. GLAutoDrawable before the GLAutoDrawable is shown or rendered to for
  293. the first time. If this is not done, it is possible that the init()
  294. method will not be called on the GLEventListener. JOGL does not
  295. maintain internal state to keep track of whether init() has been
  296. called on a particular GLEventListener since the last time an OpenGL
  297. context was created for that GLAutoDrawable.
  298. </P>
  299. <P>
  300. The <CODE>display()</CODE> method is called to perform per-frame
  301. rendering. The <CODE>reshape()</CODE> method is called when the
  302. drawable has been resized; the default implementation automatically
  303. resizes the OpenGL viewport so often it is not necessary to do any
  304. work in this method. The <CODE>displayChanged()</CODE> method is
  305. designed to allow applications to support on-the-fly screen mode
  306. switching, but support for this is not yet implemented so the body of
  307. this method should remain empty.
  308. </P>
  309. <P>
  310. It is strongly recommended that applications always refetch the GL
  311. object out of the GLAutoDrawable upon each call to the
  312. <CODE>init()</CODE>, <CODE>display()</CODE> and <CODE>reshape()</CODE>
  313. methods and pass the GL object down on the stack to any drawing
  314. routines, as opposed to storing the GL in a field and referencing it
  315. from there. The reason is that multithreading issues inherent to the
  316. AWT toolkit make it difficult to reason about which threads certain
  317. operations are occurring on, and if the GL object is stored in a field
  318. it is unfortunately too easy to accidentally make OpenGL calls from a
  319. thread that does not have a current context. This will usually cause
  320. the application to crash. For more information please see the section
  321. on multithreading.
  322. </P>
  323. <H2> Using the Composable Pipeline </H2>
  324. <P>
  325. Jogl supports the "composable pipeline" paradigm introduced by the
  326. Magician Java binding for OpenGL. The DebugGL pipeline calls
  327. <CODE>glGetError</CODE> after each OpenGL call, reporting any errors
  328. found. It can greatly speed up development time because of its
  329. fine-grained error checking as opposed to the manual error checking
  330. usually required in OpenGL programs written in C. The TraceGL prints
  331. logging information upon each OpenGL call and is helpful when an
  332. application crash makes it difficult to see where the error occurred.
  333. </P>
  334. <P>
  335. To use these pipelines, call <CODE>GLAutoDrawable.setGL</CODE> at the
  336. beginning of the <CODE>init</CODE> method in your GLEventListener. For
  337. example,
  338. <PRE>
  339. class MyListener implements GLEventListener {
  340. public void init(GLDrawable drawable) {
  341. drawable.setGL(new DebugGL(drawable.getGL()));
  342. // ...
  343. }
  344. // ...
  345. }
  346. </PRE>
  347. </P>
  348. <P>
  349. Note that the GLAutoDrawable.setGL() method simply calls setGL() on
  350. the default OpenGL context created by the GLAutoDrawable, so
  351. sophisticated applications creating their own OpenGL contexts can use
  352. the composable pipeline with these contexts by setting the GL object
  353. in the context object itself. The composable pipeline needs to be
  354. re-installed every time GLContext.makeCurrent() returns
  355. CONTEXT_CURRENT_NEW.
  356. </P>
  357. <H2> Heavyweight and Lightweight Issues </H2>
  358. <P>
  359. As mentioned above, JOGL supplies both a heavyweight (GLCanvas) and a
  360. lightweight (GLJPanel) widget to be able to provide the fastest
  361. possible performance for applications which need it as well as 100%
  362. correct Swing integration, again for applications which need it. The
  363. GLCanvas usually provides higher performance than the GLJPanel, though
  364. in recent releases the GLJPanel's speed has been improved when the
  365. Java2D/OpenGL pipeline is active as described in <a
  366. href="http://www.javagaming.org/forums/index.php?topic=10813.0">this
  367. forum discussion</a>. Nonetheless, the GLCanvas can be used in almost
  368. every kind of application except those using JInternalFrames. Please
  369. see the Swing Connection article mentioned above for details on mixing
  370. heavyweight and lightweight widgets. A couple of common pitfalls are
  371. described here.
  372. </P>
  373. <P>
  374. When using JPopupMenus or Swing tool tips in conjunction with the
  375. GLCanvas, it is necessary to disable the use of lightweight widgets
  376. for the popups. See the methods
  377. <CODE>ToolTipManager.setLightWeightPopupEnabled</CODE>,
  378. <CODE>JPopupMenu.setLightWeightPopupEnabled</CODE>, and
  379. <CODE>JPopupMenu.setDefaultLightWeightPopupEnabled</CODE>.
  380. </P>
  381. <P>
  382. There are occasionally problems with certain LayoutManagers and
  383. component configurations where if a GLCanvas is placed in the middle
  384. of a set of lightweight widgets then it may only grow and never
  385. shrink. These issues are documented somewhat in <a href =
  386. "https://jogl.dev.java.net/issues/show_bug.cgi?id=135">JOGL Issue
  387. 135</a> and most recently in the thread <a
  388. href="http://javagaming.org/forums/index.php?topic=8699.0">"Resize
  389. behaviour"</a> in the JOGL forum. The root cause is behavior of the
  390. Canvas, and in particular its ComponentPeer. The implementation of
  391. getPreferredSize() calls getMinimumSize() and getMinimumSize() turns
  392. around and calls Component.getSize(). This effectively means that the
  393. Canvas will report its preferred size as being as large as the
  394. component has ever been. For some layout managers this doesn't seem to
  395. matter, but for others like the BoxLayout it does. See the test case
  396. attached to Issue 135 for an example. Replacing the GLCanvas with an
  397. ordinary Canvas yields the same behavior.
  398. </P>
  399. <P>
  400. One suggestion was to override getPreferredSize() so that if a
  401. preferred size has not been set by the user, to default to (0,
  402. 0). This works fine for some test cases but breaks all of the other
  403. JOGL demos because they use a different LayoutManager. There appear to
  404. be a lot of interactions between heavyweight vs. lightweight widgets
  405. and layout managers. One experiment which was done was to override
  406. setSize() in GLCanvas to update the preferred size. This works down
  407. to the size specified by the user; if the window is resized any
  408. smeller the same problem appears. If reshape() (the base routine of
  409. setSize(), setBounds(), etc.) is changed to do the same thing, the
  410. demo breaks in the same way it originally did. Therefore this solution
  411. is fragile because it isn't clear which of these methods are used
  412. internally by the AWT and for what purposes.
  413. </P>
  414. <P>
  415. There are two possible solutions, both application-specific. The best
  416. and most portable appears to be to put the GLCanvas into a JPanel and
  417. set the JPanel's preferred size to (0, 0). The JPanel will cause this
  418. constraint to be enforced on its contained GLCanvas. The other
  419. workaround is to call <CODE>setPreferredSize(new Dimension(0,
  420. 0))</CODE> on a newly-created GLCanvas; this method is new in 1.5.
  421. </P>
  422. <P>
  423. Another issue that occasionally arises on Windows is flickering during
  424. live resizing of a GLCanvas. This is caused by the AWT's repainting
  425. the background of the Canvas and can not be overridden on a per-Canvas
  426. basis, for example when subclassing Canvas into GLCanvas. The
  427. repainting of the background of Canvases on Windows can be disabled by
  428. specifying the system property
  429. <CODE>-Dsun.awt.noerasebackground=true</CODE>. Whether to specify this
  430. flag depends on the application and should not be done universally,
  431. but instead on a case-by-case basis. Some more detail is in the thread
  432. <a href="http://javagaming.org/forums/index.php?topic=8770.0">"TIP:
  433. JOGL + Swing flicker"</a> in the JOGL forum.
  434. </P>
  435. <H2> Multithreading Issues </H2>
  436. <P>
  437. Jogl was designed to interoperate with the AWT, an inherently
  438. multithreaded GUI toolkit. OpenGL, in contrast, was originally
  439. designed in single-threaded C programming environments. For this
  440. reason Jogl provides a framework in which it is possible to write
  441. correct multithreaded OpenGL applications using the GLEventListener
  442. paradigm.
  443. </P>
  444. <P>
  445. If an application written using Jogl interacts in any way with the
  446. mouse or keyboard, the AWT is processing these events and the
  447. multithreaded aspects of the program must be considered.
  448. </P>
  449. <P>
  450. OpenGL applications usually behave in one of two ways: either they
  451. repaint only on demand, for example when mouse input comes in, or they
  452. repaint continually, regardless of whether user input is coming in. In
  453. the repaint-on-demand model, the application can merely call
  454. <CODE>GLAutoDrawable.display()</CODE> manually at the end of the mouse
  455. or keyboard listener to cause repainting to be done. Alternatively if
  456. the application knows the concrete type of the GLDrawable it can call
  457. repaint() to have the painting scheduled for a later time.
  458. </P>
  459. <P>
  460. In the continuous repaint model, the application typically has a main
  461. loop which is calling <CODE>GLAutoDrawable.display()</CODE>
  462. repeatedly, or is using the Animator class, which does this
  463. internally. In both of these cases the OpenGL rendering will be done
  464. on this thread rather than the internal AWT event queue thread which
  465. dispatches mouse and keyboard events.
  466. </P>
  467. <P>
  468. Both of these models (repaint-on-demand and repaint continually) still
  469. require the user to think about which thread keyboard and mouse events
  470. are coming in on, and which thread is performing the OpenGL rendering.
  471. OpenGL rendering <B>may not</B> occur directly inside the mouse or
  472. keyboard handlers, because the OpenGL context for the drawable is not
  473. current at this point (hence the warning about storing a GL object in
  474. a field, where it can be fetched and accidentally used by another
  475. thread). However, a mouse or keyboard listener may invoke
  476. <CODE>GLAutoDrawable.display()</CODE>.
  477. </P>
  478. <P>
  479. It is generally recommended that applications perform as little work
  480. as possible inside their mouse and keyboard handlers to keep the GUI
  481. responsive. However, since OpenGL commands can not be run from
  482. directly within the mouse or keyboard event listener, the best
  483. practice is to store off state when the listener is entered and
  484. retrieve this state during the next call to
  485. <CODE>GLEventListener.display()</CODE>.
  486. </P>
  487. <P>
  488. Furthermore, it is recommended that if there are long computational
  489. sequences in the GLEventListener's <CODE>display</CODE> method which
  490. reference variables which may be being simultaneously modified by the
  491. AWT thread (mouse and keyboard listeners) that copies of these
  492. variables be made upon entry to <CODE>display</CODE> and these copies
  493. be referenced throughout display() and the methods it calls. This will
  494. prevent the values from changing while the OpenGL rendering is being
  495. performed. Errors of this kind show up in many ways, including certain
  496. kinds of flickering of the rendered image as certain pieces of objects
  497. are rendered in one place and other pieces are rendered elsewhere in
  498. the scene. Restructuring the display() method as described has solved
  499. all instances of this kind of error that have been seen with Jogl to
  500. date.
  501. </P>
  502. <P>
  503. Prior to Jogl 1.1 b10, the Jogl library attempted to give applications
  504. strict control over which thread or threads performed OpenGL
  505. rendering. The <CODE>setRenderingThread()</CODE>,
  506. <CODE>setNoAutoRedrawMode()</CODE> and <CODE>display()</CODE> APIs
  507. were originally designed to allow the application to create its own
  508. animation thread and avoid OpenGL context switching on platforms that
  509. supported it. Unfortunately, serious stability issues caused by
  510. multithreading bugs in either vendors' OpenGL drivers or in the Java
  511. platform implementation have arisen on three of Jogl's major supported
  512. platforms: Windows, Linux and Mac OS X. In order to address these
  513. bugs, the threading model in Jogl 1.1 b10 and later has changed.
  514. </P>
  515. <P>
  516. All GLEventListener callbacks and other internal OpenGL context
  517. management are now performed on one thread. (In the current
  518. implementation, this thread is the AWT event queue thread, which is a
  519. thread internal to the implementation of the AWT and which is always
  520. present when the AWT is being used. Future versions of Jogl may change
  521. the thread on which the OpenGL work is performed.) When the
  522. <CODE>GLAutoDrawable.display()</CODE> method is called from user code,
  523. it now performs the work synchronously on the AWT event queue thread,
  524. even if the calling thread is a different thread. The
  525. <CODE>setRenderingThread()</CODE> optimization is now a no-op. The
  526. <CODE>setNoAutoRedrawMode()</CODE> API still works as previously
  527. advertised, though now that all work is done on the AWT event queue
  528. thread it no longer needs to be used in most cases. (It was previously
  529. useful for working around certain kinds of OpenGL driver bugs.)
  530. </P>
  531. <P>
  532. Most applications will not see a change in behavior from this change
  533. in the Jogl implementation. Applications which use thread-local
  534. storage or complex multithreading and synchronization may see a change
  535. in their control flow requiring code changes. While it is strongly
  536. recommended to change such applications to work under the new
  537. threading model, the old threading model can be used by specifying the
  538. system property <CODE>-Djogl.1thread=auto</CODE> or
  539. <CODE>-Djogl.1thread=false</CODE>. The "auto" setting is equivalent to
  540. the behavior in 1.1 b09 and before, where on ATI cards the
  541. single-threaded mode would be used. The "false' setting is equivalent
  542. to disabling the single-threaded mode. "true" is now the default
  543. setting.
  544. </P>
  545. <P>
  546. In the JSR-231 APIs the single-threaded behavior continues to be the
  547. default and the <CODE>setRenderingThread()</CODE> and
  548. <CODE>setNoAutoRedrawMode()</CODE> APIs have been removed. The public
  549. <CODE>Threading</CODE> class still provides some control over the
  550. internal use of threads in the library as well as external access to
  551. these mechanisms.
  552. </P>
  553. <H2> Pbuffers </H2>
  554. <P>
  555. Jogl exposes hardware-accelerated offscreen rendering (pbuffers) with
  556. a minimal and platform-agnostic API. Several recent demos have been
  557. successfully ported from C/C++ to Java using Jogl's pbuffer APIs.
  558. However, the pbuffer support in Jogl remains one of the more
  559. experimental aspects of the package and the APIs may need to change in
  560. the future.
  561. </P>
  562. <P>
  563. To create a pbuffer, call
  564. <CODE>GLDrawableFactory.createGLPbuffer()</CODE>. It is wise to call
  565. <CODE>GLDrawableFactory.canCreateGLPbuffer()</CODE> first to ensure
  566. the graphics card has pbuffer support first. The pbuffer is created
  567. immediately and is available for rendering as soon as
  568. <CODE>createGLPbuffer</CODE> returns.
  569. </P>
  570. <P>
  571. A pbuffer is used in conjunction with the GLEventListener mechanism by
  572. calling its display() method. Rendering, as always, occurs while the
  573. pbuffer's OpenGL context is current. There are render-to-texture
  574. options that can be specified in the GLCapabilities for the pbuffer
  575. which can make it easier to operate upon the resulting pixels. These
  576. APIs are however highly experimental and not yet implemented on all
  577. platforms.
  578. </P>
  579. <H2> GLU </H2>
  580. <P>
  581. Jogl contains support for the GLU (OpenGL Utility Library) version
  582. 1.3. Jogl originally supported GLU by wrapping the C version of the
  583. APIs, but over time, and thanks to the contributions of several
  584. individuals, it now uses a pure-Java version of SGI's GLU library. The
  585. pure Java port is enabled by default, and addresses stability issues
  586. on certain Linux distributions as well as the lack of native GLU 1.3
  587. support on the Windows platform. In case of problems with the Java
  588. port, the C version of the GLU library may be used by specifying the
  589. system property <CODE>-Djogl.glu.nojava</CODE> on the command
  590. line. All of the same functionality is exposed with both the Java and
  591. C versions of the GLU library; currently NURBS support is the only
  592. missing feature on both sides. If you run into problems with the Java
  593. port of the GLU library please file a bug using the Issue Tracker on
  594. the Jogl home page.
  595. </P>
  596. <P>
  597. To use the GLU, simply instantiate a GLU object via <CODE>new
  598. GLU()</CODE> at the beginning of your program. The methods on the GLU
  599. object may be called at any point when an OpenGL context is current.
  600. Because the GLU implementation is not thread-safe, one GLU object
  601. should be created for each GLEventListener or other entity performing
  602. OpenGL rendering in a given thread.
  603. </P>
  604. <H2> More Resources </H2>
  605. <P>
  606. The <A HREF="http://javagaming.org/forums/index.php?board=25.0">JOGL
  607. forum</A> on <A HREF="http://javagaming.org/">javagaming.org</A> is
  608. the best place to ask questions about the library. Many users, as well
  609. as the Jogl developers, read this forum frequently, and the archived
  610. threads contain a lot of useful information (which still needs to be
  611. distilled into documentation).
  612. </P>
  613. <P>
  614. The <A HREF="http://jogl-demos.dev.java.net/">JOGL demos</A> provide
  615. several examples of usage of the library.
  616. </P>
  617. <P>
  618. Pepijn Van Eeckhoudt, Kevin Duling and Abdul Bezrati have done <A
  619. HREF="http://pepijn.fab4.be/software/nehe-java-ports/">JOGL ports of
  620. many of the the NeHe demos</A>. These are small examples of various
  621. pieces of OpenGL functionality. See also the <A
  622. HREF="http://nehe.gamedev.net/">NeHe web site</A>.
  623. </P>
  624. <P>
  625. Pepijn also did a <A
  626. HREF="http://www.glexcess.com/files/glexcess.jar">JOGL port</a> of
  627. Paolo Martella's <A HREF="http://www.glexcess.com/">GLExcess</A>
  628. demo. To see the news update about this port, go to the main GLExcess
  629. site and scroll down.
  630. </P>
  631. <P>
  632. Gregory Pierce's <A
  633. HREF="http://javagaming.org/forums/index.php?topic=1474.0">introduction
  634. to JOGL</a> is a useful tutorial on starting to use the JOGL library.
  635. </P>
  636. <P>
  637. For release information about the JOGL library, please see the <A
  638. HREF="http://javagaming.org/forums/index.php?topic=1596.0">JOGL Release
  639. Information</A> thread on the JOGL forum on javagaming.org.
  640. </P>
  641. <P>
  642. Please post on the JOGL forum if you have a resource you'd like to add
  643. to this documentation.
  644. </P>
  645. <H2> Platform Notes </H2>
  646. <H3> All Platforms </H3>
  647. <P>
  648. The following issues, among others, are outstanding on all platforms:
  649. </P>
  650. <UL>
  651. <LI> A few remaining stability issues, mostly on older graphics cards.
  652. <LI> JOGL now supports experimental integration and interoperability
  653. with the Java2D/OpenGL pipeline in Java SE 6 (Mustang), enabling a
  654. much faster GLJPanel as well as other features. Please see <a
  655. href="http://www.javagaming.org/forums/index.php?topic=10813.0">this
  656. forum discussion</a> for more details.
  657. </UL>
  658. <H3> Windows </H3>
  659. <P>
  660. For correct operation, it is necessary to specify the system property
  661. <CODE>-Dsun.java2d.noddraw=true</CODE> when running JOGL applications
  662. on Windows; this system property disables the use of DirectDraw by
  663. Java2D. There are driver-level incompatibilities between DirectDraw
  664. and OpenGL which manifest themselves as application crashes, poor
  665. performance, bad flickering, and other artifacts. This poor behavior
  666. may exhibit itself when OpenGL and DirectDraw are simply used in the
  667. same application, not even just in the same window, so disabling
  668. Java2D's DirectDraw pipeline and forcing it to use its GDI pipeline is
  669. the only way to work around these issues. Java Web Start applications
  670. may set this system property by adding the following line to the
  671. <CODE>&lt;resources&gt;</CODE> section of the JNLP file: <PRE>
  672. &lt;property name="sun.java2d.noddraw" value="true"/&gt; </PRE>
  673. </P>
  674. <P>
  675. There is a serious memory leak in ATI's OpenGL drivers which is
  676. exhibited on Windows XP on Mobility Radeon 9700 hardware. It's
  677. possible it will be present on other hardware as well though it was
  678. not reproducible at the time of this writing on desktop Radeon
  679. hardware or older ATI mobile chips. The bug is documented in <A
  680. HREF="https://jogl.dev.java.net/issues/show_bug.cgi?id=166">JOGL Issue
  681. 166</A> and a bug has been filed with ATI. You can confirm the
  682. presence of the bug either with the test case in that bug report or by
  683. simply running the Gears demo; if the process size grows over time in
  684. the Task Manager, the memory leak is present on your hardware. For the
  685. time being, you can work around this memory leak by specifying the
  686. system property <CODE>-Djogl.GLContext.nofree</CODE> on the command
  687. line when launching your JOGL applications. There is no good
  688. general-purpose workaround for this bug which behaves well on all
  689. hardware.
  690. </P>
  691. <H3> Linux </H3>
  692. <P>
  693. The Sun JDK "compatibility" RPMs (java-1.5.0-sun-compat,
  694. java-1.6.0-sun-compat) provided by jpackage.org are incompatible with
  695. JOGL. These RPMs symlink an internal JDK directory to /usr/lib, which
  696. overrides how both NVidia and ATI currently provide their drivers to
  697. some Linux distributions, which is through an override in
  698. /etc/ld.so.conf (actually, in /etc/ld.so.conf.d). The implicit
  699. presence of /usr/lib on LD_LIBRARY_PATH forces the /usr/lib/libGL.so.1
  700. version of OpenGL to be used, which is typically Mesa and which will
  701. provide only software rendering.
  702. </P>
  703. <P>
  704. Unfortunately the JPackage maintainers have so far been unreceptive to
  705. changing their installation mechanism; see <a
  706. href="https://www.zarb.org/pipermail/jpackage-discuss/2007-January/010871.html">this
  707. mailing list posting</a>. Until this is resolved, we strongly
  708. discourage the use of the JPackage installers for the Sun JDK.
  709. Instead, download the JRE or JDK installers directly from Sun's
  710. website.
  711. </P>
  712. <P>
  713. Archived forum postings illustrating this problem are <a
  714. href="http://www.javagaming.org/forums/index.php?topic=15610.0">here</a>
  715. and <a
  716. href="http://www.javagaming.org/forums/index.php?topic=16105.0">here</a>.
  717. </P>
  718. <H3> Solaris, Linux (X11 platforms) </H3>
  719. <P>
  720. Support has been added to the JOGL library for allowing multiple
  721. threads to each have an OpenGL context current simultaneously, for
  722. example to implement multi-head CAVE-like environments. Normally a
  723. global AWT lock is held between calls to GLContext.makeCurrent() /
  724. release() for on-screen heavyweight contexts (for example, those
  725. associated with a Canvas or GLCanvas). We have found this to be
  726. necessary for stability purposes on all supported X11 platforms, even
  727. with relatively robust drivers such as those from NVidia.
  728. </P>
  729. <P>
  730. To enable multiple GLContexts to be made current simultaneously on X11
  731. platforms, specify the command line argument
  732. <CODE>-Djogl.GLContext.optimize</CODE> when starting the JVM. Note
  733. that this may incur robustness problems, in particular when resizing
  734. or moving windows. We have also found that ATI's proprietary drivers
  735. do not work at all with this flag, apparently because they cause GLX
  736. tokens to be sent to the X server for various GL calls even for direct
  737. contexts. For this reason if the GLX vendor is ATI then this flag
  738. currently has no effect.
  739. </P>
  740. <H3> Mac OS X </H3>
  741. <P>
  742. There are some problems with visual artifacts and stability problems
  743. with some of the Jogl demos on Mac OS X. It appears that at least some
  744. of these problems are due to bugs in Apple's OpenGL support. Bugs have
  745. been filed about these problems and it is hoped they will be addressed
  746. in the near future.
  747. </P>
  748. <P>
  749. The Mac OS X port of Jogl, in particular the GL interface and its
  750. implementation, can be used either with the provided GLCanvas widget
  751. or with the Cocoa NSOpenGLView. In order to use it with Cocoa the
  752. following steps should be taken:
  753. <UL>
  754. <LI> Create an "external" OpenGL context using the
  755. <CODE>GLDrawableFactory.createExternalGLContext()</CODE> API. The
  756. context object must be created while a real underlying OpenGL context
  757. is current.
  758. <LI> Fetch the GL instance out of the context using getGL() as usual.
  759. Only use the GL instance when the OpenGL context from the NSOpenGLView
  760. is current.
  761. </UL>
  762. <B>NOTE:</B> the Cocoa interoperability has not been retested
  763. recently, though similar interoperability has been tested on other
  764. platforms. Please report any problems found with using Jogl with an
  765. NSOpenGLView.
  766. </P>
  767. <P>
  768. The following issues remain with the Mac OS X port:
  769. <UL>
  770. <LI> Due to the mechanism by which the Cocoa graphics system selects
  771. OpenGL pixel formats, the GLCapabilitiesChooser mechanism can not be
  772. implemented on Mac OS X as on other platforms. Currently the
  773. underlying Cocoa pixel format selection is used on an
  774. NSOpenGLPixelFormat derived from the settings in the GLCapabilities,
  775. and the GLCapabilitiesChooser is ignored.
  776. </UL>
  777. </P>
  778. <H2> Version History </H2>
  779. <P>
  780. JOGL's version history can be found online in the <a
  781. href="http://javagaming.org/forums/index.php?topic=1596.0">"JOGL Release
  782. Information"</a> thread in the JOGL forum. Comments about the 1.1
  783. release train are in the thread <a
  784. href="http://javagaming.org/forums/index.php?topic=4217.0">"JOGL 1.1
  785. released"</a>.
  786. </BODY>
  787. </HTML>