3dtrial/renderer4/hrend/render.go

342 lines
10 KiB
Go
Raw Normal View History

2024-08-05 07:25:58 +00:00
package hrend
import (
// "log"
// "math"
)
type ObjectDef struct {
Model *ObjModel
Texture Framebuffer // This needs to go somewhere else eventually!
Pos Vec3f
LookVec Vec3f
Color Vec3f
Scale float32
Lighting bool
}
func (o *ObjectDef) FV(f *Facei, i int) *Vec3f {
return &o.Model.Vertices[f[i].Posi] //o.ModelFaces[i][f].Posi]
}
func NewObjectDef(model *ObjModel, texture Framebuffer) *ObjectDef {
result := ObjectDef{
Model: model,
Texture: texture,
LookVec: Vec3f{X: 0, Y: 0, Z: -1},
Scale: 1,
Lighting: true,
}
return &result
}
// 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)}
}
func ComputeBoundingBoxF(v0, v1, v2 Vec3f) (Vec3f, Vec3f) {
return Vec3f{min(v0.X, v1.X, v2.X), min(v0.Y, v1.Y, v2.Y), min(v0.Z, v1.Z, v2.Z)},
Vec3f{max(v0.X, v1.X, v2.X), max(v0.Y, v1.Y, v2.Y), max(v0.Z, v1.Z, v2.Z)}
}
// 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 Vec3f) 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 Vec3f) (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 ZClip(v0f Vec3f, v1f Vec3f, v2f Vec3f) bool {
maxz := max(v0f.Z, v1f.Z, v2f.Z)
return maxz < -1 || maxz > 1
}
func TriangleFlat(fb *RenderBuffer, color *Vec3f, v0f Vec3f, v1f Vec3f, v2f Vec3f) {
v0 := v0f.ToVec2i()
v1 := v1f.ToVec2i()
v2 := v2f.ToVec2i()
//r, g, b := Uint2Col(color)
boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2)
if boundsBR.X < 0 || boundsBR.Y < 0 || boundsTL.X >= int(fb.Width) || boundsTL.Y >= int(fb.Height) {
return
}
parea := EdgeFunctioni(v0, v1, v2)
if parea <= 0 {
return
}
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}
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)
r := byte(255 * color.X)
g := byte(255 * color.Y)
b := byte(255 * color.Z)
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 {
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] {
fb.ZBuffer[x+y*fb.Width] = pz
fb.Data.Set(x, y, r, g, b)
}
}
w0 += w0_xi
w1 += w1_xi
w2 += w2_xi
}
w0_y += w0_yi
w1_y += w1_yi
w2_y += w2_yi
}
}
func TriangleTextured(fb *RenderBuffer, texture Framebuffer, intensity float32, face *Facef) {
v0v := face[0]
v1v := face[1]
v2v := face[2]
// min, max
boundsTLf, boundsBRf := ComputeBoundingBoxF(face[0].Pos, face[1].Pos, face[2].Pos)
// The triangle is fully out of bounds; we don't have a proper clipper, so this
// check still needs to be performed
if boundsBRf.Y < 0 || boundsBRf.X < 0 || boundsTLf.X >= float32(fb.Width) || boundsTLf.Y >= float32(fb.Height) { //||
return
}
v0 := face[0].Pos.ToVec2i()
v1 := face[1].Pos.ToVec2i()
v2 := face[2].Pos.ToVec2i()
parea := EdgeFunctioni(v0, v1, v2)
// Don't even bother with drawing backfaces or degenerate triangles;
// don't even give the user the option
if parea <= 0 {
return
}
boundsTL := Vec2i{
X: int(max(boundsTLf.X, 0)),
Y: int(max(boundsTLf.Y, 0)),
}
boundsBR := Vec2i{
X: int(min(boundsBRf.X, float32(fb.Width-1))),
Y: int(min(boundsBRf.Y, float32(fb.Height-1))),
}
// Where to start our scanning
pstart := Vec2i{boundsTL.X, boundsTL.Y}
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)
// I don't know what happened to my z but it's nearly unusable.
// I simply use w instead... don't know if that's ok
tiz0 := 1 / v0v.W
tiz1 := 1 / v1v.W
tiz2 := 1 / v2v.W
// tiz0 := v0v.Pos.Z
// tiz1 := v1v.Pos.Z
// tiz2 := v2v.Pos.Z
// tiz0 := 1 / v0v.Pos.Z
// tiz1 := 1 / v1v.Pos.Z
// tiz2 := 1 / v2v.Pos.Z
tiu0 := v0v.Tex.X * tiz0 //v0v.W //* tiz0
tiu1 := v1v.Tex.X * tiz1 //v1v.W //* tiz1
tiu2 := v2v.Tex.X * tiz2 //v2v.W //* tiz2
tiv0 := v0v.Tex.Y * tiz0 //v0v.W //* tiz0
tiv1 := v1v.Tex.Y * tiz1 //v1v.W //* tiz1
tiv2 := v2v.Tex.Y * tiz2 //v2v.W //* tiz2
2024-08-05 07:25:58 +00:00
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 {
//if w0 >= 0 && w1 >= 0 && w2 >= 0 {
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
pz := w0a*tiz0 + w1a*tiz1 + w2a*tiz2
if pz > fb.ZBuffer[x+y*fb.Width] {
2024-08-05 07:25:58 +00:00
fb.ZBuffer[x+y*fb.Width] = pz
//pw := w0a/tiz0 + w1a/tiz1 + w2a/tiz2
//pw := w0a/v0v.W + w1a/v1v.W + w2a/v2v.W
pz = 1 / pz
2024-08-05 07:25:58 +00:00
r, g, b := texture.GetUv(
(w0a*tiu0+w1a*tiu1+w2a*tiu2)*pz,
(w0a*tiv0+w1a*tiv1+w2a*tiv2)*pz,
2024-08-05 07:25:58 +00:00
)
fb.Data.Set(x, y, byte(float32(r)*intensity), byte(float32(g)*intensity), byte(float32(b)*intensity))
}
}
w0 += w0_xi
w1 += w1_xi
w2 += w2_xi
}
w0_y += w0_yi
w1_y += w1_yi
w2_y += w2_yi
}
}
// Return true if the face should be culled
func BackfaceCull(v1, v2, v3 Vec3f) bool {
// This is what it essentially is
// e1 := v1.Sub(&v2)
// e2 := v1.Sub(&v3)
// // If viewing front face, it should be pointing in the positive z direction
// return e1.CrossProduct(e2).Z <= 0
// But we know we can just use x and y since this is post projection
return (v1.X-v2.X)*(v3.Y-v2.Y)-(v1.Y-v2.Y)*(v3.X-v2.X) >= 0
}
func conditionalAddTriangle(sc []HVec3f, tx []Vec3f, out []Facef) []Facef {
var f Facef
// The triangle is fine
for i := range 3 {
f[i].Pos = sc[i].MakeConventional()
f[i].Tex = tx[i]
f[i].W = sc[i].W
2024-08-05 07:25:58 +00:00
}
// Backface culling: no need to do anything with triangles facing the wrong way
if EdgeFunction(f[0].Pos, f[1].Pos, f[2].Pos) <= 0 {
out = append(out, f)
}
return out
}
// Apply perspective projection to all vertices, but don't convert homegenous
// coordinates yet
func PerspectiveAll(in []Vec3f, matrix3d *Mat44f, out []HVec3f) []HVec3f {
out = out[:len(in)]
for i := range in {
out[i] = matrix3d.MultiplyPoint3(in[i])
}
return out
}
func ClipFace(face Facei, vecs []HVec3f, texs []Vec3f) []Facef {
outfaces := make([]Facef, 0, 2)
outers := make([]int, 0, 3)
inners := make([]int, 0, 3)
var hf [3]HVec3f
var tx [3]Vec3f
var d [3]float32
for i := range 3 {
hf[i] = vecs[face[i].Posi]
tx[i] = texs[face[i].Texi]
d[i] = hf[i].Pos.Z + hf[i].W
if d[i] < 0.001 {
outers = append(outers, i)
} else {
inners = append(inners, i)
}
}
if len(outers) == 2 { // The one triangle thing
ai := inners[0]
bi := outers[0]
ci := outers[1]
// Calc how far along we are on each of these lines. These are the new points
tba := d[bi] / (d[bi] - d[ai])
tca := d[ci] / (d[ci] - d[ai])
// The two points that aren't a need to be the interpolated values
hf[bi].LerpSelf(&hf[ai], tba) // lerp b between it and a, store in self.
hf[ci].LerpSelf(&hf[ai], tca)
tx[bi] = LerpVec3f(tx[bi], tx[ai], tba)
tx[ci] = LerpVec3f(tx[ci], tx[ai], tca)
outfaces = conditionalAddTriangle(hf[:], tx[:], outfaces)
} else if len(outers) == 1 { // The two triangle thing, two new corners
ai := outers[0]
bi := inners[0]
ci := inners[1]
tab := d[ai] / (d[ai] - d[bi])
tac := d[ai] / (d[ai] - d[ci])
hfa := hf[ai]
txa := tx[ai]
// This time, we're generating two new points. But,
// Only ONE point needs to be modified: the one outer. Remember that
// tab and tac are the distance to that point itself, so a still needs
// to be the first value here
hf[ai].LerpSelf(&hf[bi], tab)
tx[ai] = LerpVec3f(tx[ai], tx[bi], tab)
outfaces = conditionalAddTriangle(hf[:], tx[:], outfaces)
// Now that we've replaced the far point, we also need to replace
// the original B point that we used, since that's part of the other
// triangle. But simply replacing it will make the triangle invisible,
// since it inverts the winding order (I think)
hf[bi] = hfa
hf[bi].LerpSelf(&hf[ci], tac)
tx[bi] = LerpVec3f(txa, tx[ci], tac)
// Now swap the a and b (or we could swap c and b)
hf[ai], hf[bi] = hf[bi], hf[ai]
tx[ai], tx[bi] = tx[bi], tx[ai]
outfaces = conditionalAddTriangle(hf[:], tx[:], outfaces)
} else if len(outers) != 3 { // Output the face itself, no modification
outfaces = conditionalAddTriangle(hf[:], tx[:], outfaces)
}
return outfaces
// TODO: Now that we're here doing it like this, might as well remove faces
// that are fully outside the other clipping zones. No need to do actual clipping...
// just full rejections. This saves a BIT of processing... though not much
// NOTE: Uh no... this is too much effort. Two points could be outside individual
// planes and thus still intersect the screen.
}