/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public function render() { // find which map block the camera is in $cameraBlockX = $this->camX >> 6; $cameraBlockY = $this->camY >> 6; $offset = 0; // clamp angle for lookup tables $angle = $this->camAngle + $this->camFov * 0.5; $width = $this->width; $this->camPCentre = $this->height / 2 - $this->camRoll; $ang = ($this->camAngle * $this->divisor) | 0; $invertZ = 64 - $this->camZ; // loop through each vertical strip while ($width-- > 0) { // set nearest face to practical infinity $nearestFace = 4294967296; $angleLookup = ($angle * $this->divisor); // find oriented face aligned to X axis of map $x = $cameraBlockX; $y = $cameraBlockY; while ($y > -1 && $x > -1 && $x < $this->mapWidth) { $absY = $y << 6; $x = $absX >> 6; // check if there is a block here if ($this->map[$x][--$y]) { $offset = $absX & 63; break; } } } else { while ($y++ < $this->mapHeight && $x > -1 && $x < $this->mapWidth) { $absY = $y << 6; $x = $absX >> 6; // check if there is a block here if ($this->map[$x][$y]) { $offset = 64 - $absX & 63; break; } } } // find closest face oriented to Y axis of map $x = $cameraBlockX; $y = $cameraBlockY; while ($x > -1 && $y > -1 && $y < $this->mapHeight) { $absX = $x << 6; $y = $absY >> 6; // check if there is a block here if ($this->map[--$x][$y]) { if ($distance < $nearestFace) { $nearestFace = $distance; $offset = 64 - $absY & 63; } break; } } } else { while ($x++ < $this->mapWidth && $y > -1 && $y < $this->mapHeight) { $absX = $x << 6; $y = $absY >> 6; // check if there is a block here if ($this->map[$x][$y]) { if ($distance < $nearestFace) { $nearestFace = $distance; $offset = $absY & 63; } break; } } } // check whether the current strip is looking left or right of centre angle if ($angleLookup < $ang) { } else { } $distanceFloor = $invertZ * $distort; $distanceCeiling = $this->camZ * $distort; $floorLevel = (int) ($this->camPCentre + $wallHeight * $this->camZ + 0.5); $wallLevel = (int) ($this->camPCentre - $wallHeight * $invertZ); $height = $this->height; // draw floor while (--$height > $floorLevel && $height >= 0) { $distance = $distanceFloor / ($height - $this->camPCentre); imagesetpixel($this->canvas, $width, $height, imagecolorat($this->texFloor, ($this->camX + $this->cos[$angleLookup] * $distance) & 63, ($this->camY + $this->sin[$angleLookup] * $distance) & 63)); } // draw wall while (--$height > $wallLevel && $height >= 0) { imagesetpixel($this->canvas, $width, $height, imagecolorat($this->texWall, $offset, ($height - $wallLevel) / $wallHeight)); } // draw ceiling while (--$height > -1) { $distance = $distanceCeiling / ($this->camPCentre - $height); imagesetpixel($this->canvas, $width, $height, imagecolorat($this->texCeil, ($this->camX + $this->cos[$angleLookup] * $distance) & 63, ($this->camY + $this->sin[$angleLookup] * $distance) & 63)); } $angle -= $this->camPerRayAngle; } }