Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:26:25+00:00 2026-06-02T20:26:25+00:00

I would like to have a gradient background in OpenGL I found these two

  • 0

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 in the perspective line again. I also changed the vertices layed order from "U" shape to the "Z" shape (as commented from Nick).
Now it looks like how I want it:android_gradient_background

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:26:26+00:00Added an answer on June 2, 2026 at 8:26 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Interface Builder components I have Gradient Button I would like to create this
My content and body background colors are different and I would like to have
I have a gradient background that I'm using like follows in an ASP.Net Webforms
I've set a CSS3 background gradient on an item, and I'd like to have
I have a full screen fixed background image. I would like the text in
I have been experimenting with HTML5 and gradient fades, I would like to use
I have a problem. I would like to have a textview with a gradient
So if you have code like this: background: url('image.png'); background: -webkit-gradient(linear, left top, left
I have a problem that I would like have solved via a SQL query.
So in my project i would like have a nice treeview that has images.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.