LWJGL 纹理不渲染
LWJGL Textures not rendering
我想弄清楚为什么我无法使用 LWJGL 3 渲染任何纹理。我尝试了多种方式来加载(PNGDecoder、STB、BufferedImage)和渲染纹理。但是结果总是一个白色的四边形。
主要Class:
public static void main(String[] args)
{
glfwInit();
window = glfwCreateWindow(640, 480, "TEST", 0, 0);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
GL11.glEnable(GL11.GL_TEXTURE_2D);
Loader loader = new Loader();
TestRenderer renderer = new TestRenderer();
ModelTexture texture = new ModelTexture(loader.loadTextureManual("blue"));
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
renderer.prepare();
renderer.renderWithFixedFunctions(texture);
glfwSwapBuffers(window);
clearErrorBuffer(true);
}
loader.cleanUp();
}
加载方式:
public int loadTextureManual(String fileName)
{
String imagePath = "./res/" + fileName + ".png";
try {
System.out.println("Trying to load texture \""+imagePath+"\"");
BufferedImage bi = ImageIO.read(new File(imagePath));
int width = bi.getWidth();
int height = bi.getHeight();
int[] pixels_raw = new int[width*height];
pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
ByteBuffer pixels = BufferUtils.createByteBuffer(width*height*4);
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
int pixel = pixels_raw[i*width+j];
pixels.put((byte) ((pixel >> 16) & 0xFF)); //RED
pixels.put((byte) ((pixel >> 8) & 0xFF)); //GREEN
pixels.put((byte) ((pixel) & 0xFF)); //BLUE
pixels.put((byte) ((pixel >> 24) & 0xFF)); //ALPHA
}
}
pixels.flip();
byte[] info = new byte[pixels.capacity()];
pixels.get(info);
int textureID = GL11.glGenTextures();
if(GL.getCapabilities().GL_EXT_texture_filter_anisotropic)
{
float amount = Math.min(ANISOTROPIC_FILTERING_AMOUNT, GL11.glGetFloat(EXTTextureFilterAnisotropic.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT));
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, amount);
}
else
{
System.out.println("Anisotropic filtering not supported!");
}
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0,
GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
return textureID;
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("Couldn'l load texture \""+imagePath+"\"");
System.exit(-1);
}
return 0;
}
渲染方法:
public void renderWithFixedFunctions(ModelTexture texture)
{
glBindTexture(GL_TEXTURE_2D, texture.getID());
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-0.5f, 0.5f);
glTexCoord2f(0, 1);
glVertex2f(0.5f, 0.5f);
glTexCoord2f(1, 1);
glVertex2f(0.5f, -0.5f);
glTexCoord2f(1, 0);
glVertex2f(-0.5f, -0.5f);
glEnd();
}
ModelTexture class 仅存储一些当前未使用的信息,blue.png 是 16x16 png 文件。
这是我在启动程序时得到的输出:
看起来像 incomplete texture。
将GL_TEXTURE_MIN_FILTER
设置为GL_NEAREST
或GL_LINEAR
(默认为GL_NEAREST_MIPMAP_LINEAR
)或上传完整的mipmap链。
我想弄清楚为什么我无法使用 LWJGL 3 渲染任何纹理。我尝试了多种方式来加载(PNGDecoder、STB、BufferedImage)和渲染纹理。但是结果总是一个白色的四边形。
主要Class:
public static void main(String[] args)
{
glfwInit();
window = glfwCreateWindow(640, 480, "TEST", 0, 0);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
GL11.glEnable(GL11.GL_TEXTURE_2D);
Loader loader = new Loader();
TestRenderer renderer = new TestRenderer();
ModelTexture texture = new ModelTexture(loader.loadTextureManual("blue"));
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
renderer.prepare();
renderer.renderWithFixedFunctions(texture);
glfwSwapBuffers(window);
clearErrorBuffer(true);
}
loader.cleanUp();
}
加载方式:
public int loadTextureManual(String fileName)
{
String imagePath = "./res/" + fileName + ".png";
try {
System.out.println("Trying to load texture \""+imagePath+"\"");
BufferedImage bi = ImageIO.read(new File(imagePath));
int width = bi.getWidth();
int height = bi.getHeight();
int[] pixels_raw = new int[width*height];
pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
ByteBuffer pixels = BufferUtils.createByteBuffer(width*height*4);
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
int pixel = pixels_raw[i*width+j];
pixels.put((byte) ((pixel >> 16) & 0xFF)); //RED
pixels.put((byte) ((pixel >> 8) & 0xFF)); //GREEN
pixels.put((byte) ((pixel) & 0xFF)); //BLUE
pixels.put((byte) ((pixel >> 24) & 0xFF)); //ALPHA
}
}
pixels.flip();
byte[] info = new byte[pixels.capacity()];
pixels.get(info);
int textureID = GL11.glGenTextures();
if(GL.getCapabilities().GL_EXT_texture_filter_anisotropic)
{
float amount = Math.min(ANISOTROPIC_FILTERING_AMOUNT, GL11.glGetFloat(EXTTextureFilterAnisotropic.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT));
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, amount);
}
else
{
System.out.println("Anisotropic filtering not supported!");
}
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0,
GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
return textureID;
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("Couldn'l load texture \""+imagePath+"\"");
System.exit(-1);
}
return 0;
}
渲染方法:
public void renderWithFixedFunctions(ModelTexture texture)
{
glBindTexture(GL_TEXTURE_2D, texture.getID());
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-0.5f, 0.5f);
glTexCoord2f(0, 1);
glVertex2f(0.5f, 0.5f);
glTexCoord2f(1, 1);
glVertex2f(0.5f, -0.5f);
glTexCoord2f(1, 0);
glVertex2f(-0.5f, -0.5f);
glEnd();
}
ModelTexture class 仅存储一些当前未使用的信息,blue.png 是 16x16 png 文件。
这是我在启动程序时得到的输出:
看起来像 incomplete texture。
将GL_TEXTURE_MIN_FILTER
设置为GL_NEAREST
或GL_LINEAR
(默认为GL_NEAREST_MIPMAP_LINEAR
)或上传完整的mipmap链。