webgl入门实例-11模型矩阵 (Model Matrix)基本概念

WebGL 模型矩阵 (Model Matrix)

在WebGL和3D图形编程中,模型矩阵(Model Matrix)是将物体从局部坐标系(模型空间)转换到世界坐标系的关键变换矩阵。

什么是模型矩阵?

模型矩阵是一个4x4的矩阵,用于表示物体在世界空间中的位置、旋转和缩放。它执行以下转换:

  • 将顶点从模型局部坐标空间转换到世界坐标空间
  • 应用物体的平移(位置)、旋转和缩放变换

模型矩阵的组成

通常,模型矩阵是多个基本变换矩阵的组合:

复制代码
ModelMatrix = TranslationMatrix × RotationMatrix × ScaleMatrix

1. 平移矩阵 (Translation)

将物体移动到世界空间中的特定位置:

javascript 复制代码
// 创建平移矩阵 (tx, ty, tz)
function translate(tx, ty, tz) {
  return [
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    tx, ty, tz, 1
  ];
}

2. 旋转矩阵 (Rotation)

绕X、Y或Z轴旋转物体:

javascript 复制代码
// 绕X轴旋转 (角度)
function rotateX(angle) {
  const c = Math.cos(angle);
  const s = Math.sin(angle);
  return [
    1, 0, 0, 0,
    0, c, s, 0,
    0, -s, c, 0,
    0, 0, 0, 1
  ];
}

// 绕Y轴旋转 (角度)
function rotateY(angle) {
  const c = Math.cos(angle);
  const s = Math.sin(angle);
  return [
    c, 0, -s, 0,
    0, 1, 0, 0,
    s, 0, c, 0,
    0, 0, 0, 1
  ];
}

// 绕Z轴旋转 (角度)
function rotateZ(angle) {
  const c = Math.cos(angle);
  const s = Math.sin(angle);
  return [
    c, s, 0, 0,
    -s, c, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1
  ];
}

3. 缩放矩阵 (Scale)

改变物体的大小:

javascript 复制代码
// 创建缩放矩阵 (sx, sy, sz)
function scale(sx, sy, sz) {
  return [
    sx, 0, 0, 0,
    0, sy, 0, 0,
    0, 0, sz, 0,
    0, 0, 0, 1
  ];
}

在WebGL中使用模型矩阵

  1. 创建模型矩阵:
javascript 复制代码
const modelMatrix = mat4.create(); // 使用gl-matrix库
mat4.identity(modelMatrix);
mat4.translate(modelMatrix, modelMatrix, [x, y, z]);
mat4.rotateX(modelMatrix, modelMatrix, angleX);
mat4.rotateY(modelMatrix, modelMatrix, angleY);
mat4.rotateZ(modelMatrix, modelMatrix, angleZ);
mat4.scale(modelMatrix, modelMatrix, [sx, sy, sz]);
  1. 将模型矩阵传递给着色器:
javascript 复制代码
const uModelMatrix = gl.getUniformLocation(program, 'uModelMatrix');
gl.uniformMatrix4fv(uModelMatrix, false, modelMatrix);
  1. 在顶点着色器中使用:
glsl 复制代码
uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;

void main() {
  gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(aPosition, 1.0);
}

注意事项

  1. 矩阵乘法顺序很重要 - WebGL/OpenGL使用列主序矩阵,变换是从右向左应用的。

  2. 对于复杂场景,通常会有多个模型矩阵,每个物体一个。

  3. 使用矩阵库如gl-matrix可以简化矩阵操作:

javascript 复制代码
import {mat4} from 'gl-matrix';
  1. 性能考虑:在JavaScript中频繁创建和修改矩阵可能会影响性能,考虑重用矩阵对象。

模型矩阵是WebGL渲染管线中模型-视图-投影矩阵(MVP)三部曲的第一部分,是将3D物体放置到3D世界中的基础。

相关推荐
爱学习的小鱼gogo2 小时前
python 单词搜索(回溯-矩阵-字符串-中等)含源码(二十)
开发语言·数据结构·python·矩阵·字符串·回溯·递归栈
熬了夜的程序员5 小时前
【LeetCode】82. 删除排序链表中的重复元素 II
数据结构·算法·leetcode·链表·职场和发展·矩阵·深度优先
前端小L6 小时前
动态规划的“降维”艺术:二维矩阵中的建筑奇迹——最大矩形
线性代数·矩阵
xhload3d7 小时前
WebGL/Canvas 内存泄露分析
低代码·3d·html5·webgl·数字孪生·可视化·软件开发·工业互联网·内存泄漏·轻量化·技术应用·hightopo
小龙8 小时前
【理论知识】Q/K/V权重矩阵学习笔记
矩阵·大模型·transformer·多头注意力机制·理论基础
无限进步_8 小时前
【C语言】在矩阵中高效查找数字的算法解析
c语言·开发语言·数据结构·c++·其他·算法·矩阵
张晓~183399481211 天前
碰一碰发抖音源码技术搭建部署方案
线性代数·算法·microsoft·矩阵·html5
dxnb221 天前
Datawhale25年10月组队学习:math for AI+Task3线性代数(下)
人工智能·学习·线性代数
woshihonghonga1 天前
PyTorch矩阵乘法函数区别解析与矩阵高级索引说明——《动手学深度学习》3.6.3、3.6.4和3.6.5 (P79)
人工智能·pytorch·python·深度学习·jupyter·矩阵
CLubiy1 天前
【研究生随笔】Pytorch中的线性代数(微分)
人工智能·pytorch·深度学习·线性代数·梯度·微分