Graphics 使用两个缓冲区尝试渲染两个三角形WebGL

Graphics 使用两个缓冲区尝试渲染两个三角形WebGL,graphics,webgl,Graphics,Webgl,我正在尝试使用两个缓冲区渲染两个三角形,我已经看过一些教程,并尝试两次调用glDrawArrays函数。但是,仅渲染最新的缓冲区数据,即仅渲染一个三角形。请帮助我解决和理解绘制多个对象的概念,提前感谢 这是密码 // HelloTriangle.js (c) 2012 matsuda // Vertex shader program var VSHADER_SOURCE = 'attribute vec4 a_Position;\n' + 'void main() {\n' + '

我正在尝试使用两个缓冲区渲染两个三角形,我已经看过一些教程,并尝试两次调用glDrawArrays函数。但是,仅渲染最新的缓冲区数据,即仅渲染一个三角形。请帮助我解决和理解绘制多个对象的概念,提前感谢

这是密码

// HelloTriangle.js (c) 2012 matsuda
// 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;
  }

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

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

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

  // Draw the rectangle
  gl.drawArrays(gl.TRIANGLES, 0, n);

   gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
     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 verticesOne = new Float32Array([
     0.5, -0.5,   0, 0.5,   0.5, 2.0
  ]);
  var n = 3; // The number of vertices

  // Create a buffer object
  var vertexBuffer = gl.createBuffer();
  if (!vertexBuffer) {
    console.log('Failed to create the buffer object');
    return -1;
  }
var vertexBufferO = gl.createBuffer();
  // 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);

    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBufferOne);
  // Write date into the buffer object
  gl.bufferData(gl.ARRAY_BUFFER, verticesOne, 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;
}
//HelloTriangle.js(c)2012 matsuda
//顶点着色器程序
var-VSHADER_源=
'属性向量4 a_位置\不+
'void main(){\n'+
'gl\U位置=a\U位置;\n'+
'}\n';
//片段着色器程序
var FSHADER_源=
'void main(){\n'+
'gl_FragColor=vec4(1.0,0.0,0.0,1.0);\n'+
'}\n';
函数main(){
//检索元素
var canvas=document.getElementById('webgl');
//获取WebGL的渲染上下文
var gl=getWebGLContext(画布);
如果(!gl){
log('未能获取WebGL的呈现上下文');
返回;
}
//初始化着色器
if(!initShaders(gl、VSHADER_源、FSHADER_源)){
log('初始化着色器失败');
返回;
}
//将顶点的位置写入顶点着色器
var n=initVertexBuffers(gl);
if(n<0){
console.log('未能设置顶点的位置');
返回;
}
//指定要清除的颜色
gl.clearColor(0,0,0,1);
//清楚的
总账清除(总账颜色缓冲位);
//画矩形
总图数组(总图三角形,0,n);
gl.bindBuffer(gl.ARRAY\u BUFFER,vertexBuffer);
gl.VertexAttribute指针(a_位置,2,gl.FLOAT,false,0,0);
总图数组(总图三角形,0,n);
}
函数initVertexBuffers(gl){
var顶点=新数组([
0, 0.5,   -0.5, -0.5,   0.5, -0.5,
]);
var verticesOne=新的Float32Array([
0.5, -0.5,   0, 0.5,   0.5, 2.0
]);
var n=3;//顶点数
//创建缓冲区对象
var vertexBuffer=gl.createBuffer();
如果(!vertexBuffer){
log('未能创建缓冲区对象');
返回-1;
}
var vertexBufferO=gl.createBuffer();
//将缓冲区对象绑定到目标
gl.bindBuffer(gl.ARRAY\u BUFFER,vertexBuffer);
//将日期写入缓冲区对象
gl.bufferData(gl.ARRAY\u BUFFER,顶点,gl.STATIC\u DRAW);
gl.bindBuffer(gl.ARRAY\u BUFFER,vertexBufferOne);
//将日期写入缓冲区对象
gl.bufferData(gl.ARRAY\u BUFFER、verticesOne、gl.STATIC\u DRAW);
var a_Position=gl.getAttribLocation(gl.program,“a_Position”);
如果(a_位置<0){
console.log('未能获取_位置的存储位置');
返回-1;
}
//将缓冲区对象指定给_位置变量
gl.VertexAttribute指针(a_位置,2,gl.FLOAT,false,0,0);
//启用对位置变量的赋值
gl.EnableVertexAttributeArray(a_位置);
返回n;
}

WebGL基本上只有两个“真实”函数
gl.DrawArray
gl.drawElements
WebGL API的其余部分几乎都只是为这两个函数设置状态

所以,对于你画的每一件东西,你都需要设置所有需要的状态

for each thing to draw
  1. bind buffers and set attributes
  2. use program
  3. set uniforms and bind texture units
  4. call draw
在初始化过程中,您只执行了两次步骤1。这需要在你画那个特定的东西之前发生


除了和

之外,WebGL基本上只有两个“真实”功能
gl.DrawArray
gl.drawElements
WebGL API的其余部分几乎都只是为这两个函数设置状态

所以,对于你画的每一件东西,你都需要设置所有需要的状态

for each thing to draw
  1. bind buffers and set attributes
  2. use program
  3. set uniforms and bind texture units
  4. call draw
在初始化过程中,您只执行了两次步骤1。这需要在你画那个特定的东西之前发生


以及和

绑定缓冲区->设置属性指针->绘制->repeat@LJᛃ 非常感谢。我了解绑定缓冲区->设置属性指针->绘制->repeat@LJᛃ 非常感谢。我理解