First homework attempt

This commit is contained in:
Carlos Sanchez 2024-07-28 18:01:15 -04:00
parent 7480b39456
commit c89a3a9ab5
12 changed files with 7153 additions and 0 deletions

1
.gitignore vendored
View File

@ -5,4 +5,5 @@
*.jpg
*.jpeg
*.prof
*.tga
a.out

2
tinyrender3_homework/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
tinyrender1
render

View File

@ -0,0 +1,3 @@
module tinyrender3
go 1.22.5

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 KiB

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,113 @@
package main
import (
"bytes"
"fmt"
"image/color"
"math"
"strings"
)
// Convert rgb to uint
func Col2Uint(r, g, b byte) uint {
return (uint(r) << 16) | (uint(g) << 8) | uint(b)
}
func Color2Uint(col color.Color) uint {
r, g, b, _ := col.RGBA()
return uint((r << 16) | (g << 8) | 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()
}

View File

@ -0,0 +1,140 @@
package main
import (
"flag"
"fmt"
"image"
"log"
"math"
//"math/rand"
"os"
"runtime/pprof" // For performance profiling (unnecessary)
_ "image/jpeg"
)
const (
Width = 512
Height = 512
ObjectFile = "head.obj"
TextureFile = "head.jpg"
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, texture %s", ObjectFile, TextureFile)
of, err := os.Open(ObjectFile)
must(err)
defer of.Close()
o, err := ParseObj(of)
must(err)
jf, err := os.Open(TextureFile)
must(err)
defer jf.Close()
texture, _, err := image.Decode(jf)
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]Vertex
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] = f[i]
sc[i].Pos.X = (f[i].Pos.X + 1) * halfwidth
sc[i].Pos.Y = hi - (f[i].Pos.Y+1)*halfheight
// NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER
// 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
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)
n := l1.CrossProduct(f[1].Pos.Sub(f[0].Pos))
n = n.Normalize()
intensity := n.MultSimp(&light)
if intensity > 0 {
Triangle3t(&fb, texture, 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")
}

158
tinyrender3_homework/obj.go Normal file
View File

@ -0,0 +1,158 @@
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
}
// A single vertex generally has multiple items associated with it
// when it's part of a face.
type Vertex struct {
Pos Vec3f
Tex Vec3f
}
type Facef [3]Vertex
// struct {
// Vertices [3]Vec3f
// TextureCoords [3]Vec2i
// }
type ObjModel struct {
Vertices []Vec3f
VTexture []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 v Vec3f
_, err := fmt.Sscan(line, &v.X, &v.Y, &v.Z)
if err != nil {
return nil, err
}
result.Vertices = append(result.Vertices, v)
} else if t == "vt" {
// Read a vertex tex coord, should be just three floats too
var vt Vec3f
_, err := fmt.Sscan(line, &vt.X, &vt.Y, &vt.Z)
if err != nil {
return nil, err
}
result.VTexture = append(result.VTexture, vt)
} 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 vti [3]int
var ti int
_, err := fmt.Sscanf(line, "%d/%d/%d %d/%d/%d %d/%d/%d",
&vi[0], &vti[0], &ti, &vi[1], &vti[1], &ti, &vi[2], &vti[2], &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].Pos = result.Vertices[vi[i]-1]
if vti[i] > len(result.VTexture) || vti[i] < 1 {
return nil, fmt.Errorf("Face vertex texture index out of bounds: %d", vti[i])
}
face[i].Tex = result.VTexture[vti[i]-1]
}
result.Faces = append(result.Faces, face)
}
}
return &result, nil
}

View File

@ -0,0 +1,345 @@
package main
import (
//"log"
"image"
"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)
// if parea < 0 {
// v1, v2 = v2, v1
// v1f, v2f = v2f, v1f
// 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
}
}
func Triangle3t(fb *Framebuffer, texture image.Image, intensity float32, v0v Vertex, v1v Vertex, v2v Vertex) {
v0 := v0v.Pos.ToVec2i()
v1 := v1v.Pos.ToVec2i()
v2 := v2v.Pos.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)
// if parea < 0 {
// v1, v2 = v2, v1
// v1f, v2f = v2f, v1f
// 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)
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++ {
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*v0v.Pos.Z + w1a*v1v.Pos.Z + w2a*v2v.Pos.Z
if pz < fb.ZBuffer[x+y*fb.Width] {
//log.Print(pz)
fb.ZBuffer[x+y*fb.Width] = pz
txo := int(float32(tw) * (w0a*v0v.Tex.X + w1a*v1v.Tex.X + w2a*v2v.Tex.X))
tyo := int(float32(th) * (w0a*v0v.Tex.Y + w1a*v1v.Tex.Y + w2a*v2v.Tex.Y))
col := texture.At(tx+txo, ty+tyo)
//c := texture.At()
fb.Set(x, y, Color2Uint(col)) //uint(texture.Bounds().Dx())
//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)))
}
w0 += w0_xi
w1 += w1_xi
w2 += w2_xi
}
w0_y += w0_yi
w1_y += w1_yi
w2_y += w2_yi
}
}

12
tinyrender3_homework/run.sh Executable file
View 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_homework/slices.sh Executable file
View 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