commit 1639d2e361c3fe53a8fb67a7172bb4c1bc01a144
parent bca297ef3f698443307d97629abf6b0bd2b1f88c
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 16 Nov 2024 09:58:44 +0900
modify Faces
Diffstat:
3 files changed, 77 insertions(+), 36 deletions(-)
diff --git a/cmd/mandelbrot/main.go b/cmd/mandelbrot/main.go
@@ -123,7 +123,11 @@ var object = tofu.Object{
{0, 0},
},
Faces: []tofu.Face{
- {V: [3]int{0, 1, 2}},
- {V: [3]int{0, 2, 3}},
+ {[3]tofu.VertexIndex{{V: 0, N: tofu.NoIndex, T: tofu.NoIndex},
+ {V: 1, N: tofu.NoIndex, T: tofu.NoIndex},
+ {V: 2, N: tofu.NoIndex, T: tofu.NoIndex}}},
+ {[3]tofu.VertexIndex{{V: 0, N: tofu.NoIndex, T: tofu.NoIndex},
+ {V: 2, N: tofu.NoIndex, T: tofu.NoIndex},
+ {V: 3, N: tofu.NoIndex, T: tofu.NoIndex}}},
},
}
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -112,18 +112,19 @@ var object = tofu.Object{
tofu.Vec2{0.0, 1.0},
},
Faces: []tofu.Face{
- {V: [3]int{0, 1, 2}, N: [3]int{0, 0, 0}, T: [3]int{0, 1, 2}},
- {V: [3]int{0, 2, 3}, N: [3]int{0, 0, 0}, T: [3]int{0, 2, 3}},
- {V: [3]int{0, 1, 5}, N: [3]int{1, 1, 1}, T: [3]int{0, 1, 2}},
- {V: [3]int{0, 5, 4}, N: [3]int{1, 1, 1}, T: [3]int{0, 2, 3}},
- {V: [3]int{1, 2, 6}, N: [3]int{2, 2, 2}, T: [3]int{0, 1, 2}},
- {V: [3]int{1, 6, 5}, N: [3]int{2, 2, 2}, T: [3]int{0, 2, 3}},
- {V: [3]int{2, 3, 7}, N: [3]int{3, 3, 3}, T: [3]int{0, 1, 2}},
- {V: [3]int{2, 7, 6}, N: [3]int{3, 3, 3}, T: [3]int{0, 2, 3}},
- {V: [3]int{3, 0, 4}, N: [3]int{4, 4, 4}, T: [3]int{0, 1, 2}},
- {V: [3]int{3, 4, 7}, N: [3]int{4, 4, 4}, T: [3]int{0, 2, 3}},
- {V: [3]int{4, 5, 6}, N: [3]int{5, 5, 5}, T: [3]int{0, 1, 2}},
- {V: [3]int{4, 6, 7}, N: [3]int{5, 5, 5}, T: [3]int{0, 2, 3}},
+ {[3]tofu.VertexIndex{{0, 0, 0}, {1, 0, 0}, {2, 0, 2}}},
+ {[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}}},
},
}
diff --git a/object.go b/object.go
@@ -2,6 +2,7 @@ package tofu
import (
"image/color"
+ "math"
"github.com/go-gl/gl/v3.3-core/gl"
)
@@ -15,30 +16,56 @@ type Object struct {
vao *VAO
}
+var NoIndex = math.MinInt
+
+type VertexIndex struct {
+ V, N, T int
+}
+
type Face struct {
- V [3]int
- N [3]int
- T [3]int
+ I [3]VertexIndex
}
-const objectStride = 11
+func (obj Object) stride() int {
+ if len(obj.Faces) < 1 {
+ return 0
+ }
+ var stride int = 3
+ if obj.Faces[0].I[0].N != NoIndex {
+ stride += 3
+ }
+ if obj.Faces[0].I[0].T != NoIndex {
+ stride += 2
+ }
+ return stride
+}
func (obj Object) data() []float32 {
- data := make([]float32, objectStride*3*len(obj.Faces))
+ stride := obj.stride()
+ if stride == 0 {
+ return nil
+ }
+ data := make([]float32, stride*3*len(obj.Faces))
for i, f := range obj.Faces {
for j := 0; j < 3; j++ {
- data[objectStride*(3*i+j)] = obj.Vertices[f.V[j]][0]
- data[objectStride*(3*i+j)+1] = obj.Vertices[f.V[j]][1]
- data[objectStride*(3*i+j)+2] = obj.Vertices[f.V[j]][2]
- r, g, b, _ := obj.Colors[f.V[j]].RGBA()
- data[objectStride*(3*i+j)+3] = float32(r) / 0xffff
- data[objectStride*(3*i+j)+4] = float32(g) / 0xffff
- data[objectStride*(3*i+j)+5] = float32(b) / 0xffff
- data[objectStride*(3*i+j)+6] = obj.TexCoords[f.T[j]][0]
- data[objectStride*(3*i+j)+7] = obj.TexCoords[f.T[j]][1]
- data[objectStride*(3*i+j)+8] = obj.Normals[f.N[j]][0]
- data[objectStride*(3*i+j)+9] = obj.Normals[f.N[j]][1]
- data[objectStride*(3*i+j)+10] = obj.Normals[f.N[j]][2]
+ 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]
+ 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]
+ 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]
+ 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]
+ }
}
}
return data
@@ -59,7 +86,7 @@ func (obj *Object) Load() {
func (obj *Object) Draw() {
gl.DrawElements(gl.TRIANGLES,
- int32(objectStride*3*len(obj.Faces)),
+ int32(obj.stride()*3*len(obj.Faces)),
gl.UNSIGNED_INT, nil)
}
@@ -77,16 +104,25 @@ const (
)
func newVAO(obj *Object) *VAO {
+ stride := obj.stride()
+ if stride == 0 {
+ return nil
+ }
var id uint32
gl.GenVertexArrays(1, &id)
vao := &VAO{id: id, vbo: newBuffer(), ebo: newBuffer()}
vao.bind()
data := obj.data()
vao.setData(data)
- vao.setAttribute(UniformVertex, 3, objectStride, 0)
- vao.setAttribute(UniformColor, 3, objectStride, 3)
- vao.setAttribute(UniformTexCoords, 2, objectStride, 6)
- vao.setAttribute(UniformNormal, 3, objectStride, 8)
+ vao.setAttribute(UniformVertex, 3, stride, 0)
+ if stride == 5 {
+ vao.setAttribute(UniformTexCoords, 2, stride, 3)
+ } else if stride == 6 {
+ vao.setAttribute(UniformNormal, 3, stride, 3)
+ } else if stride == 8 {
+ vao.setAttribute(UniformTexCoords, 2, stride, 3)
+ vao.setAttribute(UniformNormal, 3, stride, 5)
+ }
fdata := obj.faceData()
vao.setFaces(fdata)
return vao