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基础(四)

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

查看所有标签

猜你喜欢:

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

网站搜索设计

网站搜索设计

[美] Shari Thurow、[美] Nick Musica / 向怡宁 / 人民邮电出版社 / 2011-4 / 35.00

本书是提高网站搜索可用性的红宝书,它将SEO 和Web 可用性两个不同领域的知识融会贯通,详细阐述了用户的各种搜索行为和行为背后的真实意图,以及网站如何迎合用户心理,以便提供令其满意的内容,进而实现网站所有者的商业目标。 本书不仅仅是SEO 专业人员和Web 可用性人员的参考必备,同时更可为网络文案、设计开发人员、营销专员以及网站所有者、管理者等其他Web 领域从业人员拓展视野、补强技能。一起来看看 《网站搜索设计》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换