呼唤远方
您发布的代码仅gl.drawXXX在 main 中调用一次,因此它永远不会再绘制任何内容。你已经设置好了,所以当按下鼠标时click将被调用但从click不调用gl.drawXXX此外,每次click调用时,您都会使用一组新的点创建一个新缓冲区。这不是使用 WebGL 的正常方式。通常的方法是设置你的点一次(每个你想画的东西一次),然后使用矩阵来改变位置、方向、比例。我建议你阅读一些关于 WebGL 的其他教程。这一种做你现在正在做的事情,但它会跟进如何改变位置、方向和比例,然后是如何用矩阵来做以获得更大的灵活性。它还涵盖了绘制多个事物在任何情况下,要按原样修复您的代码,您都需要在更改顶点后进行绘制// Vertex shader programvar VSHADER_SOURCE = 'attribute vec4 a_Position;\n' + 'void main() {\n' + ' gl_Position = a_Position;\n' + '}\n';// Fragment shader programvar FSHADER_SOURCE = 'precision mediump float;\n' + 'uniform vec4 u_FragColor;\n' + 'void main() {\n' + ' gl_FragColor = u_FragColor;\n' + '}\n';var m = 0;function main() { var canvas = document.getElementById('webgl'); // Get the rendering context for WebGL var uniform1i = 0; var gl = canvas.getContext('webgl'); if (!gl) { console.log('Failed to get the rendering context for WebGL'); return; } // 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'); if (a_Position < 0) { console.log('Failed to get the storage location of a_Position'); return; } // Get the storage location of u_FragColor var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor'); if (!u_FragColor) { console.log('Failed to get the storage location of u_FragColor'); return; } // Register function (event handler) to be called on a mouse press canvas.onmousedown = function (ev) { click(ev, gl, canvas, a_Position, u_FragColor) };}function click(ev, gl, canvas, a_Position, u_FragColor) { var x = ev.clientX; // x coordinate of a mouse pointer var y = ev.clientY; // y coordinate of a mouse pointer var rect = ev.target.getBoundingClientRect(); x = ((x - rect.left) - canvas.width / 2) / (canvas.width / 2); y = (canvas.height / 2 - (y - rect.top)) / (canvas.height / 2); if (ev.button == 0) { var depth = 4; gl.uniform4f(u_FragColor, 1.0, 0, 0, 1.0);// Red //red tree, 4 steps, length 50, halved each step // Write the positions of vertices to a vertex shader var n = initVertexBuffers(gl, x, y); if (n < 0) { console.log('Failed to set the positions of the vertices'); return; } m = n; } if (ev.button == 2) { var depth = 6; //blue tree, 6 steps, length 40, halved each step gl.uniform4f(u_FragColor, 0, 0, 1.0, 1.0);// Blue // Write the positions of vertices to a vertex shader var n = initVertexBuffers(gl, x, y); if (n < 0) { console.log('Failed to set the positions of the vertices'); return; } m = n; } // Specify the color for clearing <canvas> gl.clearColor(1.0, 1.0, 1.0, 1.0); // Clear <canvas> gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINES, 0, m); }function initVertexBuffers(gl, x, y) { let start = []; let points = createPoints(x, y, 0.4, 4, Math.PI / 2, start); console.log(points); var vertices = new Float32Array(points); let n = points.length / 2; // The number of vertices // Create a buffer object var vertexBuffer = gl.createBuffer(); if (!vertexBuffer) { console.log('Failed to create the buffer object'); return -1; } // Bind the buffer object to target gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); // Write date into the buffer object gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(gl.program, 'a_Position'); if (a_Position < 0) { console.log('Failed to get the storage location of a_Position'); return -1; } // Assign the buffer object to a_Position variable gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); // Enable the assignment to a_Position variable gl.enableVertexAttribArray(a_Position); return n;}//var points = [x, y];//var angle = 0;//var prevPoints = [];//var prevPointIndex = 0;function createPoints(x, y, length, depth, angle, points) { if (depth > 0) { //draws line let x2 = x + length * Math.cos(angle); let y2 = y + length * Math.sin(angle); points.push(x, y, x2, y2); //draw left branch; createPoints(x2, y2, length / 2, depth - 1, angle + Math.PI / 4, points); //goes back //points.push(x2, y2); //draw right branch createPoints(x2, y2, length / 2, depth - 1, angle - Math.PI / 4, points); //goes back //points.push(x2, y2); //console.log(points); return points; } return;}main();//----function initShaders(gl, vSrc, fSrc) { const program = twgl.createProgram(gl, [vSrc, fSrc]); gl.program = program; // THIS IS EXTREMELY BAD AND WRONG CODE!!! gl.useProgram(program); // THIS IS ALSO WRONG! return program;}canvas { border: 1px solid black; }<canvas id="webgl"></canvas><script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>代码有很多问题gl.program不是一回事。无论你从中学到什么书或教程,都是一件可怕的、糟糕的和错误的事情。WebGL 程序通常有多个着色器程序,因此一个函数initShaders需要返回创建的程序,以便您可以使用不同的顶点着色器源和片段着色器源多次调用它来制作多个程序。它不应该将一个创建的程序破解到它不属于的 webgl 上下文。注意:即使我实现了initShaders因为你没有提供它从你的代码中可以清楚地看出它在做什么gl.program = gl.createProgram。那是无意义的代码。有多个程序是很常见的。通话gl.useProgram中initShaders也可以说是错误的。(再次清楚,因为它没有在您的代码中的任何地方调用您的版本initShaders正在这样做。同样,这没有意义,因为在普通的 WebGL 页面中,您有多个着色器,因此您需要gl.useProgram多次调用。您正在阅读的教程看起来很旧,因为它为 GLSL 连接字符串。制作 GLSL 的最简单方法是使用多行模板文字var VSHADER_SOURCE = `
attribute vec4 a_Position;
void main() {
gl_Position = a_Position;
}
`;容易多了它正在调用一些函数getWebGLContext。不需要那样的东西。只需使用canvas.getContext('webgl')调用gl.drawArrays有m错了地方。正确的调用方式gl.drawArrays是gl.drawArrays(primitiveType, offset, vertexCount)。偏移量几乎是 0。没有第四个参数。请考虑阅读一些更好的教程,例如上面链接的教程。