GLSL着色器将不会呈现不统一的变彩

GLSL着色器将不会呈现不统一的变彩

本文介绍了GLSL着色器将不会呈现不统一的变彩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设为Android的应用程序,但遇到了一些问题,着色器拒绝渲染。

I am currently building an app for Android, but have run into some problems with a shader that refuses to render.

考虑下面的片段着色器:

Consider the following fragment shader:

uniform vec4 color;
void main(){
    gl_FragColor = vec4(1.0);
}

此着色器工作正常固体颜色(白色在这种情况下)绘制对象。均匀矢量颜色被优化掉,而不能与 glGetUniformLocation()找到(返回-1)。

This shader works fine for drawing an object in a solid color (white in this case). The uniform vector color is optimized away, and cannot be found with glGetUniformLocation() (returns -1).

试图从统一的变量的颜色可以像这样做:

Trying to get the color from the uniform variable can be done like so:

uniform vec4 color;
void main(){
    gl_FragColor = color;
}

然而,当我用这个,没有什么渲染。该着色器已成功创建和 glGetUniformLocation()返回颜色的有效值(0)。但没有显示在屏幕上,甚至不黑。所做的唯一的变化是更换 vec4(1.0)颜色。这code具有相同的结果:

However, when I use this, nothing renders. The shader is created successfully and glGetUniformLocation() returns a valid value (0) for color. But nothing shows on screen, not even black. The only change made is replacing vec4(1.0) with color.This code has the same result:

uniform vec4 color;
void main(){
    gl_FragColor = vec4(1.0)+color;
}

但奇怪的是,当我试图着色器在不同的项目,它的作品,因为它应该,所以问题一定是出在我的code做的其他地方。
这是我的绘制方法(请记住,它的工作原理当彩色变量不是在着色器):

The strange thing is that when I tried the shader in a different project, it works as it should, so the problem must be something I do elsewhere in the code.
This is my drawing method (keep in mind that it works when the color-variable is not in the shader):

GLES20.glUseProgram(colorshader);
GLES20.glUniform4f(colorIndex, 1, 1, 1, 1); //colorIndex is the result of glGetUniformLocation() called with the correct shader index and variable name.

Matrix.multiplyMM(mvpMatrix, 0, vpMatrix, 0, matrix, 0);
GLES20.glUniformMatrix4fv(matrixindex, 1, false, mvpMatrix, 0);

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertices);
GLES20Fix.glVertexAttribPointer(Shader.ATTRIBUTE_VERTEX, 3, GLES20.GL_FLOAT, false, 12, 0);

GLES20.glLineWidth(width);
GLES20.GL_UNSIGNED_SHORT, 0);
GLES20.glDrawArrays(GLES20.GL_LINES, 0, count);

我完全不知道什么可能导致这种奇怪的行为,如果任何人有任何意见或可能的解决方案,请帮助我。

I have absolutely no idea what might be causing this odd behaviour, if anyone have any ideas or possible solutions, please help me.

更新:
看来,使用不采样任何统一的变量会导致在这这种行为(也只有这个)着色器。

Update:
It seems that using any uniform variable that is not a sampler causes this behaviour in this (and only this) shader.

更新2 使用 glGetError()返回错误code 502:GL_INVALID_OPERATION

Update 2Using glGetError() return error code 502: GL_INVALID_OPERATION.

推荐答案

好了,我终于想通测试的负载后出来。
在我的code,我使用的多个着色器的,但我不小心只得到了mvpMatrix,制服的一个的着色器,并使用一个用于每个着色器,这有趣的是工作了所有的着色器我创建这个以前有(它有ID为0所有早期着色器)。
然而,随着这个shader看来,我的新vec4有ID为0,这引起了code错误地分配我的矢量数据的MVP矩阵。获得新的ID为每个着色器所做的工作方案。

Ok, so I finally figured it out after a load of testing.
In my code, I was using multiple shaders, but I had accidentally only got the mvpMatrix-uniform for one shader and was using that one for every shader, which funnily enough worked for all shaders I had before creating this one (it got id 0 for all the earlier shaders).
However with this shader it seems that my new vec4 got id 0, which caused the code to wrongly assign my vector data to the mvp-matrix. getting new ids for each shader made the program work.

这篇关于GLSL着色器将不会呈现不统一的变彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:49