3

Would anyone share an example on how to implement GradienRectangle that would have different colors for each vertex?

I've tried to call glColorPointer from GL10 passing float buffer and GL11 using similar to vertices selectOnHardware approach but both methods failed for me...

On AndEngine forum I found this code, but it does not work, however maby it will help someone to find a better solution.

michael
  • 3,250
  • 10
  • 43
  • 58
  • What specific errors/exceptions are you getting? are the elements just not showing up, or are you getting compile/runtime errors? – Codeman Nov 29 '11 at 15:01
  • Everything is black, no colors... – michael Nov 29 '11 at 17:28
  • @michael I don't know how to send PM on this site but can you hlep me in this http://stackoverflow.com/questions/9698997/add-visual-below-finger-swipe-triangle-strip-like-fruit-ninja-andengine – AZ_ Mar 16 '12 at 07:04

2 Answers2

3

that example does not work for you because author have not shown piece of code responsible for setting vertexes.

Here is my example(it is long, but that's opengl...) NOTE - remember to setUp viewport correctly.

public static void drawGradientRectangle(GL10 gl, float centerX, float centerY,
            float width, float height) {
        gl.glPushMatrix();
        gl.glDisable(GL10.GL_TEXTURE_2D);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //just in case if you have not done that before
        gl.glFrontFace(GL10.GL_CCW); //Set the face

        gl.glTranslatef(centerX, centerY, 0);
        if (width != 1 || height != 1) {
            gl.glScalef(width, height, 1);
        }
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, GLDrawConstants.vertexBuffer0_5);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, GLDrawConstants.gradOrangeWhiteBuffer);

        // Draw the vertices as triangle strip
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glPopMatrix();
    }

GLDrawConstants class:

public class GLDrawConstants {

    public static final FloatBuffer gradOrangeWhiteBuffer;
    public static final FloatBuffer vertexBuffer0_5;

private static final float vertices0_5[] = { 
        -0.5f, -0.5f,// Bottom Left
        0.5f, -0.5f,// Bottom right
        -0.5f, 0.5f,// Top Left
        0.5f, 0.5f// Top Right
    };

private static final float gradOrangeWhiteColor[] = {
        255/255f, 239/255f, 196/255f, 0f, // Bottom Left 
        255/255f, 239/255f, 196/255f, 0f, // Bottom right
        250/255f, 200/255f, 62/255f, 0.3f, // Top Left
        250/255f, 200/255f, 62/255f, 0.3f  // Top Right
    };

static {
        gradOrangeWhiteBuffer = WDUtils.floatBuffer(gradOrangeWhiteColor);
        vertexBuffer0_5 = WDUtils.floatBuffer(vertices0_5);
}

}

WDUtils class:

public class WDUtils {
    /**
     * Make a direct NIO FloatBuffer from an array of floats
     * 
     * @param arr
     *            The array
     * @return The newly created FloatBuffer
     */
    public static final FloatBuffer floatBuffer(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;
    }
}

example on my phone

Yuriy
  • 817
  • 1
  • 7
  • 17
  • Hi, thanks for the answer, but are you sure that this will work within AndEngine, because thats my problem with this engine, I've found many examples for doing it in opengl es, but none of them can be move to the specifics of AndEngine. And this is my problem: how to implement it in AndEngine not how to implement it in general. – michael Nov 30 '11 at 14:18
  • AndEngine uses OpenGL so it should work there with very minor adjustments..have you tried it? – Yuriy Nov 30 '11 at 14:41
  • I did and it works, thanks, I think that previously I've been calling glColorPointer with a first argument 3 instead of 4 and it usually makes everything balck. Thanks – michael Nov 30 '11 at 22:04
  • @michael please share your AndEngine code sample. You can paste it in your question like Updated and code below. It will help me and the comunity. thnx – AZ_ Mar 16 '12 at 07:01
0

Or you use (or backport) the Gradient class from the GLES2-AnchorCenter branch, which gives you a super easy to use and feature rich API without worrying about OpenGL. =)

Nicolas Gramlich
  • 2,790
  • 19
  • 19