2015-10-06 56 views
0

当我尝试将纹理绑定到LWJGL中的球体时,窗口约30秒未响应。然后出现一个错误,说java堆空间。我试着给java增加更多的空间,但它仍然不起作用。 这是球体和质地代码:LWJGL - texture.bind没有足够的空间

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; 
} 

}

回答

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; 
} 
} 
+0

我该怎么做? –

+0

@ D.White代码添加到我的反应 – Reinard

+0

这给了我这个错误:异常线程“main”显示java.lang.NullPointerException \t在com.game.main.GameWindow.gameLoop(GameWindow.java:108) \t在com.game.main.GameWindow.main(GameWindow.java:31) –

相关问题