Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Face culling on voxels.  (Read 355 times)
0 Members and 1 Guest are viewing this topic.
Offline EliwoodL

Senior Newbie





« Posted 2013-07-12 19:56:41 »

I have a segment of code here that's supposed to cull faces of voxels that should be invisible.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
   private FloatBuffer voxelData(int x, int y, int z) {
      float r = rand.nextFloat(), g = rand.nextFloat(), b = rand.nextFloat();
      float xscale = x * scale, yscale = y * scale, zscale = z * scale;
      int numfaces = 6;

      boolean top = true, bottom = false, left = true, right = true, front = true, back = true;
      if (x != width--  && voxels[x + 1][y][z].isActive) {right = false; numfaces--;}
      if (x != 0         && voxels[x - 1][y][z].isActive) {left = false; numfaces--;}
      if (y != height-- && voxels[x][y + 1][z].isActive) {top = false; numfaces--;}
      if (y != 0         && voxels[x][y - 1][z].isActive) {bottom = false; numfaces--;}
      if (z != length-- && voxels[x][y][z + 1].isActive) {front = false; numfaces--;}
      if (z != 0         && voxels[x][y][z - 1].isActive) {back = false; numfaces--;}

      if (numfaces == 0) return null;

      FloatBuffer data = BufferUtils.createFloatBuffer(numfaces * 24);

      if (top) { data.put(new float[] {
            this.x + xscale + 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
      });}

      if (bottom) { data.put(new float[] {
            this.x + xscale - 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
      });}

      if (left) { data.put(new float[] {
            this.x + xscale - 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
      });}

      if (right) { data.put(new float[] {
            this.x + xscale + 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
      });}

      if (front) { data.put(new float[] {
            this.x + xscale + 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale + 0.5f * scale,   r, g, b,
      });}

      if (back) { data.put(new float[] {
            this.x + xscale + 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale - 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale - 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
            this.x + xscale + 0.5f * scale,      this.y + yscale + 0.5f * scale,      this.z + zscale - 0.5f * scale,   r, g, b,
      });}

      data.flip();
      return data;
   }

The segment is passed the x, y, and z values through a triple-nested for-loop checking each voxel in a 3d array.  However, the code seems to only output left and back sides; never any others.  Even then, it's only the left-bottom-back corner that renders, and not even the bottom face; just the back, and three left faces from the edge, and a face one above that.
Here's a screenshot:  
Offline Longor1996
« Reply #1 - Posted 2013-07-13 00:54:17 »

I recommend you to use some kind of interface to access the voxels in your world/chunk.
It makes the code look better and some things easier.

For example:
1  
2  
3  
interface IVoxelAccess{
    public ?Voxel? getVoxel(int x,int y,int z);
}


Then make your world implement the interface:
1  
2  
3  
4  
5  
class ?World? implements IVoxelAccess{
    public ?Voxel? getVoxel(int x,int y,int z){
        return ?voxels[x][y][z]?;
    }
}

(?XXX? means that I don't know how the class is called)


And then let the renderer use that interface:
1  
2  
3  
4  
5  
6  
      if (x != width--  && getVoxel(x + 1,y,z).isActive) {right = false; numfaces--;}
      if (x != 0         && getVoxel(x - 1,y,z).isActive) {left = false; numfaces--;}
      if (y != height-- && getVoxel(x,y + 1,z).isActive) {top = false; numfaces--;}
      if (y != 0         && getVoxel(x,y - 1,z).isActive) {bottom = false; numfaces--;}
      if (z != length-- && getVoxel(x,y,z + 1).isActive) {front = false; numfaces--;}
      if (z != 0         && getVoxel(x,y,z - 1).isActive) {back = false; numfaces--;}


Trust me: It makes your life easier.

Next thing: Wrong directions.

You should check all additions/subtractions to your coordinates,
and also make drawings for youself to know which side belongs to which axis in which direction.

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline EliwoodL

Senior Newbie





« Reply #2 - Posted 2013-07-13 01:26:48 »

Ahh, that's actually what I was busy doing.  I eventually figured out the whole access issue, although I'm not sure the method that I went about is correct.

By the way, there is no World class;  each voxel group effectively needs to know about all the voxels it contains.  So i suppose there are many World classes in that respect; the basic group and all of the ones that extend it (so they can behave differently - ie a turret of a ship needs to rotate differently than the ship itself.)  To that end, this is what I've come up with:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
   public void updateVBO() {
      int numfaces = 0;
      boolean front = false, back = false, left = false, right = false, top = false, bottom = false;
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height; y++) {
            for (int z = 0; z < length; z++) {

               if (isOOB(x+1, y, z) || !isOCC(x+1, y, z)) {right = true; numfaces++;}
               if (isOOB(x-1, y, z) || !isOCC(x-1, y, z)) {left = true; numfaces++;}
               if (isOOB(x, y+1, z) || !isOCC(x, y+1, z)) {top = true; numfaces++;}
               if (isOOB(x, y-1, z) || !isOCC(x, y-1, z)) {bottom = true; numfaces++;}
               if (isOOB(x, y, z+1) || !isOCC(x, y, z+1)) {right = true; numfaces++;}
               if (isOOB(x, y, z-1) || !isOCC(x, y, z-1)) {left = true; numfaces++;}

               voxelData(x, y, z, voxels[x][y][z].type, numfaces, front, back, left, right, top, bottom);

            }
         }
      }
   }

   protected boolean isOOB(int x, int y, int z) {
      if (x * y * z > width * height * length || x * y * z < 0) return true;
      return false;
   }
   protected boolean isOCC(int x, int y, int z) {
      if (!isOOB(x, y, z)) if (voxels[x][y][z].opaque) return true;
      return false;
   }


Also, I've refrained from putting data into the actual floatbuffer for now because I don't want the buffer being too big.  That being said, compacting it would work to remove the extra space, right?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (32 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!