欢迎投稿

今日深度:

webgl第18课-图形的复合变换,webgl18课图形

webgl第18课-图形的复合变换,webgl18课图形


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

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

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

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

这一节课我们将学习矩阵的高级变换--图形的复合变换

如果看到这里的同学,相信你对于矩阵的运算原理,着色器的定义与数值传递有一定的了解,如果不了解,请从前面第一课开始再复习一遍

后期会给各位童鞋程序流程图说明,现在暂且将就看下!!!

在这特别要提一下,如果设置平移矩阵和旋转矩阵的次序不同,运行结果也会不同

  //在这里先计算旋转矩阵再乘以平移矩阵
  //	与先计算平移矩阵再乘以旋转矩阵的运行结果是不一样的
	//先计算旋转矩阵再乘以平移矩阵
modelMatrix.setRotate(ANGLE, 0, 0, 1);  // 设置模型矩阵为旋转矩阵
modelMatrix.translate(Tx, 0, 0);        // 将模型矩阵乘以平移矩阵

可以将代码注释掉,然后打开:

//先计算平移矩阵再乘以旋转矩阵
//modelMatrix.setTranslate(Tx, 0, 0);
//modelMatrix.rotate(ANGLE, 0, 0, 1);

运行的结果是应该是这样的:

下文代码是先计算旋转矩阵再乘以平移矩阵

运行结果是这样的:

给各位一个图,应该是能看得明白一点:

全部代码:

<!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="RotatedTranslatedTriangle.js"></script>
  </body>
</html>
//顶点着色器,u_ModelMatrix表示一个4*4的矩阵,a_Position表示顶点坐标
//u_ModelMatrix * a_Position两者相乘表示变换后的坐标
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'uniform mat4 u_ModelMatrix;\n' +
  'void main() {\n' +
  '  gl_Position = u_ModelMatrix * a_Position;\n' +
  '}\n';

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

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

  // 为旋转矩阵创建Matrix4对象
  var modelMatrix = new Matrix4();

  // 计算模型矩阵
  var ANGLE = 60.0; // 旋转角
  var Tx = 0.5;     // 平移距离
  //在这里先计算旋转矩阵再乘以平移矩阵
  //	   与先计算平移矩阵再乘以旋转矩阵的运行结果是不一样的
  
//先计算旋转矩阵再乘以平移矩阵
modelMatrix.setRotate(ANGLE, 0, 0, 1);  // 设置模型矩阵为旋转矩阵
modelMatrix.translate(Tx, 0, 0);        // 将模型矩阵乘以平移矩阵

//先计算平移矩阵再乘以旋转矩阵
//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);

  // 设置背景色
  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.3,   -0.3, -0.3,   0.3, -0.3
  ]);
  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;
}

 

 

www.htsjk.Com true http://www.htsjk.com/shujukunews/10209.html NewsArticle webgl第18课-图形的复合变换,webgl18课图形 需要源码可以Q群:828202939 或者点击这里  希望可以和大家一起学习、一起进步!!纯手打!! 书籍是PDF电子档,也在Q群里,所有的课程源代...
相关文章
    暂无相关文章
评论暂时关闭