2
$\begingroup$

I am attempting to implement chunks/octrees to speed up my render times. However, sometimes the walls show horizontal or vertical lines (rays hitting the inside of voxels?) or some blocks disappear altogether, although sometimes everything renders fine.

Edit: Yes, the chunks are stored fine. OctrA stands for OctreeA, which is the chunk. Edit: I reversed the ray for two steps after hitting a chunk. Although the glitches have gotten better, there is still the same problem.


      float cameraX = x/(float)HALFRESX -1;// -1 to 1
      struct Point3D rayd;
      rayd.x = dir.x + plane.x * cameraX;
      rayd.y = dir.y + plane.y * cameraX;

      struct Point3D delta;
      delta.x = fabsf(1/rayd.x);
      delta.y = fabsf(1/rayd.y);

      int zy = -HALFRESY;

      struct pos map;
      map.x = (char)pos.x;
      map.y = (char)pos.y;

      float sdistX;
      float sdistY;
      struct pos s;
      struct pos step;
      struct Point3D sdist;
      if(rayd.x < 0) {
        step.x = -1;
        s.x = 1;
        sdistX = (pos.x - map.x) * delta.x; 
      }
      else {
        step.x = 1;
        s.x = 0;
        sdistX = (map.x + 1 - pos.x) * delta.x; 
      }
      if(rayd.y < 0) {
        step.y = -1;
        s.y = 1;
        sdistY = (pos.y - map.y) * delta.y; 
      }
      else {
        step.y = 1;
        s.y = 0;
        sdistY = (map.y + 1 - pos.y) * delta.y; 
      }

      for(int y = 0; y < RESY; y += 1) { 

        zy++;
        float cameraY = y/(float)HALFRESY - 1;
        rayd.z = dir.z + plane.z * cameraY;
        delta.z = fabsf(1/rayd.z);

        map.x = (char)pos.x;
        map.y = (char)pos.y;
        map.z = (char)pos.z;
        sdist.x = sdistX;
        sdist.y = sdistY; 

        if(rayd.z < 0) {
          step.z = -1;
          s.z = 1;
          sdist.z = (pos.z - map.z) * delta.z; 
        }
        else {
          step.z = 1;
          s.z = 0;
          sdist.z = (map.z + 1 - pos.z) * delta.z; 
        }
#ifdef USE_OCTREE

        while( !octrA[map.x/2][map.y/2][map.z/2]) {
          if(sdist.y < sdist.x ) {
            if(sdist.y < sdist.z) {
              sdist.y += delta.y*2;
              map.y += step.y*2;
            }
            else {
              sdist.z += delta.z*2;
              map.z += step.z*2;
            }
          }
          else {
            if(sdist.x < sdist.z) {
              sdist.x += delta.x*2;
              map.x += step.x*2;
            }
            else {
              sdist.z += delta.z*2;
              map.z += step.z*2; 
            }
          }
        }
        if(octr) {
          if(sdist.y > sdist.x ) {
            if(sdist.y > sdist.z) {
              sdist.y -= delta.y*2;
              map.y -= step.y*2;
            }
            else {
              sdist.z -= delta.z*2;
              map.z -= step.z*2;
            }
          }
          else {
            if(sdist.x > sdist.z) {
              sdist.x -= delta.x*2;
              map.x -= step.x*2;
            }
            else {
              sdist.z -= delta.z*2;
              map.z -= step.z*2; 
            }
          }
        }

#endif
        char side; //either 0 (NS), or 1 (EW), or 2(UD)
        while( !MAP[map.x][map.y][map.z]) {
          if(sdist.y < sdist.x ) {
            if(sdist.y < sdist.z) {
              sdist.y += delta.y;
              map.y += step.y;
              side = 1; // y
            }
            else {
              sdist.z += delta.z;
              map.z += step.z;
              side = 2; 
            }
          }
          else {
            if(sdist.x < sdist.z) {
              sdist.x += delta.x;
              map.x += step.x;
              side = 0; 
            }
            else {
              sdist.z += delta.z;
              map.z += step.z; 
              side = 2; 
            }
          }
        }

$\endgroup$
  • $\begingroup$ rayd.x = dir.x + plane.x * cameraX; rayd.y = dir.y + plane.y * cameraX; -- Shouldn't that second rayd.y assignment be taking a cameraY variable instead ? $\endgroup$ – PaulHK Dec 18 '19 at 5:47
  • $\begingroup$ CameraX is -1 to 1 and corresponds with the X axis on screen, so no $\endgroup$ – Russell356 Dec 18 '19 at 9:24
0
$\begingroup$

Have you confirmed that objects that clip a chunk border are stored in both chunks?

Have you confirmed that octrA[map.x/2][map.y/2][map.z/2] can't point to anything weird?

In fact, what even is octrA? I assume that it's the array storing the chunks but I can't know for sure because I couldn't find it anywhere else in your code.

Other than that I'm having a bit of a hard time interpreting the problem and your code. Could you add some pictures and comments to your code?

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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