Activating/using GL_TEXTURE1 at OpenGL ES 2.0 for Android -
i'm trying use gl_texture1 texture unit draw simple shape. know how draw using standard gl_texture0, when changing not working.
i thought code below, had change following:
glactivetexture(gl_texture1); gluniform1i(utexturelocation, 1);
what i'm missing?
code:
public class rendererclass implements renderer { context context; floatbuffer verticesinbuffer; int apositionlocation; int atexturelocation; int utexturelocation; int program; public rendererclass(context context){ this.context = context; } @override public void onsurfacecreated(gl10 arg0, eglconfig config) { gles20.glclearcolor(1.0f, 0.0f, 0.0f, 0.0f); float[] vertices = { -0.5f, 0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f }; verticesinbuffer = bytebuffer.allocatedirect(vertices.length*4).order(byteorder.nativeorder()).asfloatbuffer().put(vertices); string vss = "attribute vec4 a_position;" + "attribute vec2 a_texture;" + "varying vec2 v_texture;" + "void main(){" + " v_texture = a_texture;" + " gl_position = a_position;" + "}"; string fss = "precision mediump float;" + "varying vec2 v_texture;" + "uniform sampler2d u_texture;" + "void main(){" + " gl_fragcolor = texture2d(u_texture, v_texture);" + "}"; int vs = glcreateshader(gl_vertex_shader); int fs = glcreateshader(gl_fragment_shader); glshadersource(vs, vss); glshadersource(fs, fss); glcompileshader(vs); glcompileshader(fs); program = glcreateprogram(); glattachshader(program, vs); glattachshader(program, fs); gllinkprogram(program); apositionlocation = glgetattriblocation(program, "a_position"); // ***** texture stuff starts here </</</</ // fase 1 glactivetexture(gl_texture0); int[] gentextures = new int[1]; glgentextures(1, gentextures, 0); glbindtexture(gl_texture_2d, gentextures[0]); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear_mipmap_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest); // fase 2 bitmapfactory.options options = new bitmapfactory.options(); options.inscaled = false; bitmap bitmap1 = bitmapfactory.decoderesource(context.getresources(), r.drawable.res_for_test_1, options); // fase 3 teximage2d(gl_texture_2d, 0, bitmap1, 0); glgeneratemipmap(gl_texture_2d); // fase 4 atexturelocation = glgetattriblocation(program, "a_texture"); utexturelocation = glgetuniformlocation(program, "u_texture"); gluniform1i(utexturelocation, 0); verticesinbuffer.position(2); glenablevertexattribarray(atexturelocation); glvertexattribpointer(atexturelocation, 2, gl_float, false, 16, verticesinbuffer); // ***** texture stuff ends here </</</</ } @override public void onsurfacechanged(gl10 arg0, int width, int height) { gles20.glviewport(0, 0, width, height); } @override public void ondrawframe(gl10 glunused) { gles20.glclear(gles20.gl_color_buffer_bit); gluseprogram(program); verticesinbuffer.position(0); glenablevertexattribarray(apositionlocation); glvertexattribpointer(apositionlocation, 2, gl_float, false, 16, verticesinbuffer); gldrawarrays(gl_triangle_fan, 0, 6); } }
you need specify active texture unit , assign loaded texture it.
for convenience, i've created helper function of this. activates given texture unit, assigns texture given id , puts value sampler2d
uniform of shader:
protected void settexture2d(int textureunit, int textureid, int uniformid) { gles20.glactivetexture(gles20.gl_texture0 + textureunit); gles20.glbindtexture(gles20.gl_texture_2d, textureid); gles20.gluniform1i(uniformid, textureunit); }
and call this:
settexture2d(0, textureid, uniformid);
Comments
Post a Comment