gl attribute和uniform的用法

gl attribute和uniform的用法

http://blog.youkuaiyun.com/jackers679/article/details/6848085


attribute
    attribute变量是只能在vertex shader中使用的变量。(它不能在fragment shader中声明attribute变量,也不能被fragment shader中使用)

    一般用attribute变量来表示一些顶点的数据,如:顶点坐标,法线,纹理坐标,顶点颜色等
    在application中,一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。
    
    glsl里面定义属性
    Line 16:     attribute vec3 aVertexPosition;
    Line 22:         gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    
    javascript使用属性
        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
    Line 97:         gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
    Line 153:         gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    Line 160:         gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    
uniform
    uniform变量就像是C语言里面的常量(const ),它不能被shader程序修改。(shader只能用,不能改)
    如果uniform变量在vertex和fragment两者之间声明方式完全一样,则它可以在vertex和fragment共享使用。(相当于一个被vertex和fragment shader共享的全局变量)
    
    uniform变量一般用来表示:变换矩阵,材质,光照参数和颜色等信息。
    
    glsl里
    Line 18:     uniform mat4 uMVMatrix;
    Line 22:         gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    
    javascript里
    Line 100:         shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    Line 109:         gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
    
3.varying变量

varying变量是vertex和fragment shader之间做数据传递用的。一般vertex shader修改varying变量的值,然后fragment shader使用该varying变量的值。因此varying变量在vertex和fragment shader二者之间的声明必须是一致的。application不能使用此变量。   

public initializer(sceneGl: WebGLRenderingContext, sceneProgram: WebGLProgram, sceneOption: FisheyeSceneOption) { this.applyOptions(sceneGl, sceneProgram, sceneOption); const gl = this.gl; gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1); const shaderProgram = this.program; const vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0]), gl.STATIC_DRAW); const xyLocation = gl.getAttribLocation(shaderProgram, 'xy'); gl.vertexAttribPointer(xyLocation, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(xyLocation); const scaleLocation = gl.getUniformLocation(shaderProgram, 'scale'); const scaleFactor = [1, 1]; gl.uniform2fv(scaleLocation, scaleFactor); const translateLocation = gl.getUniformLocation(shaderProgram, 'translate'); const translateFactor = [0, 0]; gl.uniform2fv(translateLocation, translateFactor); const tempStrengthLocation = gl.getUniformLocation(shaderProgram, 'strength'); if (!tempStrengthLocation) return; this.strengthLocation = tempStrengthLocation; gl.uniform1f(this.strengthLocation, this.strengthValue); const texture = gl.createTexture(); const enableMipMap = this.options.isAntialias ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR; gl.bindTexture(gl.TEXTURE_2D, texture); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, enableMipMap); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); }
06-26
import { SceneOption, Scene, SCENE } from '../gl-renderer/types' import Texture from '../texture' // 目前没有额外配置项,先预留配置项类型,方便后续扩展 export type WebcodecNormalSceneOption = SceneOption<object> /** * @description: 顶点着色器 * @return {*} */ export const vertexShaderSource = ` attribute vec2 xy; uniform vec2 scale; uniform vec2 translate; varying highp vec2 uv; void main(void) { vec2 scaledPosition = xy * scale+ translate; gl_Position = vec4(scaledPosition, 0.0, 1.0); // Map vertex coordinates (-1 to +1) to UV coordinates (0 to 1). // UV coordinates are Y-flipped relative to vertex coordinates. uv = vec2((1.0 + xy.x) / 2.0, (1.0 - xy.y) / 2.0); } ` /** * @description: 片元着色器 * @return {*} */ export const fragmentShaderSource = ` varying highp vec2 uv; uniform sampler2D texture; void main(void) { gl_FragColor = texture2D(texture, uv); } ` export class WebcodecNormalScene extends Scene { static sceneType = SCENE.WEBCODEC_NORMAL vertexShaderSource = vertexShaderSource fragmentShaderSource = fragmentShaderSource gl!: WebGLRenderingContext program!: WebGLProgram options!: WebcodecNormalSceneOption yTexture!: Texture uTexture!: Texture vTexture!: Texture get sceneType() { return WebcodecNormalScene.sceneType } private applyOptions(sceneGl: WebGLRenderingContext, sceneProgram: WebGLProgram, sceneOption: WebcodecNormalSceneOption) { this.gl = sceneGl this.program = sceneProgram this.options = sceneOption } public initializer(sceneGl: WebGLRenderingContext, sceneProgram: WebGLProgram, sceneOption: WebcodecNormalSceneOption) { this.applyOptions(sceneGl, sceneProgram, sceneOption) const gl = this.gl const shaderProgram = this.program const vertexBuffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer) gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0]), gl.STATIC_DRAW) const xyLocation = gl.getAttribLocation(shaderProgram, 'xy') gl.vertexAttribPointer(xyLocation, 2, gl.FLOAT, false, 0, 0) gl.enableVertexAttribArray(xyLocation) const scaleLocation = gl.getUniformLocation(shaderProgram, 'scale') const scaleFactor = [1, 1] // 缩放因子为 1.5x gl.uniform2fv(scaleLocation, scaleFactor) const translateLocation = gl.getUniformLocation(shaderProgram, 'translate') const translateFactor = [0, 0] // 平移因子为 0,0 gl.uniform2fv(translateLocation, translateFactor) //纹理 const texture = gl.createTexture() const enableMipMap = this.options.isAntialias ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR gl.bindTexture(gl.TEXTURE_2D, texture) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, enableMipMap) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) } public render(frameData: VideoFrame) { const gl = this.gl gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, frameData) this.options.isAntialias && gl.generateMipmap(gl.TEXTURE_2D) // Configure and clear the drawing area. gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight) gl.clearColor(0.0, 0.0, 0.0, 0.0) gl.clear(gl.COLOR_BUFFER_BIT) gl.drawArrays(gl.TRIANGLE_FAN, 0, 4) } public clear() { const gl = this.gl gl.clearColor(0.0, 0.0, 0.0, 0.0) gl.clear(gl.COLOR_BUFFER_BIT) gl.getExtension('WEBGL_lose_context')?.loseContext() } } 结合这段代码说一下优化的方式
最新发布
07-05
请帮我检查下面代码为什么没有绘制图形const VSHADER_SOURCE = ` attribute vec4 aPosition; // 接收纹理坐标 attribute vec2 a_TexCoord; varying vec2 v_TexCoord; void main() { gl_Position = aPosition; v_TexCoord = a_TexCoord; }`; const FSHADER_SOURCE=`precision highp float; uniform vec2 uResolution; uniform sampler2D u_Sampler; // 接收纹理坐标 varying vec2 v_TexCoord; void main() { vec2 center = vec2(0.5, 0.5); // 圆心 float radius = 0.2; vec2 fragCoord = gl_FragCoord.xy / uResolution; // 归一化坐标 // 计算宽高比 使圆保持正圆 float aspectRatio = uResolution.x / uResolution.y; // 根据宽高比调整坐标 vec2 scaledCoord = vec2((fragCoord.x - center.x) * aspectRatio, fragCoord.y - center.y); float distance = length(scaledCoord); if (distance > radius) discard; gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // 红色 // texture2D 用于抽取纹理图片中纹素的颜色 gl_FragColor = texture2D(u_Sampler, v_TexCoord); }`; const program = initShaders(gl,VSHADER_SOURCE,FSHADER_SOURCE); // 设置顶点数据(矩形覆盖整个画布) const vertices = new Float32Array([ -1.0, 1.0, // 左上角 -1.0, -1.0, // 左下角 1.0, 1.0, // 右上角 1.0, -1.0 // 右下角 ]); const FSIZE = vertices.BYTES_PER_ELEMENT // 创建并绑定顶点缓冲区 const vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); // 获取并启用顶点属性 const aPosition = gl.getAttribLocation(program, "aPosition"); gl.enableVertexAttribArray(aPosition); gl.vertexAttribPointer(aPosition, 2, gl.FLOAT, false, 0, 0); // 设置纹理坐标 const a_TexCoord = gl.getAttribLocation(program, 'a_TexCoord') // 注意步进参数、偏移参数设置 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2) gl.enableVertexAttribArray(a_TexCoord) // 设置分辨率统一变量 const uResolution = gl.getUniformLocation(program, 'uResolution'); gl.uniform2f(uResolution, canvas.width, canvas.height); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); // 创建纹理 const texture = gl.createTexture();
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值