1
0
Fork 0
go-raytracer/render/engine.go
2025-06-08 15:51:07 +02:00

47 lines
1.3 KiB
Go

package render
import (
"github.com/pnx/go-raytracer/graphics"
"github.com/pnx/go-raytracer/math"
"github.com/pnx/go-raytracer/world"
)
func DrawColumn(ctx *graphics.Context, x int32, wall_h int32, color math.Color) {
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})
// // Middle
// renderer.SetDrawColor(color.R, color.G, color.B, color.A)
ctx.DrawLine(x, y1, int32(x), y2, 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})
}
return
}
// ctx.SetDrawColor(color.R, color.G, color.B, color.A)
ctx.DrawLine(x, 0, int32(x), ctx.Height(), color)
}
func DrawScene(ctx *graphics.Context, camera math.Transform, level *world.Level, colorMap []math.Color) {
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]
if result.Side > 0 {
color = color.Shade(2)
}
DrawColumn(ctx, int32(x), int32(lineHeight), color)
}
}