3dtrial/renderer2/main.go

324 lines
9.1 KiB
Go
Raw Normal View History

2024-08-02 19:35:30 +00:00
package main
import (
"flag"
"fmt"
2024-08-02 21:55:21 +00:00
"image"
2024-08-02 20:50:14 +00:00
"image/color"
2024-08-02 19:35:30 +00:00
"log"
"math"
"os"
2024-08-02 21:55:21 +00:00
"path/filepath"
2024-08-02 21:14:14 +00:00
"renderer2/hrend"
2024-08-02 19:35:30 +00:00
"runtime/pprof" // For performance profiling (unnecessary)
"time"
_ "image/jpeg"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
2024-08-03 02:53:21 +00:00
NearClip = 0.0001
FarClip = 10
2024-08-02 19:35:30 +00:00
Movement = 1.0
Rotation = 0.25
LookLock = math.Pi / 32
//ObjectFile = "../head.obj"
//TextureFile = "../head.jpg"
)
func must(err error) {
if err != nil {
panic(err)
}
}
2024-08-02 21:55:21 +00:00
type ObjectDef struct {
2024-08-02 22:59:18 +00:00
Model *hrend.ObjModel
Texture hrend.Framebuffer // This needs to go somewhere else eventually!
Pos hrend.Vec3f
LookVec hrend.Vec3f
Scale float32
Lighting bool
2024-08-02 21:55:21 +00:00
//Transform hrend.Mat44f
}
func NewObjectDef(model *hrend.ObjModel, texture hrend.Framebuffer) *ObjectDef {
result := ObjectDef{
2024-08-02 22:59:18 +00:00
Model: model,
Texture: texture,
LookVec: hrend.Vec3f{X: 0, Y: 0, Z: -1},
Scale: 1,
Lighting: true,
2024-08-02 21:55:21 +00:00
}
//result.Transform.SetIdentity()
return &result
}
func loadObject(name string) (*hrend.ObjModel, hrend.Framebuffer) {
ofile := filepath.Join("../", name+".obj")
tfile := filepath.Join("../", name+".jpg")
log.Printf("Loading obj %s, texture %s", ofile, tfile)
of, err := os.Open(ofile)
must(err)
defer of.Close()
o, err := hrend.ParseObj(of)
must(err)
jf, err := os.Open(tfile)
must(err)
defer jf.Close()
timg, _, err := image.Decode(jf)
must(err)
texture := hrend.NewTexture(timg, 4)
return o, texture
}
2024-08-02 19:35:30 +00:00
// func loadDefault() (*hrend.ObjModel, hrend.Framebuffer) {
// log.Printf("Loading obj %s, texture %s", ObjectFile, TextureFile)
//
// of, err := os.Open(ObjectFile)
// must(err)
// defer of.Close()
// o, err := hrend.ParseObj(of)
// must(err)
//
// jf, err := os.Open(TextureFile)
// must(err)
// defer jf.Close()
// timg, _, err := image.Decode(jf)
// must(err)
// texture := hrend.NewTexture(timg, 4)
//
// return o, texture
// }
// However flag works... idk
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var width = flag.Int("width", 640, "width of window or frame")
2024-08-02 21:14:14 +00:00
var height = flag.Int("height", 480, "height of window or frame")
2024-08-02 19:35:30 +00:00
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.")
2024-08-02 21:14:14 +00:00
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)")
2024-08-02 21:55:21 +00:00
var minlight = flag.Float64("minlight", 0.5, "Minimum light level")
2024-08-02 19:35:30 +00:00
2024-08-02 20:50:14 +00:00
// var renderconfig = flag.String("renderconfig", "", "if set, rendering is written out")
2024-08-02 19:35:30 +00:00
func IsRealtime() bool {
return *renderout == ""
}
// Do next inputs, whether they come from raylib or a file
func CameraInput(yaw, pitch float32) (float32, float32, hrend.Vec3f) {
2024-08-02 21:14:14 +00:00
Fps := float32(*fps)
2024-08-02 19:35:30 +00:00
mouse := rl.GetMouseDelta()
pitch += Rotation * mouse.Y / Fps
yaw += Rotation * mouse.X / Fps
pitch = hrend.Clamp(pitch, LookLock, math.Pi-LookLock)
newcamtrans := hrend.Vec3f{X: 0, Y: 0, Z: 0}
if rl.IsKeyDown(rl.KeyD) {
newcamtrans.X += Movement / Fps
}
if rl.IsKeyDown(rl.KeyA) {
newcamtrans.X -= Movement / Fps
}
// Moving forward moves in the negative z direction, since we FACE
// the -z axis (the camera does anyway)
if rl.IsKeyDown(rl.KeyW) {
newcamtrans.Z -= Movement / Fps
}
if rl.IsKeyDown(rl.KeyS) {
newcamtrans.Z += Movement / Fps
}
if rl.IsKeyDown(rl.KeySpace) {
newcamtrans.Y += Movement / Fps
}
if rl.IsKeyDown(rl.KeyLeftShift) {
newcamtrans.Y -= Movement / Fps
}
// translate the new camera movement based on the yaw
var moverot hrend.Mat44f
moverot.SetRotationY(-yaw)
newcamtrans = moverot.MultiplyPoint3(newcamtrans)
2024-08-02 21:14:14 +00:00
return yaw, pitch, newcamtrans
2024-08-02 19:35:30 +00:00
}
func main() {
log.Printf("Program start")
flag.Parse()
if *cpuprofile != "" {
log.Printf("CPU profiling requested, write to %s", *cpuprofile)
f, err := os.Create(*cpuprofile)
must(err)
defer f.Close()
err = pprof.StartCPUProfile(f)
must(err)
defer pprof.StopCPUProfile()
}
Width := uint(*width)
Height := uint(*height)
2024-08-02 20:50:14 +00:00
var timer hrend.FrameTimer
2024-08-02 19:35:30 +00:00
var fb hrend.Framebuffer
2024-08-02 20:50:14 +00:00
var drawFunc func()
2024-08-02 19:35:30 +00:00
if IsRealtime() {
rl.InitWindow(int32(Width), int32(Height), "Simple renderer with raylib")
defer rl.CloseWindow()
2024-08-02 21:14:14 +00:00
rl.SetTargetFPS(int32(*fps))
2024-08-02 19:35:30 +00:00
rl.DisableCursor()
rfb := NewRaylibBuffer(Width, Height)
defer rl.UnloadTexture(rfb.Texture)
defer rl.UnloadImageColors(rfb.Data)
defer rl.UnloadImage(rfb.Image)
fb = rfb
2024-08-02 20:50:14 +00:00
drawFunc = func() {
rl.UpdateTexture(rfb.Texture, rfb.Data)
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(rfb.Texture, 0, 0, rl.White)
rl.DrawText(fmt.Sprintf("Frame: %.2fms", timer.LastAverage.Seconds()*1000), 5, 5, 20, rl.Red)
rl.EndDrawing()
}
} else {
2024-08-02 19:35:30 +00:00
}
rb := hrend.NewRenderbuffer(fb, Width, Height)
2024-08-02 20:50:14 +00:00
// Generate world
2024-08-02 21:14:14 +00:00
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)
2024-08-02 20:50:14 +00:00
wtex := hrend.NewTexture(wtexraw, 1)
2024-08-03 02:53:21 +00:00
world := FlatTerrain(1)
2024-08-02 19:35:30 +00:00
2024-08-02 22:59:18 +00:00
// Generate skybox
2024-08-03 02:53:21 +00:00
// skyraw := Gradient1px(color.RGBA{R: 100, G: 100, B: 255, A: 255}, color.RGBA{R: 255, G: 255, B: 255, A: 255}, 32)
// skytex := hrend.NewTexture(skyraw, 1)
// sky := Skybox()
2024-08-02 22:59:18 +00:00
2024-08-02 21:55:21 +00:00
// Some static models we could put in the scene
modnames := []string{"head", "diablo"}
models := make([]*hrend.ObjModel, len(modnames))
textures := make([]hrend.Framebuffer, len(modnames))
for i, name := range modnames {
models[i], textures[i] = loadObject(name)
}
// And the actual objects for the scene. We also put the world in there
objects := make([]*ObjectDef, 0)
objects = append(objects, NewObjectDef(world, wtex))
2024-08-03 02:53:21 +00:00
// objects = append(objects, NewObjectDef(sky, skytex)) // the actual skybox
// skyobj := objects[len(objects)-1]
// skyobj.Scale = 50
// skyobj.Lighting = false
// objects = append(objects, NewObjectDef(models[1], textures[1]))
// objects[len(objects)-1].Pos.Y += 1
// objects[len(objects)-1].Pos.Z -= 2
2024-08-02 21:55:21 +00:00
2024-08-02 19:35:30 +00:00
// These don't really change
2024-08-02 20:50:14 +00:00
var projection, viewport hrend.Mat44f
2024-08-02 21:14:14 +00:00
projection.SetProjection(float32(*fov), float32(Width)/float32(Height), NearClip, FarClip)
2024-08-02 19:35:30 +00:00
viewport.SetViewportSimple(int(Width), int(Height), 1) //65535)
2024-08-02 20:50:14 +00:00
var camera hrend.Mat44f
2024-08-02 19:35:30 +00:00
var newcamtrans hrend.Vec3f
2024-08-02 21:14:14 +00:00
camtrans := hrend.Vec3f{X: float32(*xofs), Y: float32(*yofs), Z: float32(*zofs)}
2024-08-02 19:35:30 +00:00
camup := hrend.Vec3f{X: 0, Y: 1, Z: 0}
2024-08-02 21:55:21 +00:00
lightang := -math.Pi / 4 // Angle offset from "straight down"
light := hrend.Vec3f{X: 0, Y: float32(-math.Cos(lightang)), Z: float32(math.Sin(lightang))}
2024-08-02 19:35:30 +00:00
// In our system, 0 degree yaw is facing -Z, into the scene
yaw := float32(0)
pitch := float32(math.Pi / 2) // Start looking flat
log.Printf("Starting render loop")
for !rl.WindowShouldClose() {
start := time.Now()
yaw, pitch, newcamtrans = CameraInput(yaw, pitch)
camtrans = *camtrans.Add(&newcamtrans)
2024-08-02 20:50:14 +00:00
_ = camera.SetCamera(&camtrans, yaw, pitch, &camup)
2024-08-02 19:35:30 +00:00
screenmat := camera.Inverse().Multiply(&projection)
screenmat = screenmat.Multiply(&viewport)
rb.ResetZBuffer()
for y := range Height {
for x := range Width {
fb.Set(x, y, 0, 0, 0)
}
}
2024-08-03 02:53:21 +00:00
//var osc [3]hrend.Vec3f
//var om3d hrend.Mat44f
2024-08-02 19:35:30 +00:00
var sc [3]hrend.Vertex
2024-08-02 21:55:21 +00:00
var modelmat hrend.Mat44f
2024-08-02 22:59:18 +00:00
var intensity float32
2024-08-03 02:53:21 +00:00
var minz = float32(math.MaxFloat32)
var maxz = float32(-math.MaxFloat32)
2024-08-02 21:55:21 +00:00
for _, o := range objects {
// Create the final matrix
modelmat.SetLookAt(&o.Pos, o.Pos.Add(&o.LookVec), &camup)
2024-08-02 22:59:18 +00:00
modelmat.ScaleSelf(o.Scale)
2024-08-02 21:55:21 +00:00
matrix3d := modelmat.Multiply(screenmat)
//matrix3d.ScaleSelf(o.Scale)
for _, f := range o.Model.Faces {
for i := range 3 {
sc[i] = f[i]
sc[i].Pos = matrix3d.MultiplyPoint3(f[i].Pos)
2024-08-03 02:53:21 +00:00
minz = min(minz, sc[i].Pos.Z)
maxz = max(maxz, sc[i].Pos.Z)
2024-08-02 21:55:21 +00:00
}
2024-08-03 02:53:21 +00:00
// for i := range 3 {
// if math.Signbit(float64(sc[i].Pos.X)) != math.Signbit(float64(osc[i].X)) ||
// math.Signbit(float64(sc[i].Pos.Y)) != math.Signbit(float64(osc[i].Y)) {
// log.Print(sc[0].Pos, sc[1].Pos, sc[2].Pos)
// log.Print(matrix3d)
// break
// }
// }
log.Print(o.Model.Faces[0][0].Pos, o.Model.Faces[0][1].Pos, o.Model.Faces[0][2].Pos)
log.Print(sc[0].Pos, sc[1].Pos, sc[2].Pos)
log.Print(matrix3d)
// osc[0] = sc[0].Pos
// osc[1] = sc[1].Pos
// osc[2] = sc[2].Pos
//om3d = *matrix3d
//log.Print(sc[0].Pos, sc[1].Pos, sc[2].Pos, matrix3d)
2024-08-02 22:59:18 +00:00
if o.Lighting {
l1 := f[2].Pos.Sub(&f[0].Pos)
n := l1.CrossProduct(f[1].Pos.Sub(&f[0].Pos))
n = n.Normalize()
// light = lookvec // use this for weird things
intensity = n.MultSimp(&light)
if intensity < 0 {
intensity = 0
}
intensity = (intensity + float32(*minlight)) / (1 + float32(*minlight))
} else {
intensity = 1.0
2024-08-02 21:55:21 +00:00
}
hrend.TriangleTextured(&rb, o.Texture, intensity, sc[0], sc[1], sc[2])
2024-08-03 02:53:21 +00:00
break
2024-08-02 21:55:21 +00:00
//hrend.TriangleFlat(&rb, hrend.Col2Uint(byte(255*intensity), byte(255*intensity), byte(255*intensity)), sc[0].Pos, sc[1].Pos, sc[2].Pos)
2024-08-02 19:35:30 +00:00
}
}
2024-08-03 02:53:21 +00:00
//log.Print(minz, maxz)
2024-08-02 19:35:30 +00:00
timer.Add(time.Since(start), 10)
2024-08-02 20:50:14 +00:00
drawFunc()
2024-08-02 19:35:30 +00:00
}
}