2017-04-05 1 views
0

J'essaie d'ajouter des textures 2d sur la partie "Image Target" de Vuforia. J'essaie d'ajouter un SquareTexture d'un de mes autres projets. J'ai donc cette classe:Ajouter une texture sur l'exemple vuforia

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; 

/** 
* A two-dimensional square for use as a 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 this 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 to 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 up the drawing object data for use in an OpenGL ES context. 
    */ 
    Context localContext; 
    public SquareTexture(Context context) { 

     localContext = context; 
     // initialize vertex byte buffer for 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 for the 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 and 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 the vertex shader to program 
     GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to 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 the resource 
      Bitmap bitmap = BitmapFactory.decodeResource(localContext.getResources(), id); 

      // Bind to the 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 a PNG file with transparency, enable alpha blending. 

     checkGlError("glGetUniformLocation"); 

// 
// 
//   // Recycle the bitmap, since its data has been loaded into OpenGL. 



     return textureHandle[0]; 


    } 



    /** 
    * Encapsulates the OpenGL ES instructions for drawing this shape. 
    * 
    * @param mvpMatrix - The Model View Project matrix in which to draw 
    * this shape. 
    */ 

    public void draw(float[] mvpMatrix) { 

     // Add program to OpenGL environment 
     GLES20.glUseProgram(mProgram); 

     GLES20.glEnable(GLES20.GL_BLEND); 

     // get handle to vertex shader's vPosition member 
     mPositionHandle  = GLES20.glGetAttribLocation(mProgram, "aPosition"); 
     //checkGlError("glGetUniformLocation"); 
     mTexturePosHandle = GLES20.glGetAttribLocation(mProgram, "aTexPos"); 
     //checkGlError("glGetUniformLocation"); 
     // Enable a handle to the triangle vertices 

     // Prepare the 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"); 

     // get handle to shape's transformation matrix 
     mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
     //checkGlError("glGetUniformLocation"); 

     // Apply the projection and 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 the 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 for compiling a OpenGL shader. 
    * 
    * <p><strong>Note:</strong> When developing shaders, use the checkGlError() 
    * method to debug shader coding errors.</p> 
    * 
    * @param type - Vertex or fragment shader type. 
    * @param shaderCode - String containing the shader code. 
    * @return - Returns an id for the shader. 
    */ 
    public static int loadShader(int type, String shaderCode){ 

     // create a vertex shader type (GLES20.GL_VERTEX_SHADER) 
     // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) 
     int shader = GLES20.glCreateShader(type); 

     // add the source code to the shader and compile it 
     GLES20.glShaderSource(shader, shaderCode); 
     GLES20.glCompileShader(shader); 


     return shader; 
    } 

    /** 
    * Utility method for debugging OpenGL calls. Provide the name of the call 
    * just after making it: 
    * 
    * <pre> 
    * mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); 
    * AGLRenderer.checkGlError("glGetUniformLocation");</pre> 
    * 
    * If the operation is not successful, the check throws an error. 
    * 
    * @param glOperation - Name of the OpenGL call to 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); 
     } 
    } 

} 

ajouter la SquareTexture en fonction onSurfaceCreated et je tire quand renderFrame de la classe ImageTargetRenderer est appel.

Il n'y a pas d'erreur mais la texture n'apparaît pas. Dans mon autre projet, la même classe fonctionne correctement, mais ici, je peux voir le carré texturé. Edit: Merci, il a été indiqué un problème d'échelle mais maintenant j'ai un autre problème. L'image ne s'affiche pas correctement, il y a des points de transparence sur l'image.

enter image description here

+0

Je vous suggère fortement d'ouvrir une nouvelle question pour d'autres problèmes, car ce problème a été résolu - il peut être très difficile pour d'autres personnes ayant des problèmes de le suivre et de l'utiliser de cette manière ... – yakobom

Répondre

0

Puisque vous dites que vous avez pas d'erreur de gl, et comme vous ne l'avez pas mentionné, je suppose que cette classe est d'un projet sans Vuforia, mon premier suspect serait les transformations - il est probablement tiré, mais en dehors de l'écran ou trop peu. Vérifiez si la manière dont vous dessinez correspond au système de coordonnées de votre projet Vuforia, commencez par la mise à l'échelle du carré - essayez de le rendre très grand et de voir s'il est dessiné.

+0

dans le shader cette ligne, "" gl_Position = vec4 (aPosition.xy, 0.0, 1.0); \ n "" pour éviter les problèmes de mise à l'échelle. Je pense que cela devrait éviter le problème d'échelle? –

+0

Hmmm, non, je ne pense pas ... Essayez d'agrandir et voir – yakobom

+0

comment pourrais-je l'intensifier? –