Android OpenGL ES 2.0黑色纹理

我在Android上学习gles2.0(我是一个新手:),我想绘制一个纹理方块,但是我不能。 我GOOGLE了很多,但我无法弄清楚答案,我只得到一个黑色的方块。

我建立了一个GLSurfaceView,然后是一个渲染器:

public class MySurfaceRenderer implements GLSurfaceView.Renderer {

    private Context context;
    private Sprite sprite1;

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        sprite1.Draw();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA,GLES20.GL_ONE_MINUS_SRC_ALPHA);
        GLES20.glClearColor(0.0f, 0.3f, 0.0f, 1.0f);
        sprite1 = new Sprite(context, R.drawable.test);
    }

}

雪碧类:

public class Sprite {

    private static String TAG = "SpriteRender";

    private Context context;

    private FloatBuffer databuffer;
    private FloatBuffer texturebuffer;
    // x, y,
    float data[] =  {-0.5f,  -0.5f,
                     -0.5f, 0.5f,
                     0.5f, 0.5f,
                     0.5f, -0.5f};
    // u,v 
    float texturedata[] =   {0.0f, 0.0f,
                            0.0f, 1.0f,
                            1.0f, 1.0f,
                            1.0f, 0.0f};
    private int mProgram;
    private int vertexShader;
    private int fragmentShader;
    private int texture;
    private int positionHandler;
    private int texCoordHandler;
    private int textureHandler;

    public Sprite(Context context, int textureid){
        this.context = context;

        ByteBuffer bb_data = ByteBuffer.allocateDirect(data.length * 4);
        bb_data.order(ByteOrder.nativeOrder());
        ByteBuffer bb_texture = ByteBuffer.allocateDirect(texturedata.length * 4);
        bb_texture.order(ByteOrder.nativeOrder());

        databuffer = bb_data.asFloatBuffer();
        databuffer.put(data);
        databuffer.position(0);
        texturebuffer = bb_texture.asFloatBuffer();
        texturebuffer.put(texturedata);
        texturebuffer.position(0);

        vertexShader = ShaderLoader.loadShader(GLES20.GL_VERTEX_SHADER, ShaderLoader.readFromAssert(context, R.raw.tvs));
        fragmentShader = ShaderLoader.loadShader(GLES20.GL_FRAGMENT_SHADER, ShaderLoader.readFromAssert(context, R.raw.tfs));

        mProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glLinkProgram(mProgram);

        int[] linkStatus = new int[1];
        GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);

        if (linkStatus[0] == 0){
            // Displays error message
        }
        positionHandler     = GLES20.glGetAttribLocation(mProgram, "a_Position");
        texCoordHandler     = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");
        textureHandler      = GLES20.glGetUniformLocation(mProgram, "u_Texture");

        texture = TextureLoader.loadTexture(context, textureid);
    }

    public void Draw(){
        GLES20.glUseProgram(mProgram);
        databuffer.position(0);
        texturebuffer.position(0);  

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);

        GLES20.glUniform1i(textureHandler, 0);

        GLES20.glEnableVertexAttribArray(texCoordHandler);
        GLES20.glEnableVertexAttribArray(positionHandler);

        GLES20.glVertexAttribPointer(positionHandler, 2, GLES20.GL_FLOAT, false, 8, databuffer);
        GLES20.glVertexAttribPointer(texCoordHandler, 2, GLES20.GL_FLOAT, false, 8, texturebuffer);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
    }

}

ShaderLoader.loadShader从文件加载着色器(在res / raw中),TextureLoader.loadTexture从资源加载纹理:

public static int loadTexture(Context context, int resource){
    int[] texture = new int[1];

    GLES20.glGenTextures(1, texture, 0);

    BitmapFactory.Options bo = new BitmapFactory.Options();
    bo.inScaled = false;
    Bitmap tex = BitmapFactory.decodeResource(context.getResources(), resource, bo);

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

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, tex, 0);
    tex.recycle();

    if(texture[0] == 0){
        // Displays error
    }

    return texture[0];

}

顶点着色器:

attribute vec4 a_Position;
attribute vec2 a_TexCoordinate;
varying vec2 v_TexCoordinate;

void main() {
  v_TexCoordinate = a_TexCoordinate;
  gl_Position = a_Position;
}

片段着色器:

precision mediump float;
varying vec2 v_TexCoordinate;
uniform sampler2D u_Texture;

void main() {
  gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
}

我看了一些东西:

  • 经过png,jpg,bmp(565)测试,分辨率:32x32和64x64
  • 在所有行glGetError()返回0后的绘制函数中
  • 加载纹理时,Sprite.texture不是0(约70001)
  • 如果我将片段着色器更改为gl_FragColor = vec4(1.0,0.0,0.0,0.5); 它正在工作。
  • 感谢帮助!


    我已经想出了答案:

    当我加载纹理时,我没有正确传递给OpenGL。

    我添加了TextureLoader.loadTexture GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texture [0]); 如下所示:

    ...
    Bitmap tex = BitmapFactory.decodeResource(context.getResources(), resource, bo);    
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]); 
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    ...
    

    现在它正在工作。

    链接地址: http://www.djcxy.com/p/33983.html

    上一篇: Android OpenGL ES 2.0 black textures

    下一篇: Rendering rectangle texture with GLSL