我无法在 lwjgl 中绘制绿色方块

I can't draw green square in lwjgl

这听起来可能很奇怪,但不知何故我的程序忽略了绿色

首先,我这样初始化 GL:(我不知道,也许这很重要)

private static void initgl() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1280, 720, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
}

我正在画这样一个正方形:

public static void gameLoop(){
    while (!Display.isCloseRequested()){
        glClear(GL_COLOR_BUFFER_BIT);
        DrawStuff.square(10, 10, 100, 100, new byte[]{127,127,127});
        Display.update();
        Display.sync(60);
    }
}

在平方法中我有

public static void square(int x, int y, int w, int h, byte[] rgb) {
    glColor3b(rgb[0], rgb[1], rgb[2]);
    glBegin(GL_QUADS);
    glVertex2i(x, y);
    glVertex2i(x + w, y);
    glVertex2i(x + w, y + h);
    glVertex2i(x,y+h);
    glEnd();

}

当我运行程序时,我看到了这个:

顺便说一下,颜色选择器说它是 #C808C7。甚至不像我预期的那样#FF00FF

  1. 绿色怎么了?
  2. 为什么颜色不对?

好的,这个答案对我有帮助:How do color and textures work together?

That's not your problem, but it still needs to be done.

弄乱我的颜色的是glEnable(GL_TEXTURE_2D);为了同时实现颜色和纹理填充,每次我想用纯色绘制东西时我都必须禁用它然后再次启用:

public static void square(int x, int y, int w, int h, byte[] rgb) {
    glDisable(GL_TEXTURE_2D);
    glColor3b(rgb[0], rgb[1], rgb[2]);
    glBegin(GL_QUADS);
    glVertex2i(x, y);
    glVertex2i(x + w, y);
    glVertex2i(x + w, y + h);
    glVertex2i(x,y+h);
    glEnd();
    glEnable(GL_TEXTURE_2D);

}

根据您的情况,未来 reader,您可能希望首先将其禁用,只有在需要添加纹理时才启用它。

沃利亚: