当前位置:网站首页>Getting started with webgl (4)
Getting started with webgl (4)
2022-07-07 15:38:00 【Vegetable chicken on the road】
Use uniform Variable
The last section talked about , uniform Variables are mainly used for all vertices that are the same ( Vertex independent ) data . And attribute Variable passing is similar , The difference is uniform The target of the transfer is the slice shader .
example , Click the production point with the mouse , The position of the point is different in different quadrants, and the color of the point is different
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n'+
'void main() {\n' +
' gl_Position = a_Position ;\n' +
' gl_PointSize = 30.0;\n' +
'}\n';
// Chip shader program
var FSHADER_SOURCE =
'precision mediump float;\n'+
'uniform vec4 u_FragColor;\n'+
'void main() {\n' +
' gl_FragColor = u_FragColor;\n' +
'}\n';
function main() {
// obtain <canvas> Elements
var canvas = document.getElementById('webgl');
// obtain WebGL Rendering context
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 u_FragColor = gl.getUniformLocation(gl.program,"u_FragColor");
// obtain attribute Storage location
var a_Position = gl.getAttribLocation(gl.program,"a_Position");
if(a_Position < 0){
console.log(" Failed to get storage location !");
return;
}
// Register mouse response events
canvas.onmousedown = function(ev){click(ev,gl,canvas,a_Position,u_FragColor);};
// Pass the vertex position to attribute Variable
// gl.vertexAttrib1f(a_Position,0.0);
// gl.vertexAttrib2f(a_Position,0.0,0,0);
// gl.vertexAttrib3f(a_Position,0.0,0.0,0.0);
// Specify empty <canvas> The color of the
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// Empty <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;
var y = ev.clientY;
var rect = ev.target.getBoundingClientRect();
x = ((x-rect.left)-canvas.height/2)/(canvas.height/2);
y = (canvas.width/2-(y-rect.top))/(canvas.width/2);
g_points.push([x,y]);
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([1.0,0.5,0.0,1.0]);
}else{
g_colors.push([1.0,1.0,1.0,1.0]);
}
// Empty <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];
gl.vertexAttrib3f(a_Position,xy[0],xy[1],0,0);
gl.uniform4f(u_FragColor,rgba[0],rgba[1],rgba[2],rgba[3]);
gl.drawArrays(gl.POINTS,0,1);
}
}
Declare in the slice shader uniform The precision is defined when variables precision mediump float
Define precision :
lowp、meduimp、highp They are low precision 、 Medium precision 、 High precision , The precision in vertices is very high, using the default high precision .
Fragment Shader uniform vec4 u_FragColor, Used uniform, It will make every vertex use the same value , Unless we change it , It is responsible for the color of each piece , There are four colors .
The process is :
(1) Declaration accuracy is precision mediump float Of uniform Variable ;
(2) Use getAttribLocation Method to get uniform The address of the variable ;
(3) Use uniform4f Method pass value .;
(4) draw .
The effect is as shown in the picture :
边栏推荐
- 【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- 简述keepalived工作原理
- Summer safety is very important! Emergency safety education enters kindergarten
- The download button and debug button in keil are grayed out
- 使用cpolar建立一个商业网站(2)
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- 15. Using the text editing tool VIM
- MongoDB数据库基础知识整理
- Why do we use UTF-8 encoding?
猜你喜欢
Wechat applet 01
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
Unity之ASE实现卡通火焰
How to release NFT in batches in opensea (rinkeby test network)
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
Create lib Library in keil and use lib Library
知否|两大风控最重要指标与客群好坏的关系分析
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
Starting from 1.5, build a microservice framework link tracking traceid
"Baidu Cup" CTF competition 2017 February, web:include
随机推荐
Ctfshow, information collection: web2
Ctfshow, information collection: web7
【目标检测】YOLOv5跑通VOC2007数据集
What is Base64?
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
【深度学习】图像超分实验:SRCNN/FSRCNN
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
Connecting FTP server tutorial
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
2. Basic knowledge of golang
Ctfshow, information collection: web9
Unity's ASE realizes cartoon flame
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
Runnable是否可以中断
知否|两大风控最重要指标与客群好坏的关系分析
Zhongang Mining: Fluorite continues to lead the growth of new energy market
微信小程序 01
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
Change win10 Screensaver
15. Using the text editing tool VIM