Re: [sdljava-users] Usage example for glGetDoublev
Status: Beta
Brought to you by:
ivan_ganza
From: Ivan Z. G. <iva...@ya...> - 2005-09-10 21:39:24
|
Hi Gregor, The direct buffers introduced with NIO are better than sliced bread...:-) Without them I don't think it would be possible to achieve any level of meaningfully performance. A direct buffer is mapped directly to a region of memory inside the JVM's memory space. When you perform operations on a direct buffer they effect the memory directly and thus and are very fast. The other option is to copy the data each time from the JVM's memory space into the native code space (or visa versa). The copy method is an order of magnitude slower. First the new array must be allocated. Then the data must be copied into the array each time. For an idea of the speed difference look at the difference in execution time between updating the screen using the two methods: =20 ByteBuffer getPixelData() and public void setPixelData32(int[] pixelData) If you are using these direct buffers you should allocate all the buffers you need before doing anything. Then utilize them later however you wish. Otherwise the performance will be much worse if a new one is constantly created. Notice that getPixelData() only creates the buffer once. The buffer directly has access to the data of the surface! =20 -Ivan/ Gregor M=FCckl wrote: >Hi, Ivan! > >Thanks for your help. Indeed, ByteBuffer.allocateDirect() is the right=20 >solution and it seems to work now. I cannot tell for sure yet as the=20 >gtk-based input handling is still not working properly and I therefore c= an't=20 >navigate the 3D view. > >However, I feel that relyinig on java.nio.ByteBuffer and derivates is a = bit=20 >strange, especially given their somewhat peculiar design. Looking at the= code=20 >I have now which has to copy data to newly created buffers every time I = need=20 >to pass an array to OpenGL, I get the feeling that this slower than deal= ing=20 >directly with arrays, which are readiy available in my case and probably= more=20 >optimizeable by the Java compiler and JVM. > >What was the reason for using these buffers instead of arrays? Am I miss= ing=20 >something here? > >Regards, >Gregor > >On Saturday 10 September 2005 01:37, Ivan Z. Ganza wrote: > =20 > >>Gregor, >> >>I had a look in the code and intact SDLSurface isn't a good example >>because the buffer is created in the native code. Have a look at >>ByteBufferPool instead. >> >>-Ivan/ >> >>Gregor M=FCckl wrote: >> =20 >> >>>Hi! >>> >>>I"m trying to use glGetDoublev to read back the modelview and projecti= on >>>matrices from OpenGL. However, it looks like I'm doing something wrong >>>with the DoubleBuffers required for that. >>> >>>What I'm doing basically is in this code: >>> >>> GlewImpl gl; >>> private ml.math.Matrix4 modelViewMatrix; >>> private ml.math.Matrix4 projectionMatrix; >>> private double viewFieldOfView; >>> private DoubleBuffer matrixBuffer; >>> >>> public ViewWindowSpace(Widget parent) >>> { >>> super(); >>> >>> matrixBuffer=3DDoubleBuffer.allocate(16); >>> modelViewMatrix=3Dnew Matrix4(); >>> projectionMatrix=3Dnew Matrix4(); >>> >>> currentRenderMode=3DRenderMode.Solid; >>> >>> initGLContext(); >>> >>> // snip: unrelated code >>> >>> addListener(new LifeCycleListener() { >>> >>> public void lifeCycleEvent(LifeCycleEvent e) >>> { >>> // TODO Auto-generated method stub >>> if(e.getType()=3D=3DLifeCycleEvent.Type.REALIZE) { >>> setupViewingAreaInitial(); >>> } >>> >>> } >>> } >>> >>> // snip: unrelated code >>> } >>> >>> protected void setupViewingAreaInitial() >>> { >>> viewFieldOfView=3D45.0; >>> >>> glBegin(); >>> GlewJNI.SWIG_glew_init(); >>> gl=3Dnew GlewImpl(); >>> int width =3D getWidth(); >>> int height =3D getHeight(); >>> gl.glViewport(0, 0, width, height); >>> gl.glMatrixMode(GL.GL_PROJECTION); // select the projectio= n >>>matrix >>> gl.glLoadIdentity(); // reset the >>>projection matrix >>> float fAspect =3D (float) width / (float) height; >>> gl.gluPerspective(viewFieldOfView, fAspect, 0.5f, 400.0f); >>> gl.glMatrixMode(GL.GL_MODELVIEW); // select the modelview >>>matrix gl.glLoadIdentity(); >>> >>> gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX,matrixBuffer); >>> modelViewMatrix.setFromOpenGLMatrix(matrixBuffer); >>> gl.glGetDoublev(GL.GL_PROJECTION_MATRIX,matrixBuffer); >>> projectionMatrix.setFromOpenGLMatrix(matrixBuffer); >>> >>> glEnd(); >>> } >>> >>>What this does: >>> >>>The constructor allocates the DoubleBuffer in question and registers a= n >>>event handler to do the rest of the initialilsation once the opengl >>>context can actually be used. When this is the case, >>>setupViewingAreaInitial is called to perform the init of the gl contex= t. >>>This fails with a NullPointerException in the first call to=20 >>>gl.glGetDoublev(). The console output for this exception is as follows= : >>> >>>Exception in thread "main" java.lang.NullPointerException: null addres= s >>>returned from GetDirectBufferAddress() call. Make sure the buffer is = a >>>_direct_ buffer. >>> at org.gljava.opengl.x.swig.GlewJNI.glGetDoublev(Native Method) >>> at org.gljava.opengl.impl.glew.GlewImpl.glGetDoublev(GlewImpl.java:14= 3) >>> at >>>ml.ui.plugins.view.ViewWindowSpace.setupViewingAreaInitial(ViewWindowS= pace >>>.java:322) at >>>ml.ui.plugins.view.ViewWindowSpace$2.lifeCycleEvent(ViewWindowSpace.ja= va:2 >>>77) at org.gnu.gtk.Widget.fireLifeCycleEvent(Widget.java:735) >>> at org.gnu.gtk.Widget.handleRealize(Widget.java:764) >>> at org.gnu.gtk.Widget.gtk_widget_show(Native Method) >>> at org.gnu.gtk.Widget.show(Widget.java:119) >>> at ml.core.State.createMainWindow(State.java:158) >>> at ml.ML3D.<init>(ML3D.java:49) >>> at ml.ML3D.main(ML3D.java:66) >>> >>>I have no clue why this happens. Do I initialize the DoubleBuffer in >>>question badly? Is there any example that shows how it should be done? >>>Any help would be appreciated. >>> >>>Regards, >>>Gregor >>> >>> >>>------------------------------------------------------- >>>SF.Net email is Sponsored by the Better Software Conference & EXPO >>>September 19-22, 2005 * San Francisco, CA * Development Lifecycle >>>Practices Agile & Plan-Driven Development * Managing Projects & Teams = * >>>Testing & QA Security * Process Improvement & Measurement * >>>http://www.sqe.com/bsce5sf >>>_______________________________________________ >>>sdljava-users mailing list >>>sdl...@li... >>>https://lists.sourceforge.net/lists/listinfo/sdljava-users >>> =20 >>> >>------------------------------------------------------- >>SF.Net email is Sponsored by the Better Software Conference & EXPO >>September 19-22, 2005 * San Francisco, CA * Development Lifecycle Pract= ices >>Agile & Plan-Driven Development * Managing Projects & Teams * Testing &= QA >>Security * Process Improvement & Measurement * http://www.sqe.com/bsce5= sf >>_______________________________________________ >>sdljava-users mailing list >>sdl...@li... >>https://lists.sourceforge.net/lists/listinfo/sdljava-users >> =20 >> > > >------------------------------------------------------- >SF.Net email is Sponsored by the Better Software Conference & EXPO >September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practi= ces >Agile & Plan-Driven Development * Managing Projects & Teams * Testing & = QA >Security * Process Improvement & Measurement * http://www.sqe.com/bsce5s= f >_______________________________________________ >sdljava-users mailing list >sdl...@li... >https://lists.sourceforge.net/lists/listinfo/sdljava-users > =20 > |