如何使用键盘输入在WebGL中创建具有n边的2D形状?

问题描述 投票:1回答:1

我正在尝试在WebGL中创建一个程序,该程序允许您通过键盘输入来绘制或创建n尺寸的形状。用户输入侧面的数量以生成具有那么多侧面的形状。因此,如果按“ 3”,将得到一个三角形,如果按“ 4”,将得到一个正方形,如果按“ 5”,将得到一个五边形,等等。

到目前为止,我已经能够创建单独的代码段,这些代码段无需键盘输入即可创建三角形,正方形,五边形等,但是我不确定如何在同一程序中通过n边生成形状。用户/键盘输入。我将如何去做?

到目前为止我的代码示例:

绘制三角形:


var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'void main() {\n' +
  '  gl_Position = a_Position;\n' +
  '}\n';

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

function main() {

  var canvas = document.getElementById('webgl');

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

  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to initialize shaders.');
    return;
  }

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

  gl.clearColor(0, 0, 0, 0);

  gl.clear(gl.COLOR_BUFFER_BIT);

  gl.drawArrays(gl.TRIANGLES, 0, n);
}

function initVertexBuffers(gl) {
  var vertices = new Float32Array([
    0, 0.5,   -0.5, -0.5,   0.5, -0.5
  ]);
  var n = 3; // The number of vertices

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

  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);

  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;
  }

  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);

  gl.enableVertexAttribArray(a_Position);

  return n;
}

绘制正方形:

var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'void main() {\n' +
  '  gl_Position = a_Position;\n' +
  '}\n';

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

function main() {

  var canvas = document.getElementById('webgl');

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

  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to initialize shaders.');
    return;
  }

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

  gl.clearColor(0, 0, 0, 0);

  gl.clear(gl.COLOR_BUFFER_BIT);

  gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}

function initVertexBuffers(gl) {
  var vertices = new Float32Array([
    -1, -1,   -1, 1,   1, 1,  1, -1,  -1, -1,
  ]);
  var n = 5; // The number of vertices

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

  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);

  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;
  }

  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);

  gl.enableVertexAttribArray(a_Position);

  return n;
}
html webgl
1个回答
1
投票

您可以编写一个函数来计算以边数为参数的多边形的顶点位置。

例如,计算给定半径的圆内多边形的极坐标。您可以编写自己的一个。

computePolygonPositions(sides, radius)
{
    let positions = []

    for (let i=0; i<sides; i++)
    {
        let i0 = i
        let i1 = (i+1) % sides

        let theta0 = 2.0 * Math.PI * i0 / sides
        let theta1 = 2.0 * Math.PI * i1 / sides

        let x0 = radius * Math.cos(theta0)
        let y0 = radius * Math.cos(theta0)

        let x1 = radius * Math.cos(theta1)
        let y1 = radius * Math.cos(theta1)

        positions.push(0, 0)
        positions.push(x0, y0)
        positions.push(x1, y1)
    }

    return positions
}

当然,您可以升级此功能以添加索引,tex坐标,颜色或所需的任何内容。

一旦完成,只需调用它即可创建一个新的顶点缓冲区,该缓冲区将绑定到ARRAY_BUFFER上,设置布局并启用position属性。

© www.soinside.com 2019 - 2024. All rights reserved.