Finished homework again

This commit is contained in:
Carlos Sanchez 2024-07-28 20:59:38 -04:00
parent d69354ea09
commit ecc9011b48
4 changed files with 42 additions and 47 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"image/color" "image/color"
//"log" //"log"
"image"
"math" "math"
"strings" "strings"
) )
@ -43,6 +44,24 @@ func NewFramebuffer(width uint, height uint) Framebuffer {
} }
} }
func NewTexture(texture image.Image, skip int) Framebuffer {
bounds := texture.Bounds()
width := bounds.Dx() / skip
height := bounds.Dy() / skip
result := Framebuffer{
Data: make([]uint, width*height),
Width: uint(width),
Height: uint(height),
}
for y := bounds.Min.Y; y < bounds.Max.Y; y += skip {
for x := bounds.Min.X; x < bounds.Max.X; x += skip {
col := texture.At(x, y)
result.Set(uint(x/skip), uint(y/skip), Color2Uint(col))
}
}
return result
}
// Fill zbuffer with pixels that are max distance away // Fill zbuffer with pixels that are max distance away
func (fb *Framebuffer) ResetZBuffer() { func (fb *Framebuffer) ResetZBuffer() {
for i := range fb.ZBuffer { for i := range fb.ZBuffer {
@ -50,6 +69,12 @@ func (fb *Framebuffer) ResetZBuffer() {
} }
} }
func (fb *Framebuffer) GetUv(u float32, v float32) uint {
x := uint(float32(fb.Width) * u)
y := uint(float32(fb.Height) * (1 - v))
return fb.Data[x+y*fb.Width]
}
// Sure hope this gets inlined... // Sure hope this gets inlined...
func (fb *Framebuffer) Set(x uint, y uint, color uint) { func (fb *Framebuffer) Set(x uint, y uint, color uint) {
fb.Data[x+y*fb.Width] = color fb.Data[x+y*fb.Width] = color

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"image" "image"
"log" "log"
"math" //"math"
//"math/rand" //"math/rand"
"os" "os"
"runtime/pprof" // For performance profiling (unnecessary) "runtime/pprof" // For performance profiling (unnecessary)
@ -18,7 +18,7 @@ const (
Height = 512 Height = 512
ObjectFile = "head.obj" ObjectFile = "head.obj"
TextureFile = "head.jpg" TextureFile = "head.jpg"
Repeat = 60 Repeat = 500
) )
func must(err error) { func must(err error) {
@ -30,10 +30,11 @@ 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 dozbuf = flag.Bool("zbuffer", false, "Write zbuffer instead of image") var dozbuf = flag.Bool("zbuffer", false, "Write zbuffer instead of image")
var zcuthigh = flag.Float64("zcuthigh", math.MaxFloat32, "High cutoff for z (values above this will be removed)")
var zcutlow = flag.Float64("zcutlow", -math.MaxFloat32, "Low cutoff for z (values below are removed)")
var p6file = flag.String("p6file", "", "Output binary ppm to given file instead") var p6file = flag.String("p6file", "", "Output binary ppm to given file instead")
// var zcuthigh = flag.Float64("zcuthigh", math.MaxFloat32, "High cutoff for z (values above this will be removed)")
// var zcutlow = flag.Float64("zcutlow", -math.MaxFloat32, "Low cutoff for z (values below are removed)")
func main() { func main() {
log.Printf("Program start") log.Printf("Program start")
@ -62,25 +63,18 @@ func main() {
jf, err := os.Open(TextureFile) jf, err := os.Open(TextureFile)
must(err) must(err)
defer jf.Close() defer jf.Close()
texture, _, err := image.Decode(jf) timg, _, err := image.Decode(jf)
must(err) must(err)
texture := NewTexture(timg, 4)
log.Printf("Running render") log.Printf("Running render")
light := Vec3f{0, 0, -1} light := Vec3f{0, 0, -1}
// for range Repeat {
// Triangle2(&fb, 0xFF0000, Vec2i{10, 70}, Vec2i{50, 160}, Vec2i{70, 80})
// Triangle2(&fb, 0xFFFFFF, Vec2i{180, 50}, Vec2i{150, 1}, Vec2i{70, 180})
// Triangle2(&fb, 0x00FF00, Vec2i{180, 150}, Vec2i{120, 160}, Vec2i{130, 180})
// }
halfwidth := float32(fb.Width / 2) halfwidth := float32(fb.Width / 2)
halfheight := float32(fb.Height / 2) halfheight := float32(fb.Height / 2)
var sc [3]Vertex var sc [3]Vertex
var hi = float32(fb.Height - 1) var hi = float32(fb.Height - 1)
minz := float32(math.MaxFloat32)
maxz := float32(-math.MaxFloat32)
for range Repeat { for range Repeat {
fb.ResetZBuffer() fb.ResetZBuffer()
for _, f := range o.Faces { for _, f := range o.Faces {
@ -93,25 +87,14 @@ func main() {
// NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER // NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER
// POINTS HAVE HIGHER Z VLAUES // POINTS HAVE HIGHER Z VLAUES
sc[i].Pos.Z = -f[i].Pos.Z // Pull Z value directly. This is fine, our z-buffer is currently float32 sc[i].Pos.Z = -f[i].Pos.Z // Pull Z value directly. This is fine, our z-buffer is currently float32
minz = min(minz, sc[i].Pos.Z)
maxz = max(maxz, sc[i].Pos.Z)
} }
// TESTING
zch := float32(*zcuthigh)
zcl := float32(*zcutlow)
if -sc[0].Pos.Z > zch || -sc[1].Pos.Z > zch || -sc[2].Pos.Z > zch ||
-sc[0].Pos.Z < zcl || -sc[1].Pos.Z < zcl || -sc[2].Pos.Z < zcl {
continue
}
// To test something, we swap vertex 2 and 3
//sc[1], sc[2] = sc[2], sc[1]
l1 := f[2].Pos.Sub(f[0].Pos) l1 := f[2].Pos.Sub(f[0].Pos)
n := l1.CrossProduct(f[1].Pos.Sub(f[0].Pos)) n := l1.CrossProduct(f[1].Pos.Sub(f[0].Pos))
n = n.Normalize() n = n.Normalize()
intensity := n.MultSimp(&light) intensity := n.MultSimp(&light)
if intensity > 0 { if intensity > 0 {
Triangle3t(&fb, texture, intensity, sc[0], sc[1], sc[2]) Triangle3t(&fb, &texture, intensity, sc[0], sc[1], sc[2])
//Triangle3(&fb, uint(rand.Int()), sc[0], sc[1], sc[2]) //Triangle3(&fb, uint(rand.Int()), sc[0], sc[1], sc[2])
//Triangle1(&fb, uint(rand.Int()), sc[0].ToVec2i(), sc[1].ToVec2i(), sc[2].ToVec2i()) //Triangle1(&fb, uint(rand.Int()), sc[0].ToVec2i(), sc[1].ToVec2i(), sc[2].ToVec2i())
//Triangle2(&fb, 0xFFFFFF, sc[0], sc[1], sc[2]) //Triangle2(&fb, 0xFFFFFF, sc[0], sc[1], sc[2])
@ -119,10 +102,6 @@ func main() {
} }
} }
log.Print("Min/max z: ", minz, maxz)
//log.Print(fb.ZBuffer)
if *dozbuf { if *dozbuf {
log.Printf("Exporting zbuffer ppm to stdout") log.Printf("Exporting zbuffer ppm to stdout")
fmt.Print(fb.ZBuffer_ExportPPM()) fmt.Print(fb.ZBuffer_ExportPPM())

View File

@ -2,7 +2,6 @@ package main
import ( import (
//"log" //"log"
"image"
"math" "math"
) )
@ -271,7 +270,7 @@ func Triangle3(fb *Framebuffer, color uint, v0f Vec3f, v1f Vec3f, v2f Vec3f) {
} }
} }
func Triangle3t(fb *Framebuffer, texture image.Image, intensity float32, v0v Vertex, v1v Vertex, v2v Vertex) { func Triangle3t(fb *Framebuffer, texture *Framebuffer, intensity float32, v0v Vertex, v1v Vertex, v2v Vertex) {
v0 := v0v.Pos.ToVec2i() v0 := v0v.Pos.ToVec2i()
v1 := v1v.Pos.ToVec2i() v1 := v1v.Pos.ToVec2i()
v2 := v2v.Pos.ToVec2i() v2 := v2v.Pos.ToVec2i()
@ -304,32 +303,24 @@ func Triangle3t(fb *Framebuffer, texture image.Image, intensity float32, v0v Ver
w1_xi, w1_yi := EdgeIncrementi(v2, v0) w1_xi, w1_yi := EdgeIncrementi(v2, v0)
w2_xi, w2_yi := EdgeIncrementi(v0, v1) w2_xi, w2_yi := EdgeIncrementi(v0, v1)
bounds := texture.Bounds()
tx := bounds.Min.X
ty := bounds.Min.Y
tw := bounds.Dx()
th := bounds.Dy()
for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ { for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ {
w0 := w0_y w0 := w0_y
w1 := w1_y w1 := w1_y
w2 := w2_y w2 := w2_y
for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ { for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ {
if (w0 | w1 | w2) >= 0 { if (w0 | w1 | w2) >= 0 {
//fb.Data[di] = color
//done = true
w0a := float32(w0) * invarea w0a := float32(w0) * invarea
w1a := float32(w1) * invarea w1a := float32(w1) * invarea
w2a := float32(w2) * invarea w2a := float32(w2) * invarea
pz := w0a*v0v.Pos.Z + w1a*v1v.Pos.Z + w2a*v2v.Pos.Z pz := w0a*v0v.Pos.Z + w1a*v1v.Pos.Z + w2a*v2v.Pos.Z
if pz < fb.ZBuffer[x+y*fb.Width] { if pz < fb.ZBuffer[x+y*fb.Width] {
//log.Print(pz)
fb.ZBuffer[x+y*fb.Width] = pz fb.ZBuffer[x+y*fb.Width] = pz
txo := int(float32(tw) * (w0a*v0v.Tex.X + w1a*v1v.Tex.X + w2a*v2v.Tex.X)) col := texture.GetUv(
tyo := int(float32(th) * (1 - (w0a*v0v.Tex.Y + w1a*v1v.Tex.Y + w2a*v2v.Tex.Y))) (w0a*v0v.Tex.X + w1a*v1v.Tex.X + w2a*v2v.Tex.X),
col := texture.At(tx+txo, ty+tyo) (w0a*v0v.Tex.Y + w1a*v1v.Tex.Y + w2a*v2v.Tex.Y),
//c := texture.At() )
fb.Set(x, y, Color2Uint(col)) //uint(texture.Bounds().Dx()) r, g, b := Uint2Col(col)
fb.Set(x, y, Col2Uint(byte(float32(r)*intensity), byte(float32(g)*intensity), byte(float32(b)*intensity))) //uint(texture.Bounds().Dx())
//0xF) // fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a))) //0xF) // fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a)))
} }
// fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a))) // fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a)))

View File

@ -10,5 +10,5 @@ fi
echo "Building" echo "Building"
go build -o render go build -o render
echo "Running" echo "Running"
./render "-cpuprofile=$1.prof" >"$1.ppm" ./render "-cpuprofile=$1.prof" "-p6file=$1.ppm"
./render "-zbuffer" >"$1_zbuffer.ppm" # ./render "-zbuffer" >"$1_zbuffer.ppm"