-1

FIXED: needed a glShadeModel(GL_SMOOTH);

current result : http://prntscr.com/d3ev6j

public static void drawBlendRectangle(double x, double y, double x1, double y1, int... colors) {
    glPushMatrix();
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glShadeModel(GL_SMOOTH);
    glBegin(GL_QUADS);

    double height = (y1 - y) / colors.length;
    for (int i = 0; i < colors.length - 1; i++) {        
        float cTop[] = RenderUtils.getRGBA(colors[i]);       
        float cBottom[] = RenderUtils.getRGBA(colors[i + 1]);       
        glColor4f(cTop[0], cTop[1], cTop[2], cTop[3]);
        glVertex2d(x, y + i * height); // top-left
        glVertex2d(x1, y + i * height); // top-right
        glColor4f(cBottom[0], cBottom[1], cBottom[2], cBottom[3]);
        glVertex2d(x1, y + i * height + height); // bottom-right
        glVertex2d(x, y + i * height + height); // bottom-left
    }
    glEnd();
    glDisable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
    glPopMatrix();
}

I've got an issue with getting my rectangle blend all of the colors in a fancy way, but can't get it to work Here's a picture: http://prntscr.com/d3767e

Here's the function I've created, I'm not very familiar with opengl so I don't really know if I'm missing a part in the blending process or I'm doing it completely wrong

public static void drawBlendRectangle(double x, double y, double x1, double y1, int... colors) {
    glPushMatrix();
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBegin(GL_QUADS);
    for (int i = 0; i < colors.length; i++) {
        float c[] = RenderUtils.getRGBA(colors[i]);
        double height = (y1 - y) / colors.length;

        glColor4d(c[0], c[1], c[2], 0.5f);
        glVertex2d(x, y + i * height); // top-left
        glVertex2d(x1, y + i * height); // top-right
        glVertex2d(x1, y + i * height + height); // bottom-right
        glVertex2d(x, y + i * height + height); // bottom-left
    }
    glEnd();
    glDisable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
    glPopMatrix();
}

getRGBA(...) only gives out a float array of colors from the integer color value, that's not an issue

Thanks in advance

  • your colors are looking wrong take a look at [RGB values of visible spectrum](http://stackoverflow.com/a/22681410/2521214) – Spektre Nov 05 '16 at 18:57

1 Answers1

0

You need different colors at the corners of the quad.

double height = (y1 - y) / colors.length;
for (int i = 0; i < colors.length - 1; i++) {        
    float cTop[] = RenderUtils.getRGBA(colors[i]);       
    float cBottom[] = RenderUtils.getRGBA(colors[i + 1]);       
    glColor4f(cTop[0], cTop[1], cTop[2], 0.5f);
    glVertex2d(x, y + i * height); // top-left
    glVertex2d(x1, y + i * height); // top-right
    glColor4f(cBottom[0], cBottom[1], cBottom[2], 0.5f);
    glVertex2d(x1, y + i * height + height); // bottom-right
    glVertex2d(x, y + i * height + height); // bottom-left
}

Be aware that this will just interpolate the RGB values. If you want physically plausible interpolation, you need a custom shader.

You might also want to consider using a triangle strip instead of a quad list. This would look as follows:

glBegin(GL_TRIANGLE_STRIP);
double height = (y1 - y) / colors.length;
for (int i = 0; i < colors.length; i++) {        
    float c[] = RenderUtils.getRGBA(colors[i]);       
    glColor4f(c[0], c[1], c[2], 0.5f);
    glVertex2d(x, y + i * height); // left
    glVertex2d(x1, y + i * height); // right
}
glEnd();
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • needed a glShadeModel(GL_SMOOTH); Thanks anyway, I needed to use the first solution to color them in order :) – NetherSoul Nov 05 '16 at 14:53