webgl chapter 2 create click events to add things to the canvas

1 html

<html lang="en">
    <head>
        <title></title>
        <meta charset="UTF-8">
        <script type="text/javascript" src="../WebGL_Guide_Code/WebGL_Guide_Code/lib/cuon-utils.js"></script>
        <script type="text/javascript" src="../WebGL_Guide_Code/WebGL_Guide_Code/lib/webgl-debug.js"></script>
        <script type="text/javascript" src="../WebGL_Guide_Code/WebGL_Guide_Code/lib/webgl-utils.js"></script>
        <style>
        </style>
    </head>
    <body onload="main()">
        <canvas id="canvas" width="400" height="400">

        </canvas>
        <script type="text/javascript" src="../WebGL_Guide_Code/WebGL_Guide_Code/lib/cuon-utils.js"></script>
        <script type="text/javascript" src="../WebGL_Guide_Code/WebGL_Guide_Code/lib/webgl-debug.js"></script>
        <script type="text/javascript" src="../WebGL_Guide_Code/WebGL_Guide_Code/lib/webgl-utils.js"></script>
        <script type="text/javascript" src="drowPoint.js"></script>
    </body>
</html>

2 js

	// HelloPint2.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE = 
  'attribute vec4 a_Position;\n' + // attribute variable
  'void main() {\n' +
  '  gl_Position = a_Position;\n' +
  '  gl_PointSize = 10.0;\n' +
  '}\n'; 

// Fragment shader program
var FSHADER_SOURCE = 
  'void main() {\n' +
  '  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
  '}\n';

function main() {
  // Retrieve <canvas> element
  var canvas = document.getElementById('canvas');

  // Get the rendering context for WebGL
  var gl = getWebGLContext(canvas);

  
  if (!gl) {
    console.log('Failed to get the rendering context for WebGL');
    return;
  }

  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to intialize shaders.');
    return;
  }

  // Get the storage location of a_Position
  var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  var a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize');
  if (a_Position < 0) {
    console.log('Failed to get the storage location of a_Position');
    return;
  }

  

  canvas.onmousedown = function (ev) {
      click(ev, gl, canvas, a_Position);
  }

  // Pass vertex position to attribute variable
  
}

var g_Points = []; // 鼠标点击的位置数组

  

  function click (ev, gl, canvas, a_Position) {
      var x = ev.clientX;
      var y = ev.clientY;
      var rect = ev.target.getBoundingClientRect();
      x = ((x - rect.left) - canvas.height/2) / (canvas.height/2);
      y = (canvas.width/2 - (y - rect.top)) / (canvas.width/2);
      g_Points.push(x);
      g_Points.push(y);
      
      gl.clear(gl.COLOR_BUFFER_BIT);

      var len = g_Points.length;
      for (var i = 0; i < len; i += 2) {
          gl.vertexAttrib3f(a_Position, g_Points[i], g_Points[i + 1], 0.0);

          gl.drawArrays(gl.POINTS, 0, 1);
      }
  }

We created two shaders. Compared to 1, the coordinates we gave to the position are entered dynamically.

'attribute vec4 a_Position;\n'

In this line of code, we add an object of type attribute, which stores several position coordinates,

var a_Position = gl.getAttribLocation(gl.program, 'a_Position');

The more important thing is that a_Position here is the address of the second parameter, no value!

 gl.vertexAttrib3f(a_Position, g_Points[i], g_Points[i + 1], 0.0);

Here we pass the a_Position variable as the value to the above attribute object, and what we pass in later is the dynamic address. The following values ​​are used

var x = ev.clientX;
  var y = ev.clientY;
  var rect = ev.target.getBoundingClientRect();
  x = ((x - rect.left) - canvas.height/2) / (canvas.height/2);
  y = (canvas.width/2 - (y - rect.top)) / (canvas.width/2);
  g_Points.push(x);
  g_Points.push(y);

Incoming.
Click on the picture
Come here today, come on!

Published 20 original articles · won praise 5 · Views 2086

Guess you like

Origin blog.csdn.net/qq_42859887/article/details/90522519