当前位置:网站首页>Getting started with webgl (3)
Getting started with webgl (3)
2022-07-07 15:39:00 【Vegetable chicken on the road】
Use attribute Variable
Purpose of use : Transfer location information from js Passed to vertex shaders in code , There are two main ways ,attribute Variables and uniform Variable ;
attribute Variable : Data related to vertices ;
uniform Variable : So the vertices are the same ( Vertex independent ) data .
Code up :
// 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 =
'void main() {\n' +
' gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);\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;
}
// obtain attribute Storage location
let a_Position = gl.getAttribLocation(gl.program,"a_Position");
if(a_Position < 0){
console.log(" Failed to get storage location !");
return;
}
// 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);
// Draw a point
gl.drawArrays(gl.POINTS, 0, 1);
}
Above , The process is :
(1)attribute vec4 a_Position Variables are declared in attribute, Must be a global variable , Passed in from outside the shader ;
(2) Use getAttribLocation Method to get attribute The address of the variable , The value returned >=0 It means that the address of the variable is returned ,=-1 It means that the variable does not exist ;
(3) Use vertexAttrib3f Method pass value .
This method has 3 Homologous functions , Represents the incoming 1-4 It's worth ,, The length of the array of numbers to be changed in the function . If declared attribute A variable is vec4 type , Only incoming 1 Parameters , The default complement of the last two components is 0.0, The last component is 1.0;
(4) draw , Or draw a dot , It's just to change the way of external value transfer
边栏推荐
- Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
- Compile advanced notes
- [original] all management without assessment is nonsense!
- TypeScript 发布 4.8 beta 版本
- 居然从408改考自命题!211华北电力大学(北京)
- What are the safest securities trading apps
- Yunxiaoduo software internal test distribution test platform description document
- Do not use memset to clear floating-point numbers
- MySQL bit类型解析
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
猜你喜欢
Ctfshow, information collection: web5
15. Using the text editing tool VIM
从 1.5 开始搭建一个微服务框架链路追踪 traceId
知否|两大风控最重要指标与客群好坏的关系分析
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
使用cpolar建立一个商业网站(2)
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
如何在opensea批量发布NFT(Rinkeby测试网)
微信小程序 01
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, information collection: web1
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
Runnable是否可以中断
Keil5 does not support online simulation of STM32 F0 series
PAT 甲级 1103 Integer Factorizatio
Nacos一致性协议 CP/AP/JRaft/Distro协议
HPDC smart base Talent Development Summit essay
Ctfshow, information collection: web9
微信小程序 01
Ctfshow, information collection: web13
一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
How to build your own super signature system (yunxiaoduo)?
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
Ctfshow, information collection: Web3
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
Oracle control file loss recovery archive mode method
TypeScript 发布 4.8 beta 版本