使用两个或多个具有不同数量属性的着色器时发生冲突 [英] Conflict when using two or more shaders with different number of attributes

查看:182
本文介绍了使用两个或多个具有不同数量属性的着色器时发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的WebGL程序中实现选择(使用颜色和readPixels).当我启动程序时,我创建了单独的shaderProgram.一个用于phong着色,另一个用于简单地给形状赋予颜色以检测单击了哪个形状.

phong着色器具有2个属性.顶点位置和顶点法线.采摘人只是有位置.

现在,我发现由于种种奇怪的原因,当这两个着色器都存在于同一程序中并且我正在使用选择项时,我的drawArray调用似乎失败了.最后发生的事情是我的gl.vertexAttribPointer调用.我一直在弄乱,发现当我使用以下方法检查活动的attrib数组时: gl.getVertexAttrib(index,gl.VERTEX_ATTRIB_ARRAY_ENABLED);

两者都返回true(这是当通过gl.useProgram(picking)激活拾取着色器时)

现在,如果我使用gl.disableVertexAttribArray(1)禁用1;一切再次正常.另一个解决方法是先使用phong着色器进行绘制,然后再使用picking着色器,以某种方式神奇地使其正常运行.我猜想在那种情况下,当使用phong着色器附加我的顶点法线缓冲区时,当我切换到picking着色器并且drawArray调用可以正常工作时,它会以某种方式保留下来.

我想知道我是否使用gl.enableAttribArray错误,并在切换着色器或类似的东西时应禁用它们.

我也尝试过以不同的顺序创建着色器程序,但没有成功.

解决方案

您可能已经发现,useProgram不会影响任何东西,但应该在下一个绘制调用中运行的程序(着色器代码)会有所影响.您必须确保仅启用当前程序使用的属性.

如果以某种方式包装了WebGL代码,则提示是为包装器中存储的每个程序保留最高的可用属性编号,然后与最新使用的程序进行比较,并在绘制调用之前相应地启用/禁用. /p>

I'm trying to implement picking(using colors and readPixels) in a WebGL program of mine. When I start my program I create to seperate shaderProgram. One for phong shading and another that simply give shapes a color to be use to detect which shape has been clicked on.

The phong shader has 2 attributes. Vertex position and vertex normal. The picking one simply has the position.

Now I discovered that for some odd reason, when both of these shaders exist in the same program and I'm using the picking one, my drawArray call seems to fail. The last thing to happen is my gl.vertexAttribPointer call. I've been messing around and found out that when I check for active attrib arrays using: gl.getVertexAttrib(index,gl.VERTEX_ATTRIB_ARRAY_ENABLED);

both 0,1 return true (this is when the picking shader is active with gl.useProgram(picking))

Now if I disable 1 with gl.disableVertexAttribArray(1); Everything works again. Another fix is to draw with the phong shader first and then use the picking shader and somehow that magically makes it ok. I'm guessing that in that case when attaching my vertex normals buffer while using the phong shader, it somehow stays when I then switch to the picking shader and the drawArray call works.

I'd like to know if I'm using gl.enableAttribArray wrong and should disable them when switching shaders or something like that.

I've also tried creating the shader programs in different order with no success.

解决方案

As you've probably figured out, useProgram doesn't affect anything but the program (shader code) supposed to run in the next draw call. You have to make sure only attributes used by the current program is enabled.

If you've wrapped your WebGL code somehow, a tip is to keep the highest available attribute number for each program stored somewhere in your wrapper, then compare with the latest used program and enable/disable accordingly before the draw call.

这篇关于使用两个或多个具有不同数量属性的着色器时发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆