webgl第34課-模型矩陣、視圖矩陣、投影矩陣融合

需要電子檔書籍可以Q羣:828202939   希望可以和大家一起學習、一起進步!!

所有的課程源代碼在我上傳的資源裏面,本來想設置開源,好像不行!博客和專欄同步!

如有錯別字或有理解不到位的地方,可以留言或者加微信15250969798,在下會及時修改!!!!!

上一節課我們學習了 透視投影矩陣之不同位置的彩色三角形

這一節課我們將學習  模型矩陣、視圖矩陣、投影矩陣融合

上節課我們學習的不同位置的三角形是每個三角形都通過手動設置的,一旦三角形的數量增多的話會變得非常麻煩和枯燥!所以接下來我們來對它進行優化下!

運行結果:

使用多個矩陣融合的方式來實現不同位置的彩色三角形!這樣做的好處就是我們可以已模型矩陣爲基礎去通過一系列的轉化,優化管理的對象和代碼

代碼:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>webgl第33課-透視投影矩陣之不同位置的彩色三角形</title>
  </head>

  <body onload="main()">
    <canvas id="webgl" width="400" height="400">
    Please use a browser that supports "canvas"
    </canvas>

    <script src="../lib/webgl-utils.js"></script>
    <script src="../lib/webgl-debug.js"></script>
    <script src="../lib/cuon-utils.js"></script>
    <script src="../lib/cuon-matrix.js"></script>
    <script src="PerspectiveView_mvpMatrix.js"></script>
  </body>
</html>
// PerspectiveView_mvpMatrix.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'attribute vec4 a_Color;\n' +
  'uniform mat4 u_MvpMatrix;\n' +
  'varying vec4 v_Color;\n' +
  'void main() {\n' +
  '  gl_Position = u_MvpMatrix * a_Position;\n' +
  '  v_Color = a_Color;\n' +
  '}\n';

// Fragment shader program
var FSHADER_SOURCE =
  '#ifdef GL_ES\n' +
  'precision mediump float;\n' +
  '#endif\n' +
  'varying vec4 v_Color;\n' +
  'void main() {\n' +
  '  gl_FragColor = v_Color;\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 vertex information');
    return;
  }

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

  //獲取 u_MvpMatrix的存儲位置
  var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
  if (!u_MvpMatrix) { 
    console.log('Failed to get the storage location of u_MvpMatrix');
    return;
  }

  var modelMatrix = new Matrix4(); // 模型矩陣
  var viewMatrix = new Matrix4();  // 視圖矩陣
  var projMatrix = new Matrix4();  // 投影矩陣
  var mvpMatrix = new Matrix4();   // 模型視圖投影矩陣

  // Calculate the model, view and projection matrices
  modelMatrix.setTranslate(0.75, 0, 0);
  viewMatrix.setLookAt(0, 0, 5, 0, 0, -100, 0, 1, 0);
  projMatrix.setPerspective(30, canvas.width/canvas.height, 1, 100);
  // 計算模型視圖投影矩陣
  mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
  // 將模型視圖投影矩陣傳給u_MvpMatrix變量
  gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);

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

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

 // 爲另一對三角形準備模型矩陣
  modelMatrix.setTranslate(-0.75, 0, 0);
  // 計算模型視圖投影矩陣
  mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
  // 將模型視圖投影矩陣傳給u_MvpMatrix變量
  gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);

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

function initVertexBuffers(gl) {
  var verticesColors = new Float32Array([
    // 頂點座標和顏色。
     0.0,  1.0,  -4.0,  0.4,  1.0,  0.4, // 綠色三角形在最後
    -0.5, -1.0,  -4.0,  0.4,  1.0,  0.4,
     0.5, -1.0,  -4.0,  1.0,  0.4,  0.4, 

     0.0,  1.0,  -2.0,  1.0,  1.0,  0.4, // 黃色三角形在中間
    -0.5, -1.0,  -2.0,  1.0,  1.0,  0.4,
     0.5, -1.0,  -2.0,  1.0,  0.4,  0.4, 

     0.0,  1.0,   0.0,  0.4,  0.4,  1.0,  // 藍色三角形在前面
    -0.5, -1.0,   0.0,  0.4,  0.4,  1.0,
     0.5, -1.0,   0.0,  1.0,  0.4,  0.4, 
  ]);
  var n = 9;

  // 創建緩衝區對象並對它進行一些列操作
  var vertexColorBuffer = gl.createBuffer();  
  if (!vertexColorBuffer) {
    console.log('Failed to create the buffer object');
    return -1;
  }

  // Write the vertex information and enable it
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);

  var FSIZE = verticesColors.BYTES_PER_ELEMENT;

  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, 3, gl.FLOAT, false, FSIZE * 6, 0);
  gl.enableVertexAttribArray(a_Position);

  var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
  if(a_Color < 0) {
    console.log('Failed to get the storage location of a_Color');
    return -1;
  }
  gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 6, FSIZE * 3);
  gl.enableVertexAttribArray(a_Color);

  return n;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章