commit 035d49ecfa6e82c604b05af5f2e2496e35d10245
parent 6d47c34842bf994589f25d6f1d99823d7fc9d10d
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 17 Nov 2024 10:24:25 +0900
change Faces
Diffstat:
2 files changed, 36 insertions(+), 38 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -104,18 +104,18 @@ var object = &tofu.Object{
tofu.Vec2{0.0, 1.0},
},
Faces: []tofu.Face{
- {[3]tofu.VertexIndex{{0, 0, 0}, {1, 0, 1}, {2, 0, 2}}},
- {[3]tofu.VertexIndex{{0, 0, 0}, {2, 0, 2}, {3, 0, 3}}},
- {[3]tofu.VertexIndex{{0, 1, 0}, {1, 1, 1}, {5, 1, 2}}},
- {[3]tofu.VertexIndex{{0, 1, 0}, {5, 1, 2}, {4, 1, 3}}},
- {[3]tofu.VertexIndex{{1, 2, 0}, {2, 2, 1}, {6, 2, 2}}},
- {[3]tofu.VertexIndex{{1, 2, 0}, {6, 2, 2}, {5, 2, 3}}},
- {[3]tofu.VertexIndex{{2, 3, 0}, {3, 3, 1}, {7, 3, 2}}},
- {[3]tofu.VertexIndex{{2, 3, 0}, {7, 3, 2}, {6, 3, 3}}},
- {[3]tofu.VertexIndex{{3, 4, 0}, {0, 4, 1}, {4, 4, 2}}},
- {[3]tofu.VertexIndex{{3, 4, 0}, {4, 4, 2}, {7, 4, 3}}},
- {[3]tofu.VertexIndex{{4, 5, 0}, {5, 5, 1}, {6, 5, 2}}},
- {[3]tofu.VertexIndex{{4, 5, 0}, {6, 5, 2}, {7, 5, 3}}},
+ {{0, 0, 0}, {1, 0, 1}, {2, 0, 2}},
+ {{0, 0, 0}, {2, 0, 2}, {3, 0, 3}},
+ {{0, 1, 0}, {1, 1, 1}, {5, 1, 2}},
+ {{0, 1, 0}, {5, 1, 2}, {4, 1, 3}},
+ {{1, 2, 0}, {2, 2, 1}, {6, 2, 2}},
+ {{1, 2, 0}, {6, 2, 2}, {5, 2, 3}},
+ {{2, 3, 0}, {3, 3, 1}, {7, 3, 2}},
+ {{2, 3, 0}, {7, 3, 2}, {6, 3, 3}},
+ {{3, 4, 0}, {0, 4, 1}, {4, 4, 2}},
+ {{3, 4, 0}, {4, 4, 2}, {7, 4, 3}},
+ {{4, 5, 0}, {5, 5, 1}, {6, 5, 2}},
+ {{4, 5, 0}, {6, 5, 2}, {7, 5, 3}},
},
}
diff --git a/object.go b/object.go
@@ -93,9 +93,9 @@ func (obj *Object) addFace(s *scanner.Scanner) error {
if err != nil {
return fmt.Errorf("Atoi: %v", err)
}
- f.I[i].V = x*sign - 1
- f.I[i].N = NoIndex
- f.I[i].T = NoIndex
+ f[i].V = x*sign - 1
+ f[i].N = NoIndex
+ f[i].T = NoIndex
default:
return fmt.Errorf("unwanted token %q of type %d. want face index.", s.TokenText(), t)
}
@@ -110,19 +110,17 @@ type VertexIndex struct {
V, N, T int
}
-type Face struct {
- I [3]VertexIndex
-}
+type Face [3]VertexIndex
func (obj Object) stride() int {
if len(obj.Faces) < 1 {
return 0
}
var stride int = 3
- if obj.Faces[0].I[0].N != NoIndex {
+ if obj.Faces[0][0].N != NoIndex {
stride += 3
}
- if obj.Faces[0].I[0].T != NoIndex {
+ if obj.Faces[0][0].T != NoIndex {
stride += 2
}
return stride
@@ -136,23 +134,23 @@ func (obj Object) data() []float32 {
data := make([]float32, stride*3*len(obj.Faces))
for i, f := range obj.Faces {
for j := 0; j < 3; j++ {
- data[stride*(3*i+j)] = obj.Vertices[f.I[j].V][0]
- data[stride*(3*i+j)+1] = obj.Vertices[f.I[j].V][1]
- data[stride*(3*i+j)+2] = obj.Vertices[f.I[j].V][2]
+ data[stride*(3*i+j)] = obj.Vertices[f[j].V][0]
+ data[stride*(3*i+j)+1] = obj.Vertices[f[j].V][1]
+ data[stride*(3*i+j)+2] = obj.Vertices[f[j].V][2]
switch stride {
case 5:
- data[stride*(3*i+j)+3] = obj.TexCoords[f.I[j].T][0]
- data[stride*(3*i+j)+4] = obj.TexCoords[f.I[j].T][1]
+ data[stride*(3*i+j)+3] = obj.TexCoords[f[j].T][0]
+ data[stride*(3*i+j)+4] = obj.TexCoords[f[j].T][1]
case 6:
- data[stride*(3*i+j)+3] = obj.Normals[f.I[j].N][0]
- data[stride*(3*i+j)+4] = obj.Normals[f.I[j].N][1]
- data[stride*(3*i+j)+5] = obj.Normals[f.I[j].N][2]
+ data[stride*(3*i+j)+3] = obj.Normals[f[j].N][0]
+ data[stride*(3*i+j)+4] = obj.Normals[f[j].N][1]
+ data[stride*(3*i+j)+5] = obj.Normals[f[j].N][2]
case 8:
- data[stride*(3*i+j)+3] = obj.TexCoords[f.I[j].T][0]
- data[stride*(3*i+j)+4] = obj.TexCoords[f.I[j].T][1]
- data[stride*(3*i+j)+5] = obj.Normals[f.I[j].N][0]
- data[stride*(3*i+j)+6] = obj.Normals[f.I[j].N][1]
- data[stride*(3*i+j)+7] = obj.Normals[f.I[j].N][2]
+ data[stride*(3*i+j)+3] = obj.TexCoords[f[j].T][0]
+ data[stride*(3*i+j)+4] = obj.TexCoords[f[j].T][1]
+ data[stride*(3*i+j)+5] = obj.Normals[f[j].N][0]
+ data[stride*(3*i+j)+6] = obj.Normals[f[j].N][1]
+ data[stride*(3*i+j)+7] = obj.Normals[f[j].N][2]
}
}
}
@@ -170,13 +168,13 @@ func (obj Object) faceData() []uint32 {
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])
+ v := obj.Vertices[f[0].V].Sub(obj.Vertices[f[1].V])
+ w := obj.Vertices[f[0].V].Sub(obj.Vertices[f[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
+ obj.Faces[i][0].N = i
+ obj.Faces[i][1].N = i
+ obj.Faces[i][2].N = i
}
}