3dtrial/renderer1/main.go

214 lines
5.2 KiB
Go
Raw Normal View History

2024-07-30 23:14:28 +00:00
package main
import (
2024-07-30 23:47:22 +00:00
"flag"
2024-07-31 05:02:05 +00:00
"fmt"
"image"
2024-07-30 23:47:22 +00:00
"log"
2024-08-02 04:24:16 +00:00
"math"
2024-07-30 23:47:22 +00:00
"os"
"renderer1/hrend"
"runtime/pprof" // For performance profiling (unnecessary)
2024-07-31 05:02:05 +00:00
"time"
2024-07-30 23:47:22 +00:00
_ "image/jpeg"
2024-07-30 23:14:28 +00:00
rl "github.com/gen2brain/raylib-go/raylib"
)
2024-07-30 23:47:22 +00:00
const (
Width = 640
Height = 480
NearClip = 0.1
FarClip = 100
FOV = 90.0
2024-08-02 03:11:50 +00:00
ZOffset = 1.5
2024-07-31 05:02:05 +00:00
Movement = 1.0
2024-08-02 04:24:16 +00:00
Rotation = 0.25
LookLock = math.Pi / 32
2024-07-31 05:02:05 +00:00
Fps = 60
ObjectFile = "../head.obj"
2024-07-30 23:47:22 +00:00
TextureFile = "../head.jpg"
)
func must(err error) {
if err != nil {
panic(err)
}
}
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
}
2024-07-30 23:47:22 +00:00
// However flag works... idk
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
// var dozbuf = flag.Bool("zbuffer", false, "Write zbuffer instead of image")
// var p6file = flag.String("p6file", "", "Output binary ppm to given file instead")
// var fov = flag.Float64("fov", 90, "Horizontal FOV in degrees")
// var xofs = flag.Float64("xofs", 0, "Offset image by x")
// var zofs = flag.Float64("zofs", -1.5, "Offset image by z (should be negative)")
// var repeat = flag.Int("repeat", 60, "Amount of times to repeat render")
2024-07-30 23:14:28 +00:00
func main() {
2024-07-30 23:47:22 +00:00
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()
}
2024-07-31 05:02:05 +00:00
rl.InitWindow(Width, Height, "Simple renderer with raylib")
defer rl.CloseWindow()
rl.SetTargetFPS(Fps)
2024-08-02 04:24:16 +00:00
rl.DisableCursor()
2024-07-31 05:02:05 +00:00
fb := NewRaylibBuffer(Width, Height)
2024-07-31 05:02:05 +00:00
defer rl.UnloadTexture(fb.Texture)
defer rl.UnloadImageColors(fb.Data)
defer rl.UnloadImage(fb.Image)
rb := hrend.NewRenderbuffer(fb, Width, Height)
o, texture := loadDefault()
2024-07-31 05:02:05 +00:00
var thing hrend.Vec2i
log.Print(thing)
2024-08-02 03:11:50 +00:00
var projection, viewport hrend.Mat44f
2024-07-31 05:02:05 +00:00
// These don't really change
projection.SetProjection(float32(FOV), float32(Width)/float32(Height), NearClip, FarClip)
viewport.SetViewportSimple(int(fb.Width), int(fb.Height), 1) //65535)
2024-07-30 23:14:28 +00:00
2024-07-31 05:02:05 +00:00
log.Printf("Starting raylib loop")
2024-07-30 23:14:28 +00:00
2024-07-31 05:02:05 +00:00
frameSum := 0.0
frameCount := 0
frameAverage := 0.0
2024-08-02 03:11:50 +00:00
camtrans := hrend.Vec3f{X: 0, Y: 0, Z: ZOffset}
camup := hrend.Vec3f{X: 0, Y: 1, Z: 0}
2024-08-02 04:24:16 +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
2024-08-02 03:11:50 +00:00
2024-08-02 04:56:29 +00:00
var camera, moverot hrend.Mat44f
2024-07-30 23:47:22 +00:00
2024-07-30 23:14:28 +00:00
for !rl.WindowShouldClose() {
2024-07-31 05:02:05 +00:00
start := time.Now()
2024-08-02 04:24:16 +00:00
mouse := rl.GetMouseDelta()
pitch += Rotation * mouse.Y / Fps
yaw += Rotation * mouse.X / Fps
// Need a clamp function or something
if pitch < LookLock {
pitch = LookLock
} else if pitch > math.Pi-LookLock {
pitch = math.Pi - LookLock
}
newcamtrans := hrend.Vec3f{X: 0, Y: 0, Z: 0}
2024-07-31 05:02:05 +00:00
if rl.IsKeyDown(rl.KeyD) {
2024-08-02 04:24:16 +00:00
newcamtrans.X += Movement / Fps
2024-07-31 05:02:05 +00:00
}
if rl.IsKeyDown(rl.KeyA) {
2024-08-02 04:24:16 +00:00
newcamtrans.X -= Movement / Fps
2024-07-31 05:02:05 +00:00
}
2024-08-02 03:11:50 +00:00
// Moving forward moves in the negative z direction, since we FACE
2024-08-02 04:56:29 +00:00
// the -z axis (the camera does anyway)
2024-07-31 05:02:05 +00:00
if rl.IsKeyDown(rl.KeyW) {
2024-08-02 04:24:16 +00:00
newcamtrans.Z -= Movement / Fps
2024-07-31 05:02:05 +00:00
}
if rl.IsKeyDown(rl.KeyS) {
2024-08-02 04:24:16 +00:00
newcamtrans.Z += Movement / Fps
2024-07-31 05:02:05 +00:00
}
if rl.IsKeyDown(rl.KeySpace) {
2024-08-02 04:24:16 +00:00
newcamtrans.Y += Movement / Fps
2024-07-31 05:02:05 +00:00
}
if rl.IsKeyDown(rl.KeyLeftShift) {
2024-08-02 04:24:16 +00:00
newcamtrans.Y -= Movement / Fps
2024-07-31 05:02:05 +00:00
}
2024-08-02 04:56:29 +00:00
// translate the new camera movement based on the yaw
moverot.SetRotationY(-yaw)
newcamtrans = moverot.MultiplyPoint3(newcamtrans)
2024-08-02 04:24:16 +00:00
camtrans = *camtrans.Add(&newcamtrans)
2024-08-02 03:11:50 +00:00
2024-08-02 04:56:29 +00:00
// The camera is really just a model
lookvec := camera.SetModel(&camtrans, yaw, pitch, 1.0, &camup)
2024-07-30 23:14:28 +00:00
2024-08-02 03:11:50 +00:00
screenmat := camera.Inverse().Multiply(&projection)
screenmat = screenmat.Multiply(&viewport)
2024-07-31 05:02:05 +00:00
for i := 0; i < Width*Height; i++ {
fb.Data[i].R = 0
fb.Data[i].G = 0
fb.Data[i].B = 0
}
var sc [3]hrend.Vertex
rb.ResetZBuffer()
for _, f := range o.Faces {
// Precompute perspective for vertices to save time. Notice Z
// is not considered: is this orthographic projection? Yeah probably...
for i := range 3 { // Triangles, bro
sc[i] = f[i]
sc[i].Pos = screenmat.MultiplyPoint3(f[i].Pos)
}
2024-08-02 04:31:33 +00:00
l1 := f[2].Pos.Sub(&f[0].Pos)
n := l1.CrossProduct(f[1].Pos.Sub(&f[0].Pos))
n = n.Normalize()
intensity := n.MultSimp(&lookvec)
if intensity < 0 {
intensity = 0
}
2024-08-02 04:31:33 +00:00
hrend.TriangleTextured(&rb, texture, intensity, sc[0], sc[1], sc[2])
//hrend.TriangleFlat(&rb, hrend.Col2Uint(byte(255*intensity), byte(255*intensity), byte(255*intensity)), sc[0].Pos, sc[1].Pos, sc[2].Pos)
}
2024-07-31 05:02:05 +00:00
rl.UpdateTexture(fb.Texture, fb.Data)
2024-07-30 23:14:28 +00:00
2024-07-31 05:02:05 +00:00
frameSum += time.Since(start).Seconds()
frameCount += 1
if frameCount&0x7 == 0 {
frameAverage = frameSum / float64(frameCount)
frameSum = 0
frameCount = 0
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(fb.Texture, 0, 0, rl.White)
rl.DrawText(fmt.Sprintf("Frame: %fms", frameAverage*1000), 5, 5, 20, rl.Red)
2024-07-30 23:14:28 +00:00
rl.EndDrawing()
}
}