我一直在尝试解决这个问题,但没有任何运气。我有四个类,主类、对象类、精灵类和图像加载器类。
我正在尝试使用图像加载器并使用此处找到的方法加载 png:http ://www.java-gaming.org/topics/bufferedimage-to-lwjgl-texture/25516/msg/220280/view.html #msg220280转换为ByteBuffer,然后绑定到OpenGL。
图像位于它自己的单独资源文件夹中。应该画的是:(http://i.imgur.com/j8GBU4c.png)(32x32),但我看到的是一个白色的盒子,它有图像的尺寸,但不是实际的纹理.
如果有人知道我可以去哪里解决这个问题,我将不胜感激。我是 OpenGL 的新手,希望避免使用外部库来了解实际代码的工作原理。谢谢。
更新!
对于任何关注的人,我实施了 Vallentin 提供的建议,但是四边形现在采用图像中第一个像素的颜色。我尝试在此处实现 CS 提供的图像加载器。但是现在我收到此错误:
javax.imageio.IIOException: Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1291)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1560)
at pImageLoader.loadBIn(pImageLoader.java:60)
at Monkeybars.main(Monkeybars.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: javax.imageio.IIOException: Destination type from ImageReadParam does not match!
at javax.imageio.ImageReader.getDestination(ImageReader.java:2862)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1263)
... 8 more
Exception in thread "main" java.lang.NullPointerException
at pImageLoader.loadpSprite(pImageLoader.java:75)
at Monkeybars.main(Monkeybars.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我在 Windows 7 上使用 IntelliJ 12,使用 Java 6。我要加载的图像链接在上面。如果有人有任何想法,我愿意接受建议。
主块:
public static void main(String[] args){
System.out.println("Sup!");
int width = 800 ;
int height = 600;
try{
Display.setDisplayMode(new DisplayMode(width,height));
Display.setTitle("The Playground");
Display.create();
}catch (LWJGLException e){
e.printStackTrace();
Display.destroy();
System.exit(1);
}
//Initialize OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0f, 1f, -1f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_BLEND);
pObject MrRedSquare = new pObject(300f,300f,pImageLoader.loadpSprite(pImageLoader.loadBIn("test.png")));
while(!Display.isCloseRequested()){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
MrRedSquare.drawMe();
Display.sync(60);
Display.update();
}
}
}
纹理加载器,新方法。
public static BufferedImage loadBIn(String filepath){
BufferedImage image;
// get the reader
ImageReader ir = ImageIO.getImageReadersByFormatName("png").next();
// get the default param
ImageReadParam p = ir.getDefaultReadParam();
p.setDestinationType(
// define the image type to return if supported
ImageTypeSpecifier.createInterleaved(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {0, 1, 2, 3}, // <-- the order of the color bands to return so the bytes are in the desired order
DataBuffer.TYPE_BYTE,
true, false)
);
try{
InputStream stream = pImageLoader.class.getClassLoader().getResourceAsStream(filepath);
ImageInputStream imageStream = ImageIO.createImageInputStream(stream);
ir.setInput(imageStream);
image = ir.read(0, p);
}catch(Exception e){
System.out.print("IMAGELOADER CANNOT OBTAIN ASSET");
e.printStackTrace();
return null;
}
return image;
}
旧代码,保留用于存档目的
主要的:
public static void main(String[] args){
System.out.println("Sup!");
try{
Display.setDisplayMode(new DisplayMode(800,600));
Display.setTitle("The Playground");
Display.create();
}catch (LWJGLException e){
e.printStackTrace();
Display.destroy();
System.exit(1);
}
//Initialize OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
pObject MrRedSquare = new pObject(300,300,pImageLoader.loadpSprite(pImageLoader.loadBI("square.png")));
while(!Display.isCloseRequested()){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
MrRedSquare.drawMe();
Display.sync(60);
Display.update();
}
}
对象(绘制代码):
public void drawMe(){
// store the current model matrix
GL11.glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
GL11.glTranslatef(x, y, 0);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, texture.getHeight());
GL11.glVertex2f(0, height);
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
GL11.glVertex2f(width,height);
GL11.glTexCoord2f(texture.getWidth(), 0);
GL11.glVertex2f(width,0);
}
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
质地:
public class pSprite {
protected ByteBuffer spriteData;
protected BufferedImage spriteImage;
protected int id;
protected int width;
protected int height;
//---------------------------------------------------------------------------------------
//Constructors:
pSprite(BufferedImage sI, ByteBuffer s, int i){
spriteImage = sI;
spriteData = s;
id = i;
width = spriteImage.getWidth();
height = spriteImage.getHeight();
}
//---------------------------------------------------------------------------------------
//Methods:
public void bind(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
}
图像加载器:
public class pImageLoader {
public static BufferedImage loadBI(String filepath){
BufferedImage image;
try{
InputStream input = pImageLoader.class.getResourceAsStream(filepath);
image = ImageIO.read(input);
}catch (Exception e){
System.out.print("IMAGELOADER: Cannot obtain asset.");
e.printStackTrace();
return null;
}
return image;
}
public static pSprite loadpSprite(BufferedImage image){
//http://www.java-gaming.org/topics/bufferedimage-to-lwjgl-texture/25516/msg/220280/view.html#msg220280
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() * 4); // <-- 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();
int textureID = glGenTextures(); //Generate texture ID
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 new pSprite(image, buffer, textureID);
}
}