gpt4 book ai didi

java - LWJGL 使用纹理时颜色困惑 + 经常禁用纹理时 FPS 较低

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:23 28 4
gpt4 key购买 nike

我最初在使用纹理时遇到了颜色困惑的问题,但我设法修复了它(问题是我没有在需要时禁用纹理)。完成此操作后,颜色发生了变化,但仍然不是我想要的颜色 - 白色而不是纯蓝色 (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 部分出现问题。这些没有纹理(目前),我希望它们只是彩色方 block - 红色(代表熔岩)和蓝色(代表水)。颜色取自静态变量并且是正确的(我通过 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

输出如下所示:Rendered scene

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

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

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

最佳答案

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

为了提高性能:

  • 如果您的场景大部分是静态的,请使用 display lists .

  • 优化 OpenGL 调用。避免使用 glDisable/glEnableglBindTexture。只要你不频繁改变状态,现代显卡的速度就非常快。您可以通过使用空白色纹理来避免 glEnable/glDisable。使用 texture atlas 可以避免 glBindTexture .

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

关于java - LWJGL 使用纹理时颜色困惑 + 经常禁用纹理时 FPS 较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221255/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com