Skip to content

Instantly share code, notes, and snippets.

@ThomasLengeling
Created August 20, 2015 21:04
Show Gist options
  • Save ThomasLengeling/284ded172b6e9bd56316 to your computer and use it in GitHub Desktop.
Save ThomasLengeling/284ded172b6e9bd56316 to your computer and use it in GitHub Desktop.
// Draws a triangle using low-level OpenGL calls.
import java.nio.*;
PGL pgl;
PShader sh;
int vertLoc;
int colorLoc;
float[] vertices;
float[] colors;
FloatBuffer vertData;
FloatBuffer colorData;
void setup() {
size(640, 360, P3D);
// Loads a shader to render geometry w/out
// textures and lights.
sh = loadShader("frag.glsl", "vert.glsl");
vertices = new float[12];
vertData = allocateDirectFloatBuffer(12);
colors = new float[12];
colorData = allocateDirectFloatBuffer(12);
}
void draw() {
background(0);
// The geometric transformations will be automatically passed
// to the shader.
rotate(frameCount * 0.01, width, height, 0);
updateGeometry();
pgl = beginPGL();
sh.bind();
vertLoc = pgl.getAttribLocation(sh.glProgram, "vertex");
colorLoc = pgl.getAttribLocation(sh.glProgram, "color");
//PGL.ARRAY_BUFFER,
//PGL.STATIC_DRAW
pgl.bindBuffer(PGL.ARRAY_BUFFER, vertLoc);
pgl.bindBuffer(PGL.ARRAY_BUFFER, colorLoc);
pgl.bufferData(PGL.ARRAY_BUFFER, vertices.length * 4, vertData, PGL.STATIC_DRAW);
pgl.bufferData(PGL.ARRAY_BUFFER, colors.length * 4, colorData, PGL.STATIC_DRAW);
pgl.vertexAttribPointer(vertLoc, 4, PGL.FLOAT, false, 0, 0);
pgl.vertexAttribPointer(colorLoc, 4, PGL.FLOAT, false, 0, 0);
pgl.enableVertexAttribArray(vertLoc);
pgl.enableVertexAttribArray(colorLoc);
pgl.drawArrays(PGL.TRIANGLES, 0, 3);
pgl.disableVertexAttribArray(vertLoc);
pgl.disableVertexAttribArray(colorLoc);
sh.unbind();
endPGL();
}
void updateGeometry() {
// Vertex 1
vertices[0] = 0;
vertices[1] = 0;
vertices[2] = 0;
vertices[3] = 1;
colors[0] = 1;
colors[1] = 0;
colors[2] = 0;
colors[3] = 1;
// Corner 2
vertices[4] = width/2;
vertices[5] = height;
vertices[6] = 0;
vertices[7] = 1;
colors[4] = 0;
colors[5] = 1;
colors[6] = 0;
colors[7] = 1;
// Corner 3
vertices[8] = width;
vertices[9] = 0;
vertices[10] = 0;
vertices[11] = 1;
colors[8] = 0;
colors[9] = 0;
colors[10] = 1;
colors[11] = 1;
vertData.rewind();
vertData.put(vertices);
vertData.position(0);
colorData.rewind();
colorData.put(colors);
colorData.position(0);
}
FloatBuffer allocateDirectFloatBuffer(int n) {
return ByteBuffer.allocateDirect(n * Float.SIZE/8).order(ByteOrder.nativeOrder()).asFloatBuffer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment