Hello! I have recently discovered a problem with my opengl game. This is my render code:
for(int j=0;j<M.L.id.getHeight()/chunkSize;j++){
for(int i=0;i<M.L.id.getWidth()/chunkSize;i++){
chunks[j][i]=glGenLists(1);
glNewList(chunks[j][i], GL_COMPILE);
System.out.println(j+" "+i);
//System.out.println(chunks[j][i]);
for(int y=0;y<chunkSize;y++){
for(int x=0;x<chunkSize;x++){
map[j*chunkSize+y][i*chunkSize+x].draw(i*chunkSize+x, j*chunkSize+y, spriteSheet[j*chunkSize+y][i*chunkSize+x]);
//System.out.print(spriteSheet[j*chunkSize+y][i*chunkSize+x]+" ");
}
//System.out.println();
}
glEndList();
//System.out.println();
}
}
It’s basicly reading from a Tile map and createing new glLists to be called at later occasions. the thing that sais
map[j*chunkSize+y][i*chunkSize+x].draw(i*chunkSize+x, j*chunkSize+y, spriteSheet[j*chunkSize+y][i*chunkSize+x]);
is basicly telling that the tile at the location specified should perform a draw(); method that looks like this:
public void draw(int x, int y, float z){
//System.out.print(id);
M.TM.getTile(id).bind();
glBegin(GL_QUADS);
glTexCoord2f((M.TM.getSpriteCoordX(z))*0.125f-0.125f,(M.TM.getSpriteCoordY(z))*0.125f-0.125f);
glVertex2i(x*64,y*64);
glTexCoord2f((M.TM.getSpriteCoordX(z))*0.125f,(M.TM.getSpriteCoordY(z))*0.125f-0.125f);
glVertex2i(x*64+64,y*64);
glTexCoord2f((M.TM.getSpriteCoordX(z))*0.125f,(M.TM.getSpriteCoordY(z))*0.125f);
glVertex2i(x*64+64,y*64+64);
glTexCoord2f((M.TM.getSpriteCoordX(z))*0.125f-0.125f,(M.TM.getSpriteCoordY(z))*0.125f);
glVertex2i(x*64,y*64+64);
glEnd();
}
The M.TM.getTile(); method is basicly binding a spritesheet and M.TM.getSpriteCoordX(); and M.TM.getSpriteCoordY(); is calculating which parts of the spritesheet to draw.
The T.TM.getTile(); method loks like this:
public Texture getTile(int id){
Texture t = null;
switch(id){
case 0: return black;
case 1: return ground;
case 2: return tree;
case 3: return water;
case 4: return bridge;
}
return t;
}
And the different variables are basic Textures variables which are initialized within the constructor.
As far as I see this should be working… But whenever i try to call a list using glCallList(chunks[][]); (chunks[][] is where I store my glLists which you can see in the first piece of code)
Now, the problem is that it’s behaving really weird. Depending on where the camera is, some tiles doesn’t get rendered, and when I maybe move about 3 tiles, they are vissible again, and all the tiles are just glitching and between black and what they’re supposed to be. Sometimes there are large chunks that aren’t beeing rendered at all.
Now the thing that makes it weird is that, whenever I change the getTile(); method to this:
public Texture getTile(int id){
Texture t = null;
try{
switch(id){
case 0: return TextureLoader.getTexture("PNG", new FileInputStream("res/tile/black.png"));
case 1: return TextureLoader.getTexture("PNG", new FileInputStream("res/tile/ground.png"));
case 2: return TextureLoader.getTexture("PNG", new FileInputStream("res/tile/tree.png"));
case 3: return TextureLoader.getTexture("PNG", new FileInputStream("res/tile/water.png"));
case 4: return TextureLoader.getTexture("PNG", new FileInputStream("res/tile/bridge.png"));
}
}catch(Exception e){}
return t;
}
Basicly loading the whole spritesheet over and over again instead of preloading them inte to texture variables, it works fine!
Ofc it get’s really laggy, and my fps drops to about 14-20 instead of being at 200 or something. But the point is that it works when I load the Texture over and over again but it doesn’t work when I store it in variables. And I know that they are being initialized in the constructor and they are somewhat registered in the glList, but it as I said, whenever I call it it laggs and sometile goes missing and reappear and such.
Is there any reason to why the first getTile(); method has an impact on the glList but the second example doesn’t?
Thx in advance