When I try to bind a texture to a sphere in LWJGL the window does not respond for a about 30 seconds. Then a error appears say java heap space. I tried adding more space to java but it still didn't work. This is the code for the sphere and texture:
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);
This is the full class:
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;
}
}
I'm guessing, you are reloading the resource every time you draw to the screen?
Load the resource once and store it to a property/var. Then draw that preloaded var every time.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.