当前位置:网站首页>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
边栏推荐
- [make a boat diary] [shapr3d STL format to gcode]
- [quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
- [quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
- Create lib Library in keil and use lib Library
- 大表delete删数据导致数据库异常解决
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- 避坑:Sql中 in 和not in中有null值的情况说明
- 什麼是數據泄露
- Window环境下配置Mongodb数据库
- Ctfshow, information collection: web14
猜你喜欢

The difference between full-time graduate students and part-time graduate students!

Create lib Library in keil and use lib Library

银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级

Ida Pro reverse tool finds the IP and port of the socket server

从 1.5 开始搭建一个微服务框架链路追踪 traceId

【兰州大学】考研初试复试资料分享

【OBS】RTMPSockBuf_ Fill, remote host closed connection.

Ctfshow, information collection: web6

【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)

微信小程序 01
随机推荐
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
2.Golang基础知识
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
Ctfshow, information collection: web10
Use cpolar to build a business website (2)
Compile advanced notes
Ctfshow, information collection: web12
Android -- jetpack: the difference between livedata setValue and postvalue
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
Ctfshow, information collection: web1
HW primary flow monitoring, what should we do
Keil5 does not support online simulation of STM32 F0 series
Unity's ASE realizes cartoon flame
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
CTFshow,信息搜集:web4
Introduction of mongod management database method