Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been working on a level-of-detail program. Right now I have the rough structure down (splitting of quads without condensing of quads). But the problem is that when you try to split certain quads after they are 1/8 the size of the original they sometimes do not split and the quads that should have been placed in that spot are in another place. I think I just need someone else to look at this code, I believe the problem rests in the index of the quads... Here is the main code:

public Quad(Vector3f v1, Vector3f v2, Vector3f v3, Vector3f v4,float size,int x, int y,boolean has,float pI, float pI2){
    first = v1;
    second = v2;
    third = v3;
    fourth = v4;
    width = size;
    index1 = x;
    index2 = y;
    children = has;
    parentIn1 = pI;
    parentIn2 = pI2;

}
public static ArrayList<Vector3f> makeVerts(Vector3f p, int x, int y, float increment){
    ArrayList<Vector3f> list = new ArrayList<Vector3f>();
    Vector3f back_left = new Vector3f ((float)x*increment, 0 , (float)y*increment).add(p);
    Vector3f front_right = new Vector3f ((float)(x*increment) + increment, 0, (float)(y*increment)+increment).add(p);
    Vector3f front_left = new Vector3f ((float)x*increment, 0 , (float)(y*increment)+increment).add(p);
    Vector3f back_right = new Vector3f((float)(x*increment) + increment, 0, (float)y*increment).add(p);

    list.add(back_left);
    list.add(front_left);
    list.add(front_right);
    list.add(back_right);
    return list;
}

public static void create(Quad quad, int i, int ii){
    quad.children = true;
    i+=quad.parentIn1;
    ii+=quad.parentIn2;
    System.out.println(i+ " "+ii);
    Vector3f parent = new Vector3f(i*quad.width, 0, ii*quad.width);
    //System.out.println(parent + " "+ toString(quad));
    if(quads == null){
        quads = new Quad[2][2];
        for(int x = 0; x<2; x++){
            for(int y = 0; y<2; y++){
                float growth = quad.width/2;
                ArrayList<Vector3f> points = makeVerts(parent,x,y,growth);
                Vector3f p1 = points.get(0);
                Vector3f p2 = points.get(1);
                Vector3f p3 = points.get(2);
                Vector3f p4 = points.get(3);
                quads[x][y] = new Quad(p1,p2,p3,p4,growth, (int) (x+quad.index1),(int)(y+quad.index2),false,quad.index1,quad.index2);
                list.add(quads[x][y]);
        }

    }
}   

    }

In addition here is the code that creates the first quad and divides it into four:

Vector3f v1 = new Vector3f(0,0,0);
Vector3f v2 = new Vector3f(0,0,32);
Vector3f v3 = new Vector3f(32,0,32);
Vector3f v4 = new Vector3f(32,0,0);
Quad q = new Quad(v1,v2,v3,v4,32,0,0,false,0,0);
Quad.create(q,0,0);
v.addAll(Quad.getPoints());
Quad.reset();

And the code that divides quads depending on one's distance:

   for(int i = 0; i<Quad.list.size(); i++){
        Quad q = Quad.list.get(i);
        Vector3f center = Quad.getCenter(q);
        float width = q.width;
        float distance = center.distance(cam.getLocation());
    if(width/distance >= .85 && !q.children){
        Quad.reset();
        Quad.create(q,q.index1,q.index2);
        VertexBuffer vb = mesh.getBuffer(Type.Position);
        v.clear();
        v.addAll(Quad.getPoints());
        verts = new Vector3f[v.size()];
        v.toArray(verts);
        vb.updateData(BufferUtils.createFloatBuffer(verts));

parentIn1&2 are the indices of the parent quad. Index1&2 are the indices of the quad. Each time a quad needs to divided the "create" method is run... Just for an idea of what is happening here is a shot of the actual scene. On this run I zoomed in exactly on the center of the quad and this was the result.. enter image description here Thanks for any and all help, it is much appreciated.

share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, Tetrad Mar 13 '13 at 23:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What values are you using to instantiate your structure, and what do you think you should be seeing instead of what you are seeing? –  Seth Battin Mar 13 '13 at 2:24
    
the initial values for the quad are (0,0,0) (0,0,32) (32,0,32) (32,0,0) and all four of those quads around the center should be subdivided and the ones even closer to the center should be further subdivided. Instead only one of the quads is subdivided and as you can see a quad within that one is also divided pointlessly... –  pl12 Mar 13 '13 at 2:43
    
How about the other values that you pass to the constructor. Would you post the code you use to create it, including your calls to makeVerts and create? –  Seth Battin Mar 13 '13 at 2:52
    
Just added it...I suppose the error could exist in the Quad.create() method called in the distance-based subdivision algorithm... –  pl12 Mar 13 '13 at 3:41
1  
This feels like a "fix my code" type question, which is really too localized for the site. Chances are you'll just have to step through the code with a debugger and see exactly what is going wrong with the data. –  Tetrad Mar 13 '13 at 16:42

1 Answer 1

I fixed the problem... Here is the revised code for the method "create()" if anyone is interested, Thanks for the time:

public static void create(Quad quad, int i, int ii){
    quad.children = true;
    i*=2;
    ii*=2;
    Vector3f parent = new Vector3f(i*quad.width/2, 0, ii*quad.width/2);
    System.out.println(parent + " "+ toString(quad));
    if(quads == null){
        quads = new Quad[2][2];
        for(int x = 0; x<2; x++){
            for(int y = 0; y<2; y++){
                float growth = quad.width/2;
                ArrayList<Vector3f> points = makeVerts(parent,x,y,growth);
                Vector3f p1 = points.get(0);
                Vector3f p2 = points.get(1);
                Vector3f p3 = points.get(2);
                Vector3f p4 = points.get(3);
                quads[x][y] = new Quad(p1,p2,p3,p4,growth,(x+i),(y+ii),false,quad.index1,quad.index2);
                list.add(quads[x][y]);
        }

    }
}   

    }
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.