WebGL基礎(三)

這一節主要講一下WebGL的基本變換,平移和旋轉,主要運用了cuon-matrix.js庫函數,其具體方法有以下:

函數

下面講解一個簡單的例子,這個例子實現一個三角形先進行一次平移,在進行一次旋轉,先看代碼:

// TranslatedRotatedTriangle.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'uniform mat4 u_ModelMatrix;\n' +
  'void main() {\n' +
  '  gl_Position = u_ModelMatrix * 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;
  }

  // Create Matrix4 object for model transformation
  var modelMatrix = new Matrix4();

  // Calculate a model matrix
  var ANGLE = 60.0; // 旋轉角
  var Tx = 0.5;     // 平移距離
  modelMatrix.setTranslate(Tx, 0, 0);  // 平移
  modelMatrix.rotate(ANGLE, 0, 0, 1);  // 旋轉

  //將模型矩陣傳輸給頂點著色器
  var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
  if (!u_ModelMatrix) {
    console.log('Failed to get the storage location of u_xformMatrix');
    return;
  }
  gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);

  // 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) {
  var vertices = new Float32Array([
    0, 0.3,   -0.3, -0.3,   0.3, -0.3
  ]);
  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 false;
  }

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

程序中先使用modelMatrix.setTranslate(Tx, 0, 0)將modelMatrix設置成平移矩陣,然后使用modelMatrix.rotate(ANGLE, 0, 0, 1)繞Z軸旋轉ANGLE角度,計算出來的矩陣放入到modelMatrix中,然后使用gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements)傳遞給模型著色器中的變量,最后顯示出來效果如下:

平移旋轉圖像
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 變換(Transformations) 我們可以嘗試著在每一幀改變物體的頂點并且重設緩沖區從而使他們移動,但這太繁...
    IceMJ閱讀 4,194評論 0 1
  • >*很不幸,沒人能告訴你母體是什么,你只能自己體會* --駭客帝國 在第四章“可視效果”中,我們研究了一些增強圖層...
    夜空下最亮的亮點閱讀 1,675評論 0 2
  • WebGL從2012年開始接觸,后面因為開始專注前端其他方面的事情,慢慢地就把它給遺忘。最近前端開始又流行起繪畫制...
    我不是傳哥閱讀 4,137評論 1 22
  • 5.1 仿射變換 5.1.1 仿射變換基礎 UIView的transform屬性是一個CGAffineTransf...
    默默_David閱讀 1,395評論 0 1
  • 下過雨的田野,是甜甜的田野。我想在這個時間,在山頭斜照相迎我的夕陽蒸發了雨水時,我在田野里奔跑。任和夕陽纏綿的風吹...
    心中的孤島閱讀 612評論 1 1