福田做网站优化乐云seo,互联网公司网站,php做自己的网站,网页传奇开服表WebGL Demohttps://mdn.github.io/dom-examples/webgl-examples/tutorial/sample5/index.html
现在让我们给之前的正方形添加五个面从而可以创建一个三维的立方体。最简单的方式就是通过调用方法 gl.drawElements() 使用顶点数组列表来替换之前的通过方法gl.drawArrays() 直接…WebGL Demohttps://mdn.github.io/dom-examples/webgl-examples/tutorial/sample5/index.html
现在让我们给之前的正方形添加五个面从而可以创建一个三维的立方体。最简单的方式就是通过调用方法 gl.drawElements() 使用顶点数组列表来替换之前的通过方法gl.drawArrays() 直接使用顶点数组。而顶点数组列表里保存着将会被引用到一个个独立的顶点。
其实现在会存在这样一个问题每个面需要 4 个顶点而每个顶点会被 3 个面共享。我们会创建一个包含 24 个顶点的数组列表通过使用数组下标来索引顶点然后把这些用于索引的下标传递给渲染程序而不是直接把整个顶点数据传递过去这样来减少数据传递。那么也许你就会问那么使用 8 个顶点就好了为什么要使用 24 个顶点呢这是因为每个顶点虽然被 3 个面共享但是它在每个面上需要使用不同的颜色信息。24 个顶点中的每一个都会有独立的颜色信息这就会造成每个顶点位置都会有 3 份副本。
定义立方体顶点位置
首先更新 initBuffers() 函数中代码来创建立方体的顶点位置缓存区。现在的代码看起来和渲染正方形时的代码很相似只是比之前的代码更长因为现在有了 24 个顶点每个面使用 4 个顶点
备注 在“init-buffers.js”文件 initPositionBuffer() 函数中用下面代码替换 positions
JSCopy to Clipboard
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 face1.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:
JSCopy to Clipboard
const numComponents 3;定义顶点颜色
然后我们还要为每个顶点定义颜色。下面的代码首先为每个面定义颜色然后用一个循环语句为每个顶点定义颜色信息。
备注 在“init-buffers.js”文件 initColorBuffer() 函数中用下面代码替换 colors 定义
JSCopy to Clipboard
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 facecolors colors.concat(c, c, c, c);
}定义元素三角形数组
既然已经创建好了顶点数组接下来就要创建元素三角形数组了。
备注 在“init-buffer.js”文件中添加下面的函数
JSCopy to Clipboard
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 triangles// position.const indices [0,1,2,0,2,3, // front4,5,6,4,6,7, // back8,9,10,8,10,11, // top12,13,14,12,14,15, // bottom16,17,18,16,18,19, // right20,21,22,20,22,23, // left];// Now send the element array to GLgl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(indices),gl.STATIC_DRAW,);return indexBuffer;
}indices 数组声明每一个面都使用两个三角形来渲染。通过立方体顶点数组的索引指定每个三角形的顶点。那么这个立方体就是由 12 个三角形组成的了。
备注 在“init-buffers.js”文件 initBuffers()函数中添加下面的代码替换之前的 return 代码片段
JSCopy to Clipboard
const indexBuffer initIndexBuffer(gl);return {position: positionBuffer,color: colorBuffer,indices: indexBuffer,
};渲染立方体
接下来就需要在 drawScene() 函数里添加代码使用立方体顶点索引数据来渲染这个立方体了。代码里添加了对 gl.bindBuffer() 和 gl.drawElements()的调用
备注 在 drawScene() 函数中gl.useProgram 代码前添加如下代码
JSCopy to Clipboard
// Tell WebGL which indices to use to index the vertices
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);备注 在“draw-scene.js”文件 drawScene() 函数中用下面这段代码替换之前 gl.drawArrays()
JSCopy to Clipboard
{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
JSCopy to Clipboard
let cubeRotation 0.0;备注 在 drawScene() 函数声明中将变量 squareRotation 替换成 cubeRotation
JSCopy to Clipboard
function drawScene(gl, programInfo, buffers, cubeRotation) {备注 在 drawScene() 函数中用下面代码替换之前的 mat4.rotate 函数
JSCopy to Clipboard
mat4.rotate(modelViewMatrix, // destination matrixmodelViewMatrix, // matrix to rotatecubeRotation, // amount to rotate in radians[0, 0, 1],
); // axis to rotate around (Z)
mat4.rotate(modelViewMatrix, // destination matrixmodelViewMatrix, // matrix to rotatecubeRotation * 0.7, // amount to rotate in radians[0, 1, 0],
); // axis to rotate around (Y)
mat4.rotate(modelViewMatrix, // destination matrixmodelViewMatrix, // matrix to rotatecubeRotation * 0.3, // amount to rotate in radians[1, 0, 0],
); // axis to rotate around (X)备注 在 main() 函数中替换 drawScene() 函数调用参数中的 squareRotation 为 cubeRotation
JSCopy to Clipboard
drawScene(gl, programInfo, buffers, cubeRotation);
cubeRotation deltaTime;到现在为止我们已经创建了一个颜色生动的并且会在场景中移动和旋转的立方体这一定很酷吧。 文章转载自: http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.pbknh.cn.gov.cn.pbknh.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.kpgms.cn.gov.cn.kpgms.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn