this is kinda a late, but maybe this helps:
add this to your import statements:
Code:import javax.media.opengl.*;
and use this code to create your image (in the setup function):
Code:
GL gl = ((PGraphicsOpenGL) g).gl;
gl.glTexParameteri( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST );
myNearestNeighbourTexture = new Texture( VIDEO_W, VIDEO_H );
and then create a new file called "texture.pde" and use this code:
Code:
import java.nio.ByteBuffer;
static int textureIDCounter;
/**
* Ok - because of the bug in the openGL updatePixels function it's not really possible to
* have dynamic textures.
* This class is disgusting workaround, which does ONLY work if all the textures are created
* before any call to image(...) is made!
*
* Most GL code is a ripp off from:
* http://wiki.java.net/bin/view/Games/TextureLoadingExample
*/
class Texture{
public int pixels[ ];
public int w, h;
public byte data[ ];
public int dataW, dataH;
private int textureID;
private GL gl;
PImage dummy;
public Texture( int w, int h ){
this( w, h, g instanceof PGraphicsOpenGL? ((PGraphicsOpenGL)g).gl:null );
}
public Texture( int w, int h, GL gl ){
this.w = w;
this.h = h;
// Create and fill dummy object...
dummy = new PImage( w, h );
this.pixels = dummy.pixels;
for( int i = 0; i < dummy.pixels.length; i++ ){
pixels[ i ] = 0xffff0000;
}
if( gl == null )
return;
this.gl = gl;
// Draw the image, so a texture id is created...
image( dummy, 0, 0 );
// Create the necessary buffers and stuff...
this.dataW = get2Fold( w );
this.dataH = get2Fold( h );
this.data = new byte[ dataW*dataH*4 ];
// Get a texture id...
//int tmp[ ] = new int[ 1 ];
//gl.glGenTextures( 1, tmp );
//textureID = tmp[ 0 ];
// This is the disgusting part!
textureID = ++textureIDCounter;
// Allright!
updatePixels();
}
/**
* Calculate the minimum texture buffer size...
*/
private int get2Fold(int fold) {
int ret = 2;
while (ret < fold)
ret *= 2;
return ret;
}
/**
* Updates some pixel data...can take quite some time!
*/
public void updatePixels(){
if( gl == null ){
dummy.updatePixels();
return;
}
int index;
// Load new pixel data...
for( int i = 0; i < pixels.length; i++ ){
index = 4*( ( i / w )*dataW + ( i % w ) );
data[ index++ ] = (byte)( ( pixels[ i ] & 0x00ff0000 ) >> 16 );
data[ index++ ] = (byte)( ( pixels[ i ] & 0x0000ff00 ) >> 8 );
data[ index++ ] = (byte)( ( pixels[ i ] & 0x000000ff ) );
data[ index++ ] = (byte)( ( pixels[ i ] & 0xff000000 ) >> 24 );
}
// Bind our id...
gl.glBindTexture( GL.GL_TEXTURE_2D, textureID );
gl.glTexParameteri( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST );
// Update video memory buffer
gl.glTexImage2D(
GL.GL_TEXTURE_2D,
0, // Level, what level?
GL.GL_RGBA, // Dest format
dataW, // Required texture width
dataH, // Required texture height
0, // Border
GL.GL_RGBA, // Source format
GL.GL_UNSIGNED_BYTE, // Datatype of data
ByteBuffer.wrap( data ) // Data
);
// Unbind the texture id, otherwise we might confuse someone else...
gl.glBindTexture( GL.GL_TEXTURE_2D, 0 );
}
/**
* Wooooho - draw draw draw!
*/
public void draw( int x, int y, int w, int h ){
image( dummy, x, y, w, h );
}
/**
* Also draw...
*/
public void draw( int x, int y ){
image( dummy, x, y );
}
}
now just use the Texture class instead of the PImage class. And instead of using the command image( myImage, x, y ) you now use myImage.draw( x, y ).
It has a fallback for other renderers, so if you don't use opengl it won't break.
Also, this code is kinda old, i think the last version I used this with was P1.0.1 or so.
Eventually it helps...
best, hansi.