当前位置:网站首页>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 :
边栏推荐
- Ctfshow, information collection: web9
- Yunxiaoduo software internal test distribution test platform description document
- 【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
- Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- 【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
- Ctfshow, information collection: web2
- Use cpolar to build a business website (2)
- [quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
- @ComponentScan
猜你喜欢
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
What is Base64?
Ida Pro reverse tool finds the IP and port of the socket server
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
【兰州大学】考研初试复试资料分享
Monthly observation of internet medical field in May 2022
Ctfshow, information collection: web8
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
CTFshow,信息搜集:web5
Summer safety is very important! Emergency safety education enters kindergarten
随机推荐
Ctfshow, information collection: web4
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
Ctfshow, information collection: web14
Share the technical details of super signature system construction
数学建模——什么是数学建模
一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
Ctfshow, information collection: web10
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
Wechat applet 01
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
Pat grade a 1103 integer factorizatio
[target detection] yolov5 Runtong voc2007 data set
What are PV and UV? pv、uv
STM32F103C8T6 PWM驱动舵机(SG90)
【兰州大学】考研初试复试资料分享
Bye, Dachang! I'm going to the factory today
How to build your own super signature system (yunxiaoduo)?