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;
}
}
}