Some kind of world gen...

This commit is contained in:
Carlos Sanchez 2024-08-02 17:14:14 -04:00
parent 4ab6b365ee
commit 699b35ce89
3 changed files with 22 additions and 19 deletions

View File

@ -3,7 +3,7 @@ package main
import ( import (
"image" "image"
"image/color" "image/color"
"renderer1/hrend" "renderer2/hrend"
) )
func Checkerboard(cols []color.Color, size int) image.Image { func Checkerboard(cols []color.Color, size int) image.Image {
@ -33,13 +33,14 @@ func FlatTerrain(size int) *hrend.ObjModel {
result.Vertices = append(result.Vertices, hrend.Vec3f{X: float32(x), Y: 0, Z: float32(z)}) result.Vertices = append(result.Vertices, hrend.Vec3f{X: float32(x), Y: 0, Z: float32(z)})
} }
} }
width := size + size + 1
// Faces are slightly different; we generate two for every "cell" inside the vertices // Faces are slightly different; we generate two for every "cell" inside the vertices
for z := 0; z < size*2; z++ { for z := 0; z < width-1; z++ {
for x := 0; x < size*2; x++ { for x := 0; x < width-1; x++ {
topleft := x + z*size*2 topleft := x + z*width
topright := x + 1 + z*size*2 topright := x + 1 + z*width
bottomleft := x + (z+1)*size*2 bottomleft := x + (z+1)*width
bottomright := x + 1 + (z+1)*size*2 bottomright := x + 1 + (z+1)*width
// remember to wind counter-clockwise // remember to wind counter-clockwise
result.Faces = append(result.Faces, hrend.Facef{ result.Faces = append(result.Faces, hrend.Facef{
hrend.Vertex{Pos: result.Vertices[topleft], Tex: result.VTexture[0]}, hrend.Vertex{Pos: result.Vertices[topleft], Tex: result.VTexture[0]},

View File

@ -1,4 +1,4 @@
module renderer1 module renderer2
go 1.22.5 go 1.22.5

View File

@ -8,7 +8,7 @@ import (
"log" "log"
"math" "math"
"os" "os"
"renderer1/hrend" "renderer2/hrend"
"runtime/pprof" // For performance profiling (unnecessary) "runtime/pprof" // For performance profiling (unnecessary)
"time" "time"
@ -20,13 +20,9 @@ import (
const ( const (
NearClip = 0.01 NearClip = 0.01
FarClip = 100 FarClip = 100
FOV = 90.0
ZOffset = 1.5
YOffset = 0.5
Movement = 1.0 Movement = 1.0
Rotation = 0.25 Rotation = 0.25
LookLock = math.Pi / 32 LookLock = math.Pi / 32
Fps = 60
//ObjectFile = "../head.obj" //ObjectFile = "../head.obj"
//TextureFile = "../head.jpg" //TextureFile = "../head.jpg"
) )
@ -59,9 +55,14 @@ func must(err error) {
// However flag works... idk // However flag works... idk
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var width = flag.Int("width", 640, "width of window or frame") var width = flag.Int("width", 640, "width of window or frame")
var height = flag.Int("width", 480, "height of window or frame") var height = flag.Int("height", 480, "height of window or frame")
var renderout = flag.String("renderout", "", "If set, rendering is done to a file instead of realtime") var renderout = flag.String("renderout", "", "If set, rendering is done to a file instead of realtime")
var renderinput = flag.String("renderinput", "", "If not realtime, the inputs are taken from here.") var renderinput = flag.String("renderinput", "", "If not realtime, the inputs are taken from here.")
var xofs = flag.Float64("xofs", 0, "starting x-offset")
var zofs = flag.Float64("zofs", 0, "starting z-offset")
var yofs = flag.Float64("yofs", 0.5, "starting y-offset")
var fov = flag.Float64("fov", 90, "the horizontal fov")
var fps = flag.Int("fps", 60, "fps to run (realtime only)")
// var renderconfig = flag.String("renderconfig", "", "if set, rendering is written out") // var renderconfig = flag.String("renderconfig", "", "if set, rendering is written out")
@ -72,6 +73,7 @@ func IsRealtime() bool {
// Do next inputs, whether they come from raylib or a file // Do next inputs, whether they come from raylib or a file
func CameraInput(yaw, pitch float32) (float32, float32, hrend.Vec3f) { func CameraInput(yaw, pitch float32) (float32, float32, hrend.Vec3f) {
Fps := float32(*fps)
mouse := rl.GetMouseDelta() mouse := rl.GetMouseDelta()
pitch += Rotation * mouse.Y / Fps pitch += Rotation * mouse.Y / Fps
yaw += Rotation * mouse.X / Fps yaw += Rotation * mouse.X / Fps
@ -104,7 +106,7 @@ func CameraInput(yaw, pitch float32) (float32, float32, hrend.Vec3f) {
moverot.SetRotationY(-yaw) moverot.SetRotationY(-yaw)
newcamtrans = moverot.MultiplyPoint3(newcamtrans) newcamtrans = moverot.MultiplyPoint3(newcamtrans)
return pitch, yaw, newcamtrans return yaw, pitch, newcamtrans
} }
func main() { func main() {
@ -131,7 +133,7 @@ func main() {
if IsRealtime() { if IsRealtime() {
rl.InitWindow(int32(Width), int32(Height), "Simple renderer with raylib") rl.InitWindow(int32(Width), int32(Height), "Simple renderer with raylib")
defer rl.CloseWindow() defer rl.CloseWindow()
rl.SetTargetFPS(Fps) rl.SetTargetFPS(int32(*fps))
rl.DisableCursor() rl.DisableCursor()
rfb := NewRaylibBuffer(Width, Height) rfb := NewRaylibBuffer(Width, Height)
defer rl.UnloadTexture(rfb.Texture) defer rl.UnloadTexture(rfb.Texture)
@ -152,18 +154,18 @@ func main() {
rb := hrend.NewRenderbuffer(fb, Width, Height) rb := hrend.NewRenderbuffer(fb, Width, Height)
// Generate world // Generate world
wtexraw := Checkerboard([]color.Color{color.RGBA{R: 255, G: 0, B: 0, A: 255}, color.RGBA{R: 0, G: 0, B: 255, A: 255}}, 32) wtexraw := Checkerboard([]color.Color{color.RGBA{R: 0, G: 255, B: 0, A: 255}, color.RGBA{R: 50, G: 150, B: 0, A: 255}}, 32)
wtex := hrend.NewTexture(wtexraw, 1) wtex := hrend.NewTexture(wtexraw, 1)
world := FlatTerrain(10) world := FlatTerrain(10)
// These don't really change // These don't really change
var projection, viewport hrend.Mat44f var projection, viewport hrend.Mat44f
projection.SetProjection(float32(FOV), float32(Width)/float32(Height), NearClip, FarClip) projection.SetProjection(float32(*fov), float32(Width)/float32(Height), NearClip, FarClip)
viewport.SetViewportSimple(int(Width), int(Height), 1) //65535) viewport.SetViewportSimple(int(Width), int(Height), 1) //65535)
var camera hrend.Mat44f var camera hrend.Mat44f
var newcamtrans hrend.Vec3f var newcamtrans hrend.Vec3f
camtrans := hrend.Vec3f{X: 0, Y: YOffset, Z: ZOffset} camtrans := hrend.Vec3f{X: float32(*xofs), Y: float32(*yofs), Z: float32(*zofs)}
camup := hrend.Vec3f{X: 0, Y: 1, Z: 0} camup := hrend.Vec3f{X: 0, Y: 1, Z: 0}
// In our system, 0 degree yaw is facing -Z, into the scene // In our system, 0 degree yaw is facing -Z, into the scene