LWJGL -纹理.Bind没有足够的空间



当我尝试将纹理绑定到LWJGL中的球体时,窗口不响应约30秒。然后出现java堆空间错误。我试着给java添加更多的空间,但它仍然不起作用。这是球体和纹理的代码:

GL11.glEnable(GL11.GL_TEXTURE_2D);               
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);   
GL11.glEnable(GL11.GL_BLEND);
Texture earthTexture = TextureLoader.getTexture("JPG",
        ResourceLoader.getResourceAsStream(earthPath));
//enable and bind the texture
earthTexture.bind();
Sphere earth = new Sphere();
earth.setDrawStyle(GLU.GLU_FILL);
earth.setTextureFlag(true);
earth.setNormals(GLU.GLU_SMOOTH);
earth.draw(2.3f, 25, 25);

这是完整的类:

package com.game.planets;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.glu.Sphere;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class EarthPlanet {
    public static String earthPath = "src/com/game/planets/textures/earth.jpg";
    static Texture earthTexture;
    public static void renderEarth(){
    earthTexture = loadTexture("earth");
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glEnable(GL11.GL_TEXTURE_2D);  
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    earthTexture.bind();
    Sphere earth = new Sphere();
    earth.setDrawStyle(GLU.GLU_FILL);
    earth.setTextureFlag(true);
    earth.setNormals(GLU.GLU_SMOOTH);
    earth.draw(2.3f, 25, 25);
    earthTexture.release();
}
private static Texture loadTexture(String key){
    try {
        return TextureLoader.getTexture("JPG", 
                new FileInputStream(new File("src/com/game/planets/textures/" + key+ ".jpg")));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

}

我猜,你是重新加载资源,每次你绘制到屏幕?

加载一次资源并将其存储到属性/var中。然后每次绘制预加载的变量。

package com.game.planets;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.glu.Sphere;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class EarthPlanet {
    public static String earthPath = "src/com/game/planets/textures/earth.jpg";
    static Texture earthTexture;
    public EarthPlanet()
    {
        earthTexture = loadTexture("earth");    
    }
    public static void renderEarth(){
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glEnable(GL11.GL_TEXTURE_2D);  
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    earthTexture.bind();
    Sphere earth = new Sphere();
    earth.setDrawStyle(GLU.GLU_FILL);
    earth.setTextureFlag(true);
    earth.setNormals(GLU.GLU_SMOOTH);
    earth.draw(2.3f, 25, 25);
    earthTexture.release();
}
private static Texture loadTexture(String key){
    try {
        return TextureLoader.getTexture("JPG", 
                new FileInputStream(new File("src/com/game/planets/textures/" + key+ ".jpg")));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
}

最新更新