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

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

查看所有标签

猜你喜欢:

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

实战Java高并发程序设计

实战Java高并发程序设计

葛一鸣、郭超 / 电子工业出版社 / 2015-10-1 / CNY 69.00

在过去单核CPU时代,单任务在一个时间点只能执行单一程序,随着多核CPU的发展,并行程序开发就显得尤为重要。 《实战Java高并发程序设计》主要介绍基于Java的并行程序设计基础、思路、方法和实战。第一,立足于并发程序基础,详细介绍Java中进行并行程序设计的基本方法。第二,进一步详细介绍JDK中对并行程序的强大支持,帮助读者快速、稳健地进行并行程序开发。第三,详细讨论有关“锁”的优化和提高......一起来看看 《实战Java高并发程序设计》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具