OpenGL ES 2.0 with iPhone - Vertex Shader uniform cannot be located
I have the following vertex shader:
uniform mediump mat4 projMx;
attribute vec2 a_position;
attribute vec4 a_color;
attribute float a_radius;
varying vec4 v_color;
void main()
{
vec4 position = vec4(100.0,60开发者_StackOverflow社区0.0,1.0,1.0);
gl_Position = projMx * position;
gl_PointSize = a_radius*2.0;
v_color = a_color;
}
..and the following fragment shader:
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
varying vec4 v_color;
void main()
{
gl_FragColor = v_color;
}
..and the following Obj-C code:
//..shaders have been created..
program = glCreateProgram();
glAttachShader(program, shaders[0]);
glAttachShader(program, shaders[1]);
GLfloat projMx[16] = {2/screenWidth,0,0,-1,0,2/-screenHeight,0,1,0,0,-2,-1,0,0,0,1};
projMxId = glGetUniformLocation(program, "projMx");
NSLog(@"uniform location:%i",projMxId);
The uniform location of 'projMx' is -1 (i.e. 'projMxId == -1' is true). Could someone please explain why this is the case?
You can only retrieve uniform locations after linking the program (what you don't do), as these are per-program state and known in every shader of the program. The GLSL compiler can optimize unused uniforms away during linking, so their locations should only be known after linking, which is also the moment when all attributes not explicitly bound get their locations.
精彩评论