试图获得每个三角形顶点多种颜色

时间:2018-10-29 21:39:00

标签: javascript html5 webgl

这是我的JavaScript代码:

// TranslatedTriangle.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'attribute vec3 a_Color;\n' +
  'varying vec3 v_Color;\n' +
  'uniform vec4 u_Translation;\n' +
  'void main() {\n' +
  '  a_Color = v_Color;\n' +
  '  gl_Position = a_Position + u_Translation;\n' +
  '}\n';

// Fragment shader program
var FSHADER_SOURCE =
  'precision mediump float;\n' +
  'varying vec3 v_Color;\n' +
  'void main() {\n' +
  '  gl_FragColor = vec4(v_Color, 1.0);\n' +
  '}\n';

// The translation distance for x, y, and z direction
var Tx = 0.5, Ty = 0.5, Tz = 0.0;

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

  // Pass the translation distance to the vertex shader
  var u_Translation = gl.getUniformLocation(gl.program, 'u_Translation');
  if (!u_Translation) {
    console.log('Failed to get the storage location of u_Translation');
    return;
  }
  gl.uniform4f(u_Translation, Tx, Ty, Tz, 0.0);

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

function initVertexBuffers(gl) {

//  // Triangle Verticies
  var vertices = new Float32Array(
  [ // x, y             r, g, b
    0.0, 0.5,           1.0, 0.0, 0.0,
    -0.5, -0.5,         0.0, 1.0, 0.0, 
    0.5, -0.5,          0.0, 0.0, 1.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;
  }
  // 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);

//  // Assign the buffer object to the attribute 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;
  }
  // Assign the buffer object to the attribute variable
  var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
  if (a_Color < 0) {
    console.log('Failed to get the storage location of a_Position');
    return -1;
  }

  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 0);
  gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 2 * Float32Array.BYTES_PER_ELEMENT);

  // Enable the assignment to a_Position variable
  gl.enableVertexAttribArray(a_Position);
  gl.enableVertexAttribArray(a_Color);
  return n;
}

我不确定出了什么问题。我收到“无法编译着色器:错误:0:6:'assign':1值必需(无法修改属性“ a_Color”)

任何人对我的问题可能有什么见解?我使用此YouTube视频https://www.youtube.com/watch?v=kB0ZVUrI4Aw作为指南。我试图了解如何在每个顶点上获得多种颜色,但是一旦弄清楚了,我就需要制作4个三角形,每个三角形都有不同的颜色。

0 个答案:

没有答案
相关问题