在WebGL中使用drawElements繪圖
程式碼摘自《WebGL Beginners Guide》一書第二章的ch2_Square.html
<html>
<head>
<title>WebGL Beginner's Guide - Chapter 2 - Rendering a Square</title>
<!-- CSS Styles //-->
<link href='css/style.css' type='text/css' rel='stylesheet'>
<link href='css/desert.css' type='text/css' rel='stylesheet'/>
<link href='css/smoothness/jquery-ui-1.8.13.custom.css' type='text/css' rel='stylesheet' />
<!-- JavaScript Libraries //-->
<script type='text/javascript' src='js/jquery-1.5.1.min.js'></script>
<script type='text/javascript' src='js/jquery-ui-1.8.13.custom.min.js'></script>
<script type='text/javascript' src='js/prettify.js'></script>
<script type='text/javascript' src='js/utils.js'></script>
<script type='text/javascript' src='js/codeview.js'></script>
<!-- Fragment Shader //-->
<script id="shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
void main(void) {
gl_FragColor = vec4(0.5,0.5,1.0, 1.0);
}
</script>
<!-- Vertex Shader //-->
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
void main(void) {
gl_Position = vec4(aVertexPosition,1.0);
}
</script>
<script id="code-js" type="text/javascript">
var gl = null; // WebGL context
var prg = null; // The program (shaders)
var c_width = 0; // Variable to store the width of the canvas
var c_height = 0; // Variable to store the height of the canvas
var squareVertexBuffer = null; //The vertex buffer for the square
var squareIndexBuffer = null; // The index buffer for the square
var indices = []; //JavaScript array to store the indices of the square
var vertices = []; //JavaScript array to store the vertices of the square
/*
* The program contains a series of instructions that tell the Graphic Processing Unit (GPU)
* what to do with every vertex and fragment that we pass it. (more about this on chapter 3)
* The vertex shader and the fragment shader together are called the program.
*/
function initProgram() {
var fgShader = utils.getShader(gl, "shader-fs");
var vxShader = utils.getShader(gl, "shader-vs");
prg = gl.createProgram();
gl.attachShader(prg, vxShader);
gl.attachShader(prg, fgShader);
gl.linkProgram(prg);
if (!gl.getProgramParameter(prg, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(prg);
//The following lines allow us obtaining a reference to the uniforms and attributes defined in the shaders.
//This is a necessary step as the shaders are NOT written in JavaScript but in a
//specialized language called GLSL. More about this on chapter 3.
prg.vertexPosition = gl.getAttribLocation(prg, "aVertexPosition");
}
/*
* Creates the buffers that contain the geometry of the square
*
* #0 (-0.5,0.5) +--------------+ (0.5,0.5) #3
* | |
* | |
* | .(0,0) |
* | |
* | |
* #1(-0.5,-0.5) +--------------+ (0.5,-0.5) #2
*/
function initBuffers() {
vertices = [
-0.5,0.5,0.0, //Vertex 0
-0.5,-0.5,0.0, //Vertex 1
0.5,-0.5,0.0, //Vertex 2
0.5,0.5,0.0]; //Vertex 3
indices = [3,2,1,3,1,0];
//The following code snippet creates a vertex buffer and binds the vertices to it
squareVertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
//The following code snippet creates a vertex buffer and binds the indices to it
squareIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
/**
* Draws the scene
*/
function drawScene(){
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.viewport(0,0,c_width, c_height);
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexBuffer);
gl.vertexAttribPointer(prg.aVertexPosition, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(prg.vertexPosition);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT,0);
}
/**
* Render Loop
*/
function renderLoop() {
utils.requestAnimFrame(renderLoop);
drawScene();
}
/**
* Executes the WebGL application
* This function is invoked on the onLoad event of the webpage.
*/
function runWebGLApp(){
//Obtains a WebGL context
gl = utils.getGLContext('canvas-element-id');
//Initializes the program (shaders). More about this on chapter 3!
initProgram();
//Initializes the buffers that we are going to use to draw the square (vertex buffer and index buffer)
initBuffers();
//Renders the square!
renderLoop();
}
</script>
</head>
<body onLoad='runWebGLApp()'>
<div id='top'>
<h1>WebGL Beginner's Guide - Chapter 2</h1>
<h2>Rendering a Square</h2>
<div id='logo-packt'><img src='packt.gif'/></div>
<p>WebGL uses buffers to store and process vertex and index data. The mechanism is the same whether we are rendering
a simple object like a square or a racing car as we will see later on.</p>
</div>
<div id='contents'>
<div id='canvasContainer'>
<canvas id='canvas-element-id' width='480' height='400'>
Your browser does not support the HTML5 canvas element.
</canvas>
</div>
</div>
<div id='bottom'>
</div>
<script>cview.run();</script>
</html>
對attribute進行賦值時,在initShaders時,首先要通過gl.getAttribLocation(shader,"attributeName")獲取attribute變數的地址,然後通過呼叫gl.enableVertexAtribArray(attributeLocation)來啟用該attribute變數(或者像上面的程式碼一樣在給attribute賦值之後再啟用它)。上面的操作只需要呼叫一次即可。然後在迴圈drawScene的時候,如果buffer之前就已經通過gl.bufferData賦值(比如在initialBuffers()的時候對其賦值),並且buffer的值沒有改變(比如座標值未變)那麼就不需要在每次渲染的時候再通過bufferData在對其賦值,只需在每次渲染的時候,通過gl.bindBuffer和gl.vertexAttribPointer將buffer的值傳遞給著色器中的attribute變數即可,對shader中的attribute變數進行賦值。
相關文章
- JavaScript WebGL 使用圖片JavaScriptWeb
- 在UnityUI中繪製線狀統計圖UnityUI
- 在 ECharts GL 中繪製三維地圖Echarts地圖
- JavaScript WebGL 使用圖片疑惑點JavaScriptWeb
- canvas在H5中的繪圖總結CanvasH5繪圖
- 在Power BI Desktop中使用Python繪圖Python繪圖
- 基於HTML5 WebGL 將拓撲圖和圖表繪製在 3D 六面體上HTMLWeb3D
- JavaScript WebGL 繪製一個面JavaScriptWeb
- WebGL之繪製三維地球Web
- 瀏覽器端繪圖技術SVG VML Canvas WebGL介紹瀏覽器繪圖SVGCanvasWeb
- [WebGL入門]三,3D繪圖的基礎知識Web3D繪圖
- 在 Maui 中自繪元件1:繪製UI元件
- 使用索引繪圖(轉)索引繪圖
- 使用joinjs繪製流程圖(五)-流程圖繪製JS流程圖
- 在搜狗瀏覽器中啟用WebGL瀏覽器Web
- WebGL半透明物體的繪製Web
- JavaScript WebGL 繪製一條直線JavaScriptWeb
- Android 中 Canvas 繪圖之 Shader 使用圖文詳解AndroidCanvas繪圖
- Android中Canvas繪圖之Shader使用圖文詳解AndroidCanvas繪圖
- CAD繪圖工具中的繪線命令繪圖
- 使用java繪圖類Graphics繪製圓圈Java繪圖
- 使用css繪製圖形CSS
- 使用Drawing 類繪圖繪圖
- [WebGL入門]十四,繪製多邊形Web
- 3D繪圖:18個WebGL開源引擎框架及Web 3D圖形庫3D繪圖Web框架
- 視覺化學習:使用WebGL繪製圓形,實現色盤視覺化Web
- python使用xlsxwriter繪圖Python繪圖
- word 的使用(七) —— 繪圖工具繪圖
- 使用CreateJS繪製圖形JS
- vue使用Echarts繪製地圖VueEcharts地圖
- [C#] 在控制檯繪圖, 如:放置影像, 繪製線條C#繪圖
- 在WebGL中使用GLSL實現光線追蹤Web
- 使用 Flutter 繪製圖表(二)餅狀圖?Flutter
- 使用 Flutter 繪製圖表(一)柱狀圖?Flutter
- ATL中使用點陣圖資源繪圖繪圖
- Android 中 Canvas 繪圖之 PorterDuffXfermode 使用及工作原理詳解AndroidCanvas繪圖
- Android中Canvas繪圖之PorterDuffXfermode使用及工作原理詳解AndroidCanvas繪圖
- 開始使用SmartDraw繪製開發中的各種圖形