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

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

View File

@ -15,9 +15,14 @@ func (texture Textures) At (wall int, offset Vector) color.RGBA {
wall --
if wall < 0 || wall >= len(texture) { return color.RGBA { } }
image := texture[wall]
xOffset := int(offset.X * float64(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) {