android - Why GLES20.glGetAttribLocation returns different values for different devices? -
i'm trying using opengl2.0 , create multiple cubes in 3d space, android devices.
the code runs in devices, not in others, , don't know why... (all devices have opengl 2.0 supported, , have recent android versions [5.0 or 6.0])
i know problem in -1 return value (see down)
int vertexshader = loadglshader(gles20.gl_vertex_shader, r.raw.vertex); int gridshader = loadglshader(gles20.gl_fragment_shader, r.raw.grid_fragment); int passthroughshader = loadglshader(gles20.gl_fragment_shader, r.raw.fragment_color); cubeprogram1 = gles20.glcreateprogram(); gles20.glattachshader(cubeprogram1, vertexshader); gles20.glattachshader(cubeprogram1, passthroughshader); gles20.gllinkprogram(cubeprogram1); gles20.gluseprogram(cubeprogram1); cubepositionparam1 = gles20.glgetattriblocation(cubeprogram1, "a_position"); cubenormalparam1 = gles20.glgetattriblocation(cubeprogram1, "a_normal"); ----> returns -1 value cubelightposparam1 = gles20.glgetuniformlocation(cubeprogram1, "u_lightpos"); cubemodelparam1 = gles20.glgetuniformlocation(cubeprogram1, "u_model") cubemodelviewparam1 = gles20.glgetuniformlocation(cubeprogram1, "u_mvmatrix"); cubemodelviewprojectionparam1 = gles20.glgetuniformlocation(cubeprogram1, "u_mvp");
....
gles20.glenablevertexattribarray(cubenormalparam1); ---> returns -1 ...
the cubenormalparam1 = gles20.glgetattriblocation(cubeprogram1, "a_normal");
returns value 2 in devices, , -1 in others. have -1 value, give error , doesn't run...
i'm trying see if vertex_shader have errors can't see problems...
uniform mat4 u_model; uniform mat4 u_mvp; uniform mat4 u_mvmatrix; uniform vec3 u_lightpos; attribute vec4 a_position; attribute vec4 a_color; attribute vec3 a_normal; attribute vec2 a_texcoordinate; varying vec4 v_color; varying vec3 v_grid; varying vec2 v_texcoordinate; void main() { v_grid = vec3(u_model * a_position); vec3 modelviewvertex = vec3(u_mvmatrix * a_position); vec3 modelviewnormal = vec3(u_mvmatrix * vec4(a_normal, 0.0)); float distance = length(u_lightpos - modelviewvertex); vec3 lightvector = normalize(u_lightpos - modelviewvertex); float diffuse = max(dot(modelviewnormal, lightvector), 0.5); diffuse = diffuse * (1.0 / (1.0 + (0.00001 * distance * distance))); v_color = a_color * diffuse; v_texcoordinate = a_texcoordinate; gl_position = u_mvp * a_position; }
why happen? hardware?
any solution fix this?
to draw cube i've this:
public void drawcube1() { cubenumber = 1; gles20.gluseprogram(cubeprogram1); gles20.gluniform3fv(cubelightposparam1, 1, lightposineyespace, 0); // set model in shader, used calculate lighting gles20.gluniformmatrix4fv(cubemodelparam1, 1, false, modelcube, 0); // set modelview in shader, used calculate lighting gles20.gluniformmatrix4fv(cubemodelviewparam1, 1, false, modelview, 0); // set modelviewprojection matrix in shader. gles20.gluniformmatrix4fv(cubemodelviewprojectionparam1, 1, false, modelviewprojection1, 0); // set position of cube gles20.glvertexattribpointer( cubepositionparam1, coords_per_vertex, gles20.gl_float, false, 0, cubevertices); // set normal positions of cube, again shading gles20.glvertexattribpointer(cubenormalparam1, 3, gles20.gl_float, false, 0, cubenormals); // set active texture unit texture unit 0. gles20.glactivetexture(gles20.gl_texture0); // bind texture unit. gles20.glbindtexture(gles20.gl_texture_2d, mtextura1); islookingatobject_number(cubenumber); // tell texture uniform sampler use texture in shader binding texture unit 0. gles20.gluniform1i(mtextureuniform, 0); gles20.glvertexattribpointer(mtexturecoordinate, 2, gles20.gl_float, false, 0, cubetexture); gles20.glenablevertexattribarray(mtexturecoordinate); gles20.glenablevertexattribarray(cubepositionparam1); gles20.glenablevertexattribarray(cubenormalparam1); gles20.gldrawarrays(gles20.gl_triangles, 0, 36); checkglerror("drawing cube"); }
why gles20.glgetattriblocation returns different values different devices?
... because different devices have different hardware setting binding tables, , in cases may optimize out redundant computations , repack used attributes save space. exactly why api has function call getting binding locations on device @ runtime; if deterministic constants wouldn't need run-time call @ ...
the fact returns -1 isn't error; showing attribute isn't needed (e.g. defined, can optimized out because doesn't contribute fragment pixel color), shouldn't cause problems.
however mean can't safely assume (e.g.) a_position
attribute 0, a_color
1, a_normal
2, etc. if you've hard coded in code it's entirely possible uploading wrong data each attribute. must query binding locations binding numbers use (or use shader-side static binding added in opengl es 3.x).
which actual gl api call setting gl error state?
Comments
Post a Comment