Having a lot of difficulty with facing normal
This commit is contained in:
parent
aac3191403
commit
12bbb61e8f
@ -80,6 +80,7 @@ func main() {
|
|||||||
// Is this really all that's required?
|
// Is this really all that's required?
|
||||||
fp := worldToCamera.MultiplyPoint3(f[i])
|
fp := worldToCamera.MultiplyPoint3(f[i])
|
||||||
fp = projection.MultiplyPoint3(fp)
|
fp = projection.MultiplyPoint3(fp)
|
||||||
|
//log.Print(uint16(fp.Z * 65535))
|
||||||
x[i] = int((fp.X + 1) * halfwidth)
|
x[i] = int((fp.X + 1) * halfwidth)
|
||||||
y[i] = hi - int((fp.Y+1)*halfheight)
|
y[i] = hi - int((fp.Y+1)*halfheight)
|
||||||
c[i] = 0xFFFFFF
|
c[i] = 0xFFFFFF
|
||||||
|
2
tinyrender3/.gitignore
vendored
Normal file
2
tinyrender3/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
tinyrender1
|
||||||
|
render
|
3
tinyrender3/go.mod
Normal file
3
tinyrender3/go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module tinyrender3
|
||||||
|
|
||||||
|
go 1.22.5
|
0
tinyrender3/go.sum
Normal file
0
tinyrender3/go.sum
Normal file
6357
tinyrender3/head.obj
Normal file
6357
tinyrender3/head.obj
Normal file
File diff suppressed because it is too large
Load Diff
107
tinyrender3/image.go
Normal file
107
tinyrender3/image.go
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Convert rgb to uint
|
||||||
|
func Col2Uint(r, g, b byte) uint {
|
||||||
|
return (uint(r) << 16) | (uint(g) << 8) | uint(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert uint to rgb (in that order)
|
||||||
|
func Uint2Col(col uint) (byte, byte, byte) {
|
||||||
|
return byte((col >> 16) & 0xFF), byte((col >> 8) & 0xFF), byte(col & 0xFF)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Color is in ARGB (alpha not used right now)
|
||||||
|
type Framebuffer struct {
|
||||||
|
Data []uint
|
||||||
|
ZBuffer []float32 //uint16 // Apparently 16 bit z-buffers are used
|
||||||
|
Width uint
|
||||||
|
Height uint
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new framebuffer for the given width and height.
|
||||||
|
func NewFramebuffer(width uint, height uint) Framebuffer {
|
||||||
|
return Framebuffer{
|
||||||
|
Data: make([]uint, width*height),
|
||||||
|
ZBuffer: make([]float32, width*height),
|
||||||
|
Width: width,
|
||||||
|
Height: height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill zbuffer with pixels that are max distance away
|
||||||
|
func (fb *Framebuffer) ResetZBuffer() {
|
||||||
|
for i := range fb.ZBuffer {
|
||||||
|
fb.ZBuffer[i] = math.MaxFloat32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sure hope this gets inlined...
|
||||||
|
func (fb *Framebuffer) Set(x uint, y uint, color uint) {
|
||||||
|
fb.Data[x+y*fb.Width] = color
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *Framebuffer) SetSafe(x uint, y uint, color uint) {
|
||||||
|
if x >= fb.Width || y >= fb.Height {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fb.Data[x+y*fb.Width] = color
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given some image data, return a string that is the ppm of it
|
||||||
|
func (fb *Framebuffer) ExportPPM() string {
|
||||||
|
var result strings.Builder
|
||||||
|
result.WriteString(fmt.Sprintf("P3\n%d %d\n255\n", fb.Width, fb.Height))
|
||||||
|
for y := range fb.Height {
|
||||||
|
for x := range fb.Width {
|
||||||
|
r, g, b := Uint2Col(fb.Data[x+y*fb.Width])
|
||||||
|
result.WriteString(fmt.Sprintf("%d %d %d\t", r, g, b))
|
||||||
|
}
|
||||||
|
result.WriteRune('\n')
|
||||||
|
}
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *Framebuffer) ExportPPMP6() []byte {
|
||||||
|
var result bytes.Buffer
|
||||||
|
result.WriteString(fmt.Sprintf("P6\n%d %d\n255\n", fb.Width, fb.Height))
|
||||||
|
for i := range fb.Data {
|
||||||
|
r, g, b := Uint2Col(fb.Data[i])
|
||||||
|
result.Write([]byte{r, g, b})
|
||||||
|
//result.WriteString(fmt.Sprintf("%d %d %d\t", r, g, b))
|
||||||
|
}
|
||||||
|
//result.WriteRune('\n')
|
||||||
|
return result.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *Framebuffer) ZBuffer_ExportPPM() string {
|
||||||
|
var result strings.Builder
|
||||||
|
mini := float32(math.MaxFloat32)
|
||||||
|
maxi := float32(-math.MaxFloat32)
|
||||||
|
for _, f := range fb.ZBuffer {
|
||||||
|
if f == math.MaxFloat32 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mini = min(f, mini)
|
||||||
|
maxi = max(f, maxi)
|
||||||
|
}
|
||||||
|
result.WriteString(fmt.Sprintf("P2\n%d %d\n255\n", fb.Width, fb.Height))
|
||||||
|
for y := range fb.Height {
|
||||||
|
for x := range fb.Width {
|
||||||
|
if fb.ZBuffer[x+y*fb.Width] == math.MaxFloat32 {
|
||||||
|
result.WriteString("0 ")
|
||||||
|
} else {
|
||||||
|
zp := byte(math.Abs(float64(255 * fb.ZBuffer[x+y*fb.Width] / (maxi - mini))))
|
||||||
|
result.WriteString(fmt.Sprintf("%d ", zp))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.WriteRune('\n')
|
||||||
|
}
|
||||||
|
return result.String()
|
||||||
|
}
|
127
tinyrender3/main.go
Normal file
127
tinyrender3/main.go
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"runtime/pprof" // For performance profiling (unnecessary)
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Width = 512
|
||||||
|
Height = 512
|
||||||
|
ObjectFile = "head.obj"
|
||||||
|
Repeat = 60
|
||||||
|
)
|
||||||
|
|
||||||
|
func must(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 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")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Printf("Program start")
|
||||||
|
|
||||||
|
// Little section for doing cpu profiling. I guess that's all you have to do?
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
fb := NewFramebuffer(Width, Height)
|
||||||
|
|
||||||
|
log.Printf("Loading obj %s", ObjectFile)
|
||||||
|
|
||||||
|
of, err := os.Open(ObjectFile)
|
||||||
|
must(err)
|
||||||
|
defer of.Close()
|
||||||
|
o, err := ParseObj(of)
|
||||||
|
must(err)
|
||||||
|
|
||||||
|
log.Printf("Running render")
|
||||||
|
|
||||||
|
//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)
|
||||||
|
halfheight := float32(fb.Height / 2)
|
||||||
|
var sc [3]Vec3f
|
||||||
|
var hi = float32(fb.Height - 1)
|
||||||
|
minz := float32(math.MaxFloat32)
|
||||||
|
maxz := float32(-math.MaxFloat32)
|
||||||
|
for range Repeat {
|
||||||
|
fb.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].X = (f[i].X + 1) * halfwidth
|
||||||
|
sc[i].Y = hi - (f[i].Y+1)*halfheight
|
||||||
|
sc[i].Z = f[i].Z // Pull Z value directly. This is fine, our z-buffer is currently float32
|
||||||
|
minz = min(minz, sc[i].Z)
|
||||||
|
maxz = max(maxz, sc[i].Z)
|
||||||
|
}
|
||||||
|
// TESTING
|
||||||
|
zch := float32(*zcuthigh)
|
||||||
|
zcl := float32(*zcutlow)
|
||||||
|
if sc[0].Z > zch || sc[1].Z > zch || sc[2].Z > zch ||
|
||||||
|
sc[0].Z < zcl || sc[1].Z < zcl || sc[2].Z < zcl {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// To test something, we swap vertex 2 and 3
|
||||||
|
//sc[1], sc[2] = sc[2], sc[1]
|
||||||
|
|
||||||
|
//l1 := f[2].Sub(f[0])
|
||||||
|
//n := l1.CrossProduct(f[1].Sub(f[0]))
|
||||||
|
//n = n.Normalize()
|
||||||
|
//intensity := n.MultSimp(&light)
|
||||||
|
//if intensity > 0 {
|
||||||
|
//Triangle3(&fb, Col2Uint(byte(255*intensity), byte(255*intensity), byte(255*intensity)), 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())
|
||||||
|
//Triangle2(&fb, 0xFFFFFF, sc[0], sc[1], sc[2])
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print("Min/max z: ", minz, maxz)
|
||||||
|
|
||||||
|
//log.Print(fb.ZBuffer)
|
||||||
|
|
||||||
|
if *dozbuf {
|
||||||
|
log.Printf("Exporting zbuffer ppm to stdout")
|
||||||
|
fmt.Print(fb.ZBuffer_ExportPPM())
|
||||||
|
} else {
|
||||||
|
if *p6file != "" {
|
||||||
|
err := os.WriteFile(*p6file, fb.ExportPPMP6(), 0660)
|
||||||
|
must(err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Exporting ppm to stdout")
|
||||||
|
fmt.Print(fb.ExportPPM())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Program end")
|
||||||
|
}
|
132
tinyrender3/obj.go
Normal file
132
tinyrender3/obj.go
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// This reads obj files?
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Vec3f struct {
|
||||||
|
X, Y, Z float32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Vec2i struct {
|
||||||
|
X, Y int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Vec2f struct {
|
||||||
|
X, Y float32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Facef [3]Vec3f
|
||||||
|
|
||||||
|
type ObjModel struct {
|
||||||
|
Vertices []Vec3f
|
||||||
|
Faces []Facef
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vi *Vec2i) ToF() Vec2f {
|
||||||
|
return Vec2f{float32(vi.X), float32(vi.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vi *Vec3f) ToVec2i() Vec2i {
|
||||||
|
return Vec2i{int(vi.X), int(vi.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v0 *Vec3f) Sub(v1 Vec3f) Vec3f {
|
||||||
|
return Vec3f{
|
||||||
|
X: v0.X - v1.X,
|
||||||
|
Y: v0.Y - v1.Y,
|
||||||
|
Z: v0.Z - v1.Z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v0 *Vec3f) CrossProduct(v1 Vec3f) Vec3f {
|
||||||
|
return Vec3f{
|
||||||
|
X: v0.Y*v1.Z - v0.Z*v1.Y,
|
||||||
|
Y: v0.Z*v1.X - v0.X*v1.Z,
|
||||||
|
Z: v0.X*v1.Y - v0.Y*v1.X,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (v
|
||||||
|
|
||||||
|
func (v *Vec3f) Normalize() Vec3f {
|
||||||
|
l := float32(math.Sqrt(float64(v.MultSimp(v))))
|
||||||
|
return Vec3f{
|
||||||
|
X: v.X / l,
|
||||||
|
Y: v.Y / l,
|
||||||
|
Z: v.Z / l,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v0 *Vec3f) MultSimp(v1 *Vec3f) float32 {
|
||||||
|
return v0.X*v1.X + v0.Y*v1.Y + v0.Z*v1.Z
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse an obj file at the given reader. Only handles v and f right now
|
||||||
|
func ParseObj(reader io.Reader) (*ObjModel, error) {
|
||||||
|
result := ObjModel{
|
||||||
|
Vertices: make([]Vec3f, 0),
|
||||||
|
Faces: make([]Facef, 0),
|
||||||
|
}
|
||||||
|
breader := bufio.NewReader(reader)
|
||||||
|
done := false
|
||||||
|
for !done {
|
||||||
|
// Scan a line
|
||||||
|
line, err := breader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
done = true
|
||||||
|
} else {
|
||||||
|
log.Printf("NOT EOF ERR?")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line = strings.Trim(line, " \t\n\r")
|
||||||
|
if len(line) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Find the first "item", whatever that is. This also gets rid of comments
|
||||||
|
// since we just don't use lines that start with # (no handler
|
||||||
|
var t string
|
||||||
|
_, err = fmt.Sscan(line, &t)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("SSCANF ERR")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
line = line[len(t):]
|
||||||
|
if t == "v" {
|
||||||
|
// Read a vertex, should be just three floats
|
||||||
|
var vertex Vec3f
|
||||||
|
_, err := fmt.Sscan(line, &vertex.X, &vertex.Y, &vertex.Z)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result.Vertices = append(result.Vertices, vertex)
|
||||||
|
} else if t == "f" {
|
||||||
|
// Read a face; in our example, it's always three sets.
|
||||||
|
// For THIS example, we throw away those other values
|
||||||
|
var face Facef
|
||||||
|
var vi [3]int
|
||||||
|
var ti int
|
||||||
|
_, err := fmt.Sscanf(line, "%d/%d/%d %d/%d/%d %d/%d/%d",
|
||||||
|
&vi[0], &ti, &ti, &vi[1], &ti, &ti, &vi[2], &ti, &ti)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for i := range 3 {
|
||||||
|
if vi[i] > len(result.Vertices) || vi[i] < 1 {
|
||||||
|
return nil, fmt.Errorf("Face vertex index out of bounds: %d", vi[i])
|
||||||
|
}
|
||||||
|
face[i] = result.Vertices[vi[i]-1]
|
||||||
|
}
|
||||||
|
result.Faces = append(result.Faces, face)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &result, nil
|
||||||
|
}
|
266
tinyrender3/render.go
Normal file
266
tinyrender3/render.go
Normal file
@ -0,0 +1,266 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
//"log"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Bresenham2(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) {
|
||||||
|
dx := int(math.Abs(float64(x1 - x0)))
|
||||||
|
sx := -1
|
||||||
|
if x0 < x1 {
|
||||||
|
sx = 1
|
||||||
|
}
|
||||||
|
dy := -int(math.Abs(float64(y1 - y0)))
|
||||||
|
sy := -1
|
||||||
|
if y0 < y1 {
|
||||||
|
sy = 1
|
||||||
|
}
|
||||||
|
err := dx + dy
|
||||||
|
|
||||||
|
for {
|
||||||
|
fb.SetSafe(uint(x0), uint(y0), color)
|
||||||
|
if x0 == x1 && y0 == y1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
e2 := 2 * err
|
||||||
|
if e2 >= dy {
|
||||||
|
if x0 == x1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err += dy
|
||||||
|
x0 += sx
|
||||||
|
}
|
||||||
|
if e2 <= dx {
|
||||||
|
if y0 == y1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err += dx
|
||||||
|
y0 += sy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) {
|
||||||
|
Bresenham2(fb, color, v0.X, v0.Y, v1.X, v1.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*func LineSweep(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
||||||
|
// The dude gets rid of "degenerate" triangles so... we do too?
|
||||||
|
if v2.Y == v1.Y && v1.Y == v0.Y {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Very silly manual sorting by Y
|
||||||
|
if v2.Y < v0.Y {
|
||||||
|
v0, v2 = v2, v0
|
||||||
|
}
|
||||||
|
if v1.Y < v0.Y {
|
||||||
|
v0, v1 = v1, v0
|
||||||
|
}
|
||||||
|
if v2.Y < v1.Y {
|
||||||
|
v1, v2 = v2, v1
|
||||||
|
}
|
||||||
|
|
||||||
|
var v02step, v01step, v12step, xlong, xshort float32
|
||||||
|
|
||||||
|
xlong = float32(v0.X)
|
||||||
|
xshort = xlong
|
||||||
|
|
||||||
|
// The first and last Y CAN'T be equal because sorting!!
|
||||||
|
if v1.Y == v0.Y {
|
||||||
|
xshort = float32(v1.X)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We can check just for greater than because we sorted the vertices
|
||||||
|
// Assume 02 is on the right(?) and 01 on the left
|
||||||
|
v02step = (float32(v2.X - v0.X)) / (float32(v2.Y-v0.Y) + 0.001) // long side always
|
||||||
|
v01step = (float32(v1.X - v0.X)) / (float32(v1.Y-v0.Y) + 0.001) // first short side
|
||||||
|
v12step = (float32(v2.X - v1.X)) / (float32(v2.Y-v1.Y) + 0.001) // second short side
|
||||||
|
|
||||||
|
for y := v0.Y; y <= v2.Y; y++ {
|
||||||
|
xleft := int(xshort)
|
||||||
|
xright := int(xlong)
|
||||||
|
if xleft > xright {
|
||||||
|
xleft, xright = xright, xleft
|
||||||
|
}
|
||||||
|
if xleft < 0 || xright >= int(fb.Width) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Draw a horizontal line from left to right
|
||||||
|
for x := xleft; x <= xright; x++ {
|
||||||
|
fb.SetSafe(uint(x), uint(y), color)
|
||||||
|
}
|
||||||
|
xlong += v02step
|
||||||
|
if y < v1.Y {
|
||||||
|
xshort += v01step
|
||||||
|
} else {
|
||||||
|
xshort += v12step
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// How does this work? Compare with your
|
||||||
|
// other barycentric function (in a different repo). In the original
|
||||||
|
// cpp code, they used an overloaded operator ^ to mean cross product
|
||||||
|
func Barycentric(v0, v1, v2, p Vec2i) Vec3f {
|
||||||
|
// WARN: Just not doing this one
|
||||||
|
u := Vec3f{}
|
||||||
|
if math.Abs(float64(u.Z)) < 1 {
|
||||||
|
return Vec3f{-1, 1, 1}
|
||||||
|
}
|
||||||
|
return Vec3f{1 - (u.X+u.Y)/u.Z, u.Y / u.Z, u.X / u.Z}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Figure out the minimum bounding box for a triangle defined by
|
||||||
|
// these vertices. Returns the top left and bottom right points,
|
||||||
|
// inclusive
|
||||||
|
func ComputeBoundingBox(v0, v1, v2 Vec2i) (Vec2i, Vec2i) {
|
||||||
|
return Vec2i{min(v0.X, v1.X, v2.X), min(v0.Y, v1.Y, v2.Y)},
|
||||||
|
Vec2i{max(v0.X, v1.X, v2.X), max(v0.Y, v1.Y, v2.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The generic edge function, returning positive if P is on the right side of
|
||||||
|
// the line drawn between v1 and v2. This is counter clockwise
|
||||||
|
func EdgeFunction(v1, v2, p Vec2f) float32 {
|
||||||
|
return (p.X-v1.X)*(v2.Y-v1.Y) - (p.Y-v1.Y)*(v2.X-v1.X)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This computes the x and y per-pixel increment for the line going
|
||||||
|
// between v1 and v2 (also counter clockwise)
|
||||||
|
func EdgeIncrement(v1, v2 Vec2f) (float32, float32) {
|
||||||
|
return (v2.Y - v1.Y), -(v2.X - v1.X)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The generic edge function, returning positive if P is on the right side of
|
||||||
|
// the line drawn between v1 and v2. This is counter clockwise
|
||||||
|
func EdgeFunctioni(v1, v2, p Vec2i) int {
|
||||||
|
return (p.X-v1.X)*(v2.Y-v1.Y) - (p.Y-v1.Y)*(v2.X-v1.X)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This computes the x and y per-pixel increment for the line going
|
||||||
|
// between v1 and v2 (also counter clockwise)
|
||||||
|
func EdgeIncrementi(v1, v2 Vec2i) (int, int) {
|
||||||
|
return (v2.Y - v1.Y), -(v2.X - v1.X)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Triangle2(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
||||||
|
boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2)
|
||||||
|
if boundsTL.Y < 0 {
|
||||||
|
boundsTL.Y = 0
|
||||||
|
}
|
||||||
|
if boundsTL.X < 0 {
|
||||||
|
boundsTL.X = 0
|
||||||
|
}
|
||||||
|
if boundsBR.Y >= int(fb.Height) {
|
||||||
|
boundsBR.Y = int(fb.Height - 1)
|
||||||
|
}
|
||||||
|
if boundsBR.X >= int(fb.Width) {
|
||||||
|
boundsBR.X = int(fb.Width - 1)
|
||||||
|
}
|
||||||
|
// Where to start our scanning
|
||||||
|
pstart := Vec2i{boundsTL.X, boundsTL.Y}
|
||||||
|
//log.Print(boundsTL, boundsBR)
|
||||||
|
// v0f := v0.ToF()
|
||||||
|
// v1f := v1.ToF()
|
||||||
|
// v2f := v2.ToF()
|
||||||
|
// parea := EdgeFunction(v0f, v1f, v2f)
|
||||||
|
// invarea := 1 / parea
|
||||||
|
w0_y := EdgeFunctioni(v1, v2, pstart)
|
||||||
|
w1_y := EdgeFunctioni(v2, v0, pstart)
|
||||||
|
w2_y := EdgeFunctioni(v0, v1, pstart)
|
||||||
|
w0_xi, w0_yi := EdgeIncrementi(v1, v2)
|
||||||
|
w1_xi, w1_yi := EdgeIncrementi(v2, v0)
|
||||||
|
w2_xi, w2_yi := EdgeIncrementi(v0, v1)
|
||||||
|
//dyi := int(fb.Width)
|
||||||
|
//dy := boundsTL.X + dyi*boundsTL.Y
|
||||||
|
|
||||||
|
for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ {
|
||||||
|
w0 := w0_y
|
||||||
|
w1 := w1_y
|
||||||
|
w2 := w2_y
|
||||||
|
//di := dy
|
||||||
|
//done := false
|
||||||
|
for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ {
|
||||||
|
if (w0 | w1 | w2) >= 0 {
|
||||||
|
//fb.Data[di] = color
|
||||||
|
fb.Set(x, y, color)
|
||||||
|
//done = true
|
||||||
|
// w0a := w0 * invarea
|
||||||
|
// w1a := w1 * invarea
|
||||||
|
// w2a := w2 * invarea
|
||||||
|
// fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a)))
|
||||||
|
} /*else if done {
|
||||||
|
break
|
||||||
|
}*/
|
||||||
|
//di += 1
|
||||||
|
w0 += w0_xi
|
||||||
|
w1 += w1_xi
|
||||||
|
w2 += w2_xi
|
||||||
|
}
|
||||||
|
//dy += dyi
|
||||||
|
w0_y += w0_yi
|
||||||
|
w1_y += w1_yi
|
||||||
|
w2_y += w2_yi
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Triangle3(fb *Framebuffer, color uint, v0f Vec3f, v1f Vec3f, v2f Vec3f) {
|
||||||
|
v0 := v0f.ToVec2i()
|
||||||
|
v1 := v1f.ToVec2i()
|
||||||
|
v2 := v2f.ToVec2i()
|
||||||
|
boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2)
|
||||||
|
if boundsTL.Y < 0 {
|
||||||
|
boundsTL.Y = 0
|
||||||
|
}
|
||||||
|
if boundsTL.X < 0 {
|
||||||
|
boundsTL.X = 0
|
||||||
|
}
|
||||||
|
if boundsBR.Y >= int(fb.Height) {
|
||||||
|
boundsBR.Y = int(fb.Height - 1)
|
||||||
|
}
|
||||||
|
if boundsBR.X >= int(fb.Width) {
|
||||||
|
boundsBR.X = int(fb.Width - 1)
|
||||||
|
}
|
||||||
|
// Where to start our scanning
|
||||||
|
pstart := Vec2i{boundsTL.X, boundsTL.Y}
|
||||||
|
parea := EdgeFunctioni(v0, v1, v2)
|
||||||
|
invarea := 1 / float32(parea)
|
||||||
|
w0_y := EdgeFunctioni(v1, v2, pstart)
|
||||||
|
w1_y := EdgeFunctioni(v2, v0, pstart)
|
||||||
|
w2_y := EdgeFunctioni(v0, v1, pstart)
|
||||||
|
w0_xi, w0_yi := EdgeIncrementi(v1, v2)
|
||||||
|
w1_xi, w1_yi := EdgeIncrementi(v2, v0)
|
||||||
|
w2_xi, w2_yi := EdgeIncrementi(v0, v1)
|
||||||
|
|
||||||
|
for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ {
|
||||||
|
w0 := w0_y
|
||||||
|
w1 := w1_y
|
||||||
|
w2 := w2_y
|
||||||
|
for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ {
|
||||||
|
if (w0 | w1 | w2) >= 0 {
|
||||||
|
//fb.Data[di] = color
|
||||||
|
//done = true
|
||||||
|
w0a := float32(w0) * invarea
|
||||||
|
w1a := float32(w1) * invarea
|
||||||
|
w2a := float32(w2) * invarea
|
||||||
|
pz := w0a*v0f.Z + w1a*v1f.Z + w2a*v2f.Z
|
||||||
|
if pz < fb.ZBuffer[x+y*fb.Width] {
|
||||||
|
//log.Print(pz)
|
||||||
|
fb.ZBuffer[x+y*fb.Width] = pz
|
||||||
|
fb.Set(x, y, color)
|
||||||
|
}
|
||||||
|
// fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a)))
|
||||||
|
}
|
||||||
|
w0 += w0_xi
|
||||||
|
w1 += w1_xi
|
||||||
|
w2 += w2_xi
|
||||||
|
}
|
||||||
|
w0_y += w0_yi
|
||||||
|
w1_y += w1_yi
|
||||||
|
w2_y += w2_yi
|
||||||
|
}
|
||||||
|
}
|
12
tinyrender3/run.sh
Executable file
12
tinyrender3/run.sh
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "You must pass the basename for the .prof and .ppm"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Building"
|
||||||
|
go build -o render
|
||||||
|
echo "Running"
|
||||||
|
./render "-cpuprofile=$1.prof" >"$1.ppm"
|
||||||
|
./render "-zbuffer" >"$1_zbuffer.ppm"
|
22
tinyrender3/slices.sh
Executable file
22
tinyrender3/slices.sh
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "You must pass the basename for the slices"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Building"
|
||||||
|
go build -o render
|
||||||
|
mkdir -p $1
|
||||||
|
|
||||||
|
frame=0
|
||||||
|
for x in $(seq -0.7 0.05 0.7); do
|
||||||
|
ff=$(printf "%03d" $frame)
|
||||||
|
./render "-p6file=$1/$ff.ppm" "-zcuthigh=$x"
|
||||||
|
frame=$((frame + 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Converting animation"
|
||||||
|
|
||||||
|
cd $1
|
||||||
|
convert -delay 10 -loop 0 *.ppm -resize 256x256 anim.gif
|
Loading…
Reference in New Issue
Block a user