【问题标题】:Rendering a Rainbow spectre in Java OpenGL does not blend the colors在 Java OpenGL 中渲染彩虹幽灵不会混合颜色
【发布时间】:2016-11-04 22:52:10
【问题描述】:

已修复:需要 glShadeModel(GL_SMOOTH);

当前结果: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();
}

让我的矩形以一种奇特的方式混合所有颜色时遇到问题,但无法让它发挥作用 这是一张图片:http://prntscr.com/d3767e

这是我创建的函数,我对opengl不是很熟悉,所以我真的不知道是我在混合过程中遗漏了一部分还是我做错了

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(...) 仅从整数颜色值中给出一个浮点颜色数组,这不是问题

提前致谢

【问题讨论】:

标签: java opengl blend


【解决方案1】:

你需要在四边形的角落使用不同的颜色。

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
}

请注意,这只会插入 RGB 值。如果您想要物理上合理的插值,则需要自定义着色器。

您可能还想考虑使用三角形条而不是四边形列表。如下所示:

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

【讨论】:

  • 需要一个 glShadeModel(GL_SMOOTH);无论如何,谢谢,我需要使用第一个解决方案来为它们着色:)
猜你喜欢
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多