Fixed texture warping when too close to walls

This commit is contained in:
Sasha Koshka 2023-02-21 18:15:41 -05:00
parent ce1d938f7a
commit ddb960571f
2 changed files with 30 additions and 24 deletions

View File

@ -110,6 +110,7 @@ func (element *Raycaster) drawAll () {
// artist.FillRectangle(element.core, artist.Uhex(0x000000FF), bounds) // artist.FillRectangle(element.core, artist.Uhex(0x000000FF), bounds)
width := bounds.Dx() width := bounds.Dx()
height := bounds.Dy() height := bounds.Dy()
halfway := bounds.Max.Y - height / 2
ray := Ray { Angle: element.Camera.Angle - element.Camera.Fov / 2 } ray := Ray { Angle: element.Camera.Angle - element.Camera.Fov / 2 }
@ -140,31 +141,31 @@ func (element *Raycaster) drawAll () {
// draw // draw
data, stride := element.core.Buffer() data, stride := element.core.Buffer()
wallStart := height / 2 - wallHeight + bounds.Min.Y wallStart := halfway - wallHeight
wallEnd := height / 2 + wallHeight + bounds.Min.Y wallEnd := halfway + wallHeight
if wallStart < 0 { wallStart = 0 }
if wallEnd > bounds.Max.Y { wallEnd = bounds.Max.Y }
for y := bounds.Min.Y; y < wallStart; y ++ { for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
switch {
case y < wallStart:
data[y * stride + x + bounds.Min.X] = ceilingColor data[y * stride + x + bounds.Min.X] = ceilingColor
}
slicePoint := 0.0 case y < wallEnd:
slicePointDelta := 1 / float64(wallEnd - wallStart) textureY :=
for y := wallStart; y < wallEnd; y ++ { float64(y - halfway) /
float64(wallEnd - wallStart) + 0.5
// fmt.Println(textureY)
wallColor := element.textures.At (wall, Vector { wallColor := element.textures.At (wall, Vector {
textureX, textureX,
slicePoint, textureY,
}) })
wallColor = shadeColor(wallColor, shade) wallColor = shadeColor(wallColor, shade)
data[y * stride + x + bounds.Min.X] = wallColor data[y * stride + x + bounds.Min.X] = wallColor
slicePoint += slicePointDelta default:
}
for y := wallEnd; y < bounds.Max.Y; y ++ {
data[y * stride + x + bounds.Min.X] = floorColor data[y * stride + x + bounds.Min.X] = floorColor
} }
}
// increment angle // increment angle
ray.Angle += element.Camera.Fov / float64(width) ray.Angle += element.Camera.Fov / float64(width)

View File

@ -15,9 +15,14 @@ func (texture Textures) At (wall int, offset Vector) color.RGBA {
wall -- wall --
if wall < 0 || wall >= len(texture) { return color.RGBA { } } if wall < 0 || wall >= len(texture) { return color.RGBA { } }
image := texture[wall] image := texture[wall]
xOffset := int(offset.X * float64(image.Stride)) xOffset := int(offset.X * float64(image.Stride))
yOffset := int(offset.Y * float64(len(image.Data) / image.Stride)) yOffset := int(offset.Y * float64(len(image.Data) / image.Stride))
return image.Data[xOffset + yOffset * image.Stride]
index := xOffset + yOffset * image.Stride
if index < 0 { return color.RGBA { } }
if index >= len(image.Data) { return color.RGBA { } }
return image.Data[index]
} }
func TextureFrom (source io.Reader) (texture Texture, err error) { func TextureFrom (source io.Reader) (texture Texture, err error) {