Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在vuforia示例上添加纹理_Android_Opengl Es 2.0_Vuforia - Fatal编程技术网

Android 在vuforia示例上添加纹理

Android 在vuforia示例上添加纹理,android,opengl-es-2.0,vuforia,Android,Opengl Es 2.0,Vuforia,我尝试在Vuforia“图像目标部分”上添加2d纹理。我尝试从我的另一个项目中添加SquareTexture。 所以我有一节课: package com.vuforia.samples.VuforiaSamples.app.ImageTargets; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.openg

我尝试在Vuforia“图像目标部分”上添加2d纹理。我尝试从我的另一个项目中添加SquareTexture。 所以我有一节课:

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);
        }
    }

}
package com.vuforia.samples.VuforiaSamples.app.ImageTargets;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.opengl.GLES20;
导入android.opengl.GLUtils;
导入android.util.Log;
导入java.nio.ByteBuffer;
导入java.nio.ByteOrder;
导入java.nio.FloatBuffer;
导入java.nio.ShortBuffer;
/**
*在OpenGL ES 2.0中用作绘制对象的二维正方形。
*/
公共类{
私有静态int[]textureHandle;
私有最终字符串vertexShaderCode=
“统一mat4 uMVPMatrix;\n”+
“属性向量2位置;\n”+
“属性向量2 aTexPos;\n”+
“可变的vec2 vTexPos;\n”+
“void main(){\n”+
“vtexos=aTexPos;\n”+
“gl_Position=uMVPMatrix*vec4(aPosition.xy,0.0,1.0);\n”+
//gl_位置=vec4(aPosition.xy,0.0,1.0);\n+
"}";
私有最终字符串碎片ShaderCode=
“精度中间泵浮动;\n”+
“统一采样器2D输出;\n”+
“可变的vec2 vTexPos;\n”+
“作废主(作废)\n”+
“{\n”+
//gl_FragColor=texture2D(uTexture,vec2(vtexos.y,vtexos.x));\n+
“gl_FragColor=vec4(1,1,0,(vtexos.x+vtexos.y)/2.0);\n”+
"}";
私有最终FloatBuffer vertexBuffer;
私有最终FloatBuffer vertexturebuffer;
私有最终短缓冲区drawListBuffer;
私人期末考试程序;
私有int-mPositionHandle;
私有int mTexturePosHandle;
米科洛汉德尔私人酒店;
私有int mMVPMatrixHandle;
//此数组中每个顶点的坐标数
每个顶点的最终整数坐标=2;
浮点方坐标[]={
-1.0f,1.0f,
1.0f,1.0f,
1.0f,-1.0f,
-1.0f,-1.0f,
};
浮点数平方坐标结构[]={
0.0f,0.0f,//顶点3
0.0f,1.0f,//顶点1
1.0f,1.0f,//顶点0
1.0f,0.0f,//顶点2
};//右上角
private final short drawOrder[]={0,1,2,0,2,3};//绘制顶点的顺序
private final int vertexStride=COORDS\u PER\u VERTEX*4;//每个顶点4字节
浮动颜色[]={0.2f,0.7098039222F,0.898039216f,1.0f};
/**
*设置图形对象数据以在OpenGL ES上下文中使用。
*/
语境局部语境;
公共结构(上下文){
localContext=context;
//初始化形状坐标的顶点字节缓冲区
ByteBuffer bb=ByteBuffer.allocateDirect(
//(#坐标值*每个浮点4字节)
方形坐标长度*4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer=bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
顶点缓冲区位置(0);
ByteBuffer bbt=ByteBuffer.allocateDirect(
//(#坐标值*每个浮点4字节)
方形坐标结构长度*4);
bbt.order(ByteOrder.nativeOrder());
VertextureBuffer=bbt.asFloatBuffer();
vertexturebuffer.put(squareCoordstexture);
VertextureBuffer.position(0);
//初始化绘图列表的字节缓冲区
ByteBuffer dlb=ByteBuffer.allocateDirect(
//(#坐标值*每短2字节)
提款单长度*2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer=dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.位置(0);
检查错误(“glGetUniformLocation”);
//准备着色器和OpenGL程序
int vertexShader=loadShader(
GLES20.GL_顶点_着色器,
顶点着色代码);
int fragmentShader=loadShader(
GLES20.GL_片段_着色器,
片段着色器代码);
mProgram=GLES20.glCreateProgram();//创建空的OpenGL程序
GLES20.glAttachShader(mProgram,vertexShader);//将顶点着色器添加到程序中
GLES20.glAttachShader(mProgram,fragmentShader);//将片段着色器添加到程序中
GLES20.glLinkProgram(mProgram);//创建OpenGL程序可执行文件
字符串LogInfo=GLES20.glGetProgramInfoLog(mProgram);
检查错误(“glGetUniformLocation”);
}
int帧=0;
公共int loadTexture()
{
textureHandle=新整数[80];
检查错误(“glGetUniformLocation”);

对于(int i=0;i既然你说你没有gl错误,而且你没有提到它,我猜这个类来自一个没有Vuforia的项目,我的第一个怀疑是转换-它可能是绘制的,但在屏幕外或太少。检查你绘制它的方式是否适合Vuforia项目的坐标系,从缩放squar-试着把它画得很大,看看它是否被画出来。

这就是我在着色器中画那条线的原因,“gl_Position=vec4(aPosition.xy,0.0,1.0);\n”为了避免缩放问题。我认为这应该可以避免缩放问题?嗯,不,我不这么认为…试着放大它,看看我如何放大它?Vuforia示例中有这个-我不知道您使用的是什么,但搜索“缩放”代码我强烈建议你为其他问题提出一个新问题,因为这个问题已经解决了-其他有问题的人可能很难跟踪它并以这种方式使用它。。。