Gl_clamp_to_border

I needed GL_CLAMP_TO_BORDER to work and I thought I share it since it might help someone (me?) in the future.

package glsl_blobscanner;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import processing.core.*;
import processing.opengl.*;

public class GL_CLAMP_TO_BORDER extends PApplet{
    
public static void main(String[] args) {
    PApplet.main(GL_CLAMP_TO_BORDER.class, args);
}


PImage source;

int GL_CLAMP_TO_BORDER = 0x812D;
int GL_TEXTURE_BORDER_COLOR = 0x1004;

PGL pgl;


public void settings() {
    size(512, 512, P2D);
}

public void setup() {

    source = loadImage("debug_05_32x32.png");
    pgl = beginPGL();
    
}

public void draw() {
    background(255);

    Texture source_tex = ((PGraphicsOpenGL)g).getTexture(source);
    source_tex.usingRepeat(false);
    source_tex.bind();
    pgl.texParameteri(source_tex.glTarget, PGL.TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    pgl.texParameteri(source_tex.glTarget, PGL.TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    
    float borderColor[] = { 1.0f, 1.0f, 0.0f, 1.0f };
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(borderColor.length * Float.BYTES);
    byteBuffer.order(ByteOrder.nativeOrder()); // Use the native byte order
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(borderColor);
    floatBuffer.flip();

    pgl.texParameterfv(source_tex.glTarget, GL_TEXTURE_BORDER_COLOR, floatBuffer); 
    
    source_tex.unbind();

    beginShape();
    texture(source);
    textureMode(NORMAL);
    vertex(64, 64, -0.5f, -0.5f);
    vertex(width-64, 64, 1.5f, -0.5f);
    vertex(width-64, height-64, 1.5f, 1.5f);
    vertex(64, height-64, -0.5f, 1.5f);
    endShape();
}

}
2 Likes