0

我想在OpenGL中有一个渐变背景

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

iPhone 上的 OpenGL 渐变填充看起来有条纹

Android上的OpenGL渐变带

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

    // 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

4

1 回答 1

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);
于 2012-04-28T08:45:26.453 回答