当前位置:网站首页>Vertex shader to slice shader procedure, varying variable
Vertex shader to slice shader procedure, varying variable
2022-07-07 15:39:00 【Vegetable chicken on the road】
varying Variable : Pass data from vertex shader to slice shader .
varying Variables can only be float( And related vec2,vec3,vec4,mat2,mat3,mat4).
We learned earlier that vertex shaders control the position of geometry 、 Dimensions even specify the color of the geometry , The final slice shader determines the color of the geometry ! But we don't know the specific process between them !
Now let's understand the two processes :
Graphic assembly process : The task of this step is , The left side of isolated vertices is assembled into geometry , The category of geometry is determined by gl.drawArrays() The first parameter of the function determines .
Grating process : The task of this step is to convert the assembled geometry into pieces .
The process from vertex shader to slice shader
******************************************************************************************
Ahead gl.deawArrays() The execution of the function (n=3):
step :
(1) according to gl.drawArrays() Parameters of n, Vertex shaders will be executed n Time ( Execute vertex shader results : Input and store the coordinates in the assembly area )
(2) Start assembly drawing . Use the incoming point coordinates , according to gl.drawArrays() The first parameter information of ( Such as :gl.TRANGLES) To decide how to assemble .
(3) Rasterize
Example :
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n'+
'attribute vec4 a_Color;\n'+
'varying vec4 v_Color;\n'+
'void main() {\n' +
'gl_Position = a_Position ;\n' +
'v_Color = a_Color;\n'+
'}\n';
// Chip shader program
var FSHADER_SOURCE =
'precision mediump float;\n' +
'varying vec4 v_Color;\n' +
'void main() {\n' +
'gl_FragColor = v_Color;\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;
}
// Set vertex position
var n = initVertexBuffers(gl);
if(n<0){
console.log("fail initialTextexBuffer");
return;
}
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// Empty <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES,0,n);
}
function initVertexBuffers(gl) {
var verticesColors = new Float32Array([
0.0 , 0.5 , 1.0,0.0,0.0,
-0.5 , -0.5 , 0.0,1.0,0.0,
0.5 , -0.5 , 0.0,0.0,1.0
]);
var n =3;
// Create buffer object
var vertexColorBuffer = gl.createBuffer();
if (!vertexColorBuffer) {
console.log("vertexColorBuffer not exit");
return;
}
// Bind the buffer object to the target
gl.bindBuffer(gl.ARRAY_BUFFER,vertexColorBuffer);
// Write data
gl.bufferData(gl.ARRAY_BUFFER,verticesColors,gl.STATIC_DRAW);
var FSIZE = verticesColors.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(gl.program,"a_Position");
// Assign cache objects to a_position
gl.vertexAttribPointer(a_Position,2,gl.FLOAT,false,FSIZE*5,0);
// Connect a_position Change to the cache object assigned to it
gl.enableVertexAttribArray(a_Position);
var a_color = gl.getAttribLocation(gl.program,"a_Color");
// Assign cache objects to a_position
gl.vertexAttribPointer(a_color,2,gl.FLOAT,false,FSIZE*5,FSIZE*2);
// Connect a_position Change to the cache object assigned to it
gl.enableVertexAttribArray(a_color);
return n;
}
Running effect :
Why is it such an effect , Because it involves the process of color interpolation :
边栏推荐
- 摘抄的只言片语
- A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
- 从 1.5 开始搭建一个微服务框架链路追踪 traceId
- Ctfshow, information collection: Web3
- @ComponentScan
- Whether runnable can be interrupted
- Pat grade a 1103 integer factorizatio
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- How to release NFT in batches in opensea (rinkeby test network)
- Ctfshow, information collection: web7
猜你喜欢
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
The download button and debug button in keil are grayed out
【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
Gd32 F3 pin mapping problem SW interface cannot be burned
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
Stream learning notes
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
随机推荐
众昂矿业:萤石继续引领新能源市场增长
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
【目标检测】YOLOv5跑通VOC2007数据集
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
How to build your own super signature system (yunxiaoduo)?
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
Ctfshow, information collection: web13
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
Unity's ASE realizes cartoon flame
Monthly observation of internet medical field in May 2022
[original] all management without assessment is nonsense!
@Introduction and three usages of controlleradvice
Mathematical modeling -- what is mathematical modeling
The significance of XOR in embedded C language
Qu'est - ce qu'une violation de données
Why do we use UTF-8 encoding?
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
Nacos一致性协议 CP/AP/JRaft/Distro协议