hi, i hope someone can help me…
i’m trying to write an opengl application without a window. i only want to process pixels in textures.
i have the class:
public abstract class GPUCalculator implements Calculator, GLEventListener {
protected GLPbuffer pbuffer;
protected GL gl;
private int texture_target = GL.GL_TEXTURE_RECTANGLE_ARB;
private int texture_format = GL.GL_RGB;
private int data_type = GL.GL_FLOAT;
private IntBuffer texID;
protected int width;
protected int height;
protected FloatBuffer data;
public GPUCalculator ( int width, int height, FloatBuffer data ) {
this.width = width;
this.height = height;
this.data = data;
GLDrawableFactory factory = GLDrawableFactory.getFactory ();
if ( factory.canCreateGLPbuffer () ) {
try {
this.pbuffer = factory.createGLPbuffer ( new GLCapabilities (), null, this.width, this.height, null );
this.pbuffer.addGLEventListener ( this );
this.pbuffer.display ();
} catch ( Exception e ) {
e.printStackTrace ();
}
}
}
public void init ( GLAutoDrawable drawable ) {
this.gl = drawable.getGL ();
GLU glu = new GLU ();
// viewport
this.gl.glMatrixMode ( GL.GL_PROJECTION );
this.gl.glLoadIdentity ();
// this.gl.glOrtho ( ... );
glu.gluOrtho2D ( 0, this.width, 0, this.height );
this.gl.glMatrixMode ( GL.GL_MODELVIEW );
this.gl.glLoadIdentity ();
this.gl.glViewport ( 0, 0, this.width, this.height );
// fbo
IntBuffer fb = IntBuffer.allocate ( 1 );
this.gl.glGenFramebuffersEXT ( 1, fb );
this.gl.glBindFramebufferEXT ( GL.GL_FRAMEBUFFER_EXT, fb.get ( 0 ) );
// texture
this.texID = IntBuffer.allocate ( 1 );
this.gl.glGenTextures ( 1, this.texID );
this.gl.glBindTexture ( this.texture_target, this.texID.get ( 0 ) );
this.gl.glTexParameteri ( this.texture_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST );
this.gl.glTexParameteri ( this.texture_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST );
this.gl.glTexParameteri ( this.texture_target, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP );
this.gl.glTexParameteri ( this.texture_target, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP );
// this.gl.glTexEnvi ( GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE );
this.gl.glTexImage2D ( this.texture_target, 0, GL.GL_RGB, this.width, this.height, 0, this.texture_format,
this.data_type, null );
// texture as render target
this.gl.glFramebufferTexture2DEXT ( GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, this.texture_target,
this.texID.get ( 0 ), 0 );
// data transfer
this.transferDataToGPU ();
}
public void transferDataToGPU () {
this.data.rewind ();
this.gl.glTexSubImage2D ( this.texture_target, 0, 0, 0, this.width, this.height, this.texture_format,
this.data_type, this.data );
}
public FloatBuffer readDataFromGPU () {
FloatBuffer outData = null;
this.gl.glReadBuffer ( GL.GL_COLOR_ATTACHMENT0_EXT );
this.gl.glReadPixels ( 0, 0, this.width, this.height, this.texture_format, this.data_type, outData );
return outData;
}
.........
}
to test the appllication, i create a FloatBuffer:
FloatBuffer in = FloatBuffer.allocate ( 3 * 9 );
TestGPUCalculator calci = new TestGPUCalculator ( 3, 3, in );
and pass it in TestGPUCalculator, which is derived from GPUCalculator.
but after that, when i call:
in = calci.readDataFromGPU ();
i get a nullpointerexception, because the Floatbuffer which returns from the method readDataFromGPU() is always null.
perhaps someone knows what i’m doing wrong.