1
0
Fork 0
go-raytracer/render/engine.go

47 lines
1.2 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 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, 0x1D)
// // 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(), 0x19)
}
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) {
for x := range ctx.Width() {
result := CastRay(camera, level, int(x), int(ctx.Width()))
lineHeight := int(float64(ctx.Height()) / result.Distance)
color := level.Cell(int(result.Cell.X), int(result.Cell.Y))
if result.Side > 0 {
color += 0x8
}
DrawColumn(ctx, int32(x), int32(lineHeight), color)
}
}