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

262
graphics/palette.go Normal file
View file

@ -0,0 +1,262 @@
package graphics
import "github.com/pnx/go-raytracer/math"
var Palette = [256]math.Color{
{R: 0, G: 0, B: 0},
{R: 0, G: 0, B: 168},
{R: 0, G: 168, B: 0},
{R: 0, G: 168, B: 168},
{R: 168, G: 0, B: 0},
{R: 168, G: 0, B: 168},
{R: 168, G: 84, B: 0},
{R: 168, G: 168, B: 168},
{R: 84, G: 84, B: 84},
{R: 84, G: 84, B: 252},
{R: 84, G: 252, B: 84},
{R: 84, G: 252, B: 252},
{R: 252, G: 84, B: 84},
{R: 252, G: 84, B: 252},
{R: 252, G: 252, B: 84},
{R: 252, G: 252, B: 252},
{R: 236, G: 236, B: 236},
{R: 220, G: 220, B: 220},
{R: 208, G: 208, B: 208},
{R: 192, G: 192, B: 192},
{R: 180, G: 180, B: 180},
{R: 168, G: 168, B: 168},
{R: 152, G: 152, B: 152},
{R: 140, G: 140, B: 140},
{R: 124, G: 124, B: 124},
{R: 112, G: 112, B: 112},
{R: 100, G: 100, B: 100},
{R: 84, G: 84, B: 84},
{R: 72, G: 72, B: 72},
{R: 56, G: 56, B: 56},
{R: 44, G: 44, B: 44},
{R: 32, G: 32, B: 32},
{R: 252, G: 0, B: 0},
{R: 236, G: 0, B: 0},
{R: 224, G: 0, B: 0},
{R: 212, G: 0, B: 0},
{R: 200, G: 0, B: 0},
{R: 188, G: 0, B: 0},
{R: 176, G: 0, B: 0},
{R: 164, G: 0, B: 0},
{R: 152, G: 0, B: 0},
{R: 136, G: 0, B: 0},
{R: 124, G: 0, B: 0},
{R: 112, G: 0, B: 0},
{R: 100, G: 0, B: 0},
{R: 88, G: 0, B: 0},
{R: 76, G: 0, B: 0},
{R: 64, G: 0, B: 0},
{R: 252, G: 216, B: 216},
{R: 252, G: 184, B: 184},
{R: 252, G: 156, B: 156},
{R: 252, G: 124, B: 124},
{R: 252, G: 92, B: 92},
{R: 252, G: 64, B: 64},
{R: 252, G: 32, B: 32},
{R: 252, G: 0, B: 0},
{R: 252, G: 168, B: 92},
{R: 252, G: 152, B: 64},
{R: 252, G: 136, B: 32},
{R: 252, G: 120, B: 0},
{R: 228, G: 108, B: 0},
{R: 204, G: 96, B: 0},
{R: 180, G: 84, B: 0},
{R: 156, G: 76, B: 0},
{R: 252, G: 252, B: 216},
{R: 252, G: 252, B: 184},
{R: 252, G: 252, B: 156},
{R: 252, G: 252, B: 124},
{R: 252, G: 248, B: 92},
{R: 252, G: 244, B: 64},
{R: 252, G: 244, B: 32},
{R: 252, G: 244, B: 0},
{R: 228, G: 216, B: 0},
{R: 204, G: 196, B: 0},
{R: 180, G: 172, B: 0},
{R: 156, G: 156, B: 0},
{R: 132, G: 132, B: 0},
{R: 112, G: 108, B: 0},
{R: 88, G: 84, B: 0},
{R: 64, G: 64, B: 0},
{R: 208, G: 252, B: 92},
{R: 196, G: 252, B: 64},
{R: 180, G: 252, B: 32},
{R: 160, G: 252, B: 0},
{R: 144, G: 228, B: 0},
{R: 128, G: 204, B: 0},
{R: 116, G: 180, B: 0},
{R: 96, G: 156, B: 0},
{R: 216, G: 252, B: 216},
{R: 188, G: 252, B: 184},
{R: 156, G: 252, B: 156},
{R: 128, G: 252, B: 124},
{R: 96, G: 252, B: 92},
{R: 64, G: 252, B: 64},
{R: 32, G: 252, B: 32},
{R: 0, G: 252, B: 0},
{R: 0, G: 252, B: 0},
{R: 0, G: 236, B: 0},
{R: 0, G: 224, B: 0},
{R: 0, G: 212, B: 0},
{R: 4, G: 200, B: 0},
{R: 4, G: 188, B: 0},
{R: 4, G: 176, B: 0},
{R: 4, G: 164, B: 0},
{R: 4, G: 152, B: 0},
{R: 4, G: 136, B: 0},
{R: 4, G: 124, B: 0},
{R: 4, G: 112, B: 0},
{R: 4, G: 100, B: 0},
{R: 4, G: 88, B: 0},
{R: 4, G: 76, B: 0},
{R: 4, G: 64, B: 0},
{R: 216, G: 252, B: 252},
{R: 184, G: 252, B: 252},
{R: 156, G: 252, B: 252},
{R: 124, G: 252, B: 248},
{R: 92, G: 252, B: 252},
{R: 64, G: 252, B: 252},
{R: 32, G: 252, B: 252},
{R: 0, G: 252, B: 252},
{R: 0, G: 228, B: 228},
{R: 0, G: 204, B: 204},
{R: 0, G: 180, B: 180},
{R: 0, G: 156, B: 156},
{R: 0, G: 132, B: 132},
{R: 0, G: 112, B: 112},
{R: 0, G: 88, B: 88},
{R: 0, G: 64, B: 64},
{R: 92, G: 188, B: 252},
{R: 64, G: 176, B: 252},
{R: 32, G: 168, B: 252},
{R: 0, G: 156, B: 252},
{R: 0, G: 140, B: 228},
{R: 0, G: 124, B: 204},
{R: 0, G: 108, B: 180},
{R: 0, G: 92, B: 156},
{R: 216, G: 216, B: 252},
{R: 184, G: 188, B: 252},
{R: 156, G: 156, B: 252},
{R: 124, G: 128, B: 252},
{R: 92, G: 96, B: 252},
{R: 64, G: 64, B: 252},
{R: 32, G: 36, B: 252},
{R: 0, G: 4, B: 252},
{R: 0, G: 0, B: 252},
{R: 0, G: 0, B: 236},
{R: 0, G: 0, B: 224},
{R: 0, G: 0, B: 212},
{R: 0, G: 0, B: 200},
{R: 0, G: 0, B: 188},
{R: 0, G: 0, B: 176},
{R: 0, G: 0, B: 164},
{R: 0, G: 0, B: 152},
{R: 0, G: 0, B: 136},
{R: 0, G: 0, B: 124},
{R: 0, G: 0, B: 112},
{R: 0, G: 0, B: 100},
{R: 0, G: 0, B: 88},
{R: 0, G: 0, B: 76},
{R: 0, G: 0, B: 64},
{R: 40, G: 40, B: 40},
{R: 252, G: 224, B: 52},
{R: 252, G: 212, B: 36},
{R: 252, G: 204, B: 24},
{R: 252, G: 192, B: 8},
{R: 252, G: 180, B: 0},
{R: 180, G: 32, B: 252},
{R: 168, G: 0, B: 252},
{R: 152, G: 0, B: 228},
{R: 128, G: 0, B: 204},
{R: 116, G: 0, B: 180},
{R: 96, G: 0, B: 156},
{R: 80, G: 0, B: 132},
{R: 68, G: 0, B: 112},
{R: 52, G: 0, B: 88},
{R: 40, G: 0, B: 64},
{R: 252, G: 216, B: 252},
{R: 252, G: 184, B: 252},
{R: 252, G: 156, B: 252},
{R: 252, G: 124, B: 252},
{R: 252, G: 92, B: 252},
{R: 252, G: 64, B: 252},
{R: 252, G: 32, B: 252},
{R: 252, G: 0, B: 252},
{R: 224, G: 0, B: 228},
{R: 200, G: 0, B: 204},
{R: 180, G: 0, B: 180},
{R: 156, G: 0, B: 156},
{R: 132, G: 0, B: 132},
{R: 108, G: 0, B: 112},
{R: 88, G: 0, B: 88},
{R: 64, G: 0, B: 64},
{R: 252, G: 232, B: 220},
{R: 252, G: 224, B: 208},
{R: 252, G: 216, B: 196},
{R: 252, G: 212, B: 188},
{R: 252, G: 204, B: 176},
{R: 252, G: 196, B: 164},
{R: 252, G: 188, B: 156},
{R: 252, G: 184, B: 144},
{R: 252, G: 176, B: 128},
{R: 252, G: 164, B: 112},
{R: 252, G: 156, B: 96},
{R: 240, G: 148, B: 92},
{R: 232, G: 140, B: 88},
{R: 220, G: 136, B: 84},
{R: 208, G: 128, B: 80},
{R: 200, G: 124, B: 76},
{R: 188, G: 120, B: 72},
{R: 180, G: 112, B: 68},
{R: 168, G: 104, B: 64},
{R: 160, G: 100, B: 60},
{R: 156, G: 96, B: 56},
{R: 144, G: 92, B: 52},
{R: 136, G: 88, B: 48},
{R: 128, G: 80, B: 44},
{R: 116, G: 76, B: 40},
{R: 108, G: 72, B: 36},
{R: 92, G: 64, B: 32},
{R: 84, G: 60, B: 28},
{R: 72, G: 56, B: 24},
{R: 64, G: 48, B: 24},
{R: 56, G: 44, B: 20},
{R: 40, G: 32, B: 12},
{R: 96, G: 0, B: 100},
{R: 0, G: 100, B: 100},
{R: 0, G: 96, B: 96},
{R: 0, G: 0, B: 28},
{R: 0, G: 0, B: 44},
{R: 48, G: 36, B: 16},
{R: 72, G: 0, B: 72},
{R: 80, G: 0, B: 80},
{R: 0, G: 0, B: 52},
{R: 28, G: 28, B: 28},
{R: 76, G: 76, B: 76},
{R: 92, G: 92, B: 92},
{R: 64, G: 64, B: 64},
{R: 48, G: 48, B: 48},
{R: 52, G: 52, B: 52},
{R: 216, G: 244, B: 244},
{R: 184, G: 232, B: 232},
{R: 156, G: 220, B: 220},
{R: 116, G: 200, B: 200},
{R: 72, G: 192, B: 192},
{R: 32, G: 180, B: 180},
{R: 32, G: 176, B: 176},
{R: 0, G: 164, B: 164},
{R: 0, G: 152, B: 152},
{R: 0, G: 140, B: 140},
{R: 0, G: 132, B: 132},
{R: 0, G: 124, B: 124},
{R: 0, G: 120, B: 120},
{R: 0, G: 116, B: 116},
{R: 0, G: 112, B: 112},
{R: 0, G: 108, B: 108},
{R: 152, G: 0, B: 136},
}

View file

@ -1,7 +1,6 @@
package graphics package graphics
import ( import (
"github.com/pnx/go-raytracer/math"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
@ -19,12 +18,14 @@ func (c Context) Height() int32 {
return c.h return c.h
} }
func (c Context) DrawLine(x1 int32, y1 int32, x2 int32, y2 int32, color math.Color) { func (c Context) DrawLine(x1 int32, y1 int32, x2 int32, y2 int32, colorIndex byte) {
color := Palette[colorIndex]
c.renderer.SetDrawColor(color.R, color.G, color.B, color.A) c.renderer.SetDrawColor(color.R, color.G, color.B, color.A)
c.renderer.DrawLine(x1, y1, x2, y2) c.renderer.DrawLine(x1, y1, x2, y2)
} }
func (c Context) DrawRect(x, y, w, h int32, color math.Color) { func (c Context) DrawRect(x, y, w, h int32, colorIndex byte) {
color := Palette[colorIndex]
c.renderer.SetDrawColor(color.R, color.G, color.B, color.A) c.renderer.SetDrawColor(color.R, color.G, color.B, color.A)
c.renderer.FillRect(&sdl.Rect{ c.renderer.FillRect(&sdl.Rect{
X: x, X: x,

View file

@ -85,7 +85,7 @@ func main() {
break break
} }
render.DrawScene(gfxContext, player.Transform, level, colorMap) render.DrawScene(gfxContext, player.Transform, level)
render.DrawMiniMap(gfxContext, player.Transform, level) render.DrawMiniMap(gfxContext, player.Transform, level)
gfxContext.Sync() gfxContext.Sync()

View file

@ -6,14 +6,14 @@ import (
"github.com/pnx/go-raytracer/world" "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() { if wall_h < ctx.Height() {
y1 := (ctx.Height() - wall_h) / 2 y1 := (ctx.Height() - wall_h) / 2
y2 := (ctx.Height() + wall_h) / 2 y2 := (ctx.Height() + wall_h) / 2
// Top // Top
// renderer.SetDrawColor(90, 90, 0, 255) // 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 // // Middle
// renderer.SetDrawColor(color.R, color.G, color.B, color.A) // 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 // Bottom
if y2 < ctx.Height() { if y2 < ctx.Height() {
// renderer.SetDrawColor(50, 50, 50, 255) // 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 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) 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() { for x := range ctx.Width() {
result := CastRay(camera, level, int(x), int(ctx.Width())) result := CastRay(camera, level, int(x), int(ctx.Width()))
lineHeight := int(float64(ctx.Height()) / result.Distance) 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 { if result.Side > 0 {
color = color.Shade(2) color += 0x8
} }
DrawColumn(ctx, int32(x), int32(lineHeight), color) 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} // offset := math.Vec2i32{X: 25, Y: 25}
// size := math.Vec2i32{X: 200, Y: 100} // 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 y := range level.H {
for x := range level.W { for x := range level.W {
if level.Wall(x, y) { 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 // Camera Position
cPos := camera.Position.Get().Scale(scale) 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 // Rays
for x := range ctx.Width() { for x := range ctx.Width() {
hit := CastRay(camera, level, int(x), int(ctx.Width())) hit := CastRay(camera, level, int(x), int(ctx.Width()))
end := camera.Position.Get().AddVec(hit.Pos.Scale(world.TileSize)).Scale(scale) 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)
} }
} }