10k it is a moment when I start having seriously problem.
I joust wont to know how to handle with so many object 10k or even more
OK, here it is (I hope it’s enough)
public class MainCanvas extends Canvas {
private static MainCanvas canvas;
private Thread gameThread;
boolean running = false;
private GameState gameState;
long lastFrame;
/** frames per second */
int fps;
/** last fps time */
long lastFPS;
public static MainCanvas get(){
if(canvas == null) {
canvas = new MainCanvas();
}
return canvas;
}
public final void addNotify(){
super.addNotify();
startLWJGL();
}
public final void removeNotify(){
stopLWJGL();
super.removeNotify();
}
public void startLWJGL(){
gameThread = new Thread (){
public void run(){
running = true;
try {
Display.setParent(canvas);
Display.create();
Display.setVSyncEnabled(true);
initGL();
gameState = GameState.prepare();
getDelta();
lastFPS = getTime();
} catch (LWJGLException e) {
e.printStackTrace();
}
gameLoop();
}
};
gameThread.start();
}
public void stopLWJGL(){
running = false;
try {
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void initGL() {
glViewport(0, 0, Conf.W,Conf.H); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
}
public void gameLoop(){
while(running) {
Display.sync(60);
int delta = getDelta();
gameState.update(delta);
gameState.render();
Display.update();
updateFPS();
}
Display.destroy();
}
public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
System.out.println("--"+fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
}
public class GameState {
private static GameState gameState;
private List<Entity> entities = new ArrayList<Entity>();
public static GameState prepare(){
if(gameState == null) {
gameState = new GameState();
}
return gameState;
}
public GameState(){
float x = 0f;
float y =0f;
for (int i = 0;i<100;i++){
for(int j = 0;j<100;j++){
entities.add(EntityCreator.get().getEntity(EntityType.BOX, x, y, 0.0f));
y+=2.0f;
}
x+=2.0f;
y=0;
}
}
public void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
for(Entity e : entities) {
GL11.glPushMatrix();
e.render();
GL11.glPopMatrix();
}
GL11.glLoadIdentity();
}
public void update(float delta){
PlayerKeyboad.checkInput(delta);
GL11.glRotatef(PlayerKeyboad.lookupdown, 1.0f, 0, 0);
GL11.glRotatef(360.0f - PlayerKeyboad.yrot, 0, 1.0f, 0);
GL11.glTranslatef(-PlayerKeyboad.xpos - PlayerKeyboad.leftRight, -PlayerKeyboad.walkbias - 0.25f - PlayerKeyboad.upDown-3, -PlayerKeyboad.zpos);
}
public void createBox(float x, float y, float z){
entities.add(EntityCreator.get().getEntity(EntityType.BOX, x, y, z));
}
}
public class Box extends AbstractEntity {
private FloatBuffer textureData;
private FloatBuffer vertexData;
private FloatBuffer vertexData2;
private final int amountOfVertices = 24;
private final int vertexSize = 3;
private final int textureVertexSize = 2;
private int[] textures;
public Box(int[] textures, float x, float y, float z, FloatBuffer vertexData, FloatBuffer vertexData2, FloatBuffer textureData) {
this.textures = textures;
this.positionX = x;
this.positionY = y;
this.positionZ = z;
this.vertexData = vertexData;
this.vertexData2 = vertexData2;
this.textureData = textureData;
}
@Override
public void render() {
GL11.glTranslatef(positionX, positionY, positionZ); // Move Right but leave screen (screen is already set)
glEnableClientState(GL_VERTEX_ARRAY);// Enable vertex
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//First part of texture.
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[0]); //Bind texture
glVertexPointer(vertexSize, 0, vertexData);//First part cube
glTexCoordPointer(textureVertexSize, 0, textureData);//Add texture to cube
glDrawArrays(GL_QUADS, 0, amountOfVertices);//Draw cube
//Second texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[1]);//Bind texture
glVertexPointer(vertexSize, 0, vertexData2);//Second part cube
glTexCoordPointer(textureVertexSize, 0, textureData);
glDrawArrays(GL_QUADS, 0, amountOfVertices);//Draw cube
glDisableClientState(GL_VERTEX_ARRAY);//Disable Vertex
glDisableClientState(GL_TEXTURE_COORD_ARRAY);//Disable Vertex
}
}
public class EntityCreator {
private static EntityCreator creator;
private FloatBuffer textureData;
private FloatBuffer vertData;
private FloatBuffer vertData2;
private final int textureVertexSize = 2;
private final int amountOfVertices = 24;
private final int vertexSize = 3;
private int[] textures = new int[2];
public static EntityCreator get(){
if(creator == null) {
creator = new EntityCreator();
}
return creator;
}
private EntityCreator(){
float size = 1f;
vertData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertData.put(CubeUtil.margeWalls(CubeUtil.getFrontWall(size, size, size)));
vertData.flip();
vertData2 = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertData2.put(CubeUtil.margeWalls(CubeUtil.getRightWall(size, size, size),
CubeUtil.getBottomWall(size, size, size), CubeUtil.getBackWall(size, size, size),
CubeUtil.getLeftWall(size, size, size), CubeUtil.getTopWall(size, size, size)));
vertData2.flip();
textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureVertexSize);
textureData.put(CubeUtil.getCubeTexture(1.0f,1.0f));
textureData.flip();
textures[0] = TextureUtil.texture("png", "res/Glass.png",TextureUtil.FILTER_MIPMAP);
textures[1] = TextureUtil.texture("png", "res/Crate.png",TextureUtil.FILTER_MIPMAP);
}
public Entity getEntity(EntityType type, float x, float y, float z) {
Entity entity = null;
switch (type) {
case BOX:
entity = new Box(textures, x, y, z, vertData, vertData2, textureData);
break;
default:
break;
}
return entity;
}
}
public interface Entity {
public void render();
public float getPositionX();
public float getPositionY();
public float getPositionZ();
}
and this is how I’m getting texture
public class TextureUtil {
public static int FILTER_NONE = 0;
public static int FILTER_LINEAR = 1;
public static int FILTER_MIPMAP = 2;
public static int texture(String extension, String fileName, int filter){
Texture texture = null;
try {
PNGDecoder decoder = new PNGDecoder(ResourceLoader.getResourceAsStream(fileName));
ByteBuffer data = ByteBuffer.allocateDirect(4 * decoder.getWidth()* decoder.getHeight());
decoder.decode(data, decoder.getWidth() * 4,Format.RGBA);
data.rewind();
texture = TextureLoader.getTexture(extension, ResourceLoader.getResourceAsStream(fileName), false, filter);
if (filter == FILTER_NONE) { // No Texture Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
} else if (filter == FILTER_LINEAR) { // Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
} else if (filter == FILTER_MIPMAP) { // MipMapping
GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
}
} catch (IOException e) {
e.printStackTrace();
}
return texture.getTextureID();
}
}