android - Draw a texture with Vuforia -


i try display simple texture on vuforia project. define squaretexture class this:

package com.vuforia.samples.vuforiasamples.app.imagetargets;  import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.opengl.gles20; import android.opengl.glutils; import android.util.log;  import java.nio.bytebuffer; import java.nio.byteorder; import java.nio.floatbuffer; import java.nio.shortbuffer;  /**  * two-dimensional square use drawn object in opengl es 2.0.  */ public class squaretexture {      private static int[] texturehandle;      private final string vertexshadercode =             "uniform mat4 umvpmatrix;\n" +                     "attribute vec2 aposition;\n" +                     "attribute vec2 atexpos;\n" +                     "varying vec2 vtexpos;\n" +                     "void main() {\n" +                     "  vtexpos = atexpos;\n" +                     "  gl_position = umvpmatrix * vec4(aposition.xy, 0.0, 1.0);\n" +                     //"  gl_position = vec4(aposition.xy, 0.0, 1.0);\n" +                     "}";      private final string fragmentshadercode =             "precision mediump float;\n"+                     "uniform sampler2d utexture;\n" +                     "varying vec2 vtexpos;\n" +                     "void main(void)\n" +                     "{\n" +                     //"  gl_fragcolor = texture2d(utexture, vec2(vtexpos.y,vtexpos.x));\n" +                     "  gl_fragcolor = vec4( 1,1,0,(vtexpos.x+vtexpos.y)/2.0);\n" +                     "}";      private final floatbuffer vertexbuffer;     private final floatbuffer vertextexturebuffer;     private final shortbuffer drawlistbuffer;     private final int mprogram;     private int mpositionhandle;     private int mtextureposhandle;     private int mcolorhandle;     private int mmvpmatrixhandle;       // number of coordinates per vertex in array     final int coords_per_vertex = 2;      float squarecoords[] = {              -1.0f, 1.0f,             1.0f, 1.0f,             1.0f,-1.0f,             -1.0f,-1.0f,     };      float squarecoordstexture[] = {               0.0f, 0.0f, // vertex 3             0.0f, 1.0f, // vertex 1             1.0f, 1.0f, // vertex 0             1.0f, 0.0f, // vertex 2          }; // top right      private final short draworder[] = { 0, 1, 2, 0, 2, 3 }; // order draw vertices      private final int vertexstride = coords_per_vertex * 4; // 4 bytes per vertex      float color[] = { 0.2f, 0.709803922f, 0.898039216f, 1.0f };      /**      * sets drawing object data use in opengl es context.      */     context localcontext;     public squaretexture(context context) {          localcontext = context;         // initialize vertex byte buffer shape coordinates         bytebuffer bb = bytebuffer.allocatedirect(                 // (# of coordinate values * 4 bytes per float)                 squarecoords.length * 4);         bb.order(byteorder.nativeorder());         vertexbuffer = bb.asfloatbuffer();         vertexbuffer.put(squarecoords);         vertexbuffer.position(0);           bytebuffer bbt = bytebuffer.allocatedirect(                 // (# of coordinate values * 4 bytes per float)                 squarecoordstexture.length * 4);         bbt.order(byteorder.nativeorder());         vertextexturebuffer = bbt.asfloatbuffer();         vertextexturebuffer.put(squarecoordstexture);         vertextexturebuffer.position(0);           // initialize byte buffer draw list         bytebuffer dlb = bytebuffer.allocatedirect(                 // (# of coordinate values * 2 bytes per short)                 draworder.length * 2);         dlb.order(byteorder.nativeorder());         drawlistbuffer = dlb.asshortbuffer();         drawlistbuffer.put(draworder);         drawlistbuffer.position(0);          checkglerror("glgetuniformlocation");         // prepare shaders , opengl program         int vertexshader = loadshader(                 gles20.gl_vertex_shader,                 vertexshadercode);         int fragmentshader = loadshader(                 gles20.gl_fragment_shader,                 fragmentshadercode);          mprogram = gles20.glcreateprogram();             // create empty opengl program         gles20.glattachshader(mprogram, vertexshader);   // add vertex shader program         gles20.glattachshader(mprogram, fragmentshader); // add fragment shader program         gles20.gllinkprogram(mprogram);                  // create opengl program executables         string loginfo = gles20.glgetprograminfolog(mprogram);            checkglerror("glgetuniformlocation");       }             int frame = 0;     public  int loadtexture()     {          texturehandle = new int[80];            checkglerror("glgetuniformlocation");          for(int i=0;i<80;i++) {             gles20.glgentextures(1, texturehandle, i);             int id = localcontext.getresources().getidentifier("ppp" + i, "drawable", localcontext.getpackagename()); //            log.d("loadtexture", id + " vs " + r.drawable.alpha0);             //final bitmapfactory.options options = new bitmapfactory.options();             //options.inscaled = false;   // no pre-scaling             // read in resource             bitmap bitmap = bitmapfactory.decoderesource(localcontext.getresources(), id);              // bind texture in opengl             gles20.glbindtexture(gles20.gl_texture_2d, texturehandle[i]);             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);             glutils.teximage2d(gles20.gl_texture_2d, 0, gles20.gl_rgba, bitmap, 0);              bitmap.recycle();         }       /*  glutils.teximage2d(gles20.gl_texture_2d, 0, gles20.gl_rgba, 640, 480,                 0, gles20.gl_rgba, gles20.gl_unsigned_byte, intbuffer.wrap(pixels));*/ //        // since we're using png file transparency, enable alpha blending.          checkglerror("glgetuniformlocation");  // // //            // recycle bitmap, since data has been loaded opengl.            return texturehandle[0];       }        /**      * encapsulates opengl es instructions drawing shape.      *      * @param mvpmatrix - model view project matrix in draw      * shape.      */      public void draw(float[] mvpmatrix) {          // add program opengl environment         gles20.gluseprogram(mprogram);          gles20.glenable(gles20.gl_blend);          // handle vertex shader's vposition member         mpositionhandle     = gles20.glgetattriblocation(mprogram, "aposition");         //checkglerror("glgetuniformlocation");         mtextureposhandle   = gles20.glgetattriblocation(mprogram, "atexpos");         //checkglerror("glgetuniformlocation");         // enable handle triangle vertices          // prepare triangle coordinate data         gles20.glvertexattribpointer(                 mpositionhandle, 2,                 gles20.gl_float, false,                 8, vertexbuffer);          gles20.glenablevertexattribarray(mpositionhandle);         //checkglerror("glgetuniformlocation");           gles20.glvertexattribpointer(                 mtextureposhandle, 2,                 gles20.gl_float, false,                 8, vertextexturebuffer);          gles20.glenablevertexattribarray(mtextureposhandle);         //checkglerror("glgetuniformlocation");          // handle shape's transformation matrix         mmvpmatrixhandle = gles20.glgetuniformlocation(mprogram, "umvpmatrix");         //checkglerror("glgetuniformlocation");          // apply projection , view transformation         gles20.gluniformmatrix4fv(mmvpmatrixhandle, 1, false, mvpmatrix, 0);         //checkglerror("gluniformmatrix4fv");            int utexture = gles20.glgetuniformlocation(mprogram, "utexture");         gles20.glactivetexture(gles20.gl_texture0);         gles20.glbindtexture(gles20.gl_texture_2d, texturehandle[((frame++)/10)%80]);         //checkglerror("glgetuniformlocation");         gles20.gluniform1i(utexture, 0);         //checkglerror("glgetuniformlocation");          // gles20.glclear(gles20.gl_color_buffer_bit);         // draw square           gles20.gldrawelements(                 gles20.gl_triangles, draworder.length,                 gles20.gl_unsigned_short, drawlistbuffer);          // disable vertex array         gles20.gldisablevertexattribarray(mpositionhandle);          gles20.gldisable(gles20.gl_blend);          frame++;     }       /**      * utility method compiling opengl shader.      *      * <p><strong>note:</strong> when developing shaders, use checkglerror()      * method debug shader coding errors.</p>      *      * @param type - vertex or fragment shader type.      * @param shadercode - string containing shader code.      * @return - returns id shader.      */     public static int loadshader(int type, string shadercode){          // create vertex shader type (gles20.gl_vertex_shader)         // or fragment shader type (gles20.gl_fragment_shader)         int shader = gles20.glcreateshader(type);          // add source code shader , compile         gles20.glshadersource(shader, shadercode);         gles20.glcompileshader(shader);           return shader;     }      /**      * utility method debugging opengl calls. provide name of call      * after making it:      *      * <pre>      * mcolorhandle = gles20.glgetuniformlocation(mprogram, "vcolor");      * aglrenderer.checkglerror("glgetuniformlocation");</pre>      *      * if operation not successful, check throws error.      *      * @param gloperation - name of opengl call check.      */     public static void checkglerror(string gloperation) {         int error;         while ((error = gles20.glgeterror()) != gles20.gl_no_error) {             log.e("squaretexture", gloperation + ": glerror " + error);             throw new runtimeexception(gloperation + ": glerror " + error);         }     }  } 

here result of display : enter image description here

i don't understand why there transparency dots on texture. me texture show draw properly.

why appends?


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -