最近我一直在尝试使用绘制纹理。 width和height是2的幂,但仍会显示 作为一个空白的白色正方形。 我已经检查过过滤,等等。
Player.java
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL12;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
public class TextureLoader {
private static final int BYTES_PER_PIXEL = 4;
int textureID;
public int loadTexture(BufferedImage image){
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); //4 for RGBA, 3 for RGB
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)); // Red component
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF)); // Blue component
buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
}
}
buffer.flip();
textureID = glGenTextures(); //Generate texture ID
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
return textureID;
}
public void bind() {
glBindTexture(GL_TEXTURE_2D , textureID);
}
public static BufferedImage loadImage(String loc)
{
try {
return ImageIO.read(new File(loc));
} catch (IOException e) {
}
return null;
}
}
主要 我很确定这是将纹理映射到四边形的方法。
BufferedImage image = tx.loadImage("Untitled.png");
int textureID = tx.loadTexture(image);
tx.bind();
glBegin(GL_QUADS);
glTexCoord2f(0 , 0);
glVertex2f(0 , 0 );
glTexCoord2f(0 , 128);
glVertex2f(0, 128);
glTexCoord2f(128 , 128);
glVertex2f(128 , 128);
glTexCoord2f(128 , 0);
glVertex2f(128 , 0);
glEnd();