0

我正在尝试在我的应用程序中加载纹理(480x720px)作为背景,但它显示为黑色。我对 GLES20 有点陌生,所以我在互联网上遵循了一些示例来构建我的代码,但这并没有让我走运。怎么修?

这是我的着色器代码:

public class Shaders {

private int mProgram;
private int maPositionHandle;
private int texCoords;

FloatBuffer texBuffer;
int iBaseMap;

private final String vertexShaderCode = 
                "attribute vec4 a_position;" +
                "attribute vec2 a_texCoords;" +
                "varying vec2 v_texCoords;" +
                "void main()" +
                "{" +
                "gl_Position = a_position;" +
                "v_texCoords = a_texCoords;" +
                "}";

private final String fragmentShaderCode = 
                "precision mediump float;" +
                "varying vec2 v_texCoords;" +
                "uniform sampler2D u_baseMap;" +
                "void main()" +
                "{" +
                "gl_FragColor = texture2D(u_baseMap, v_texCoords);" +
                "}";

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

public void initShaders() {

    GLES20.glEnable(GLES20.GL_TEXTURE_2D);

    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

    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);                  // creates OpenGL program executables

    // get handle to the vertex shader's vPosition member
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_position");
    iBaseMap = GLES20.glGetUniformLocation(mProgram, "u_baseMap");
    texCoords = GLES20.glGetAttribLocation(mProgram, "a_texCoords");

    GLES20.glDeleteShader(vertexShader);
    GLES20.glDeleteShader(fragmentShader);

    GLES20.glViewport(0, 0, Settings.width, Settings.height);

}

public int getMPH() {

    return maPositionHandle;

}

public int getMProg() {

    return mProgram;

}

public int getIBaseMap() {

    return iBaseMap;

}   

public int getTexCoords() {

    return texCoords;

}   

public int loadTexture(GLSurfaceView view, int imgResID){

    Bitmap img = null;
    int textures[] = new int[1];

    img = BitmapFactory.decodeResource(view.getResources(), imgResID);

    System.out.println(imgResID);

    GLES20.glGenTextures(1, textures, 0);

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);

    img.recycle();

    return textures[0];

}

}

以及我绘制背景的课程:

public class Background {

Shaders shaders;

int texId;

private GLSurfaceView mGLView;

private float BackgroundCoords[] = {

        // X, Y, Z
        -1f, -1f, 0, 0,0,
        -1f,  1f, 0, 0, 1,
         1f,  1f, 0, 1, 0,
         1f, -1f, 0, 1, 1

    };

private FloatBuffer backBuffer;

public void setAct(Shaders s, GLSurfaceView v) {

    shaders = s;
    mGLView = v;

    texId = shaders.loadTexture(mGLView, R.raw.ground_tex);

    System.out.println(texId);

}

public Background() {

    // a float has 4 bytes so we allocate for each coordinate 4 bytes
    ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(BackgroundCoords.length * 4);
    vertexByteBuffer.order(ByteOrder.nativeOrder());

    // allocates the memory from the byte buffer
    backBuffer = vertexByteBuffer.asFloatBuffer();

    // fill the vertexBuffer with the vertices
    backBuffer.put(BackgroundCoords);

    // set the cursor position to the beginning of the buffer
    backBuffer.position(0);

}

/** The draw method for the square with the GL context */
public void draw() {

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

    // Point to our vertex buffer
    backBuffer.position(0);
    GLES20.glVertexAttribPointer(shaders.getMPH(), 3, GLES20.GL_FLOAT, false, 5 * 4, backBuffer);
    GLES20.glEnableVertexAttribArray(shaders.getMPH());

    backBuffer.position(3);
    GLES20.glVertexAttribPointer(shaders.getTexCoords(), 2, GLES20.GL_FLOAT, false, 5 * 4, backBuffer);
    GLES20.glEnableVertexAttribArray(shaders.getTexCoords());   

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
    GLES20.glUniform1i(shaders.getIBaseMap(), 0);

    // Draw the vertices as triangle strip
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 4);


}

}

如果我只尝试使用简单的着色器,则此代码有效,尽管填充背景的两个三角形中只有一个被着色。

每次调用 GLES 时都会出现源自 loadTexture 函数的错误“E/libEGL(8971):在没有当前上下文的情况下调用 OpenGL ES API(每个线程记录一次)”=。

我真的希望有人能帮助我。

4

1 回答 1

0

"color = texture2D(u_baseMap, gl_PointCoord);"

Why did you decide to use gl_PointCoord here? You're drawing triangles, and according to the documentation:

If the current primitive is not a point, then values read from gl_PointCoord are undefined.

If you want to use gl_PointCoord, than draw GL_POINTS, not GL_TRIANGLES. Otherwise generate and send texture coordinates.

于 2012-05-15T01:50:35.697 回答