Android OpenGL-ES gradient background

我的未来我决定 提交于 2020-01-17 12:46:32

问题


I would like to have a gradient background in OpenGL

I found these two links, but I cannot reproduce it:

OpenGL gradient fill on iPhone looks striped

OpenGL gradient banding on Android

I tried the following of the first link:

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

But it just shows a rectangle in the right upper corner. But I don't know if the

glGetIntegerv

would have an effect? Any ideas/links how to make it run?

=== SOLUTION

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

I forgot to comment the perspective line in again. I also changed the vertices order from "U" shape to the "Z" shape (Thanks Nick). Now it looks like this:


回答1:


This is a problem:

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

You are using signed four-byte integers for your color channels, and then telling opengl that they are unsigned one-byte integers. You should be using a buffer full of unsigned bytes.

It would be easier however, to just use floats instead:

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


来源:https://stackoverflow.com/questions/10361569/android-opengl-es-gradient-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!