【WebGL】《WebGL编程指南》读书笔记——第5章
一、前言
终于到了第五章了,貌似开始越来越复杂了。
二、正文
Example1:使用一个缓冲区去赋值多个顶点数据(包含坐标及点大小)
function initVertexBuffers(gl) {
var verticesSizes = new Float32Array([
0.0, 0.5, 10.0,
-0.5, -0.5, 20.0,
0.5, -0.5, 30.0
]);
var n = ;
var vertexSizeBuffer = gl.createBuffer();
if (!vertexSizeBuffer) {
console.log('Failed to create the buffer object');
return -;
}
gl.bindBuffer(gl.ARRAY_BUFFER, vertexSizeBuffer);
gl.bufferData(gl.ARRAY_BUFFER, verticesSizes, gl.STATIC_DRAW);
var FSIZE = verticesSizes.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < ) {
console.log('Failed to get the storage location of a_Position');
return -;
}
gl.vertexAttribPointer(a_Position, , gl.FLOAT, false, FSIZE * , );
gl.enableVertexAttribArray(a_Position);
var a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize');
if(a_PointSize < ) {
console.log('Failed to get the storage location of a_PointSize');
return -;
}
gl.vertexAttribPointer(a_PointSize, , gl.FLOAT, false, FSIZE * , FSIZE * );
gl.enableVertexAttribArray(a_PointSize);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return n;
}
Example2:使用varying变量从顶点着色器传输颜色信息给片元着色器
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'attribute vec4 a_Color;\n' + //attribute变量
'varying vec4 v_Color;\n' + // varying变量
'void main() {\n' +
' gl_Position = a_Position;\n' +
' gl_PointSize = 10.0;\n' +
' v_Color = a_Color;\n' + // 将attribute变量赋给varying变量
'}\n'; var FSHADER_SOURCE =
'#ifdef GL_ES\n' +
'precision mediump float;\n' +
'#endif GL_ES\n' +
'varying vec4 v_Color;\n' + //同名varying变量
'void main() {\n' +
' gl_FragColor = v_Color;\n' + //!!!!!
'}\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 = ;
var vertexColorBuffer = gl.createBuffer();
if (!vertexColorBuffer) {
console.log('Failed to create the buffer object');
return false;
}
gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);
var FSIZE = verticesColors.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < ) {
console.log('Failed to get the storage location of a_Position');
return -;
}
gl.vertexAttribPointer(a_Position, , gl.FLOAT, false, FSIZE * , );
gl.enableVertexAttribArray(a_Position);
var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
if(a_Color < ) {
console.log('Failed to get the storage location of a_Color');
return -;
}
gl.vertexAttribPointer(a_Color, , gl.FLOAT, false, FSIZE * , FSIZE * );
gl.enableVertexAttribArray(a_Color);
return n;
}
Example3:纹理(将图片的纹理赋给webgl对象)
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'attribute vec2 a_TexCoord;\n' + // 声明一个attribute变量
'varying vec2 v_TexCoord;\n' + // 声明一个varying变量
'void main() {\n' +
' gl_Position = a_Position;\n' +
' v_TexCoord = a_TexCoord;\n' + // attribute变量赋给varying变量
'}\n'; var FSHADER_SOURCE =
'#ifdef GL_ES\n' +
'precision mediump float;\n' +
'#endif\n' +
'uniform sampler2D u_Sampler;\n' +
'varying vec2 v_TexCoord;\n' +
'void main() {\n' + // texture2D(sampler2D sampler, vec2 coord)
// (纹理单元编号,纹理坐标) 这里是赋值的关键
' gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n' +
'}\n'; function main() {
var canvas = document.getElementById('webgl');
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
} // 设置顶点缓存
var n = initVertexBuffers(gl);
if (n < ) {
console.log('Failed to set the vertex information');
return;
} gl.clearColor(0.0, 0.0, 0.0, 1.0); // 设置纹理
if (!initTextures(gl, n)) {
console.log('Failed to intialize the texture.');
return;
}
} function initVertexBuffers(gl) {
var verticesTexCoords = new Float32Array([
//webgl顶点坐标, 纹理坐标相应点
-0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, 0.0, 0.0,
0.5, 0.5, 1.0, 1.0,
0.5, -0.5, 1.0, 0.0,
]);
var n = ;
// 创建缓存区对象
var vertexTexCoordBuffer = gl.createBuffer();
if (!vertexTexCoordBuffer) {
console.log('Failed to create the buffer object');
return -;
} gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW); var FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < ) {
console.log('Failed to get the storage location of a_Position');
return -;
}
gl.vertexAttribPointer(a_Position, , gl.FLOAT, false, FSIZE * , );
gl.enableVertexAttribArray(a_Position); // 将纹理坐标分配给该存储位置并开启
var a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');
if (a_TexCoord < ) {
console.log('Failed to get the storage location of a_TexCoord');
return -;
}
gl.vertexAttribPointer(a_TexCoord, , gl.FLOAT, false, FSIZE * , FSIZE * );
gl.enableVertexAttribArray(a_TexCoord); return n;
} function initTextures(gl, n) {
// Step1:设置纹理对象
var texture = gl.createTexture();
if (!texture) {
console.log('Failed to create the texture object');
return false;
} // Step2: 获取u_Sampler(取样器)存储位置
var u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler');
if (!u_Sampler) {console.log('Failed to get the storage location of u_Sampler');
return false;
} // Step3: 创建图片对象
var image = new Image();
if (!image) {console.log('Failed to create the image object');
return false;
} image.onload = function(){ loadTexture(gl, n, texture, u_Sampler, image); };
image.src = '../resources/sky.jpg';
return true;
} function loadTexture(gl, n, texture, u_Sampler, image) {
// Step1:对图像进行y轴反转
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, ); // Step2: 开启0号纹理单元(textunit0~7)
gl.activeTexture(gl.TEXTURE0);
// Step3: 绑定纹理对象(target,texture)
// target可以是:gl.TEXTURE或gl.TEXTURE_CUBE_MAP
gl.bindTexture(gl.TEXTURE_2D, texture); // Step4: 设置纹理参数(target,pname,param)
// gl.TEXTURE_MAG_FILTER (纹理放大) 默认值: gl.LINEAR
// gl.TEXTURE_MIN_FILTER (纹理缩小) 默认值: gl.NEAREST_MIPMAP_LINEAR
// gl.TEXTURE_WRAP_S (纹理水平填充) 默认值: gl.REPEAT(平铺式)
// gl.MIRRORED_REPEAT (镜像对称)
// gl.CLAMP_TO_EDGE (使用纹理图像边缘值)
// gl.TEXTURE_WRAP_T (纹理垂直填充) 默认值: gl.REPEAT
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
// Step5:配置纹理图片(target,level,internalformat,format,type,image)
// level: 0
// internalformat:图像的内部格式
// format: 纹理数据的格式,必须与internalformat一致
// type: 纹理数据的类型
// image:包含纹理的图像的image对象
gl.texImage2D(gl.TEXTURE_2D, , gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
// Step6:将0号纹理传递至取样器
gl.uniform1i(u_Sampler, ); gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, , n);
}
三、结尾
以上代码均来自《WebGL编程指南》。
【WebGL】《WebGL编程指南》读书笔记——第5章的更多相关文章
- Android权威编程指南读书笔记(1-2章)
第一章 Android应用初体验 1.4用户界面设计 <?xml version="1.0" encoding="utf-8"?> ADT21开发版 ...
- hive编程指南——读书笔记(无知拾遗)
set hive.metastore.warehouse.dir=/user/myname/hive/warehouse; 用户设定自己的数据仓库目录.不影响其他用户.也在$HOME/.hiverc中 ...
- Hive编程指南读书笔记(1):
1.Mapreduce是一种计算模型,将计算任务分割成多个可以在服务器集群中并行执行的任务,然后分散到一群家用的或者服务器级别的硬件机器上,从而降低成本并提供水平可伸缩性. 2.mapreduce的两 ...
- <<Hive编程指南>>读书笔记
1. 设置hive以本地模式运行(即使当前用户是在分布式模式或伪分布式模式下执行也使用这种模式) set hive.exec.model.local.auto=true; 若想默认使用这个配置,可以将 ...
- 《python核心编程》读书笔记--第16章 网络编程
在进行网络编程之前,先对网络以及互联网协议做一个了解. 推荐阮一峰的博客:(感谢) http://www.ruanyifeng.com/blog/2012/05/internet_protocol_s ...
- 《Linux多线程服务器端编程》读书笔记第3章
<Linux多线程服务器端编程>第3章主要讲的是多线程服务器的适用场合与常用的编程模型. 1.进程和线程 一个进程是"内存中正在运行的程序“.每个进程都有自己独立的地址空间(ad ...
- css权威指南读书笔记-第10章浮动和定位
这一章看了之后真是豁然开朗,之前虽然写了圣杯布局和双飞翼布局,有些地方也是模糊的,现在打算总结之后再写一遍. 以下都是从<css权威指南>中摘抄的我认为很有用的说明. 浮动元素 一个元素浮 ...
- 《python核心编程》读书笔记--第18章 多线程编程
18.1引言 在多线程(multithreaded,MT)出现之前,电脑程序的运行由一个执行序列组成.多线程对某些任务来说是最理想的.这些任务有以下特点:它们本质上就是异步的,需要多个并发事务,各个事 ...
- JavaScript权威指南读书笔记【第一章】
第一章 JavaScript概述 前端三大技能: HTML: 描述网页内容 CSS: 描述网页样式 JavaScript: 描述网页行为 特点:动态.弱类型.适合面向对象和函数式编程的风格 语法源自J ...
- 《Unix环境高级编程》读书笔记 第13章-守护进程
1. 引言 守护进程是生存期长的一种进程.它们常常在系统引导装入时启动,仅在系统关闭时才终止.它们没有控制终端,在后台运行. 本章说明守护进程结构.如何编写守护进程程序.守护进程如何报告出错情况. 2 ...
随机推荐
- win7下使用apache ab 比较测试node与 tomcat
最近在研究node,都说node单线程.事件环机制,高并发效率高,亲测一下,一探究竟 apache ab 安装 进入:http://httpd.apache.org/download.cgi#apac ...
- Redis 高可用集群
Redis 高可用集群 Redis 的集群主从模型是一种高可用的集群架构.本章主要内容有:高可用集群的搭建,Jedis连接集群,新增集群节点,删除集群节点,其他配置补充说明. 高可用集群搭建 集群(c ...
- [特斯拉组件]ios高性能PageController
本文来自于腾讯Bugly公众号(weixinBugly),作者:sparrowchen,未经作者同意,请勿转载,原文地址: http://mp.weixin.qq.com/s/hBgvPBP12IQ1 ...
- 第四届河南省ACM 表达式求值 栈
表达式求值 时间限制: 1 Sec 内存限制: 128 MB 提交: 14 解决: 7 [提交][状态][讨论版] 题目描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简 ...
- Linux磁盘分区(二):删除
***********************************************声明************************************************ 原创 ...
- Ubuntu TensorFlow 源码 Android Demo的编译运行
Ubuntu TensorFlow 源码 Android Demo的编译运行 一. 安装 Android 的SDK和NDK SDK 配置 A:下载 国内下载地址选最新的: SDK: https://d ...
- java基础解析系列(十一)---equals、==和hashcode方法
java基础解析系列(十一)---equals.==和hashcode方法 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系 ...
- SqlParameter 用法总结
作用 解决恶意的T-sql语句攻击第一种 //传入参数 string ProductGroupCode, string Ismaintain, int HierarchyID, string BOMN ...
- C#真的过时了吗?
现在有一种言论:C#过时了!!! 有人说现在是BS的时代,C#开发BS网站的那一套,相对于Java.PHP来说,效率太低了! 有人说现在是移动互联网时代,C#作为微软主推的语言,无法开发移动应用成为其 ...
- form表单提交和ajax提交优先级
form中若定义action,那么,ajax将不能执行.form默认提交的请求优先级高于ajax