java - drawing textures in opengl es 2.0 -


i have been having problem drawing texture in opengl es 2 on android 3 days , decided should after researching lead dead ends. problem texture drawn black rectangle , not image.

public class sprite  {     private static final shader spriteshader = shadermanager.getinstance().getshader(1);     private static final short[] draworder = { 0, 1, 2, 0, 2, 3 };     private static shortbuffer drawlistbuffer;      static      {         bytebuffer bb = bytebuffer.allocatedirect(draworder.length * 2);         bb.order(byteorder.nativeorder());          drawlistbuffer = bb.asshortbuffer();         drawlistbuffer.put(draworder);         drawlistbuffer.position(0);          }      private context m_context;     private int m_textureid = 0;     private floatbuffer m_vertexbuffer;     private float[] m_coords =          {             -0.5f,  0.5f, 0.0f, // position 0              0.0f,  0.0f, // texcoord 0             -0.5f, -0.5f, 0.0f, // position 1              0.0f,  1.0f, // texcoord 1              0.5f, -0.5f, 0.0f, // position 2              1.0f,  1.0f, // texcoord 2              0.5f,  0.5f, 0.0f, // position 3              1.0f,  0.0f // texcoord 3         };       public sprite(context context)     {         m_context = context;          bytebuffer bb = bytebuffer.allocatedirect(m_coords.length * 4);         bb.order(byteorder.nativeorder());          m_vertexbuffer = bb.asfloatbuffer();         m_vertexbuffer.put(m_coords);         m_vertexbuffer.position(0);     }      public void initialize(string file)     {         inputstream inputstream = null;          try          {             inputstream = m_context.getassets().open(file);             m_textureid = loadtexture(inputstream);         }          catch (ioexception e)          {}                 {             util.closestream(inputstream);         }     }      public void draw()     {         spriteshader.use();          int position = spriteshader.getattriblocation("vposition");         int texturecoords = spriteshader.getattriblocation("atexcoords");         int texture = spriteshader.getuniformlocation("utexture");          m_vertexbuffer.position(0);         gles20.glvertexattribpointer(position, 3, gles20.gl_float, false, 20, m_vertexbuffer);         util.checkglerror("vertex pointer poistion");         m_vertexbuffer.position(3);         gles20.glvertexattribpointer(texturecoords, 2, gles20.gl_float, false, 20, m_vertexbuffer);         util.checkglerror("vertex pointer texturecoords");         gles20.glenablevertexattribarray(position);         gles20.glenablevertexattribarray(texturecoords);          gles20.glactivetexture(gles20.gl_texture0);         util.checkglerror("active trexture");         gles20.glbindtexture(gles20.gl_texture_2d, m_textureid);         util.checkglerror("bind texture");          gles20.gluniform1i(texture, 0);         util.checkglerror("uniform1i");         gles20.gldrawelements(gles20.gl_triangles, draworder.length, gles20.gl_unsigned_short, drawlistbuffer);         util.checkglerror("drawelements");      }      private int loadtexture(inputstream inputstream)     {         int[] texture = new int[1];         gles20.glgentextures(1, texture, 0);          gles20.glbindtexture(gles20.gl_texture_2d, texture[0]);           gles20.gltexparameteri(gles20.gl_texture_2d, gles20.gl_texture_min_filter, gles20.gl_nearest);         gles20.gltexparameteri(gles20.gl_texture_2d, gles20.gl_texture_mag_filter, gles20.gl_nearest);         gles20.gltexparameteri(gles20.gl_texture_2d, gles20.gl_texture_wrap_s, gles20.gl_clamp_to_edge);         gles20.gltexparameteri(gles20.gl_texture_2d, gles20.gl_texture_wrap_t, gles20.gl_clamp_to_edge);          bitmap bitmap = bitmapfactory.decodestream(inputstream);         glutils.teximage2d(gles20.gl_texture_2d, 0, bitmap, 0);         bitmap.recycle();          return texture[0];     } } 

vertex shader

attribute vec2 atexcoords; attribute vec4 vposition;  varying vec2 vtexcoords;  void main() { vtexcoords = atexcoords; gl_position = vposition; } 

fragment shader

precision mediump float;  uniform sampler2d utexture; varying vec2 vtexcoords;  void main() { gl_fragcolor = texture2d(utexture, vtexcoords); } 

you try using separate arrays vertexcoords , texcoords.

private float[] texcoords = {0,0,                              0,1,                              1,1,                              1,0}; 

and create separate float buffer texture coords. use stride value of 0 in glvertexatrribpointer call, mean array considered tightly packed.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -