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 .
👋 I'm Jim, a full-stack product engineer. Want to build an amazing product and a profitable business? Read more about me or Get in touch!

More by Jim

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