gpt4 book ai didi

javascript - findAttribLocation 在 WebGL 中返回 -1

转载 作者:行者123 更新时间:2023-11-30 09:56:46 24 4
gpt4 key购买 nike

我知道如果在我的顶点着色器中找不到该属性可能会发生这种情况,但我确信它存在。我将尝试提供大型代码主体中最相关的片段:

  vertices = new Float32Array(dataArrays.verts)
normals = new Float32Array(dataArrays.norms)
uvs = new Float32Array(dataArrays.txtcos)

我的片段着色器:

precision highp float;
attribute vec3 aPosition;
attribute vec3 vnorm;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
uniform mat4 pMatrix;
uniform mat4 vMatrix;
uniform mat4 mMatrix;
void main(void) {
gl_Position = pMatrix * vMatrix * mMatrix * vec4(aPosition, 1.0);
vTexCoord = aTexCoord;
}

属性查找器:

findAttribLocations = (gl, program, attributes) ->
out = {}
i = 0
while i < attributes.length
attrib = attributes[i]
console.log attrib
out[attrib] = gl.getAttribLocation(program, attrib)
console.log out[attrib]
i++
out

初始化器:

 TObject::init = (drawingState) ->
gl = drawingState.gl
@program = createGLProgram(gl, vertexSource, fragmentSource)
gl.useProgram @program
@attributes = findAttribLocations(gl, @program, [
'aPosition'
'vnorm'
'aTexCoord'
])
@uniforms = findUniformLocations(gl, @program, [
'pMatrix'
'vMatrix'
'mMatrix'
'uTexture'
'bTexture'
'lightdir'
])
@texture = createGLTexture(gl, image, true)
@bumpTexture = createGLTexture(gl, bumpImage, true)
@buffers[0] = createGLBuffer(gl, vertices, gl.STATIC_DRAW)
@buffers[1] = createGLBuffer(gl, normals, gl.STATIC_DRAW)
@buffers[2] = createGLBuffer(gl, uvs, gl.STATIC_DRAW)
return

程序创建者:

createGLProgram = (gl, vSrc, fSrc) ->
program = gl.createProgram()
vShader = createGLShader(gl, gl.VERTEX_SHADER, vSrc)
fShader = createGLShader(gl, gl.FRAGMENT_SHADER, fSrc)
gl.attachShader program, vShader
gl.attachShader program, fShader
gl.linkProgram program
if !gl.getProgramParameter(program, gl.LINK_STATUS)
console.log 'warning: program failed to link'
return null
console.log "program"
console.log program
program

除了着色器中缺少变量之外,还有其他原因可能导致此错误吗?我正在该程序的其他地方切换着色器,但我相信这里使用的是正确的着色器。我该如何进一步调查?

最佳答案

着色器未使用的属性称为非事件属性,并且没有绑定(bind)。

由于您没有在顶点着色器中使用 vnorm,它会在 glGetAttribLocation 查询时变为非事件状态并返回 -1。激活它的唯一方法是在着色器中以有助于输出的方式使用它。

另见 the answer here

关于javascript - findAttribLocation 在 WebGL 中返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com