Reputation: 1808
I'm using the following vertex shader found in a tutorial :
"uniform mat4 matriceDessin;" +
"uniform mat4 matriceVueCamera;" +
"uniform vec3 positionLumiere;" +
"attribute vec4 positionsSommets;" +
"attribute vec3 vecteursNormaux;" +
"attribute vec4 couleursSommets;" +
"varying vec4 v_Color;" +
"void main() {" +
"vec3 sommet,vecteurNormal,directionLumiere;"+
"float distance,diffuse;" +
"sommet = vec3(matriceVueCamera * positionsSommets);" +
"vecteurNormal = vec3(matriceVueCamera * vec4(vecteursNormaux, 0.0));" +
"distance = length(positionLumiere - sommet);" +
"directionLumiere = normalize(positionLumiere - sommet);" +
"diffuse = max(dot(vecteurNormal, directionLumiere), 0.1);" +
"diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));" +
"v_Color = couleursSommets;" +
"gl_Position = matriceDessin * positionsSommets;" +
"}";
To get the location of my variables, I use, after having successfully created and linked the program :
GLES31.glUseProgram(programme);
pointeurSurPosition = GLES31.glGetAttribLocation(programme, "positionsSommets");
pointeurSurVecteurNormal = GLES31.glGetAttribLocation(programme, "vecteursNormaux");
pointeurSurPositionLumière = GLES31.glGetUniformLocation(programme, "positionLumiere");
pointeurSurMatriceDessin = GLES31.glGetUniformLocation(programme, "matriceDessin");
pointeurSurMatriceVueCaméra = GLES31.glGetUniformLocation(programme, "matriceVueCamera");
The problem I face is that the calls made to retrieve the locations of matriceVueCamera
, positionLumiere
and vecteursNormaux
always return -1 (other location are correctly retrieved).
According to the doc, this could happen if the variables aren't actives, but one can notice they are used in the shader, hence they should be considered actives, at least as far as I understand the concept.
I tried to bind location to this variables with glBindAttribLocation
before linking the program but it didn't help.
Does anyone sees where my problem could be?
I use Open GL ES 3.1.
Upvotes: 1
Views: 82
Reputation: 210968
The local variable diffuse
is calculated by but never use in the program. Since the shader code is optimized by the driver, this causes that all the code is discarded, which is used to calculate diffuse
.
Since the uniform variables matriceVueCamera
, positionLumiere
and vecteursNormaux
are necessary only, to calculate diffuse
, they don't become an active program resources. They are not needed to process the program.
Probably you missed to use diffuse
, when v_Color
is calculated:
v_Color = couleursSommets * diffuse ;
See OpenGL ES Shading Language 3.00 Specification - 2.12.6 Uniform Variables; page 58:
7.6 Uniform Variables
Shaders can declare named uniform variables, as described in the OpenGL ES Shading Language Specification. Values for these uniforms are constant over a primitive, and typically they are constant across many primitives. A uniform is considered active if it is determined by the compiler and linker that the uniform will actually be accessed when the executable code is executed. In cases where the compiler and linker cannot make a conclusive determination, the uniform will be considered active.
Upvotes: 2