WebGL基础(四)

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

内容简介: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基础(四)

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

查看所有标签

猜你喜欢:

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

从零开始做运营

从零开始做运营

张亮 / 中信出版社 / 2015-11-1 / 49.00元

运营是什么?怎样做运营?产品和运营是什么关系?我是否适合从事互联网运营?为什么我做的运营活动收效甚微? 在互联网大热的今天,互联网运营成为一个越来越重要的岗位,事关网站、产品的发展与存亡。很多年轻人带着对互联网的热情投身到这个行业,却发现自己对这个行业所知甚少,对互联网运营更加陌生,甚至有一些有志于从事互联网运营的人,因为对运营缺乏了解而难以确定自己的职业发展方向。本书的出发点就在于此,它将......一起来看看 《从零开始做运营》 这本书的介绍吧!

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

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具