Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: glCallList(); behaving strangely.

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    4

    Question glCallList(); behaving strangely.

    Hello! I have recently discovered a problem with my opengl game. This is my render code:

    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+"\t"+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
    Code :
    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:
    Code :
    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:
    Code :
    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:
    Code :
    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
    Last edited by SporreKing; 09-24-2013 at 09:01 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    498
    Quote Originally Posted by SporreKing View Post
    Is there any reason to why the first getTile(); method has an impact on the glList but the second example doesn't?
    It would appear that you're changing some state which the lists depend upon, and whatever TextureLoader.getTexture() does (apart from actually uploading the texture) is setting it to the correct value, so when you include those calls in the display list it works.

    I suggest examining TextureLoader.getTexture() to see what else it does apart from uploading the texture.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by GClements View Post
    I suggest examining TextureLoader.getTexture() to see what else it does apart from uploading the texture.
    I checked it out and as far I i can see it's only a way of simply initializing a Texture. There seems to be nothing more to it :/ I changed my code from loading from a single image to loading from a spriteSheet, but I didn't expect this :/

  4. #4
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    4
    Another thing I also noticed is that if I put the first piece of code in the main render function so that it repeats itself over and over again, and remove the glList functions, so that it render the tiles instantly, it works perfectly fine! But ofc it's more laggy to render each tile on it's own :/ But for some reason whenever I apply the glList code, it gets weird as stated above :/

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    4
    Turns out all I had to do was change M.TM.getTile(id).bind(); to glBindTexture(GL_TEXTURE_2D, M.TM.getTile(id).getTextureID());

    Thanks anyways!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •