Skip to content

Commit e564988

Browse files
author
HUGE | Raed Atoui
committed
assimp wrapper in go and working model loading
1 parent aeea29d commit e564988

File tree

5 files changed

+86
-58
lines changed

5 files changed

+86
-58
lines changed

sections/modelloading/1.model_loading.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (ml *ModelLoading) Draw() {
6464
// Transformation matrices
6565
projection := mgl32.Perspective(float32(ml.camera.Zoom), float32(utils.WIDTH)/float32(utils.HEIGHT), 0.1, 100.0)
6666
view := ml.camera.GetViewMatrix()
67+
6768
projLoc := gl.GetUniformLocation(ml.shader, gl.Str("projection\x00"))
6869
viewLoc := gl.GetUniformLocation(ml.shader, gl.Str("view\x00"))
6970
gl.UniformMatrix4fv(viewLoc, 1, false, &view[0])
@@ -72,6 +73,7 @@ func (ml *ModelLoading) Draw() {
7273
// Draw the loaded model
7374
model := mgl32.Translate3D(0, -1.75, 0.0) // Translate it down a bit so it's at the center of the scene
7475
model = model.Mul4(mgl32.Scale3D(0.2, 0.2, 0.2)) // It's a bit too big for our scene, so scale it down
76+
7577
modelLoc := gl.GetUniformLocation(ml.shader, gl.Str("model\x00"))
7678
gl.UniformMatrix4fv(modelLoc, 1, false, &model[0])
7779
ml.model.Draw(ml.shader)

tutorial.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ func setup() (*glfw.Window, error) {
165165
func setupSlides() []sections.Slide {
166166
// make a slice of pointers to sketch instances
167167
return []sections.Slide{
168-
new(modelloading.ModelLoading),
169168
new(sections.TitleSlide),
170169
new(getstarted.HelloCube),
171170
new(sections.TitleSlide),
@@ -186,10 +185,14 @@ func setupSlides() []sections.Slide {
186185
new(getstarted.HelloTransformations),
187186
new(getstarted.HelloCoordinates),
188187
new(getstarted.HelloCamera),
188+
189189
new(sections.TitleSlide),
190190
new(lighting.LightingColors),
191191
new(lighting.BasicSpecular),
192+
192193
new(sections.TitleSlide),
194+
new(modelloading.ModelLoading),
195+
193196
new(sections.TitleSlide),
194197
new(sections.TitleSlide),
195198
new(sections.TitleSlide),

utils/consts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package utils
33
const GL_FLOAT32_SIZE = 4
44

55
// WIDTH is the width of the window
6-
const WIDTH = 800
6+
const WIDTH = 1280
77

88
// HEIGHT is the height of the window
9-
const HEIGHT = 600
9+
const HEIGHT = 1024

utils/helpers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package utils
22

33
import (
44
"reflect"
5+
"github.com/go-gl/mathgl/mgl32"
6+
"fmt"
7+
"strconv"
58
)
69

710
// checks if o implements i
@@ -19,3 +22,20 @@ func IsType(o, i interface{}) bool {
1922
b := reflect.ValueOf(i).Type()
2023
return a == b
2124
}
25+
26+
func PrintMat4(m mgl32.Mat4) {
27+
fmt.Printf("%s\n%s\n%s\n%s\n-------\n",
28+
ftos(m[0:4]),
29+
ftos(m[4:8]),
30+
ftos(m[8:12]),
31+
ftos(m[12:16]),
32+
)
33+
}
34+
35+
func ftos(f []float32) string {
36+
out := ""
37+
for i := range f {
38+
out += strconv.FormatFloat(float64(f[i]), 'f', 2, 32) + ", "
39+
}
40+
return out
41+
}

utils/model.go

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,6 @@ import (
1313
"unsafe"
1414
)
1515

16-
type Model struct {
17-
texturesLoaded map[string]Texture
18-
meshes []Mesh
19-
director string
20-
gammaCorrection bool
21-
basePath string
22-
fileName string
23-
}
24-
25-
func NewModel(b, f string, g bool) (Model, error) {
26-
27-
m := Model{
28-
basePath: b,
29-
fileName: f,
30-
gammaCorrection: g,
31-
}
32-
m.texturesLoaded = make(map[string]Texture)
33-
err := m.loadModel()
34-
return m, err
35-
}
36-
func (m *Model) Draw(shader uint32) {
37-
for i := 0; i < len(m.meshes); i++ {
38-
m.meshes[i].Draw(shader)
39-
}
40-
}
41-
4216
type Mesh struct {
4317
id int
4418
vertices []Vertex
@@ -103,7 +77,7 @@ func (m *Mesh) setup() {
10377
gl.BindVertexArray(0)
10478
}
10579

106-
func (m *Mesh) Draw(program uint32) {
80+
func (m *Mesh) draw(program uint32) {
10781
// Bind appropriate textures
10882
var (
10983
diffuseNr uint64
@@ -118,7 +92,8 @@ func (m *Mesh) Draw(program uint32) {
11892
heightNr = 1
11993
i = 0
12094
for i = 0; i < uint32(len(m.textures)); i++ {
121-
gl.ActiveTexture(m.textures[i].Id) // Active proper texture unit before binding
95+
gl.ActiveTexture(gl.TEXTURE0 + i) // Active proper texture unit before binding
96+
12297
// Retrieve texture number (the N in diffuse_textureN)
12398
ss := ""
12499
switch m.textures[i].TextureType {
@@ -170,6 +145,33 @@ type Texture struct {
170145
Path string
171146
}
172147

148+
type Model struct {
149+
texturesLoaded map[string]Texture
150+
meshes []Mesh
151+
director string
152+
gammaCorrection bool
153+
basePath string
154+
fileName string
155+
}
156+
157+
func NewModel(b, f string, g bool) (Model, error) {
158+
159+
m := Model{
160+
basePath: b,
161+
fileName: f,
162+
gammaCorrection: g,
163+
}
164+
m.texturesLoaded = make(map[string]Texture)
165+
err := m.loadModel()
166+
return m, err
167+
}
168+
169+
func (m *Model) Draw(shader uint32) {
170+
for i := 0; i < len(m.meshes); i++ {
171+
m.meshes[i].draw(shader)
172+
}
173+
}
174+
173175
// Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
174176
func (m *Model) loadModel() error {
175177
// Read file via ASSIMP
@@ -178,11 +180,10 @@ func (m *Model) loadModel() error {
178180
assimp.Process_Triangulate|assimp.Process_FlipUVs))
179181

180182
// Check for errors
181-
if scene.Flags()&assimp.SceneFlags_Incomplete != 0 { // if is Not Zero
183+
if scene.Flags() & assimp.SceneFlags_Incomplete != 0 { // if is Not Zero
182184
fmt.Println("ERROR::ASSIMP:: %s\n", scene.Flags())
183185
return errors.New("shit failed")
184186
}
185-
// Retrieve the directory path of the filepath
186187

187188
// Process ASSIMP's root node recursively
188189
m.processNode(scene.RootNode(), scene)
@@ -196,7 +197,6 @@ func (m *Model) processNode(n *assimp.Node, s *assimp.Scene) {
196197
// The node object only contains indices to index the actual objects in the scene.
197198
// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
198199
mesh := s.Meshes()[n.Meshes()[i]]
199-
fmt.Printf("%s %s\n", n.Name() ,mesh.Name())
200200
ms := m.processMesh(mesh, s)
201201
ms.id = i
202202
m.meshes = append(m.meshes, ms)
@@ -224,14 +224,21 @@ func (m *Model) processMeshVertices(mesh *assimp.Mesh) []Vertex {
224224
//t.WriteString(mesh.Name() + "\n")
225225

226226
positions := mesh.Vertices()
227+
227228
normals := mesh.Normals()
229+
useNormals := len(normals) > 0
230+
228231
tex := mesh.TextureCoords(0)
229232
useTex := true
230233
if tex == nil {
231234
useTex = false
232235
}
233-
//tangents := mesh.Tangents()
234-
//bitangents := mesh.Bitangents()
236+
237+
tangents := mesh.Tangents()
238+
useTangents := len(tangents) > 0
239+
240+
bitangents := mesh.Bitangents()
241+
useBitTangents := len(bitangents) > 0
235242

236243
for i := 0; i < mesh.NumVertices(); i++ {
237244
// We declare a placeholder vector since assimp uses its own vector class that
@@ -242,8 +249,10 @@ func (m *Model) processMeshVertices(mesh *assimp.Mesh) []Vertex {
242249
vertex.Position = mgl32.Vec3{positions[i].X(), positions[i].Y(), positions[i].Z()}
243250

244251
// Normals
245-
vertex.Normal = mgl32.Vec3{normals[i].X(), normals[i].Y(), normals[i].Z()}
246-
//n.WriteString(fmt.Sprintf("[%f, %f, %f]\n", tmp[i].X(), tmp[i].Y(), tmp[i].Z()))
252+
if useNormals {
253+
vertex.Normal = mgl32.Vec3{normals[i].X(), normals[i].Y(), normals[i].Z()}
254+
//n.WriteString(fmt.Sprintf("[%f, %f, %f]\n", tmp[i].X(), tmp[i].Y(), tmp[i].Z()))
255+
}
247256

248257
// Texture Coordinates
249258
if useTex {
@@ -255,11 +264,15 @@ func (m *Model) processMeshVertices(mesh *assimp.Mesh) []Vertex {
255264
vertex.TexCoords = mgl32.Vec2{0.0, 0.0}
256265
}
257266

258-
//// Tangent
259-
//vertex.Tangent = mgl32.Vec3{tangents[i].X(), tangents[i].Y(), tangents[i].Z()}
260-
//
261-
//// Bitangent
262-
//vertex.Bitangent = mgl32.Vec3{bitangents[i].X(), bitangents[i].Y(), bitangents[i].Z()}
267+
// Tangent
268+
if useTangents {
269+
vertex.Tangent = mgl32.Vec3{tangents[i].X(), tangents[i].Y(), tangents[i].Z()}
270+
}
271+
272+
// Bitangent
273+
if useBitTangents {
274+
vertex.Bitangent = mgl32.Vec3{bitangents[i].X(), bitangents[i].Y(), bitangents[i].Z()}
275+
}
263276

264277
vertices = append(vertices, vertex)
265278
}
@@ -320,7 +333,6 @@ func (ml *Model) processMesh(m *assimp.Mesh, s *assimp.Scene) Mesh {
320333
}
321334

322335
func (m *Model) loadMaterialTextures(ms *assimp.Material, tm assimp.TextureMapping, tt string) []Texture {
323-
//mat := cScene.Materials()[cScene.Meshes()[0].MaterialIndex()]
324336
textureType := assimp.TextureType(tm)
325337
textureCount := ms.GetMaterialTextureCount(textureType)
326338
result := []Texture{}
@@ -331,7 +343,7 @@ func (m *Model) loadMaterialTextures(ms *assimp.Material, tm assimp.TextureMappi
331343
if val, ok := m.texturesLoaded[filename]; ok {
332344
result = append(result, val)
333345
} else {
334-
texId := textureFromFile(filename)
346+
texId := m.textureFromFile(filename)
335347
texture := Texture{Id: texId, TextureType: tt, Path: file}
336348
result = append(result, texture)
337349
m.texturesLoaded[filename] = texture
@@ -340,19 +352,20 @@ func (m *Model) loadMaterialTextures(ms *assimp.Material, tm assimp.TextureMappi
340352
return result
341353
}
342354

343-
func textureFromFile(f string) uint32 {
355+
func (ml *Model) textureFromFile(f string) uint32 {
344356
//Generate texture ID and load texture data
345357
var textureID uint32
346358

347359
gl.GenTextures(1, &textureID)
348-
var width, height int32
349360
rgba, err := ImageToPixelData(f)
350361
if err != nil {
351362
panic(err)
352363
}
364+
width := int32(rgba.Rect.Size().X)
365+
height := int32(rgba.Rect.Size().Y)
353366
// Assign texture to ID
354367
gl.BindTexture(gl.TEXTURE_2D, textureID)
355-
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGB, width, height, 0, gl.RGB, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))
368+
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))
356369
gl.GenerateMipmap(gl.TEXTURE_2D)
357370

358371
// Parameters
@@ -362,15 +375,5 @@ func textureFromFile(f string) uint32 {
362375
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
363376
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
364377
gl.BindTexture(gl.TEXTURE_2D, 0)
365-
366378
return textureID
367379
}
368-
369-
func pos(slice []assimp.Mesh, value assimp.Mesh) int {
370-
for p, v := range slice {
371-
if v == value {
372-
return p
373-
}
374-
}
375-
return -1
376-
}

0 commit comments

Comments
 (0)