webgl第15课-矩阵变换之平移

需要源码可以Q群:828202939 或者点击这里  希望可以和大家一起学习、一起进步!!纯手打!!

书籍是PDF电子档,也在Q群里,所有的课程源代码在我上传的资源里面,本来想设置开源,好像不行!

如有错别字或有理解不到位的地方,可以留言或者加微信15250969798,在下会及时修改!!!!!

上一节课我们学习了矩阵的变换之旋转

这一节课我们将学习矩阵的变换之平移

在这之前,你得了解前面的一些内容:

1.了解什么是平移矩阵、矢量、齐次坐标

2.了解矩阵相乘的原理,3*3矩阵和4*4矩阵的阶数怎么统一

3.理解前面第12课中的平移变换的数学表达式

4.理解三角函数的一些基础知识

平移和旋转的整个流程架构基本是一样的,我们在上节课的基础上只要改动几个地方就可以实现矩阵的平移变换

注意看JS代码中注释掉的代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>三角形的矩阵变换之平移</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="TtanslatedTriangle_Matrix.js"></script>
  </body>
</html>
//顶点着色器,u_xformMatrix表示一个4*4的矩阵,a_Position表示顶点坐标
//u_xformMatrix * a_Position两者相乘表示变换后的坐标
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'uniform mat4 u_xformMatrix;\n' +
  'void main() {\n' +
  '  gl_Position = u_xformMatrix * a_Position;\n' +
  '}\n';

// 片元着色器
var FSHADER_SOURCE =
  'void main() {\n' +
  '  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
  '}\n';

// 旋转角度
//var ANGLE = 45.0;
//平移变量
var Tx=0.5, Ty=0.5, Tz=0.0;

function main() {
  // 获取canvas元素
  var canvas = document.getElementById('webgl');

  // 获取canvas上下文
  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 intialize shaders.');
    return;
  }
 
  // 设置顶点位置
  var n = initVertexBuffers(gl);
  if (n < 0) {
    console.log('Failed to set the positions of the vertices');
    return;
  }

  // 创建旋转矩阵
//var radian = Math.PI * ANGLE / 180.0; // 角度转弧度
//var cosB = Math.cos(radian), sinB = Math.sin(radian);

  // 注意webgl中是矩阵是列主序的,这里有相关的公式表示各种类型的矩阵
  var xformMatrix = new Float32Array([
//   cosB, sinB, 0.0, 0.0,
//  -sinB, cosB, 0.0, 0.0,
//    0.0,  0.0, 1.0, 0.0,
//    0.0,  0.0, 0.0, 1.0
//平移矩阵
			1.0 , 0.0,  0.0, 0.0,
			0.0 , 1.0,  0.0, 0.0,
			0.0 , 0.0,  1.0, 0.0,
			Tx  , Ty ,  Tz , 1.0,
			
  ]);

  // 将旋转矩阵传输给顶点着色器
  var u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix');
  if (!u_xformMatrix) {
    console.log('Failed to get the storage location of u_xformMatrix');
    return;
  }
  gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix);

  // 设置背景色
  gl.clearColor(0, 0, 0, 1);

  // 清空 <canvas>
  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; // 顶点数

  // 创建缓冲区对象
  var vertexBuffer = gl.createBuffer();
  if (!vertexBuffer) {
    console.log('Failed to create the buffer object');
    return false;
  }

  // 绑定缓冲区对象
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  // 将数据写入缓冲区对象以供着色器使用
  gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
	//获取attribute的位置
  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;
  }
  // 将缓冲区对象分配给attribute变量,这里只有平面的顶点xy,所谓分量为2,第三四分量会默认为0.0,1.0
  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);

  // 开启attribute变量
  gl.enableVertexAttribArray(a_Position);

  return n;
}

运行结果::

猜你喜欢

转载自blog.csdn.net/weixin_39452320/article/details/81210122