当前位置:网站首页>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 :
边栏推荐
- 简述keepalived工作原理
- Ctfshow, information collection: web13
- Stream learning notes
- Briefly describe the working principle of kept
- [original] all management without assessment is nonsense!
- 2. Heap sort "hard to understand sort"
- Integer learning
- Do not use memset to clear floating-point numbers
- 使用cpolar建立一个商业网站(2)
- 【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
猜你喜欢

知否|两大风控最重要指标与客群好坏的关系分析

With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly

Gd32 F3 pin mapping problem SW interface cannot be burned

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
![[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)

Mathematical modeling -- what is mathematical modeling

数学建模——什么是数学建模

【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例

Niuke real problem programming - day18

“百度杯”CTF比赛 2017 二月场,Web:include
随机推荐
Ctfshow, information collection: web10
How to build your own super signature system (yunxiaoduo)?
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
Gd32 F3 pin mapping problem SW interface cannot be burned
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
Unity's ASE realizes cartoon flame
HW初级流量监控,到底该怎么做
Nacos conformance protocol cp/ap/jraft/distro protocol
Ctfshow, information collection: web12
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
最安全的证券交易app都有哪些
[target detection] yolov5 Runtong voc2007 data set
Matlab experience summary
【数字IC验证快速入门】26、SystemVerilog项目实践之AHB-SRAMC(6)(APB协议基本要点)
Pat grade a 1103 integer factorizatio
Android -- jetpack: the difference between livedata setValue and postvalue
什么是数据泄露
TypeScript 发布 4.8 beta 版本
Summer safety is very important! Emergency safety education enters kindergarten
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)