ç°å¨è®©æä»¬ç»ä¹åçæ£æ¹å½¢æ·»å äºä¸ªé¢ä»èå¯ä»¥å建ä¸ä¸ªä¸ç»´çç«æ¹ä½ãæç®åçæ¹å¼å°±æ¯éè¿è°ç¨æ¹æ³ gl.drawElements()
使ç¨é¡¶ç¹æ°ç»åè¡¨æ¥æ¿æ¢ä¹åçéè¿æ¹æ³gl.drawArrays()
ç´æ¥ä½¿ç¨é¡¶ç¹æ°ç»ãèé¡¶ç¹æ°ç»å表éä¿åçå°ä¼è¢«å¼ç¨å°ä¸ä¸ªä¸ªç¬ç«çé¡¶ç¹ã
å ¶å®ç°å¨ä¼åå¨è¿æ ·ä¸ä¸ªé®é¢ï¼æ¯ä¸ªé¢éè¦ 4 个顶ç¹ï¼èæ¯ä¸ªé¡¶ç¹ä¼è¢« 3 个é¢å ±äº«ãæä»¬ä¼å建ä¸ä¸ªå å« 24 个顶ç¹çæ°ç»å表ï¼éè¿ä½¿ç¨æ°ç»ä¸æ æ¥ç´¢å¼é¡¶ç¹ï¼ç¶åæè¿äºç¨äºç´¢å¼ç䏿 ä¼ éç»æ¸²æç¨åºè䏿¯ç´æ¥ææ´ä¸ªé¡¶ç¹æ°æ®ä¼ éè¿å»ï¼è¿æ ·æ¥åå°æ°æ®ä¼ éãé£ä¹ä¹è®¸ä½ å°±ä¼é®ï¼é£ä¹ä½¿ç¨ 8 个顶ç¹å°±å¥½äºï¼ä¸ºä»ä¹è¦ä½¿ç¨ 24 个顶ç¹å¢ï¼è¿æ¯å 为æ¯ä¸ªé¡¶ç¹è½ç¶è¢« 3 个é¢å ±äº«ä½æ¯å®å¨æ¯ä¸ªé¢ä¸éè¦ä½¿ç¨ä¸åçé¢è²ä¿¡æ¯ã24 个顶ç¹ä¸çæ¯ä¸ä¸ªé½ä¼æç¬ç«çé¢è²ä¿¡æ¯ï¼è¿å°±ä¼é ææ¯ä¸ªé¡¶ç¹ä½ç½®é½ä¼æ 3 份坿¬ã
å®ä¹ç«æ¹ä½é¡¶ç¹ä½ç½®é¦å
ï¼æ´æ° initBuffers()
彿°ä¸ä»£ç æ¥åå»ºç«æ¹ä½çé¡¶ç¹ä½ç½®ç¼ååºãç°å¨ç代ç çèµ·æ¥åæ¸²ææ£æ¹å½¢æ¶ç代ç å¾ç¸ä¼¼ï¼åªæ¯æ¯ä¹åçä»£ç æ´é¿å 为ç°å¨æäº 24 个顶ç¹ï¼æ¯ä¸ªé¢ä½¿ç¨ 4 个顶ç¹ï¼ï¼
夿³¨ï¼ å¨âinit-buffers.jsâæä»¶ initPositionBuffer()
彿°ä¸ï¼ç¨ä¸é¢ä»£ç æ¿æ¢ positions
ï¼
const positions = [
// Front face
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0,
];
ç±äºæä»¬ç»é¡¶ç¹æ·»å äº Z åéï¼å æ¤æä»¬éè¦å° vertexPosition
屿§ç numComponents
æ´æ°ä¸º 3ã
夿³¨ï¼ å¨âdraw-scene.jsâæä»¶ setPositionAttribute()
彿°ä¸ï¼å° numComponents
ä» 2
æ¹ä¸º 3
:
ç¶åæä»¬è¿è¦ä¸ºæ¯ä¸ªé¡¶ç¹å®ä¹é¢è²ãä¸é¢ç代ç é¦å 为æ¯ä¸ªé¢å®ä¹é¢è²ï¼ç¶åç¨ä¸ä¸ªå¾ªç¯è¯å¥ä¸ºæ¯ä¸ªé¡¶ç¹å®ä¹é¢è²ä¿¡æ¯ã
夿³¨ï¼ å¨âinit-buffers.jsâæä»¶ initColorBuffer()
彿°ä¸ï¼ç¨ä¸é¢ä»£ç æ¿æ¢ colors
å®ä¹ï¼
const faceColors = [
[1.0, 1.0, 1.0, 1.0], // Front face: white
[1.0, 0.0, 0.0, 1.0], // Back face: red
[0.0, 1.0, 0.0, 1.0], // Top face: green
[0.0, 0.0, 1.0, 1.0], // Bottom face: blue
[1.0, 1.0, 0.0, 1.0], // Right face: yellow
[1.0, 0.0, 1.0, 1.0], // Left face: purple
];
// Convert the array of colors into a table for all the vertices.
var colors = [];
for (var j = 0; j < faceColors.length; ++j) {
const c = faceColors[j];
// Repeat each color four times for the four vertices of the face
colors = colors.concat(c, c, c, c);
}
å®ä¹å
ç´ ï¼ä¸è§å½¢ï¼æ°ç»
æ¢ç¶å·²ç»å建好äºé¡¶ç¹æ°ç»ï¼æ¥ä¸æ¥å°±è¦å建å ç´ ï¼ä¸è§å½¢ï¼æ°ç»äºã
夿³¨ï¼ å¨âinit-buffer.jsâæä»¶ä¸æ·»å ä¸é¢ç彿°ï¼
function initIndexBuffer(gl) {
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
// This array defines each face as two triangles, using the
// indices into the vertex array to specify each triangle's
// position.
const indices = [
0,
1,
2,
0,
2,
3, // front
4,
5,
6,
4,
6,
7, // back
8,
9,
10,
8,
10,
11, // top
12,
13,
14,
12,
14,
15, // bottom
16,
17,
18,
16,
18,
19, // right
20,
21,
22,
20,
22,
23, // left
];
// Now send the element array to GL
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(indices),
gl.STATIC_DRAW,
);
return indexBuffer;
}
indices
æ°ç»å£°ææ¯ä¸ä¸ªé¢é½ä½¿ç¨ä¸¤ä¸ªä¸è§å½¢æ¥æ¸²æãéè¿ç«æ¹ä½é¡¶ç¹æ°ç»çç´¢å¼æå®æ¯ä¸ªä¸è§å½¢çé¡¶ç¹ãé£ä¹è¿ä¸ªç«æ¹ä½å°±æ¯ç± 12 个ä¸è§å½¢ç»æçäºã
夿³¨ï¼ å¨âinit-buffers.jsâæä»¶ initBuffers()
彿°ä¸ï¼æ·»å ä¸é¢çä»£ç æ¿æ¢ä¹åç return
代ç çæ®µï¼
const indexBuffer = initIndexBuffer(gl);
return {
position: positionBuffer,
color: colorBuffer,
indices: indexBuffer,
};
渲æç«æ¹ä½
æ¥ä¸æ¥å°±éè¦å¨ drawScene()
彿°éæ·»å 代ç 使ç¨ç«æ¹ä½é¡¶ç¹ç´¢å¼æ°æ®æ¥æ¸²æè¿ä¸ªç«æ¹ä½äºã代ç éæ·»å äºå¯¹ gl.bindBuffer()
å gl.drawElements()
çè°ç¨ï¼
夿³¨ï¼ å¨ drawScene()
彿°ä¸ï¼gl.useProgram
代ç åæ·»å å¦ä¸ä»£ç ï¼
// Tell WebGL which indices to use to index the vertices
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
夿³¨ï¼ å¨âdraw-scene.jsâæä»¶ drawScene()
彿°ä¸ï¼ç¨ä¸é¢è¿æ®µä»£ç æ¿æ¢ä¹å gl.drawArrays()
ï¼
{
const vertexCount = 36;
const type = gl.UNSIGNED_SHORT;
const offset = 0;
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
}
ç«æ¹ä½çæ¯ä¸ªé¢é½ç± 2 个ä¸è§å½¢ç»æï¼é£å°±æ¯æ¯ä¸ªé¢éè¦ 6 个顶ç¹ï¼æè 说æ»å ± 36 个顶ç¹ï¼å°½ç®¡æè®¸å¤éå¤çãç¶èï¼å ä¸ºç´¢å¼æ°ç»çæ¯ä¸ªå ç´ é½æ¯ç®åçæ´æ°ç±»åï¼æä»¥æ¯ä¸å¸§å¨ç»éè¦ä¼ éç»æ¸²æç¨åºçæ°æ®ä¹ä¸æ¯å¾å¤ã
æåï¼è®©æä»¬æåé squareRotation
æ¿æ¢æ cubeRotation
å¹¶æ·»å X è½´ç第äºä¸ªæè½¬ã
夿³¨ï¼ å¨âwebgl-demo.jsâæä»¶ç头é¨ï¼æåé squareRotation
æ¿æ¢æ cubeRotation
ï¼
夿³¨ï¼ å¨ drawScene()
彿°å£°æä¸ï¼å°åé squareRotation
æ¿æ¢æ cubeRotation
ï¼
function drawScene(gl, programInfo, buffers, cubeRotation) {
夿³¨ï¼ å¨ drawScene()
彿°ä¸ï¼ç¨ä¸é¢ä»£ç æ¿æ¢ä¹åç mat4.rotate
彿°ï¼
mat4.rotate(
modelViewMatrix, // destination matrix
modelViewMatrix, // matrix to rotate
cubeRotation, // amount to rotate in radians
[0, 0, 1],
); // axis to rotate around (Z)
mat4.rotate(
modelViewMatrix, // destination matrix
modelViewMatrix, // matrix to rotate
cubeRotation * 0.7, // amount to rotate in radians
[0, 1, 0],
); // axis to rotate around (Y)
mat4.rotate(
modelViewMatrix, // destination matrix
modelViewMatrix, // matrix to rotate
cubeRotation * 0.3, // amount to rotate in radians
[1, 0, 0],
); // axis to rotate around (X)
夿³¨ï¼ å¨ main()
彿°ä¸ï¼æ¿æ¢ drawScene()
彿°è°ç¨åæ°ä¸ç squareRotation
为 cubeRotation
ï¼
drawScene(gl, programInfo, buffers, cubeRotation);
cubeRotation += deltaTime;
å°ç°å¨ä¸ºæ¢ï¼æä»¬å·²ç»å建äºä¸ä¸ªé¢è²çå¨çå¹¶ä¸ä¼å¨åºæ¯ä¸ç§»å¨åæè½¬çç«æ¹ä½ï¼è¿ä¸å®å¾é ·å§ã
æ¥çå ¨é¨æºä»£ç | 卿°é¡µé¢æå¼ç¤ºä¾
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4