当经常禁用纹理时,LWJGL颜色在使用纹理+较低的FPS时搞砸了

时间:2011-08-28 13:26:36

标签: java opengl colors textures lwjgl

我最初在使用纹理时遇到了混乱颜色的问题,但我设法修复它(问题是我在需要时没有禁用纹理)。在这之后,颜色发生了变化,但仍然不是我想要的颜色 - 白色而不是纯蓝色(0,0,255)RGB。 这是完整的渲染方法:

private void render() {

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    GL11.glLoadIdentity(); // Reset The View

    GL11.glRotatef(lookupdown, 1.0f, 0, 0);
    GL11.glRotatef(360.0f - yrot, 0, 1.0f, 0);

    GL11.glTranslatef(-xpos, 0, -zpos);



    /*                   RENDERING BLOCKS                      */
    for (Block block : lvLoader.currentLevel.blocks)
    {
        if (block.created)
        {
            if (block.texturePos != null)
            {
                if (block.texturePos.pos != -1)
                {
                    Texture txt = TextureManager.getInstance().blocks[block.texturePos.pos];
                    if (txt != null)
                    {
                        GL11.glEnable(GL11.GL_TEXTURE_2D);
                        GL11.glBindTexture(GL11.GL_TEXTURE_2D, txt.getTextureID());
                    }
                }
            }

            GL11.glColor3ub(block.color.getRedByte(), block.color.getGreenByte(), block.color.getBlueByte());
            GL11.glBegin(GL11.GL_QUADS);

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (block.texturePos != null)
                    {
                        switch (j)
                        {
                            case 0:
                                GL11.glTexCoord2f(1.0f, 1.0f);
                                break;
                            case 1:
                                GL11.glTexCoord2f(0.0f, 1.0f);
                                break;
                            case 2:
                                GL11.glTexCoord2f(0.0f, 0.0f);
                                break;
                            case 3:
                                GL11.glTexCoord2f(1.0f, 0.0f);
                                break;
                            default:
                                break;
                        }
                    }

                    GL11.glVertex3f(block.walls[i].vertices[j].x, block.walls[i].vertices[j].y, block.walls[i].vertices[j].z);
                }
            }



            GL11.glEnd();

            //if (block.texturePos != null)
                //if (block.texturePos.pos != -1)
                    GL11.glDisable(GL11.GL_TEXTURE_2D);
        }
    }


    /*                   RENDERING TILES                      */
    for (Tile tile : lvLoader.currentLevel.tiles)
    {
        if (tile.created)
        {
            if (tile.texturePos != null)
            {
                GL11.glEnable(GL11.GL_TEXTURE_2D);
                if (tile.texturePos.pos != -1)
                {
                    Texture txt = TextureManager.getInstance().tiles[tile.texturePos.pos];
                    if (txt != null)
                    {

                        GL11.glBindTexture(GL11.GL_TEXTURE_2D, txt.getTextureID());
                    }
                }
            }

            GL11.glBegin(GL11.GL_QUADS);
            GL11.glColor3ub(tile.color.getRedByte(), tile.color.getGreenByte(), tile.color.getBlueByte());


            for (int jj = 0; jj < 4; jj++)
            {
                if (tile.texturePos != null)
                {
                    switch (jj)
                    {
                        case 0:
                            GL11.glTexCoord2f(1.0f, 1.0f);
                            break;
                        case 1:
                            GL11.glTexCoord2f(0.0f, 1.0f);
                            break;
                        case 2:
                            GL11.glTexCoord2f(0.0f, 0.0f);
                            break;
                        case 3:
                            GL11.glTexCoord2f(1.0f, 0.0f);
                            break;
                        default:
                            break;
                    }
                } 

                GL11.glVertex3f(tile.surface.vertices[jj].x, tile.surface.vertices[jj].y, tile.surface.vertices[jj].z);
            }



            GL11.glEnd();

            //if (tile.texturePos != null)
                //if (tile.texturePos.pos != -1)
                    GL11.glDisable(GL11.GL_TEXTURE_2D);
        }
    }


    /*                   RENDERING ROOF                      */ 
    for (Tile rTile : lvLoader.currentLevel.roof)
    {
        if (rTile != null)
        {
            if (rTile.created)
            {
                if (rTile.texturePos != null)
                {
                    if (rTile.texturePos.pos != -1)
                    {
                        Texture txt = TextureManager.getInstance().tiles[rTile.texturePos.pos];
                        if (txt != null)
                        {
                            GL11.glEnable(GL11.GL_TEXTURE_2D);
                            GL11.glBindTexture(GL11.GL_TEXTURE_2D, txt.getTextureID());
                        }
                    }
                }

                GL11.glColor3ub(rTile.color.getRedByte(), rTile.color.getGreenByte(), rTile.color.getBlueByte());
                GL11.glBegin(GL11.GL_QUADS);

                for (int k = 0; k < 4; k++)
                {
                    if (rTile.texturePos != null)
                    {
                        switch (k)
                        {
                            case 0:
                                GL11.glTexCoord2f(1.0f, 1.0f);
                                break;
                            case 1:
                                GL11.glTexCoord2f(0.0f, 1.0f);
                                break;
                            case 2:
                                GL11.glTexCoord2f(0.0f, 0.0f);
                                break;
                            case 3:
                                GL11.glTexCoord2f(1.0f, 0.0f);
                                break;
                            default:
                                break;
                        }
                    }

                    GL11.glVertex3f(rTile.surface.vertices[k].x, rTile.surface.vertices[k].y, rTile.surface.vertices[k].z);
                }

                GL11.glEnd();

                //if (rTile.texturePos != null)
                    //if (rTile.texturePos.pos != -1)
                        GL11.glDisable(GL11.GL_TEXTURE_2D);
            }
        }
    }
}

RENDERING TILES部分出现问题。那些没有纹理(现在),我希望它们只是彩色方块 - 红色(熔岩)和蓝色(水)。颜色取自静态var并且是正确的(我通过System.out.ln检查它(tile.color.getRed()...... - 输出为0,0,255)。这是静态var:< / p>

/* TILES */
//ShallowWater
public static ColorStruct V00255 = new ColorStruct(new Color(0, 0, 255), "Tile", "ShallowWater"); //Pure blue

输出如下所示:Rendered scene

白色的田野是水 - 它们应该是蓝色的!

另一个问题是FPS - 正如你在屏幕上看到的那样,它是41.在渲染部分中添加多个glEnable和glDisables纹理之前,FPS是60.这些是多个启用和禁用导致这个并且可以要避免吗?

我是openGL的新手,这也是我的第一个问题,如果我做错了,请原谅我。

1 个答案:

答案 0 :(得分:1)

glColor3ub采用无符号字节。也许你正在使用有符号字节?您是否尝试过glColor3ub(0,0,255)glColor3f(0,0,1)?如果确实有效,请确保getRedByte等返回正确的值。

提高绩效:

  • 如果您的场景主要是静态的,请使用display lists

  • 优化您的OpenGL调用。避免使用glDisable / glEnableglBindTexture。只要你不改变状态很多,现代图形卡真的很快。您可以使用空白纹理来避免glEnable / glDisable。使用texture atlas可以避免glBindTexture

  • 如果显示列表太慢,请使用VBO s。