WebGL基础(四)

栏目: 编程工具 · 发布时间: 8年前

内容简介:WebGL基础(四)

本节主要讲解一下颜色的设置。1、WebGL可以使用多个多个缓冲器对象向顶点着色器传输数据。

WebGL基础(四)

使用两个缓冲器对象传输数据

看一个示例程序

// MultiAttributeColor.js (c) 2012 matsuda // Vertex shader program var VSHADER_SOURCE =   'attribute vec4 a_Position;\n' +   'attribute vec4 a_Color;\n' +   'varying vec4 v_Color;\n' + // varying 变量   'void main() {\n' +   '  gl_Position = a_Position;\n' +   '  gl_PointSize = 10.0;\n' +   '  v_Color = a_Color;\n' +  // 将数据传输给片元着色器   '}\n';  // Fragment shader program var FSHADER_SOURCE =   '#ifdef GL_ES\n' +   'precision mediump float;\n' + // Precision qualifier (See Chapter 6)   '#endif GL_ES\n' +   'varying vec4 v_Color;\n' +    // Receive the data from the vertex shader   'void main() {\n' +   '  gl_FragColor = v_Color;\n' +   '}\n';  function main() {   // Retrieve <canvas> element   var canvas = document.getElementById('webgl');    // Get the rendering context for WebGL   var gl = getWebGLContext(canvas);   if (!gl) {     console.log('Failed to get the rendering context for WebGL');     return;   }    // Initialize shaders   if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {     console.log('Failed to intialize shaders.');     return;   }    //    var n = initVertexBuffers(gl);   if (n < 0) {     console.log('Failed to set the vertex information');     return;   }    // Specify the color for clearing <canvas>   gl.clearColor(0.0, 0.0, 0.0, 1.0);    // Clear <canvas>   gl.clear(gl.COLOR_BUFFER_BIT);    // Draw three points   gl.drawArrays(gl.POINTS, 0, n); }  function initVertexBuffers(gl) {   var verticesColors = new Float32Array([     // 顶点坐标和颜色      0.0,  0.5,  1.0,  0.0,  0.0,      -0.5, -0.5,  0.0,  1.0,  0.0,       0.5, -0.5,  0.0,  0.0,  1.0,    ]);   var n = 3; // The number of vertices    // Create a buffer object   var vertexColorBuffer = gl.createBuffer();     if (!vertexColorBuffer) {     console.log('Failed to create the buffer object');     return false;   }    // Write the vertex coordinates and colors to the buffer object   gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);   gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);    var FSIZE = verticesColors.BYTES_PER_ELEMENT;   //获取a_Position存储位置,分配缓冲区并开启   var a_Position = gl.getAttribLocation(gl.program, 'a_Position');   if (a_Position < 0) {     console.log('Failed to get the storage location of a_Position');     return -1;   }   gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 5, 0);   gl.enableVertexAttribArray(a_Position);  // Enable the assignment of the buffer object    // 获取a_Color存储位置,分配缓冲区并开启   var a_Color = gl.getAttribLocation(gl.program, 'a_Color');   if(a_Color < 0) {     console.log('Failed to get the storage location of a_Color');     return -1;   }   gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 5, FSIZE * 2);   gl.enableVertexAttribArray(a_Color);  // Enable the assignment of the buffer object   return n; }

程序中vertexAttribPointer函数:

WebGL基础(四)

vertexAttribPointer定义

var FSIZE = verticesColors.BYTES_PER_ELEMENT;是计算verticesColors中每个元素的大小,

WebGL基础(四)

stride和offset

最终程序的流程如下图所示

WebGL基础(四)

内部运行方式

程序运行效果图如下:

WebGL基础(四)

运行结果

在着色其中定义varying变量,他最后获取的值被传给片元着色器中的同名、同变量类型的变量。

WebGL基础(四)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据驱动:从方法到实践

数据驱动:从方法到实践

桑文锋 / 电子工业出版社 / 2018-3 / 49

本书是从理论到实践的全面且细致的企业数据驱动指南,从作者的百度大数据工作说起,完整还原其从零到一构建百度用户行为大数据处理平台经历。详解大数据本质、理念与现状,围绕数据驱动四环节——采集、建模、分析、指标,深入浅出地讲述企业如何将数据驱动方案落地,并指出数据驱动的价值在于“数据驱动决策”、“数据驱动产品智能”。最后通过互联网金融、电子商务、企业服务、零售四大行业实践,从需求梳理、事件指标设计、数据......一起来看看 《数据驱动:从方法到实践》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具