LWJGL - texture.bind没有足够的空间

时间:2015-10-06 15:03:58

标签: java lwjgl

当我尝试将纹理绑定到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;
}

}

1 个答案:

答案 0 :(得分:0)

我猜,你每次画到屏幕都在重新加载资源?

加载资源一次并将其存储到属性/ var。然后每次都绘制预加载的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;
}
}