1
지난 한 시간 동안 렌더링하려고 시도한 텍스처가 단색이되도록 버그를 수정하려고했습니다. 이상한 점은 텍스쳐를로드하는이 방법이 전에는 전혀 문제없이이 기능을 완벽하게 지원한다는 것입니다. 여기에 내가 (BufferedImage의에서) 텍스처를로드하는 방법은 다음과 같습니다LWJGL 텍스처를 단일 색상으로 렌더링
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);
위의 코드는 표시 목록에 있습니다. 쿼드가 단일 색상으로 렌더링되는 원인은 무엇입니까?
그래, 내가 그 기능을 glBegin과 glEnd 사이에 지정하는 것을 잊었다처럼 코드가 보일 것입니다 생각 ... 내가 코드를 단축 한 것은 것은 좀 더 읽기 쉽게하기 문제. 그러나 glTexCoord 위치가 그것을 고정! 지금 나는 그것을 깨닫지 못하는 것에 대해 비논리적이라고 느낀다 ... 도와 줘서 고마워! – Quaggles