java - Android: texture shows up as solid color -


i'm attempting texture show on on square made triangle fan, texture made canvas. main color yellow , smaller box drawn inside of it, final texture solid yellow.

yellow square no texture (picture)

enter image description here

fragment shadder:

public static final string fragmentshadercode_textured =                 "precision mediump float;" +                 "varying vec2 v_texcoord;" +                 "uniform sampler2d s_texture;" +                 "void main() {" +                 //"gl_fragcolor = vcolor;"+                 "  gl_fragcolor = texture2d( s_texture, v_texcoord );" +                 "}"; 

texture generation:

public static int loadgltexture(string s){     rect r = new rect();     threaddat.get().paint.gettextbounds(s, 0, 1, r); //get string dimensions, yeilds 8x9 pxls     bitmap bitmap = bitmap.createbitmap(bestsize(r.width()),bestsize(r.height()), bitmap.config.argb_8888);     //example size 16x16pxls     log.i("texturesize", r.width() + " " + r.height());     canvas c = new canvas(bitmap);      //some temporary test code setting background yellow     //paint colors stored per thread, 1 right     threaddat.get().paint.setargb(255, 255, 255, 0);     c.drawrect(0, 0, c.getwidth(), c.getheight(), threaddat.get().paint);     //type letter, in case "a" in blue     threaddat.get().paint.setargb(255, 0, 0, 255);     threaddat.get().paint.settypeface(typeface.create("consolas", typeface.normal));     c.drawtext(s.charat(0) + "", 0, 0, threaddat.get().paint);     //draw square half width , height, should blue     c.drawrect(0, 0, c.getwidth() / 2, c.getheight() / 2, threaddat.get().paint);     return loadtexture(bitmap); } 

draw code:

@override public void draw() {     //clearing error check if program has error     gles20.glgeterror();     //get compiled shader textured shapes     int prgm = myglrenderer.getstrd_txtr_shdr();     gles20.gluseprogram(prgm);     //check new errors , log logcat (nothing)     myglrenderer.logerror();      //setup projection view matrix     float[] scratch = new float[16];     matrix.setidentitym(scratch, 0);     matrix.multiplymm(scratch, 0, myglrenderer.getmmvpmatrix(), 0, scratch, 0);     //apply translations matrix     matrix.translatem(scratch, 0, xoffset, yoffset, zoffset);     matrix.setrotateeulerm(scratch, 0, yaw, pitch, roll);       //get vposition variable handle chosen shader     mposhandle = gles20.glgetattriblocation(prgm, "vposition");     gles20.glenablevertexattribarray(mposhandle);      gles20.glvertexattribpointer(mposhandle, coords_per_vertex, gles20.gl_float,             false, vertex_stride, vertexbuffer);      ////pass color data (set white)     //mcolorhandle = gles20.glgetuniformlocation(prgm, "vcolor");     //gles20.gluniform4fv(mcolorhandle, 1, color, 0);       //use texture0     gles20.glactivetexture(gles20.gl_texture0);     //use texture -> int textureid = myglrenderer.loadgltexture("a");     gles20.glbindtexture(gles20.gl_texture_2d, textureid);     //get handel "uniform sampler2d s_texture;" set value     int txturehandle = gles20.glgetuniformlocation(prgm, "s_texture");     gles20.gluniform1i(txturehandle, 0); //set s_texture use binded texture 0       //pass in texture coords (u,v / s,t)     int texturecoordhndl = gles20.glgetattriblocation(prgm, "a_texcoord");     gles20.glvertexattribpointer(texturecoordhndl, 2/*size, 2 points per vector*/,             gles20.gl_float, false, 0, texturebuffer);      //pass in  model view projection matrix     mmvpmatrixhandle = gles20.glgetuniformlocation(prgm, "umvpmatrix");     gles20.gluniformmatrix4fv(mmvpmatrixhandle, 1, false, scratch, 0);        gles20.gldrawarrays(gles20.gl_triangle_strip, 0, vertex_count);      gles20.gldisablevertexattribarray(mposhandle);     myglrenderer.logerror(); } 

i tried using same coordinate set used in example:

vertices square:

{           0, 0, 0,  //bottom left             0, height, 0,  //topleft             width, 0, 0,  // bottom right             width, height, 0)}; //topright 

texture coords:

        0.0f, 1.0f,     // top left     (v2)         0.0f, 0.0f,     // bottom left  (v1)         1.0f, 1.0f,     // top right    (v4)         1.0f, 0.0f      // bottom right (v3) 

similar issue

this sound there issue texture coordinates. since whole thing yellow suspect v_texcoord (0,0) in fragment shader first texture pixel being repeated.

the texture seems ok since color being drawn. without texture see black rectangle.

anyway handle such issues need bit inventive in debugging, testing. testing coordinates use gl_fragcolor = vec4( v_texcoord.x, v_texcoord.y, .0, 1.0 );. should output gradient rectangle top left black, top right red, bottom left green. if not see result texture coordinates incorrect. in case first check if varying correctly connected vertex shader. may use v_texcoord = vec2(1.0, 0.0) in vertex shader , result should red rectangle (assuming still have previous test in fragment shader). if rectangle red issue in handles , not in shaders (otherwise varying incorrectly set. maybe mismatch in naming). check value of handle texturecoordhndl. if negative value handle not connected. due mismatch in naming.

from further inspection:

you missing enabling of attribute texture coordinates gles20.glenablevertexattribarray(texturecoordhndl);. remember each of attributes must enabled before use them.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -