一、前言

       终于到了第五章了,貌似开始越来越复杂了。

二、正文

        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章的更多相关文章

  1. Android权威编程指南读书笔记(1-2章)

    第一章 Android应用初体验 1.4用户界面设计 <?xml version="1.0" encoding="utf-8"?> ADT21开发版 ...

  2. hive编程指南——读书笔记(无知拾遗)

    set hive.metastore.warehouse.dir=/user/myname/hive/warehouse; 用户设定自己的数据仓库目录.不影响其他用户.也在$HOME/.hiverc中 ...

  3. Hive编程指南读书笔记(1):

    1.Mapreduce是一种计算模型,将计算任务分割成多个可以在服务器集群中并行执行的任务,然后分散到一群家用的或者服务器级别的硬件机器上,从而降低成本并提供水平可伸缩性. 2.mapreduce的两 ...

  4. <<Hive编程指南>>读书笔记

    1. 设置hive以本地模式运行(即使当前用户是在分布式模式或伪分布式模式下执行也使用这种模式) set hive.exec.model.local.auto=true; 若想默认使用这个配置,可以将 ...

  5. 《python核心编程》读书笔记--第16章 网络编程

    在进行网络编程之前,先对网络以及互联网协议做一个了解. 推荐阮一峰的博客:(感谢) http://www.ruanyifeng.com/blog/2012/05/internet_protocol_s ...

  6. 《Linux多线程服务器端编程》读书笔记第3章

    <Linux多线程服务器端编程>第3章主要讲的是多线程服务器的适用场合与常用的编程模型. 1.进程和线程 一个进程是"内存中正在运行的程序“.每个进程都有自己独立的地址空间(ad ...

  7. css权威指南读书笔记-第10章浮动和定位

    这一章看了之后真是豁然开朗,之前虽然写了圣杯布局和双飞翼布局,有些地方也是模糊的,现在打算总结之后再写一遍. 以下都是从<css权威指南>中摘抄的我认为很有用的说明. 浮动元素 一个元素浮 ...

  8. 《python核心编程》读书笔记--第18章 多线程编程

    18.1引言 在多线程(multithreaded,MT)出现之前,电脑程序的运行由一个执行序列组成.多线程对某些任务来说是最理想的.这些任务有以下特点:它们本质上就是异步的,需要多个并发事务,各个事 ...

  9. JavaScript权威指南读书笔记【第一章】

    第一章 JavaScript概述 前端三大技能: HTML: 描述网页内容 CSS: 描述网页样式 JavaScript: 描述网页行为 特点:动态.弱类型.适合面向对象和函数式编程的风格 语法源自J ...

  10. 《Unix环境高级编程》读书笔记 第13章-守护进程

    1. 引言 守护进程是生存期长的一种进程.它们常常在系统引导装入时启动,仅在系统关闭时才终止.它们没有控制终端,在后台运行. 本章说明守护进程结构.如何编写守护进程程序.守护进程如何报告出错情况. 2 ...

随机推荐

  1. Nvidia TX2 Robot 环境配置记录

    p.p1 { margin: 0.0px 0.0px 2.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #454545 } p. ...

  2. CNCC2017中的深度学习与跨媒体智能

    CNCC2017中的深度学习与跨媒体智能 转载请注明作者:梦里茶 目录 机器学习与跨媒体智能 传统方法与深度学习 图像分割 小数据集下的深度学习 语音前沿技术 生成模型 基于贝叶斯的视觉信息编解码 珠 ...

  3. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  4. Cesium基础使用介绍

    前言 最近折腾了一下三维地球,本文简单为大家介绍一款开源的三维地球软件--Cesium,以及如何快速上手Cesium.当然三维地球重要的肯定不是数据显示,这只是数据可视化的一小部分,重要的应该是背后的 ...

  5. leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?

    /* Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...

  6. P1373 小a和uim之大逃离

    转自:http://www.cnblogs.com/CtsNevermore/p/6028138.html 题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一 ...

  7. 2016普及组t3海港

    好的,说说这道题的思路,爆搜队列嘛: 用一个结构体队列存每个人来的时间和他的国籍,用一个vis数组存每个人来的次数,是第一次来sum便加一. 然后从前面第一个人开始扔(原谅我用这个词,因为我找不到更好 ...

  8. Android 项目使用TensorFlow

    Android 项目使用TensorFlow 首先需要搭建TensorFlow编译环境 参考:(http://www.cnblogs.com/dyufei/p/8027764.html) Tensor ...

  9. SpringMvc+JavaConfig+Idea 搭建项目

    1.介绍 之前搭建SpringMvc项目要配置一系列的配置文件,比如web.xml,applicationContext.xml,dispatcher.xml.Spring 3.X之后推出了基于Jav ...

  10. node.js入门必知

    目录: 一.node.js简介 1.1特点 1.2适合开发什么 1.3Node.js无法挑战老牌后台语言 二.http模块 一.node.js简介 Node.js开发服务器,数据.路由.本地关心的效果 ...