gpt4 book ai didi

java - LWJGL 纹理渲染为单色

转载 作者:行者123 更新时间:2023-12-01 14:23:58 25 4
gpt4 key购买 nike

在过去的一个小时左右的时间里,我一直在尝试修复一个错误,该错误导致我尝试渲染的纹理只是单色。奇怪的是,这种加载纹理的方法以前对我来说效果很好,没有出现这样的问题。以下是我加载纹理的方法(从 BufferedImage):

public static int loadTexture(BufferedImage image)
{

glEnable(GL_TEXTURE_2D);

TextureImpl.unbind();

try
{

int[ ] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0,
image.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth()
* image.getHeight() * BYTES_PER_PIXEL);
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF));
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF));
buffer.put((byte) ((pixel >> 24) & 0xFF));
}
}

buffer.flip();

int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);

//Setup wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL12.GL_CLAMP_TO_EDGE);

//Setup texture scaling filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//Send texel data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(),
image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

return textureID;

}
catch (Exception e)
{
e.printStackTrace();
}

glDisable(GL_TEXTURE_2D);

//Return the texture ID so we can bind it later again
return 0;

}

这就是纹理绘制到屏幕上的方式:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, blockTexture.getID());

glVertex2i(offsetX * cubeSize + x * cubeSize, y * cubeSize);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
y * cubeSize);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
(y * cubeSize) + cubeSize);
glVertex2i(offsetX * cubeSize + x * cubeSize,
(y * cubeSize) + cubeSize);

glTexCoord2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);

glDisable(GL_TEXTURE_2D);

以上代码位于显示列表中。知道什么可能导致四边形渲染为单一颜色吗?

最佳答案

你有两个错误。

首先,您不能混合使用 glEnable 和 glVertex2i 等函数。glVertex2i 和 glTexCoord2f 必须位于 glBegin 和 gEnd 之间。

其次,一旦调用 glVertex2i,顶点将具有先前指定的纹理状态。

所以我相信你的代码应该看起来像

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, blockTexture.getID());

glBegin(...); // you have to specify a primitive type

glTexCoord2f(0.0f, 0.0f);
glVertex2i(offsetX * cubeSize + x * cubeSize, y * cubeSize);

glTexCoord2f(1.0f, 0.0f);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
y * cubeSize);

glTexCoord2f(1.0f, 1.0f);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
(y * cubeSize) + cubeSize);

glTexCoord2f(0.0f, 1.0f);
glVertex2i(offsetX * cubeSize + x * cubeSize,
(y * cubeSize) + cubeSize);

glEnd();

glDisable(GL_TEXTURE_2D);

关于java - LWJGL 纹理渲染为单色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17309713/

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