WebGL clipspace point visualization

Above you should see a black square with a little crosshair. The crosshair marks a point in space. This is drawn with WebGL. You can change the position of the point using the sliders to the right, representing four components of the point called X, Y, Z, and W.

By playing around with these sliders, you can discover many things about WebGL!

I implemented the above with a passthrough vertex shader, which passes on the vertex attributes as the vertex position:

attribute vec4 coord;
void main(void) {
  gl_Position = coord;
}

Then, whenever the sliders change, I pass in the six vertices of a 3D crosshair:

function redraw() {
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
    pos.x-CROSSHAIR_SIZE,  pos.y,                 pos.z,                pos.w,
    pos.x+CROSSHAIR_SIZE,  pos.y,                 pos.z,                pos.w,
    pos.x,                 pos.y-CROSSHAIR_SIZE,  pos.z,                pos.w,
    pos.x,                 pos.y+CROSSHAIR_SIZE,  pos.z,                pos.w,
    pos.x,                 pos.y,                 pos.z-CROSSHAIR_SIZE, pos.w,
    pos.x,                 pos.y,                 pos.z+CROSSHAIR_SIZE, pos.w,
  ]), gl.STATIC_DRAW);

  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawArrays(gl.LINES, 0, 6);
}
Tagged .

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.