需要源码可以Q群:828202939 或者点击这里 <https://jq.qq.com/?_wv=1027&k=5NiS63x> 
希望可以和大家一起学习、一起进步!!纯手打!!

书籍是PDF电子档,也在Q群里,所有的课程源代码在我上传的资源
<https://download.csdn.net/user/weixin_39452320/uploads>里面,本来想设置开源,好像不行!

如有错别字或有理解不到位的地方,可以留言或者加微信15250969798,在下会及时修改!!!!!

这节课主要讲鼠标点击绘制一个在不同的象限绘制出不同颜色的点

大致过程与上一节差不多,只是片元着色器有点改动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" />
<title>改变点的颜色</title> </head> <body onload="main()"> <canvas id="webgl"
width="400" height="400"> Please use a browser that supports "canvas" </canvas>
<script src="../lib/webgl-utils.js"></script> <script
src="../lib/webgl-debug.js"></script> <script
src="../lib/cuon-utils.js"></script> <script src="ColoredPoints.js"></script>
</body> </html> // 顶点着色器 var VSHADER_SOURCE = 'attribute vec4 a_Position;\n' +
'void main() {\n' + ' gl_Position = a_Position;\n' + ' gl_PointSize = 10.0;\n'
+ '}\n'; // 片元着色器,这里通过attribute赋值动态更改片元着色器的颜色 var FSHADER_SOURCE = 'precision
mediump float;\n' + 'uniform vec4 u_FragColor;\n' + // uniform変数 'void main()
{\n' + ' gl_FragColor = u_FragColor;\n' + '}\n'; function main() { //
查找canvas元素 var canvas = document.getElementById('webgl'); // 获取canvas上下文 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变量的存储位置 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; } //获取u_FragColor变量的储存位置 var u_FragColor =
gl.getUniformLocation(gl.program, 'u_FragColor'); if (!u_FragColor) {
console.log('Failed to get the storage location of u_FragColor'); return; }
//注册鼠标点击时的响应函数 canvas.onmousedown = function(ev){ click(ev, gl, canvas,
a_Position, u_FragColor) }; // 指定清除canvas的背景颜色 gl.clearColor(0.0, 0.0, 0.0,
1.0); // 清空canvas gl.clear(gl.COLOR_BUFFER_BIT); } var g_points = []; //
鼠标点击位置数组 var g_colors = []; // 存储点的颜色数组 function click(ev, gl, canvas,
a_Position, u_FragColor) { var x = ev.clientX; // 鼠标点击时的X坐标 var y = ev.clientY;
// 鼠标点击时的Y坐标 //获取canvas在浏览器客户区中的坐标 var rect =
ev.target.getBoundingClientRect(); //这里主要是先把客户区的坐标转换为canvas的坐标,然后再转化为webgl的坐标 x
= ((x - rect.left) - canvas.width/2)/(canvas.width/2); y = (canvas.height/2 -
(y - rect.top))/(canvas.height/2); // 将坐标存储到g_points数组中 g_points.push([x, y]);
// 将点的颜色存储到g_colors点数组中 if (x >= 0.0 && y >= 0.0) { // 第一象限 g_colors.push([1.0,
0.0, 0.0, 1.0]); // 红色 } else if (x < 0.0 && y < 0.0) { // 第三象限
g_colors.push([0.0, 1.0, 0.0, 1.0]); // 绿色 } else if (x < 0.0 && y > 0.0) { //
第二象限 g_colors.push([1.0, 1.0, 0.0, 1.0]); // 黄色 } else { // 其他象限
g_colors.push([1.0, 1.0, 1.0, 1.0]); // 白色 } // 清除canvas
gl.clear(gl.COLOR_BUFFER_BIT); var len = g_points.length; for(var i = 0; i <
len; i++) { var xy = g_points[i]; var rgba = g_colors[i]; //
将点的位置传输到a_Position变量中 gl.vertexAttrib3f(a_Position, xy[0], xy[1], 0.0); //
将点的颜色传输到a_Position变量中 gl.uniform4f(u_FragColor, rgba[0], rgba[1], rgba[2],
rgba[3]); // 绘制 gl.drawArrays(gl.POINTS, 0, 1); } }
运行展示:


友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信