1
0
Fork 0

draw using color palette rather than RGB directly.

This commit is contained in:
Henrik Hautakoski 2025-06-25 22:15:26 +02:00
parent 82b0b0a386
commit 575791cfc1
5 changed files with 277 additions and 14 deletions

View file

@ -6,14 +6,14 @@ import (
"github.com/pnx/go-raytracer/world"
)
func DrawColumn(ctx *graphics.Context, x int32, wall_h int32, color math.Color) {
func DrawColumn(ctx *graphics.Context, x int32, wall_h int32, color byte) {
if wall_h < ctx.Height() {
y1 := (ctx.Height() - wall_h) / 2
y2 := (ctx.Height() + wall_h) / 2
// Top
// renderer.SetDrawColor(90, 90, 0, 255)
ctx.DrawLine(x, 0, int32(x), y1, math.Color{R: 90, G: 90, B: 0, A: 255})
ctx.DrawLine(x, 0, int32(x), y1, 0x1D)
// // Middle
// renderer.SetDrawColor(color.R, color.G, color.B, color.A)
@ -22,7 +22,7 @@ func DrawColumn(ctx *graphics.Context, x int32, wall_h int32, color math.Color)
// Bottom
if y2 < ctx.Height() {
// renderer.SetDrawColor(50, 50, 50, 255)
ctx.DrawLine(x, y2, int32(x), ctx.Height(), math.Color{R: 50, G: 50, B: 50, A: 255})
ctx.DrawLine(x, y2, int32(x), ctx.Height(), 0x19)
}
return
}
@ -31,15 +31,15 @@ func DrawColumn(ctx *graphics.Context, x int32, wall_h int32, color math.Color)
ctx.DrawLine(x, 0, int32(x), ctx.Height(), color)
}
func DrawScene(ctx *graphics.Context, camera math.Transform, level *world.Level, colorMap []math.Color) {
func DrawScene(ctx *graphics.Context, camera math.Transform, level *world.Level) {
for x := range ctx.Width() {
result := CastRay(camera, level, int(x), int(ctx.Width()))
lineHeight := int(float64(ctx.Height()) / result.Distance)
color := colorMap[level.Cell(int(result.Cell.X), int(result.Cell.Y))-1]
color := level.Cell(int(result.Cell.X), int(result.Cell.Y))
if result.Side > 0 {
color = color.Shade(2)
color += 0x8
}
DrawColumn(ctx, int32(x), int32(lineHeight), color)

View file

@ -12,25 +12,25 @@ func DrawMiniMap(ctx *graphics.Context, camera math.Transform, level *world.Leve
// offset := math.Vec2i32{X: 25, Y: 25}
// size := math.Vec2i32{X: 200, Y: 100}
ctx.DrawRect(0, 0, int32(level.W*tileSize)+3, int32(level.H*tileSize)+3, math.Color{})
ctx.DrawRect(0, 0, int32(level.W*tileSize)+3, int32(level.H*tileSize)+3, 0)
for y := range level.H {
for x := range level.W {
if level.Wall(x, y) {
ctx.DrawRect(int32(x*tileSize), int32(y*tileSize), tileSize, tileSize, math.Color{R: 255, G: 255, B: 255, A: 255})
ctx.DrawRect(int32(x*tileSize), int32(y*tileSize), tileSize, tileSize, 4)
}
}
}
// Camera Position
cPos := camera.Position.Get().Scale(scale)
ctx.DrawRect(int32(cPos.X-2), int32(cPos.Y-2), 4, 4, math.Color{R: 255, G: 0, B: 255, A: 255})
ctx.DrawRect(int32(cPos.X-2), int32(cPos.Y-2), 4, 4, 2)
// Rays
for x := range ctx.Width() {
hit := CastRay(camera, level, int(x), int(ctx.Width()))
end := camera.Position.Get().AddVec(hit.Pos.Scale(world.TileSize)).Scale(scale)
ctx.DrawLine(int32(cPos.X), int32(cPos.Y), int32(end.X), int32(end.Y), math.Color{R: 150, G: 150, B: 0, A: 255})
ctx.DrawLine(int32(cPos.X), int32(cPos.Y), int32(end.X), int32(end.Y), 2)
}
}