1

我最初在使用纹理时遇到了颜色混乱的问题,但我设法修复了它(问题是我没有在需要时禁用纹理)。之后,颜色发生了变化,但仍然不是我想要的颜色 - 白色而不是纯蓝色 (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 部分。那些没有纹理(目前),我希望它们只是彩色方块 - 红色(用于熔岩)和蓝色(用于水)。颜色取自静态变量并且是正确的(我通过 System.out.ln(tile.color.getRed()...... - 输出为 0,0,255 进行了检查)。这是静态变量:

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

输出如下所示:渲染场景

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

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

我是openGL的新手,这也是我在这里的第一个问题,所以如果我做错了什么,请原谅我。

4

1 回答 1

1

glColor3ub采用无符号字节。也许您正在使用有符号字节?你试过了吗glColor3ub(0,0,255)glColor3f(0,0,1)如果它确实有效,请确保getRedByteetc. 返回正确的值。

要提高性能:

  • 如果您的场景大部分是静态的,请使用显示列表

  • 优化您的 OpenGL 调用。避免glDisable/glEnableglBindTexture. 只要您不经常更改状态,现代图形卡就会非常快。您可以通过使用空白纹理来避免glEnable/ 。可以通过使用纹理图集来避免。glDisableglBindTexture

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

于 2011-08-28T15:48:59.153 回答