commit 5b96d576e8246136082a29fbda54d97824f207aa
parent 2948e3da4df14788d2b96aa4de3b40242beded34
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 17 Nov 2024 10:09:02 +0900
set normals
Diffstat:
2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -242,6 +242,7 @@ func main() {
 	if err != nil {
 		log.Fatal(err)
 	}
+	teapot.SetNormals()
 	teapot.Load()
 
 	app.program, err = tofu.NewProgram(vpath, fpath)
diff --git a/object.go b/object.go
@@ -167,6 +167,19 @@ func (obj Object) faceData() []uint32 {
 	return fdata
 }
 
+func (obj *Object) SetNormals() {
+	obj.Normals = make([]Vec3, len(obj.Faces))
+	for i, f := range obj.Faces {
+		v := obj.Vertices[f.I[0].V].Sub(obj.Vertices[f.I[1].V])
+		w := obj.Vertices[f.I[0].V].Sub(obj.Vertices[f.I[2].V])
+		n := v.Cross(w).Normalize()
+		obj.Normals[i] = n
+		obj.Faces[i].I[0].N = i
+		obj.Faces[i].I[1].N = i
+		obj.Faces[i].I[2].N = i
+	}
+}
+
 // Load copies object data into the GPU.
 func (obj *Object) Load() {
 	obj.vao = newVAO(obj)