/** * @(#) GLContext.java */ package gl4java; import gl4java.jau.awt.WinHandleAccess; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.io.*; import java.lang.reflect.*; import java.security.*; import java.util.*; /** * The base manager class for the OpenGL language mapping for Java ! * *
* If you are interessting in further Documentation and/or * the history of GL4Java follow the following link. * *
The GL4Java Documentation **
* * All native libraries and GLFunc* and GLUFunc* implementations * can/should be loaded right here ! * *
doLoadNativeLibraries createGLFunc createGLUFunc* *To check the library versions, you can start the main function with
java gl4java.GLContext
*
* * This class creates a GLContext in the constructor which fits to the native Window * of the given Component ! * *
glc2glj - A simple c to java converter for C- and GL-types **
* * The native libraries are plattform depended. * *
The default native library's are:* * Make sure that this library is installed in your library path. * The library path is for Unices one path of the environment * variable ´LD_LIBRARY_PATH´ and for Windows 32 * ´c:/winnt/system32´ (WinNT) or 'c:/windows/system' (Windows 95) . * Or it should be installed in the 'jre/bin' or * 'Netscape/communicator/Program/java/bin' path ! * *GL4JavaJauGljJNI (jdk ==1.1), GL4JavaJauGljJNI12 (jdk ==1.2), GL4JavaJauGljJNI13 (jdk ==1.3) GL4JavaJauGljJNI14 (jdk >=1.4)
To check wich version and vendors are currently used, just call while a GLContext is created :
Or just run at the command line:
java gl4java.GLContext -info
*
* * To have a convinient usage, e.g. automatic resizing, GLContext * and painting/gl-rendering, some classes in the package gl4java.awt * may help you. * *
E.g.: gl4java.awt.GLCanvas* *init to create the java-stuff and GL-inits display to render one frame, called by paint reshape to reshape (window resize)
Or look for animation at gl4java.awt.GLAnimCanvas ! *
* * IF you remove/release a The component which is binded to this GLContext, * you have to release the GLContext also - while calling: * *
gljDestroy ** * before releasing/dispose it´s Window ! * *
* * We also define our own OpenGL extension. This extension functions * start with the prefix glj like you can see here: * *
THIS FUNCTIONS ARE FOR USERS PURPOSES: ====================================== doLoadNativeLibraries createGLFunc createGLUFunc gljIsInit gljMakeCurrent gljSwap gljFree gljDestroy gljGetNativeLibVersion gljGetClassVersion gljShowVersions gljCheckGL gljCheckGLTypes gljResize **
* * You can see our example demo sources: *
olympicCvs.java as java source glLogoCvs.java as java source glDemosCvs.java as java source **
* * If you are interessting in further Documentation, downloading the latest * version, and/or the history of GL4Java click on the following link. * *
The GL4Java Homepage **
*
* @version 2.00, 21. April 1999
* @author Sven Goethel
*
*/
public class GLContext extends Object
implements Runnable
{
protected boolean isInitialized = false;
/**
* Flag is the native library is loaded.
* The native library is loaded at startup.
* If we failed loading the lib.,
* gljMakeCurrent and gljIsInit will return false.
*
* @see gl4java.GLContext#gljIsInit
*/
protected static boolean libsLoaded = false;
/**
* The default extra native library for Windows 95/NT && MS-JVM
*
*/
public final static String defGljMSWinLib = "GL4JavaGljMSJDirect" ;
/**
* The default GLContext/GLFunc/GLUFunc native library for all
*
*/
public final static String defGljLib = "GL4JavaJauGljJNI" ;
/**
* The default GLFunc implementation
*
*/
public static final String defGLFuncClass = "GLFuncJauJNI" ;
public static final String defGLFunc14Class = "GLFunc14JauJNI" ;
/**
* The default GLUFunc implementation
*
*/
public static final String defGLUFuncClass = "GLUFuncJauJNI" ;
public static final String defGLUFunc14Class = "GLUFunc14JauJNI" ;
/**
* The default native GL library ... for X11
*/
public final static String defNativeGLLibX11 = "libGL.so";
/**
* The default native GL library ... for MacOS9
*/
public final static String defNativeGLLibMacOS9 = "\\pOpenGLLibrary";
/**
* The default native GL library ... for MacOSX
*/
public final static String defNativeGLLibMacOSX = "libGL.so";
/**
* The default native GL library ... for Win32
*/
public final static String defNativeGLLibWin32 = "OPENGL32.DLL";
/**
* The default native GLU library ... for X11
*/
public final static String defNativeGLULibX11 = "libGLU.so";
/**
* The default native GLU library ... for MacOS9
*/
public final static String defNativeGLULibMacOS9 = "\\pOpenGLULibrary";
/**
* The default native GLU library ... for MacOSX
*/
public final static String defNativeGLULibMacOSX = "libGLU.so";
/**
* The default native GLU library ... for Win32
*/
public final static String defNativeGLULibWin32 = "GLU32.DLL";
/**
* the version of this java-class
*
*
*
* The user must call loadNativeLibrary !
* E.g. he can add the default loader like this:
*
*
* The user must call doLoadNativeLibrary !
* E.g. he can add the default loader like this:
*
*
* The user can use this method to check if he can start rendering
* and to be sure his initialisation (init) is done !
*
*
*
* If you use gl4java.awt.GLCanvas, you should use the cvsIsInit
* method !
*
*
* @return boolean
* @see gl4java.GLContext#gljInit
* @see gl4java.awt.GLCanvas#cvsIsInit
*/
public final boolean gljIsInit()
{
return isInitialized;
}
/**
* Resizes the gl-viewport
*
* Should be called, if the component is resized.
* The user should take advantage of this functionality.
*
* Be sure to resize not within the event-method of the
* ComponentHandler, you better resize while normal painting.
* This can be done while using a boolean flag ;-)
*
* Look at GLComponent !
*/
public final void gljResize(int width, int height)
{
if ( ! isInitialized || !glEnabled ) return;
if(offScreenRenderer)
{
/**
* nothing to do here
*
* we just have to recreate the whole bitmap and context
*/
return;
}
if ( useMSJDirect && !offScreenRenderer )
{
try
{
Point p = _comp.getLocationOnScreen();
gl4java.system.GljMSJDirect.moveOGLWindowNative(
windowHandle,p.x,p.y,width,height);
}
catch (Exception e)
{ }
}
else
gljResizeNative( createOwnWindow,
displayHandle, windowHandle,
width,height);
}
private final native void gljResizeNative( boolean isOwnWindow,
long disp, long thisWin,
int width, int height );
/**
* native C function of GLJ Library,
* which query if it uses the JDK 1.3 JAWT interface
* to fetch the native window handle
*/
protected static final native boolean useJAWT();
private static String makeLibName(String name)
{
int type = getNativeOSType();
if (type == OsWindoof) {
return name + ".dll";
} else if ( type == OsX11 || type == OsMacX ) {
return "lib" + name + ".so";
} else {
throw new RuntimeException("Please port makeLibName to your platform");
}
}
private static final native boolean loadJAWT0(String name);
private static final String findInPath(String libpath, String libname) {
if (libpath == null) return null;
String pathsep = System.getProperty("path.separator");
String filesep = System.getProperty("file.separator");
StringTokenizer tok = new StringTokenizer(libpath, pathsep);
while (tok.hasMoreTokens()) {
String curEntry = tok.nextToken();
String path = curEntry + filesep + libname;
if (new File(path).exists()) {
return path;
}
}
return null;
}
/**
* Routine which causes jawt.dll/libjawt.so to be loaded if that is
* how the window handles are being obtained.
*/
protected static final boolean loadJAWT() {
// Locate jawt.dll/libjawt.so by looking through
// java.library.path and sun.boot.library.path
String jawtName0 = makeLibName("jawt");
String jawtName1 = makeLibName("awt");
boolean found = false;
String libpath = findInPath(System.getProperty("java.library.path"), jawtName0);
if (libpath == null) {
libpath = findInPath(System.getProperty("sun.boot.library.path"), jawtName0);
} else if (gljClassDebug) {
System.err.println("Located: " + jawtName0 + ": "+libpath+", within: java.library.path: "+
System.getProperty("java.library.path"));
found = true;
}
if (libpath == null) {
/**
* E.g. IBM's JVM
*/
if (gljClassDebug)
{
System.err.println("Unable to locate " + jawtName0);
System.err.println(" java.library.path = " + System.getProperty("java.library.path"));
System.err.println(" sun.boot.library.path = " + System.getProperty("sun.boot.library.path"));
System.err.println(" trying " + jawtName1);
}
libpath = findInPath(System.getProperty("java.library.path"), jawtName1);
} else if (gljClassDebug && !found) {
System.err.println("Located: " + jawtName0 + ": "+libpath+", within: sun.boot.library.path: "+
System.getProperty("sun.boot.library.path"));
found = true;
}
if (libpath == null) {
libpath = findInPath(System.getProperty("java.library.path"), jawtName1);
}
if (libpath == null) {
libpath = findInPath(System.getProperty("sun.boot.library.path"), jawtName1);
} else if (gljClassDebug && !found) {
System.err.println("Located: " + jawtName1 + ": "+libpath+", within: java.library.path: "+
System.getProperty("java.library.path"));
found = true;
}
if (libpath == null) {
System.err.println("Unable to locate neither " + jawtName0 +
" nor " + jawtName1 );
System.err.println(" java.library.path = " + System.getProperty("java.library.path"));
System.err.println(" sun.boot.library.path = " + System.getProperty("sun.boot.library.path"));
return false;
} else if (gljClassDebug && !found) {
System.err.println("Located: " + jawtName0 + ": "+libpath+", within: sun.boot.library.path: "+
System.getProperty("sun.boot.library.path"));
found = true;
}
return loadJAWT0(libpath);
}
/**
* native C function of GLJ Library,
* which query if the JAWT Surface has changed !
*
* Use this after gljMakeCurrent -> jawt_lock !
*
* If true, we need a new GLContext !
*/
protected final native boolean hasJAWTSurfaceChanged(long thisWin);
/**
* native C function to open the OpenGLwidget
*/
protected final native boolean openOpenGLNative(Component canvas);
/**
* native C function to check the gl types.
* At this time, all GL* types will be checked if they fit's
* in the used java JNI types.
*
* this checks is allready performed, while the GL context is
* fetched with gljInit !
*
* BUT we used this function, to perform a check without an
* X11-connection to our AIX host ;-))
* So a call to this function is not needed !
*/
public final static boolean gljCheckGLTypes()
{
return gljCheckGLTypesNative();
}
private final static native boolean gljCheckGLTypesNative();
public final boolean gljCheckGL()
{
if ( ! isInitialized || !glEnabled ) return false;
int ec = gl.glGetError();
if(ec!=GLFunc.GL_NO_ERROR)
{
String errStr = glu.gluErrorString(ec);
try
{
throw new Exception();
}
catch (Exception e)
{
System.out.println("GL ERROR : "+errStr);
System.out.println("GL ERROR : "+ec+" == 0x"+Integer.toHexString(ec));
e.printStackTrace();
System.out.println();
System.out.flush();
}
return false;
}
return true;
}
private Thread ctxThread = null;
private Thread nextThread = null;
/**
*
* gljIsCurrent checks
* if the current Thread holds the GL context of this
* GLContext instance !
*
* @return boolean
*
* @see gl4java.GLContext#gljIsRequested
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#getNativeGLContext
* @see gl4java.GLContext#gljGetCurrentContext
*/
public synchronized final boolean gljIsCurrent()
{
if ( ! isInitialized || !glEnabled )
return false;
Thread thisThread = Thread.currentThread();
if (ctxThread==thisThread && glContext==gljGetCurrentContext())
return true;
return false;
}
/**
*
* gljIsRequested checks
* if the this GLContext instance's native context
* is requested by another thread !
*
* @return boolean
*
* @see gl4java.GLContext#gljIsCurrent
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#getNativeGLContext
* @see gl4java.GLContext#gljGetCurrentContext
*/
public synchronized final boolean gljIsRequested()
{
if ( ! isInitialized || !glEnabled )
return false;
Thread thisThread = Thread.currentThread();
if (nextThread!=null && nextThread!=thisThread)
return true;
return false;
}
/**
*
* gljMakeCurrent checks whether GL4Java is initializes
* AND makes the GL-Context current for this thread.
*
*
*
* This functions now optimizes the context-switch !
*
* The context is changed, only if :
*
*
* You MUST encapsulate your OpenGL call's within:
*
*
* This functions now optimizes the context-switch !
*
* The context is changed, only if :
*
*
* @return boolean
*
* @see gl4java.GLContext#gljFree
* @see gl4java.GLContext#gljIsCurrent
* @see gl4java.GLContext#gljIsRequested
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#gljSwap
* @see gl4java.awt.GLCanvas#display
* @see gl4java.awt.GLCanvas#sDisplay
*/
public final boolean gljFree()
{
return gljFree(false);
}
/**
*
* gljFree free´s the GL Context
*
* This MUST be called at last in your display function !
*
*
*
* This functions now optimizes the context-switch !
*
* The context is changed, only if one of the following is true:
*
*
* @return boolean
*
* @see gl4java.GLContext#gljIsCurrent
* @see gl4java.GLContext#gljIsRequested
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#gljSwap
* @see gl4java.GLRunnable#run
*/
public synchronized final boolean gljFree(boolean force)
{
if ( ! isInitialized ) return false;
boolean result = true;
Thread thisThread = Thread.currentThread();
if ( ctxThread!=thisThread )
{
if(gljThreadDebug)
{
System.out.println("gljFree: denied, not holding context ! ");
/** JAU NVidia 2314 Bug workaround
* System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext+", currentContext="+gljGetCurrentContext());
*/
System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext);
Exception e = new Exception();
e.printStackTrace();
}
return true;
}
if( (_comp instanceof GLRunnable) == false ||
((GLRunnable)_comp).ownsThread(thisThread) == false
)
force=true;
/**
* only free the context, if another thread does
* request this context ...
* or the force-flag is true
*/
if ( force==true || ( nextThread!=null && nextThread!=thisThread ) )
{
result = gljFreeNative ( _comp, displayHandle,
windowHandle,
glContext);
ctxThread = null ;
if(gljThreadDebug)
{
System.out.println("gljFree: gljFreeNative result: "+result);
/** JAU NVidia 2314 Bug workaround
* System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext+", currentContext="+gljGetCurrentContext());
*/
System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext);
}
} else {
result = unlockJAWT(windowHandle, false);
if(gljThreadDebug)
{
System.out.println("gljFree: no CTX change, no requests, unlockJAWT: "+result);
/** JAU NVidia 2314 Bug workaround
* System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext+", currentContext="+gljGetCurrentContext());
*/
System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext);
}
}
notifyAll();
return result;
}
/**
* if using JAWT, this function handles the JAWT unlock also
*/
private final static native boolean gljFreeNative( Component canvas,
long disp,
long thisWin,
long glContext );
private final static native boolean unlockJAWT(long thisWin, boolean verbose);
/**
* swap method are for double buffering
*/
public final boolean gljSwap()
{
if ( ! isInitialized || !glEnabled ) return false;
return gljSwapNative( displayHandle,
windowHandle,
glContext,
glCaps.getDoubleBuffered());
}
private final static native boolean gljSwapNative( long disp,
long thisWin,
long glContext,
boolean doubleBuffer );
/**
* This function enables, disables the GL-Context !
* If false is given, the openGL renderer/context is
* disabled and disconected (gljFree is called, if initialized) !
*
* If disabled, all GL Functions are disabled but the
* Destroy & Free are not !
*
* @return boolean
*
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#gljDestroy
* @see gl4java.GLContext#gljFree
*/
public void setEnabled(boolean b)
{
glEnabled = b;
if ( b==false && isInitialized )
gljFree();
}
/**
* This function queries, if the GL-Context is enabled !
*
* @return boolean
*
* @see gl4java.GLContext#setEnabled
* @see gl4java.GLContext#gljMakeCurrent
*/
public boolean isEnabled()
{
return glEnabled;
}
/**
* This functions fetches/dispatches the GL/GLU functions,
* which must be allready loaded via the doLoadNativeLibraries
* function !
*
* @see gl4java.GLContext#doLoadNativeLibraries
*/
public final static boolean gljFetchGLFunctions(String gllibname,
String glulibname,
boolean force)
{
return gljFetchGLFunctions0(gllibname, glulibname, force, false, gljNativeDebug);
}
/**
* for example Windows it appears
* that extensions only become visible once an OpenGL context is
* made current for the first time (at least with NVidia's
* drivers). To avoid making drastic changes to the code
* structure, we reload all of the OpenGL functions the first time
* a context is made current. (This could be made more efficient
* by only loading extensions' routines at this time.)
*
* @see gl4java.GLContext#gljFetchGLFunctions
*/
private volatile static boolean firstContextMakeCurrent = true;
private final static native boolean gljFetchGLFunctions0 (String gllibname,
String glulibname,
boolean force,
boolean reload, boolean verbose);
/**
* This functions checks the existence of
* the GL functions !
*/
public final static boolean gljTestGLProc ( String name, boolean verbose ) {
// Special case any routines which are exposed in a different fashion
if (name.equals("glAllocateMemoryNV")) {
int os = getNativeOSType();
if (os == OsWindoof) {
name = "wglAllocateMemoryNV";
} else if (os == OsX11) {
name = "glXAllocateMemoryNV";
} // else will fail anyway; fall through
}
return gljTestGLProc0(name, verbose);
}
private static final native boolean gljTestGLProc0(String name, boolean verbose);
/**
* This functions reads the pixel from the GL frame
* and puts it into the pixelDest array,
* while converting them correctly to the AWT pixel format,
* using GL_BGRA and BufferedImage.TYPE_INT_ARGB !
*/
public final static native void gljReadPixelGL2AWT (
int pack_rowlen,
int pack_x, int pack_y,
int x, int y, int width, int height,
int format, int type, int bufferName,
int[] pixelDest);
/**
* This functions reads the pixel from the GL frame
* and puts it into the pixelDest array,
* while converting them correctly to the AWT pixel format,
* using GL_RGB and BufferedImage.TYPE_USHORT_565_RGB !
*/
public final static native void gljReadPixelGL2AWT (
int pack_rowlen,
int pack_x, int pack_y,
int x, int y, int width, int height,
int format, int type, int bufferName,
short[] pixelDest);
/**
* This functions reads the pixel from the GL frame
* and puts it into the pixelDest array,
* while using hardware correct AWT and GL pixel format,
* using GL_BGR and BufferedImage.TYPE_3BYTE_BGR !
*/
public final static native void gljReadPixelGL2AWT (
int pack_rowlen,
int pack_x, int pack_y,
int x, int y, int width, int height,
int format, int type, int bufferName,
byte[] pixelGLDest);
/**
* native C function to achieve the native lib vendor !
*
* now it is possible to check the native-lib at runtime !
*/
public final static String gljGetNativeLibVendor()
{
return gljGetNativeLibVendorNative();
}
private final static native String gljGetNativeLibVendorNative();
/**
* native C function to achieve the native lib version !
*
* now it is possible to check the native-lib at runtime !
*/
public final static String gljGetNativeLibVersion()
{
return gljGetNativeLibVersionNative();
}
private final static native String gljGetNativeLibVersionNative();
/**
* function to achieve the java-class version !
*
* now it is possible to check the java-class at runtime !
*/
public final static String gljGetClassVersion()
{ return version; }
/**
* function to achieve the java-class vendor !
*
* now it is possible to check the java-class at runtime !
*/
public final static String gljGetClassVendor()
{ return "Jausoft - Sven Goethel Software Development"; }
/**
* function to achieve complete version info
*
* Be sure that the native library must be loaded !
*/
public final String gljGetVersions()
{
return gljGetVersions(false);
}
public final String gljGetVersions(boolean verbose)
{
if(libsLoaded==false || gl==null || glu==null || !gljIsInit())
return null;
String jvmstr = "Java Virtual Machine: Version "+ getJVMVersion() +
" ("+getJVMVersionMajor()+"."+getJVMVersionMinor()+")"+
", Vendor: "+ getJVMVendor() + "\n" ;
gl4java.utils.glf.GLF glf = new gl4java.utils.glf.GLF();
String info1= "GL4Java - LGPL-Version" + "\n" +
"-------------------------------------------------\n" +
"-------------------------------------------------\n" +
"Java-Class : GL4Java.GLContext \n" +
" : Version: "+gljGetClassVersion() + "\n" +
" Vendor : "+gljGetClassVendor() + "\n" +
"Native-Library : GL4Java.GLContext \n" +
" Version: "+gljGetNativeLibVersion()+"\n" +
" Vendor : "+gljGetNativeLibVendor() +"\n" +
"-------------------------------------------------\n" +
"Java-Class : GL4Java.GLFunc impl. \n" +
" : Version: "+gl.getClassVersion() + "\n" +
" Vendor : "+gl.getClassVendor() + "\n" +
"Native-Library : GL4Java.GLFunc impl. \n" +
" Version: "+gl.getNativeVersion() + "\n" +
" Vendor : "+gl.getNativeVendor() + "\n" +
"-------------------------------------------------\n" +
"Java-Class : GL4Java.GLUFunc impl. \n" +
" : Version: "+glu.getClassVersion() + "\n" +
" Vendor : "+glu.getClassVendor() + "\n" +
"Native-Library : GL4Java.GLUFunc impl. \n" +
" Version: "+glu.getNativeVersion() + "\n" +
" Vendor : "+glu.getNativeVendor() + "\n" +
"-------------------------------------------------\n" +
"Java-Class : GL4Java.utils.glf.GLFFuncJNI \n" +
" : Version: "+glf.getClassVersion() + "\n" +
" Vendor : "+glf.getClassVendor() + "\n" +
"Native-Library : GL4Javautils.glf..GLFFuncJNI \n" +
" Version: "+glf.getNativeVersion() + "\n" +
" Vendor : "+glf.getNativeVendor() + "\n" +
"-------------------------------------------------\n" +
"\n"+
jvmstr +
"\n" ;
String glVen = gl.glGetString(GLFunc.GL_VENDOR);
String glRen = gl.glGetString(GLFunc.GL_RENDERER);
String glVer = gl.glGetString(GLFunc.GL_VERSION);
String glExt = gl.glGetString(GLFunc.GL_EXTENSIONS);
String gluVer = glu.gluGetString(GLUFunc.GLU_VERSION);
String gluExt = glu.gluGetString(GLUFunc.GLU_EXTENSIONS);
String info2= "OpenGL - Versions \n" +
"-----------------------------------------------\n" +
"GL VENDOR: "+glVen+"\n"+
"GL RENDERER: "+glRen+"\n"+
"GL VERSION: "+glVer+"\n"+
"GL EXTENSIONS: "+glExt+"\n"+
"GLU VERSION: "+gluVer+"\n"+
"GLU EXTENSIONS: "+gluExt+"\n"+"\n" ;
String info3= "OpenGL - Function Test ("+
GLFunc.GL_PROC_NAMES.length +" Functions) \n" +
"-----------------------------------------------\n";
if(verbose)
{
System.out.println(info1);
System.out.println(info2);
System.out.println(info3);
}
String h;
String tmp;
int okNum=0;
for(int i=0; i
static {
if(GLContext.loadNativeLibraries(null, null, null)==false)
System.out.println("could not load def. native libs.");
}
*
*
* @param gljLibName The name of the GLContex/GLFunc/GLUFunc
* native library.
* If gljLibName==null, the default library will be used !
*
* @param glLibName deprecated
*
* @param gluLibName deprecated
*
* @return boolean, true if succesfull !
*
* @see gl4java.GLContext#defGljLib
*
* @deprecated The arguments glLibName and gluLibName are obsolete,
* because all glj/gl/glu stuff resides within
* the gljLib !
* Now you can use doLoadNativeLibraries !
*/
public final static boolean loadNativeLibraries ( String gljLibName,
String glLibName,
String gluLibName
)
{
return doLoadNativeLibraries(gljLibName, null, null);
}
/**
* Support of loading the native library seperatly.
*
* Link with the given OpenGL library.
* If we cannot link, an exception is thrown.
*
* You can also specify the OpenGL and GLU library by
* the environment variables:
*
GLTOOL_USE_GLLIB - OpenGL library name
GLTOOL_USE_GLULIB - GLU library name
*
* these environment variables does _always_ overrides
* any given ones at this point !!
*
* The name of the library is named e.g.: "GL4JavaJauGljJNI" at the
* Java level, or "libGL4JavaJauGljJNI.so" at the solaris level,
* or "GL4JavaJauGljJNI.dll" at the win32 level :-).
*
*
static {
if(GLContext.doLoadNativeLibraries(null, null, null)==false)
System.out.println("could not load def. native libs.");
}
*
*
* @param gljLibName The name of the GLContex/GLFunc/GLUFunc
* native library.
* If gljLibName==null, the default library will be used !
*
* @param nativeGLLibName The name of the native GL library.
* If nativeGLLibName==null, the default library will be used !
*
* @param nativeGLULibName The name of the native GLU library.
* If nativeGLULibName==null, the default library will be used !
*
* @return boolean, true if succesfull !
*
* @see gl4java.GLContext#defGljLib
* @see gl4java.GLContext#defNativeGLLibX11
* @see gl4java.GLContext#defNativeGLLibMacOS9
* @see gl4java.GLContext#defNativeGLLibMacOSX
* @see gl4java.GLContext#defNativeGLLibWin32
* @see gl4java.GLContext#defNativeGLULibX11
* @see gl4java.GLContext#defNativeGLULibMacOS9
* @see gl4java.GLContext#defNativeGLULibMacOSX
* @see gl4java.GLContext#defNativeGLULibWin32
*/
public final static boolean doLoadNativeLibraries ( String gljLibName,
String nativeGLLibName,
String nativeGLULibName
)
{
if(libsLoaded) return true;
if(gljClassDebug)
System.out.println("GLContext.doLoadNativeLibraries will do it !");
jvmVendor = java.lang.System.getProperty("java.vendor");
jvmVersion = java.lang.System.getProperty("java.version");
if(gljClassDebug)
{
System.out.println("jvm vendor: "+jvmVendor);
System.out.println("jvm version: "+jvmVersion);
}
int i0 = 0;
int i1 = jvmVersion.indexOf(".", i0);
String strhlp = null;
if(i1>0)
{
strhlp = jvmVersion.substring(i0,i1);
try {
jvmVersionMajor = Integer.valueOf(strhlp).intValue();
} catch (Exception e)
{System.out.println("Not a number: "+strhlp+" ("+jvmVersion+")");}
}
i0 = i1+1;
i1 = jvmVersion.indexOf(".", i0);
if( i1 < 0 )
i1 = jvmVersion.length(); // no 2nd dot, no bug version number
if( 0
- another thread has requested this context -> release it
this gives the other thread a chance to get it ..
- this thread does not own the current context
*
*
*
- gljMakeCurrent()
YOUR OpenGL commands here !
- gljFree()
*
*
* @return boolean
*
* @see gl4java.GLContext#gljSwap
* @see gl4java.awt.GLCanvas#display
* @see gl4java.awt.GLCanvas#sDisplay
*/
public synchronized final boolean gljMakeCurrent()
{
if ( ! isInitialized || !glEnabled )
return false;
Thread thisThread = Thread.currentThread();
boolean dbgPrinted = false;
/**
* force a thread switch to improve responsiveness !
*
* if an earmarked thread exist (nextThread)
* we have to wait ..
*/
while (nextThread!=null && nextThread!=thisThread)
{
if(gljThreadDebug && !dbgPrinted)
{
System.out.println("wait-switch: "+thisThread);
/** JAU NVidia 2314 Bug workaround
* System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext+", currentContext="+gljGetCurrentContext());
*/
System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext);
}
if(ctxThread==thisThread)
{
/**
* Force freeing this threads context .. to avoid a deadlock ..
* This makes sense, because it is possible,
* that the same thread enters this point twice,
* before calling gljFree !
*/
if(gljThreadDebug && !dbgPrinted)
{
System.out.println("wait-current: "+thisThread+" for earmarked: "+nextThread);
System.out.println("\tfreeing context force .. ctxThread="+ctxThread);
}
gljFree(true); // force freeing the context
}
dbgPrinted=true;
try {
// wait till earmarked nextThread has its chance ..
wait();
} catch (InterruptedException e) { }
}
dbgPrinted=false;
/**
* put all req. threads to the wait-state,
* if another thread owns the GLXContext !
*/
while (ctxThread!=null && ctxThread!=thisThread)
{
/**
* remember this thread as earmarked,
* to be sure being the next one ...
*/
nextThread = thisThread;
if(gljThreadDebug && !dbgPrinted)
{
System.out.println("wait-earmarked: "+thisThread);
/** JAU NVidia 2314 Bug workaround
* System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext+", currentContext="+gljGetCurrentContext());
*/
System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext);
dbgPrinted=true;
}
if( _comp instanceof GLRunnable )
{
if ( ((GLRunnable)_comp).ownsThread(ctxThread) )
{
synchronized (_comp) {
((GLRunnable)_comp).freeGLContext();
((GLRunnable)_comp).notifyAll();
}
notifyAll();
if(gljThreadDebug)
System.out.println("\tfreeGLContext -> "+ctxThread);
}
}
try {
// wait for gljFree to release the GLXContext
wait();
} catch (InterruptedException e) { }
}
boolean result = false;
/* is this thread allready owning the context ? */
if ( gljIsCurrent() )
{
result = lockJAWT(_comp, windowHandle, gljThreadDebug);
if(gljThreadDebug)
System.out.println("MakeCurrent: "+thisThread+" no CTX change, allready own, lockJAWT: "+result);
return result;
} else if( ctxThread!=null && ctxThread!=thisThread ) {
System.out.println("MakeCurrent: ctxThread ain't zero, funny failure");
/** JAU NVidia 2314 Bug workaround
* System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext+", currentContext="+gljGetCurrentContext());
*/
System.out.println("\tctxstate thisThread="+thisThread+", ctxThread="+ctxThread+", nextThread="+nextThread+", thisContext="+glContext);
return result;
}
ctxThread = thisThread ; // blocking asap ..
if(gljThreadDebug)
{
if(nextThread==thisThread)
{
System.out.println("MakeCurrent: "+thisThread+"
- another thread has requested this context -> release it
*
*
*
- another thread has requested this context
- the force flag is true
- this thread is the AWT thread
- the component of this context does _not_ implement GLRunnable
- the calling thread is not the thread, this GLRunnable component holds !
*
*
*