We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › turning off bilinear texture filtering in opengl
Page Index Toggle Pages: 1
turning off bilinear texture filtering in opengl (Read 688 times)
turning off bilinear texture filtering in opengl
Dec 31st, 2008, 11:08am
 
Does anyone know how to turn of bilinear filtering in OpenGl and use a nearest neighbour mode for texture filtering?

found some info here, but don't know how to use this in processing or if it's even possible.

http://gregs-blog.com/2008/01/14/how-to-turn-off-bilinear-filtering-in-opengl/

thx
Re: turning off bilinear texture filtering in opengl
Reply #1 - Aug 30th, 2009, 10:12am
 
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.
Page Index Toggle Pages: 1