本系列僅作為本人學(xué)習(xí)《WebGL編程指南》這本書的筆記所用

image.jpeg
y軸放大1.5倍,縮放因子為S = 1.5
縮放因子為1.0時圖形不變動,為0.0時會導(dǎo)致圖形縮小到不可見

image.jpeg
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>13繪制三角形-變換矩陣-縮放4x4</title>
<script src="./lib/cuon-matrix.js"></script>
<script src="./lib/cuon-utils.js"></script>
<script src="./lib/webgl-debug.js"></script>
<script src="./lib/webgl-utils.js"></script>
</head>
<body onload="main()">
<canvas id="webgl" width="400" height="400">
不支持canvas的瀏覽器會展示這段文字
</canvas>
<script>
// 頂點(diǎn)著色器程序
//======
// ━━ ━━ ━━ ━━ ━━ ━━
// ┃ x' ┃ ┃ Sx 0 0 0 ┃ ┃ x ┃
// ┃ y' ┃ = ┃ 0 Sy 0 0 ┃ X ┃ y ┃
// ┃ z' ┃ ┃ 0 0 Sz 0 ┃ ┃ z ┃
// ┃ 1 ┃ ┃ 0 0 0 1 ┃ ┃ 1 ┃
// ━━ ━━ ━━ ━━ ━━ ━━
// x' = Sx * x
// y' = Sy * y
// z' = Sz * z
//======
let VSHADER_SOURCE =
`
attribute vec4 a_Position;
uniform mat4 u_xformMatrix;
void main() {
gl_Position = u_xformMatrix * a_Position;
}
`;
// 片元著色器程序
let FSHADER_SOURCE =
`
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;
// ===========
// 設(shè)置在寫x,y,z方向上平移的距離
let Sx = 1.0;
let Sy = 1.5;
let Sz = 1.0;
// ===========
// 主程序
function main() {
let canvas = document.getElementById('webgl');
// 獲取WebGL繪圖上下文
let gl = getWebGLContext(canvas);
if(!gl) {
console.error('無法使用WebGL');
return;
}
// 初始化著色器
if(!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.error('無法使用著色器');
return;
}
// 設(shè)置頂點(diǎn)坐標(biāo)
let nLength = initVertexBuffers(gl);
if ( nLength < 0) {
console.log('無法設(shè)置點(diǎn)的位置');
return;
}
// 主要WebGL中矩陣是列主序的,設(shè)置矩陣
let xformMatrix = new Float32Array([
Sx, 0.0, 0.0, 0.0,
0.0, Sy, 0.0, 0.0,
0.0, 0.0, Sz, 0.0,
0.0, 0.0, 0.0, 1.0,
]);
// 將旋轉(zhuǎn)矩陣傳輸給頂點(diǎn)著色器
let u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix');
gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix);
//=============
// 設(shè)置<canvas> 背景色
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// 清除<canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
// 繪制三角形
gl.drawArrays(gl.TRIANGLES, 0, nLength); // nLength為n個點(diǎn)
}
function initVertexBuffers(gl) {
// 類型化數(shù)組, 用于儲存頂點(diǎn)的坐標(biāo)或顏色。比Array性能更高,不支持push(),和pop()
let vertices = new Float32Array([
0.0, 0.5, -0.5, -0.5, 0.5, -0.5
]); // 坐標(biāo) x1,y1,x2,y2,x3,y3
let n = 3; // 點(diǎn)的個數(shù)
// 1.創(chuàng)建緩沖區(qū)對象
let vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('不能創(chuàng)建緩沖區(qū)對象');
return -1;
}
// 2.將緩沖區(qū)對象綁定到目標(biāo)
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// 3.向緩沖區(qū)對象寫入數(shù)據(jù),不能直接向緩沖區(qū)寫入數(shù)據(jù),只能向綁定的目標(biāo)輸入數(shù)據(jù)
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
// 獲取a_Position變量的存儲位置
let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0) {
console.log('無法獲取 a_Position')
return -1;
}
// 4.將緩沖區(qū)對象分配給attribute變量--所有點(diǎn)數(shù)據(jù)一次性傳入
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
// 5.連接a_Position變量與分配給它的緩沖區(qū)對象,開啟attribute變量
gl.enableVertexAttribArray(a_Position);
return n; // 返回待繪制頂點(diǎn)的數(shù)量, 函數(shù)內(nèi)部發(fā)生錯誤后返回-1
}
</script>
</body>
</html>