【WebGL】《WebGL编程指南》读书笔记——第2章

时间:2024-01-02 14:40:09

一、前言

最近看了《WebGL编程指南》这本书,发现还是很有意思的,故每章阅读后做个笔记。

二、正文

Example1:在canvas中绘制2D矩形

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Draw a blue rectangle (canvas version)</title>
</head> <body onload="main()">
<canvas id="example" width="" height="">
Please use a browser that supports "canvas"
</canvas>
<script src="DrawRectangle.js"></script>
</body>
</html>
function main() {
var canvas = document.getElementById('example');
if (!canvas) {
console.log('Failed to retrieve the <canvas> element');
return false;
} var ctx = canvas.getContext('2d'); //获取绘图上下文
ctx.fillStyle = 'rgba(0, 0, 255, 1.0)'; //设置颜色,rgb为三原色通道,a为alphaa透明度
ctx.fillRect(, , , ); //设置矩形的位置及大小,margin-left为120,margin-top为10;绘制长350px,宽为150px的矩形
}

Example2:清空canvas

function main() {
var canvas = document.getElementById('webgl');
var gl = getWebGLContext(canvas); //获取webgl绘图上下文
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
} gl.clearColor(0.0, 0.0, 0.0, 1.0); //设置清空颜色,颜色值都是0-1之间!不是0-255!!!
gl.clear(gl.COLOR_BUFFER_BIT); //清空颜色缓存
} 

      

        Example3:在canvas中绘制一个红点

//顶点着色器数据源
var VSHADER_SOURCE =
'void main() {\n' +
' gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n' + //位置,vec4由四个浮点型数据组成,即(float,float,float,float)
' gl_PointSize = 10.0;\n' + //点的大小
'}\n'; //片元着色器数据源
var FSHADER_SOURCE =
'void main() {\n' +
' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' + //颜色rgba
'}\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;
} // Step1:初始化着色器
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}

// Step2:清空背景色及canvas颜色缓存
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT); // 画点
gl.drawArrays(gl.POINTS, , ); //(mode,first,count);
}
function initShaders(gl, vshader, fshader) {
// Step1:创建程序对象
var program = createProgram(gl, vshader, fshader);
if (!program) {
console.log('Failed to create program');
return false;
}

// Step2:设置用于渲染的程序对象 链接地址:https://msdn.microsoft.com/library/dn302459(v=vs.85).aspx
gl.useProgram(program);
gl.program = program;
return true;
}
//********************************以上为主程序************************************ function createProgram(gl, vshader, fshader) {
// Step1:加载着色器
  var vertexShader = loadShader(gl, gl.VERTEX_SHADER, vshader);
  var fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fshader);   if (!vertexShader || !fragmentShader)
{
    return null;
  } // Step2:创建空程序对象 链接地址: https://msdn.microsoft.com/library/Dn302384
  var program = gl.createProgram();
  if (!program)
{
    return null;
  } // Step3:着色器附着至程序对象 链接地址:https://msdn.microsoft.com/library/Dn302364
  gl.attachShader(program, vertexShader);
  gl.attachShader(program, fragmentShader);   // Step4:链接上程序对象
  gl.linkProgram(program);
  // Step5:检查链接结果
  var linked = gl.getProgramParameterprogram, gl.LINK_STATUS);
  if (!linked) {
    var error = gl.getProgramInfoLog(program);
    console.log('Failed to link program: ' + error);
    gl.deleteProgram(program);
    gl.deleteShader(fragmentShader);
    gl.deleteShader(vertexShader);
    return null;
  }
  return program;
} function loadShader(gl, type, source) { // Step1:创建着色器对象(依据着色器类型)
  var shader = gl.createShader(type);
  if (shader == null) {
    console.log('unable to create shader');
    return null;
  }   // Step2: 设置着色器数据源(着色器对象,数据源)
  gl.shaderSource(shader, source);   // Step3: 编译着色器
  gl.compileShader(shader);   // Step4: 检查编译结果
  var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  if (!compiled) {
    var error = gl.getShaderInfoLog(shader);
    console.log('Failed to compile shader: ' + error);
    gl.deleteShader(shader);
    return null;
  }
  return shader;
}

      

        Example4:使用attribute变量在canvas中绘制一个红点

var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' + //限定符,类型,变量名称 attribute变量传输与顶点有关的数据
'void main() {\n' +
' gl_Position = a_Position;\n' +
' gl_PointSize = 10.0;\n' +
'}\n'; var FSHADER_SOURCE =
'void main() {\n' +
' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\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;
} // 获取attribute变量的存储位置(程序对象,attribute变量名称)
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < ) {
console.log('Failed to get the storage location of a_Position');
return;
} // 将顶点位置赋给attribute变量的存储位置,3f表述3个float型参数
gl.vertexAttrib3f(a_Position, 0.0, 0.0, 0.0); gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.POINTS, , );
}

        Example5:浏览器坐标->Canvas坐标->webgl坐标

  var x = ev.clientX; // x coordinate of a mouse pointer
var y = ev.clientY; // y coordinate of a mouse pointer
var rect = ev.target.getBoundingClientRect() ; x = ((x - rect.left) - canvas.width/)/(canvas.width/);
y = (canvas.height/ - (y - rect.top))/(canvas.height/);

三、结尾

      以上代码除了部分注释外均来自于《WebGL编程指南》,发现其实每一章内容还是挺多的,争取一星期一章的总结。