Android OpenGL-ES渐变背景

时间:2012-04-28 07:35:09

标签: android opengl-es gradient

我想在OpenGL中有渐变背景

我找到了这两个链接,但我无法重现它:

OpenGL gradient fill on iPhone looks striped

OpenGL gradient banding on Android

我尝试了以下第一个链接:

    // Begin Render
    //IntBuffer redBits = null, greenBits = null, blueBits = null;
    //gl.glGetIntegerv (GL10.GL_RED_BITS, redBits); // ==> 8
    //gl.glGetIntegerv (GL10.GL_GREEN_BITS, greenBits); // ==> 8
    //gl.glGetIntegerv (GL10.GL_BLUE_BITS, blueBits); // ==> 8

    gl.glDisable(GL10.GL_BLEND);
    gl.glDisable(GL10.GL_DITHER);
    gl.glDisable(GL10.GL_FOG);
    gl.glDisable(GL10.GL_LIGHTING);
    gl.glDisable(GL10.GL_TEXTURE_2D);
    gl.glShadeModel(GL10.GL_SMOOTH);

    float[] vertices = {
        0, 0,
        320, 0,
        0, 480,
        320, 480,
    };
    FloatBuffer vertsBuffer = makeFloatBuffer(vertices); 

    int[] colors = {
    255, 255, 255, 255,
    255, 255, 255, 255,
    200, 200, 200, 255,
    200, 200, 200, 255,
    };

    IntBuffer colorBuffer = makeIntBuffer(colors);

    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertsBuffer);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, colorBuffer);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    // End Render




protected static FloatBuffer makeFloatBuffer(float[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(arr);
    fb.position(0);
    return fb;
}

protected static IntBuffer makeIntBuffer(int[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
    bb.order(ByteOrder.nativeOrder());
    IntBuffer ib = bb.asIntBuffer();
    ib.put(arr);
    ib.position(0);
    return ib;
}

但它只是在右上角显示一个矩形。但我不知道是否

glGetIntegerv

会产生影响吗?任何想法/链接如何让它运行?

=== 解

    // set orthographic perspective
    setOrtho2D(activity, gl);

    gl.glDisable(GL10.GL_BLEND);
    //gl.glDisable(GL10.GL_DITHER);
    gl.glDisable(GL10.GL_FOG);
    gl.glDisable(GL10.GL_LIGHTING);
    gl.glDisable(GL10.GL_TEXTURE_2D);
    gl.glShadeModel(GL10.GL_SMOOTH);

    float[] vertices = {
            0, 0,
            _winWidth, 0,
            0, _winHeight,
            _winWidth, _winHeight
        };

    FloatBuffer vertsBuffer = makeFloatBuffer(vertices); 

    float[] colors = {
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    0.2f, 0.2f, 0.2f, 1.0f,
    0.2f, 0.2f, 0.2f, 1.0f
    };

    FloatBuffer colorBuffer = makeFloatBuffer(colors);

    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertsBuffer);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

我忘了再次评论透视线。我还将顶点顺序从“U”形改变为“Z”形(谢谢尼克)。 现在它看起来像这样:android_gradient_background

1 个答案:

答案 0 :(得分:1)

这是一个问题:

int[] colors;
....
gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, colorBuffer);

您正在为颜色通道使用带符号的四字节整数,然后告诉opengl它们是无符号的单字节整数。你应该使用一个充满无符号字节的缓冲区。

然而,仅使用浮点数会更容易:

float[] colors = {
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    0.5f, 0.5f, 0.5f, 1.0f,
    0.5f, 0.5f, 0.5f, 1.0f,
    };

float vertices[] = {
    0,  0,
    800, 0,
    0, 480,
    480, 800,
    };

FloatBuffer colorBuffer = makeFloatBuffer(colors);

gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);