I currently am hiding objects that are not in the frustums view. However, some objects are still being rendered that are in the frustums view, but are hidden. My goal is to not render objects that won't be seen at all rather than not render objects that are in the POV but are hidden.
Let me better explain.
There are three boxes, Box A, Box B, and Box C. They are all aligned in the x axis, However, Box C has a greater y value than the rest of the boxes. Well if I'm looking straight towards Box C, I will only see Box C. But Box A and Box B are still being rendered.
I want to know if there is a method that I can use to not render objects that are hidden behind other objects.
Thanks in advanced, Jedi.
Answer
zOrdering can be used to prevent the sprites from being rendered on to the screen,Since LibGdx does not support zOrdering you can write your own custom zOrdering the advatage of using zOrdering is you can logically decide which sprite to be rendered on or sprites that must not be rendered on the screen
Here is an example : textureComponets class is used to draw sprite on the screen and it can setZorder for the box to decide which box to be drawn first,second or third or boxes that are never to be drawn on the screen it can also set postions width and height for boxes .We create an array list called textureComponetList to hold three boxes I have created arraylist in MyGdxGame class .
Class textureComponents
public class TextureComponets {
public TextureComponets(Texture texture,float x,float y,float width,float height)
{
this.texture=texture;
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public int getZorder()
{
return zOrder;
}
public void setZorder(int x)
{
zOrder=x;
}
public void draw(SpriteBatch batch)
{
if(zOrder!=0)//This condition will avoid the boxes with zOrder value zero from rendering onto screen
batch.draw(texture,x,y,width,height);
}
public static Comparator zOrderComperator = new Comparator() {
public int compare(TextureComponets s1, TextureComponets s2) {
int zOrder1 = s1.getZorder();
int zOrder2 = s2.getZorder();
}};
}
class MyGdxGame
public class MyGdxGame implements ApplicationListener
{
ArrayListtextureComponetsList;
SpriteBatch batch;
@Override
public void create(){
Texture box = new Texture(Gdx.files.internal("box.png"));
batch = new SpriteBatch();
textureComponetsList=new ArrayList();
TextureComponets BoxA=new TextureComponets(box,10,10,30,30);
BoxA.setZorder(0);
//when you setZorder to zero your sprites wont be rendered on the screen
//The maximum zOrder value it will be drawn in the top most layer or that will be drawn last in the screen
textureComponetsList.add(BoxA);
TextureComponets BoxB=new TextureComponets(box,10,20,30,30);
BoxB.setZorder(0);
textureComponetsList.add(BoxB);
TextureComponets BoxC=new TextureComponets(box,10,100,30,30);
BoxC.setZorder(1);
//BoxC will be rendered on the screen since its zOrder value greater than zero
textureComponetsList.add(BoxC);
}
@Override
public void render(){
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.glClear(GL20_GL_COLOR_BUFFER_BIT);
batch.begin();
Collections.sort(textureComponetsList, TextureComponets.zOrderComperator);
//collections.sort function will sort all the components based on the
//z order set for each box and sort them in the arraylist TextureComponetList
// you can also logically setZorder value in program
//like if(BoxC.y>100){BoxC.setZorder(1)} or like if(BoxC.x<100){BoxC.setZorder(0)} thus preventing BoxC from rendering on to screen
for(TextureComponets textureComponets:textureComponetsList)
{
//This loop draws the sorted list on to the screen
//if you look above code we have set zOrder for BoxA and BoxB as zero so they
//wont be rendered on the screen but BoxC will be rendered on screen since it have zOrder value greater than zero
textureComponets.draw(batch);
}
batch.end();
}
@Override
public void dispose(){
}
//I want to zoom in to the map here
@Override
public void resize(int width, int height){
}
@Override
public void pause(){
}
@Override
public void resume(){
}
}
No comments:
Post a Comment