@@ -13,32 +13,6 @@ import (
13
13
"unsafe"
14
14
)
15
15
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
-
42
16
type Mesh struct {
43
17
id int
44
18
vertices []Vertex
@@ -103,7 +77,7 @@ func (m *Mesh) setup() {
103
77
gl .BindVertexArray (0 )
104
78
}
105
79
106
- func (m * Mesh ) Draw (program uint32 ) {
80
+ func (m * Mesh ) draw (program uint32 ) {
107
81
// Bind appropriate textures
108
82
var (
109
83
diffuseNr uint64
@@ -118,7 +92,8 @@ func (m *Mesh) Draw(program uint32) {
118
92
heightNr = 1
119
93
i = 0
120
94
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
+
122
97
// Retrieve texture number (the N in diffuse_textureN)
123
98
ss := ""
124
99
switch m .textures [i ].TextureType {
@@ -170,6 +145,33 @@ type Texture struct {
170
145
Path string
171
146
}
172
147
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
+
173
175
// Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
174
176
func (m * Model ) loadModel () error {
175
177
// Read file via ASSIMP
@@ -178,11 +180,10 @@ func (m *Model) loadModel() error {
178
180
assimp .Process_Triangulate | assimp .Process_FlipUVs ))
179
181
180
182
// 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
182
184
fmt .Println ("ERROR::ASSIMP:: %s\n " , scene .Flags ())
183
185
return errors .New ("shit failed" )
184
186
}
185
- // Retrieve the directory path of the filepath
186
187
187
188
// Process ASSIMP's root node recursively
188
189
m .processNode (scene .RootNode (), scene )
@@ -196,7 +197,6 @@ func (m *Model) processNode(n *assimp.Node, s *assimp.Scene) {
196
197
// The node object only contains indices to index the actual objects in the scene.
197
198
// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
198
199
mesh := s .Meshes ()[n .Meshes ()[i ]]
199
- fmt .Printf ("%s %s\n " , n .Name () ,mesh .Name ())
200
200
ms := m .processMesh (mesh , s )
201
201
ms .id = i
202
202
m .meshes = append (m .meshes , ms )
@@ -224,14 +224,21 @@ func (m *Model) processMeshVertices(mesh *assimp.Mesh) []Vertex {
224
224
//t.WriteString(mesh.Name() + "\n")
225
225
226
226
positions := mesh .Vertices ()
227
+
227
228
normals := mesh .Normals ()
229
+ useNormals := len (normals ) > 0
230
+
228
231
tex := mesh .TextureCoords (0 )
229
232
useTex := true
230
233
if tex == nil {
231
234
useTex = false
232
235
}
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
235
242
236
243
for i := 0 ; i < mesh .NumVertices (); i ++ {
237
244
// 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 {
242
249
vertex .Position = mgl32.Vec3 {positions [i ].X (), positions [i ].Y (), positions [i ].Z ()}
243
250
244
251
// 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
+ }
247
256
248
257
// Texture Coordinates
249
258
if useTex {
@@ -255,11 +264,15 @@ func (m *Model) processMeshVertices(mesh *assimp.Mesh) []Vertex {
255
264
vertex .TexCoords = mgl32.Vec2 {0.0 , 0.0 }
256
265
}
257
266
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
+ }
263
276
264
277
vertices = append (vertices , vertex )
265
278
}
@@ -320,7 +333,6 @@ func (ml *Model) processMesh(m *assimp.Mesh, s *assimp.Scene) Mesh {
320
333
}
321
334
322
335
func (m * Model ) loadMaterialTextures (ms * assimp.Material , tm assimp.TextureMapping , tt string ) []Texture {
323
- //mat := cScene.Materials()[cScene.Meshes()[0].MaterialIndex()]
324
336
textureType := assimp .TextureType (tm )
325
337
textureCount := ms .GetMaterialTextureCount (textureType )
326
338
result := []Texture {}
@@ -331,7 +343,7 @@ func (m *Model) loadMaterialTextures(ms *assimp.Material, tm assimp.TextureMappi
331
343
if val , ok := m .texturesLoaded [filename ]; ok {
332
344
result = append (result , val )
333
345
} else {
334
- texId := textureFromFile (filename )
346
+ texId := m . textureFromFile (filename )
335
347
texture := Texture {Id : texId , TextureType : tt , Path : file }
336
348
result = append (result , texture )
337
349
m .texturesLoaded [filename ] = texture
@@ -340,19 +352,20 @@ func (m *Model) loadMaterialTextures(ms *assimp.Material, tm assimp.TextureMappi
340
352
return result
341
353
}
342
354
343
- func textureFromFile (f string ) uint32 {
355
+ func ( ml * Model ) textureFromFile (f string ) uint32 {
344
356
//Generate texture ID and load texture data
345
357
var textureID uint32
346
358
347
359
gl .GenTextures (1 , & textureID )
348
- var width , height int32
349
360
rgba , err := ImageToPixelData (f )
350
361
if err != nil {
351
362
panic (err )
352
363
}
364
+ width := int32 (rgba .Rect .Size ().X )
365
+ height := int32 (rgba .Rect .Size ().Y )
353
366
// Assign texture to ID
354
367
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 ))
356
369
gl .GenerateMipmap (gl .TEXTURE_2D )
357
370
358
371
// Parameters
@@ -362,15 +375,5 @@ func textureFromFile(f string) uint32 {
362
375
gl .TexParameteri (gl .TEXTURE_2D , gl .TEXTURE_MIN_FILTER , gl .LINEAR_MIPMAP_LINEAR )
363
376
gl .TexParameteri (gl .TEXTURE_2D , gl .TEXTURE_MAG_FILTER , gl .LINEAR )
364
377
gl .BindTexture (gl .TEXTURE_2D , 0 )
365
-
366
378
return textureID
367
379
}
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