tofu

Making something with OpenGL in Go
Log | Files | Refs

commit 5672e19193c39b893f6149de7e0c31ff63ebe8d6
parent 74f1e1e3c90c4b3d58abd348221d98b543f4f55a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat,  9 Nov 2024 17:52:22 +0900

add Face struct

Diffstat:
Mcmd/sample/main.go | 26+++++++++++++-------------
Mobject.go | 45++++++++++++++++++++++++++-------------------
2 files changed, 39 insertions(+), 32 deletions(-)

diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -137,19 +137,19 @@ var object = tofu.Object{ tofu.Vec2{0.0, 0.0}, tofu.Vec2{0.0, 1.0}, }, - Faces: [][3]uint32{ - {0, 1, 2}, - {0, 2, 3}, - {0, 1, 5}, - {0, 5, 4}, - {1, 2, 6}, - {1, 6, 5}, - {2, 3, 7}, - {2, 7, 6}, - {3, 0, 4}, - {3, 4, 7}, - {4, 5, 6}, - {4, 6, 7}, + Faces: []tofu.Face{ + {V: [3]int{0, 1, 2}}, + {V: [3]int{0, 2, 3}}, + {V: [3]int{0, 1, 5}}, + {V: [3]int{0, 5, 4}}, + {V: [3]int{1, 2, 6}}, + {V: [3]int{1, 6, 5}}, + {V: [3]int{2, 3, 7}}, + {V: [3]int{2, 7, 6}}, + {V: [3]int{3, 0, 4}}, + {V: [3]int{3, 4, 7}}, + {V: [3]int{4, 5, 6}}, + {V: [3]int{4, 6, 7}}, }, } diff --git a/object.go b/object.go @@ -2,6 +2,7 @@ package tofu import ( "image/color" + "log" "github.com/go-gl/gl/v3.3-core/gl" ) @@ -11,37 +12,41 @@ type Object struct { Colors []color.Color TexCoords []Vec2 Normals []Vec3 - Faces [][3]uint32 + Faces []Face vao *VAO } +type Face struct { + V [3]int +} + const objectStride = 11 func (obj Object) data() []float32 { - data := make([]float32, objectStride * len(obj.Vertices)) - for i := 0; i < len(obj.Vertices); i++ { - data[objectStride * i] = obj.Vertices[i][0] - data[objectStride * i + 1] = obj.Vertices[i][1] - data[objectStride * i + 2] = obj.Vertices[i][2] - r, g, b, _ := obj.Colors[i].RGBA() - data[objectStride * i + 3] = float32(r)/0xffff - data[objectStride * i + 4] = float32(g)/0xffff - data[objectStride * i + 5] = float32(b)/0xffff - data[objectStride * i + 6] = obj.TexCoords[i][0] - data[objectStride * i + 7] = obj.TexCoords[i][1] - data[objectStride * i + 8] = obj.Normals[i][0] - data[objectStride * i + 9] = obj.Normals[i][1] - data[objectStride * i + 10] = obj.Normals[i][2] + data := make([]float32, objectStride * 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.V[j]][0] + data[objectStride * (3*i+j) + 7] = obj.TexCoords[f.V[j]][1] + data[objectStride * (3*i+j) + 8] = obj.Normals[f.V[j]][0] + data[objectStride * (3*i+j) + 9] = obj.Normals[f.V[j]][1] + data[objectStride * (3*i+j) + 10] = obj.Normals[f.V[j]][2] + } } return data } func (obj Object) faceData() []uint32 { fdata := make([]uint32, 3 * len(obj.Faces)) - for i, v := range obj.Faces { - fdata[3 * i] = v[0] - fdata[3 * i + 1] = v[1] - fdata[3 * i + 2] = v[2] + for i := 0; i < 3 * len(obj.Faces); i++ { + fdata[i] = uint32(i) } return fdata } @@ -70,12 +75,14 @@ func newVAO(obj *Object) *VAO { vao := &VAO{id: id, vbo: newBuffer(), ebo: newBuffer()} vao.bind() data := obj.data() + log.Println(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) fdata := obj.faceData() + log.Println(fdata) vao.setFaces(fdata) return vao }