Texture with nearest filtering

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Texture with nearest filtering

Marco
I'd like to use the nearest interpolation for the texture, I tried to use the following methods:

final GL2 gl = drawable.getGL().getGL2();
gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MAG_FILTER,GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MIN_FILTER,GL2.GL_NEAREST);

in the init method, but it doesn't work.
The full code is:

public class TextureTest extends GLJPanel implements GLEventListener{
   
    private final GLProfile profile = GLProfile.get(GLProfile.GL2);
    private final GLCapabilities capabilities = new GLCapabilities(profile);
   
    private Texture texture=null;
   
    private BufferedImage im;

    /** Creates a new instance of TextureTest */
    public TextureTest() {
        super();
        this.setRequestedGLCapabilities(capabilities);
        this.addGLEventListener(this);
    }
   
    public void init(GLAutoDrawable drawable) {
        final GL2 gl = drawable.getGL().getGL2();
           
        gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MAG_FILTER,GL2.GL_NEAREST);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MIN_FILTER,GL2.GL_NEAREST);
    }
   
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    }
   
    public void display(GLAutoDrawable drawable) {
       
        final GL2 gl = drawable.getGL().getGL2();
       
        gl.glEnable(GL2.GL_TEXTURE_2D);
       
        int Nx=10;
        int Ny=10;
        im=new BufferedImage(Nx, Ny, BufferedImage.TYPE_INT_RGB);
       
        Random rand = new Random(5L);
        byte[] bytes=new byte[3];
       
        for(int ix=0;ix<Nx;ix++){
            for(int iy=0;iy<Ny;iy++){
                rand.nextBytes(bytes);
                int rgb=(bytes[0]<<16)+(bytes[1]<<8)+(bytes[2]);                
                im.setRGB(ix,iy,rgb);
            }
        }
       
        if(texture!=null) texture.destroy(gl);
        texture=AWTTextureIO.newTexture(profile,im,true);
        int t=texture.getTextureObject(gl);
       
       
        gl.glBindTexture(GL2.GL_TEXTURE_2D, t);
        gl.glBegin(GL2.GL_QUADS);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
       
        gl.glTexCoord2d(0.0, 1.0); gl.glVertex2d(-1.0, 1.0);
       
        gl.glTexCoord2d(1.0, 1.0); gl.glVertex2d( 1.0, 1.0);
       
        gl.glTexCoord2d(1.0, 0.0); gl.glVertex2d( 1.0,-1.0);
       
        gl.glTexCoord2d(0.0, 0.0); gl.glVertex2d(-1.0,-1.0);
       
        gl.glEnd();
       
        gl.glDisable(GL2.GL_TEXTURE_2D);
       
        gl.glFlush();
       
    }
   
    public void dispose(GLAutoDrawable drawable) {
    }

    //==========================================================================
    // Main method
    //==========================================================================
   
    public static void main(String[] args) {
       
        final JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        TextureTest textureTest=new TextureTest();
       
        frame.getContentPane().add(textureTest);
        frame.setSize(600,600);
        frame.setVisible(true);
       
    }
   
}

How can I obtain the nearest interpolation?

P.S. I create the texture in the display method because in the final version of code the texture is different every call.

Marco
Reply | Threaded
Open this post in threaded view
|

Re: Texture with nearest filtering

Marco
I found the solution:

the calls:

gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MAG_FILTER,GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MIN_FILTER,GL2.GL_NEAREST);

must be put after:

gl.glBindTexture(GL2.GL_TEXTURE_2D, t);