当前位置:网站首页>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 :
边栏推荐
- Matlab experience summary
- [quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- 2022年5月互联网医疗领域月度观察
- Runnable是否可以中断
- Ctfshow, information collection: web12
- [quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
- Android -- jetpack: the difference between livedata setValue and postvalue
- What is data leakage
- Use cpolar to build a business website (2)
猜你喜欢

Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan

Why do we use UTF-8 encoding?
![[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)](/img/91/16a370ac41adc8fe31507765a82b0a.png)
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)

Briefly describe the working principle of kept
![[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)](/img/cf/45775b712f60869186a25d3657ee1b.png)
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)

Ctfshow, information collection: web1
![[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper](/img/38/cc5bb5eaa3dcee5ae2d51a904cf26a.png)
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper

简述keepalived工作原理

【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)

Create lib Library in keil and use lib Library
随机推荐
Ctfshow, information collection: web12
Compile advanced notes
Briefly describe the working principle of kept
Ctfshow, information collection: Web3
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
What is Base64?
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
Ctfshow, information collection: web4
如何在opensea批量发布NFT(Rinkeby测试网)
【深度学习】语义分割实验:Unet网络/MSRC2数据集
Ctfshow, information collection: web7
Ctfshow, information collection: web13
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
Unity之ASE实现卡通火焰
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
[server data recovery] data recovery case of raid failure of a Dell server
Win10 or win11 taskbar, automatically hidden and transparent
Typescript release 4.8 beta