gpt4 book ai didi

javascript - webGL通过鼠标点击绘制2D三 Angular 形

转载 作者:行者123 更新时间:2023-12-03 03:16:06 28 4
gpt4 key购买 nike

我想在鼠标单击的地方绘制一个 2D 三 Angular 形。已经制作了鼠标事件处理程序,并且可以看到鼠标单击的点。我在缓冲区对象中写入了三 Angular 形的顶点位置。它将是三 Angular 形大小。如何连接鼠标事件处理程序(功能单击)和三 Angular 形的位置(positionBuffer)你能给我答案吗?

     //Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'void main() {\n' +
' gl_Position = a_Position;\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('webgl');

// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
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;
}

var n = initVertexBuffers(gl);
if(n < 0){
console.log('Failed to set the positions of the vertices');
return;
}


// Register function (event handler) to be called on a mouse press
canvas.onmousedown = function(ev){ click(ev, gl, canvas) };

// Specify the color for clearing <canvas>
gl.clearColor(0.0, 0.0, 0.0, 1.0);

// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
}


var shapes = []; // The array for the position of Triangle with mouse click

function click(ev, gl, canvas) {
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);


// Store the coordinates to shapes array
shapes.push([x,y]);

// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);


var len = shapes.length;
for(var i = 0; i < len; i++) {

gl.bufferData(gl.ARRAY_BUFFER, shapes[i], gl.STATIC_DRAW);
}
// Draw
gl.drawArrays(gl.TRIANGLES, 0, 3);
}


//Make the BO for making triangle
function initVertexBuffers(gl){

var vertices = new Float32Array([
0.0, 0.1,
-0.1, -0.1,
0.1, -0.1,
]);
var n = 3;

//Create a buffer Object
var positionBuffer = gl.createBuffer();
if(!positionBuffer){
console.log('Failed to create the buffer object');
return -1;
}

//Bind the buffer object to target
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
//Write date into the buffer object
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

//Assign the buffer object to a_Position variable
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;
}

//Connect the assignment 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;
}

错误信息-> "image here"

最佳答案

目前还不清楚你想要发生什么。你想绘制一个包含多个三 Angular 形的网格还是想绘制N个三 Angular 形?

无论如何,这段代码没有任何意义

var shapes = [];  // The array for the position of Triangle with mouse click

function click(ev, gl, canvas) {
...

// Store the coordinates to shapes array
shapes.push([x,y]);

...

var len = shapes.length;
for(var i = 0; i < len; i++) {

gl.bufferData(gl.ARRAY_BUFFER, shapes[i], gl.STATIC_DRAW);
}

gl.bufferData需要 typed array ,而不是您传递给它的 JavaScript native 数组数组。

但也不清楚你想做什么。在 initVertexBuffer您创建一个缓冲区并上传一个三 Angular 形(3 个顶点)

然后在 click您尝试用点数据替换位置缓冲区中的三 Angular 形,如果成功,将删除该三 Angular 形。它不成功,因为您没有使用类型化数组,但即使成功,它也不起作用,因为您最终会删除三 Angular 形。

可以说,确实存在太多错误,无法真正选择一个起点。我建议reading some other tutorials on WebGL

这是您的代码,可以正常工作

我添加了制服u_Offset到顶点着色器。然后我循环遍历 click 中的形状坐标,将每个记录的偏移量设置为 gl.uniform2f并调用gl.drawArrays对于每个三 Angular 形。

     //Vertex shader program
var VSHADER_SOURCE = `
attribute vec4 a_Position;
uniform vec2 u_Offset;
void main() {
gl_Position = a_Position + vec4(u_Offset, 0, 0);
}`;

// Fragment shader program
var FSHADER_SOURCE = `
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}`;

var offsetLoc;

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

// Get the rendering context for WebGL
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;
}

offsetLoc = gl.getUniformLocation(gl.program, "u_Offset");

var n = initVertexBuffers(gl);
if(n < 0){
console.log('Failed to set the positions of the vertices');
return;
}


// Register function (event handler) to be called on a mouse press
canvas.onmousedown = function(ev){ click(ev, gl, canvas) };

// Specify the color for clearing <canvas>
gl.clearColor(0.0, 0.0, 0.0, 1.0);

// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
}


var shapes = []; // The array for the position of Triangle with mouse click

function click(ev, gl, canvas) {
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);


// Store the coordinates to shapes array
shapes.push([x,y]);

// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);


var len = shapes.length;
for(var i = 0; i < len; i++) {
// Draw
gl.uniform2f(offsetLoc, shapes[i][0], shapes[i][1]);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
}


//Make the BO for making triangle
function initVertexBuffers(gl){

var vertices = new Float32Array([
0.0, 0.1,
-0.1, -0.1,
0.1, -0.1,
]);
var n = 3;

//Create a buffer Object
var positionBuffer = gl.createBuffer();
if(!positionBuffer){
console.log('Failed to create the buffer object');
return -1;
}

//Bind the buffer object to target
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
//Write date into the buffer object
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

//Assign the buffer object to a_Position variable
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;
}

//Connect the assignment 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;
}

function initShaders(gl, vsrc, fsrc) {
// initShaders is really poorly designed. Most WebGL programs need multiple shader programs
// but this function assumes there will only ever be one shader program
// Also you should never assign values to the gl context.
gl.program = twgl.createProgram(gl, [vsrc, fsrc]);
gl.useProgram(gl.program);
return gl.program;
}

main();
canvas { border: 1px solid black; }
<canvas id="webgl"></canvas>
<script src="https://twgljs.org/dist/3.x/twgl.min.js"></script>

关于javascript - webGL通过鼠标点击绘制2D三 Angular 形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com