tofu

Making something with OpenGL in Go
Log | Files | Refs

commit a8b156fbd2bca8873e8b09d678ba134a0cffc4bb
parent c1541bec81d065f1e5bb576a675cda2fdd121a4f
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue, 12 Nov 2024 07:30:52 +0900

remove gl dependency from cmd/sample

Diffstat:
Mcmd/sample/main.go | 6++----
Mobject.go | 44+++++++++++++++++++++++++-------------------
2 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -8,8 +8,6 @@ import ( "runtime" "time" - "github.com/go-gl/gl/v3.3-core/gl" - "git.mtkn.jp/tofu" ) @@ -174,13 +172,13 @@ func (app *App) Update() error { app.program.SetMat4("model", model) app.program.SetVec3("objCol", app.objCol) - gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, nil) + object.Draw() app.lightProgram.Use() app.lightProgram.SetMat4("view", view) app.lightProgram.SetMat4("projection", projection) app.lightProgram.SetMat4("model", lightModel) app.lightProgram.SetVec3("lightCol", app.lightCol) - gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, nil) + object.Draw() return nil } diff --git a/object.go b/object.go @@ -23,29 +23,29 @@ type Face struct { const objectStride = 11 func (obj Object) data() []float32 { - data := make([]float32, objectStride * 3 * len(obj.Faces)) + 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.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[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.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] } } return data } func (obj Object) faceData() []uint32 { - fdata := make([]uint32, 3 * len(obj.Faces)) - for i := 0; i < 3 * len(obj.Faces); i++ { + fdata := make([]uint32, 3*len(obj.Faces)) + for i := 0; i < 3*len(obj.Faces); i++ { fdata[i] = uint32(i) } return fdata @@ -56,8 +56,14 @@ func (obj *Object) Load() { obj.vao = newVAO(obj) } +func (obj *Object) Draw() { + gl.DrawElements(gl.TRIANGLES, + int32(objectStride*3*len(obj.Faces)), + gl.UNSIGNED_INT, nil) +} + type VAO struct { - id uint32 + id uint32 vbo *Buffer ebo *Buffer } @@ -91,19 +97,19 @@ func (vao *VAO) bind() { func (vao *VAO) setAttribute(uniformNum uint32, size int, stride int, offset int) { gl.VertexAttribPointerWithOffset(uniformNum, int32(size), gl.FLOAT, - false, int32(stride) * 4, uintptr(offset) * 4) + false, int32(stride)*4, uintptr(offset)*4) gl.EnableVertexAttribArray(uniformNum) } func (vao *VAO) setData(data []float32) { gl.BindBuffer(gl.ARRAY_BUFFER, vao.vbo.id) - gl.BufferData(gl.ARRAY_BUFFER, len(data) * 4, + gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, gl.Ptr(data), gl.STATIC_DRAW) } func (vao *VAO) setFaces(data []uint32) { gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, vao.ebo.id) - gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(data) * 4, + gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(data)*4, gl.Ptr(data), gl.STATIC_DRAW) }