【问题标题】:OpenGL + LWJGL - glColor4d() not coloring shapes properlyOpenGL + LWJGL - glColor4d() 不能正确着色形状
【发布时间】:2013-06-28 00:02:44
【问题描述】:

我无法让颜色与 OpenGL 一起正常工作。 我正确地绘制了我的形状,它们看起来很好,但是当我调用 glColor4d(r,g,b,a) 时,它没有正确使用颜色,因为它的 RGB 应该指示,而是绘制不同的但相似的颜色。例如,大多数绿色被绘制为完全黄色或完全绿色,而任何灰色都被绘制为纯白色。

Color[r=132,g=234,b=208,a=255,hexa=84EAD0]
Color[r=150,g=1,b=59,a=255,hexa=96013B]
Color[r=88,g=117,b=170,a=255,hexa=5875AA]
Color[r=219,g=190,b=26,a=255,hexa=DBBE1A]
Color[r=208,g=51,b=164,a=255,hexa=D033A4]
Color[r=85,g=43,b=228,a=255,hexa=552BE4]
Color[r=167,g=123,b=184,a=255,hexa=A77BB8]
Color[r=241,g=183,b=25,a=255,hexa=F1B719]

在这个随机颜色值的简短列表中,它们都被绘制为纯 FFFFFF 白色,即使它们都不应该是白色的。

我用来绘制矩形的代码:

public void drawRectangle(Color fill, double x, double y, double width, double height, double rot){
    GL11.glPushMatrix();
    GL11.glTranslated(x, y, 0);
    GL11.glRotated(rot, 0, 0, 1);
    GL11.glTranslated(-x, -y, 0);
    GL11.glBegin(GL11.GL_TRIANGLES);
    if(fill != null)GL11.glColor4d(fill.getRed(), fill.getGreen(), fill.getBlue(), fill.getAlpha());
    double width2 = width/2;
    double height2 = height/2;
    GL11.glVertex2d(x - width2, y + height2);
    GL11.glVertex2d(x - width2, y - height2);
    GL11.glVertex2d(x + width2, y + height2);
    GL11.glEnd();
    GL11.glBegin(GL11.GL_TRIANGLES);
    if(fill != null)GL11.glColor4d(fill.getRed(), fill.getGreen(), fill.getBlue(), fill.getAlpha());
    GL11.glVertex2d(x - width2, y - height2);
    GL11.glVertex2d(x + width2, y + height2);
    GL11.glVertex2d(x + width2, y - height2);
    GL11.glEnd();
    GL11.glPopMatrix();
}

【问题讨论】:

    标签: java opengl rendering lwjgl


    【解决方案1】:

    glColor4d() 接受从 0.0 到 1.0 的参数,而不是像 glColor4i() 那样到 255。 1.0 上面的所有内容都更改为 1.0 并在大多数情况下导致白色。
    改变

    GL11.glColor4d(fill.getRed(), fill.getGreen(), fill.getBlue(), fill.getAlpha());
    

    GL11.glColor4d(fill.getRed() / 255.0, fill.getGreen() / 255.0, fill.getBlue() / 255.0, fill.getAlpha() / 255.0);
    

    【讨论】:

    • "glColor4i() 会" - 不,不会,glColor4ub 会! glColor4i 将颜色从 -2,147,483,648 变为 2,147,483,647。你也不应该除以256.0,而是255.0
    • @CalStephens 如果这个答案是您问题的解决方案,那么accepting 是正确的答案。
    • 谢谢你,克里斯,我忘记了 255.0。我想说glColor4i 参数不限于 [0, 255] 区间,但它们不适合 >255 或负数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多