Skip to content

Commit 1a6bd16

Browse files
committed
It's alive! It's bad, but it's alive!
1 parent 8e3b4d6 commit 1a6bd16

32 files changed

+875
-288
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# axe-go
22
Axe Game Engine for Go
33

4+
### Todo
5+
- [ ] OBJ loader
6+
47
### Use
58
- Load preferences
69
- Initialize systems (audio, graphics, input, window)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ require (
99
)
1010

1111
require github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6
12+
13+
require github.com/udhos/gwob v0.0.0-20200524213453-619810f75817 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOY
44
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
55
github.com/sparkkoori/go-vulkan v0.0.0-20190815142752-7cafa944f8f2 h1:X/+cw8BWv95cDwT/ON5IWYSaatQfdCEBI1hKfApLOUg=
66
github.com/sparkkoori/go-vulkan v0.0.0-20190815142752-7cafa944f8f2/go.mod h1:dFKNCFrbk5+XCiRYZAPBFRmV++R/OJ93a8Jdg0gzErw=
7+
github.com/udhos/gwob v0.0.0-20200524213453-619810f75817 h1:4M105Yb9NHJ/sI3xAS3WbXt4d09q940504X5zE5JM7s=
8+
github.com/udhos/gwob v0.0.0-20200524213453-619810f75817/go.mod h1:kOhibXY50yGPKNcoFg+KDoC4hGzyJ6YX0wfKHhThntk=
79
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
810
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=

pkg/asset-json.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"regexp"
77
)
88

9-
type JsonGenericAssetLoader struct{}
9+
type JsonGenericAssetFormat struct{}
1010
type JsonValueKind int
1111

1212
const (
@@ -28,18 +28,18 @@ type JsonValue struct {
2828
Parent *JsonValue
2929
}
3030

31-
var _ AssetFormat = &JsonGenericAssetLoader{}
32-
var jsonGenericAssetLoaderRegex, _ = regexp.Compile(`\.json$`)
31+
var _ AssetFormat = &JsonGenericAssetFormat{}
32+
var jsonGenericAssetFormatRegex, _ = regexp.Compile(`\.json$`)
3333

34-
func (loader *JsonGenericAssetLoader) Handles(ref AssetRef) bool {
35-
return jsonGenericAssetLoaderRegex.MatchString(ref.URI)
34+
func (format *JsonGenericAssetFormat) Handles(ref AssetRef) bool {
35+
return jsonGenericAssetFormatRegex.MatchString(ref.URI)
3636
}
3737

38-
func (loader *JsonGenericAssetLoader) Types() []AssetType {
38+
func (format *JsonGenericAssetFormat) Types() []AssetType {
3939
return []AssetType{AssetTypeJson}
4040
}
4141

42-
func (loader *JsonGenericAssetLoader) Load(asset *Asset) error {
42+
func (format *JsonGenericAssetFormat) Load(asset *Asset) error {
4343
decoder := json.NewDecoder(asset.SourceReader)
4444

4545
var setValue func(out *JsonValue) error
@@ -136,15 +136,15 @@ func (loader *JsonGenericAssetLoader) Load(asset *Asset) error {
136136

137137
return err
138138
}
139-
func (loader *JsonGenericAssetLoader) Unload(asset *Asset) error {
139+
func (format *JsonGenericAssetFormat) Unload(asset *Asset) error {
140140
asset.LoadStatus.Reset()
141141
return nil
142142
}
143-
func (loader *JsonGenericAssetLoader) Activate(asset *Asset) error {
143+
func (format *JsonGenericAssetFormat) Activate(asset *Asset) error {
144144
asset.ActivateStatus.Success()
145145
return nil
146146
}
147-
func (loader *JsonGenericAssetLoader) Deactivate(asset *Asset) error {
147+
func (format *JsonGenericAssetFormat) Deactivate(asset *Asset) error {
148148
asset.ActivateStatus.Reset()
149149
return nil
150150
}

pkg/asset-mtl.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package axe
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/udhos/gwob"
7+
)
8+
9+
var _ AssetFormat = &MtlFormat{}
10+
var mtlFormatRegex, _ = regexp.Compile(`\.mtl$`)
11+
12+
type MtlFormat struct {
13+
}
14+
15+
func (format *MtlFormat) Handles(ref AssetRef) bool {
16+
return mtlFormatRegex.MatchString(ref.URI)
17+
}
18+
19+
func (format *MtlFormat) Types() []AssetType {
20+
return []AssetType{AssetTypeModel}
21+
}
22+
23+
func (format *MtlFormat) Load(asset *Asset) error {
24+
lib, err := gwob.ReadMaterialLibFromReader(asset.SourceReader, &gwob.ObjParserOptions{})
25+
26+
if err != nil {
27+
asset.LoadStatus.Fail(err)
28+
return err
29+
}
30+
31+
materials := Materials{}
32+
asset.Dependent = make([]AssetRef, 0)
33+
34+
for materialName, material := range lib.Lib {
35+
mat := Material{
36+
Name: material.Name,
37+
Illum: material.Illum,
38+
Opacity: material.D,
39+
Refraction: material.Ni,
40+
Shininess: material.Ns,
41+
Ambient: TextureColor{
42+
Texture: material.MapKa,
43+
Color: material.Ka,
44+
},
45+
Diffuse: TextureColor{
46+
Texture: material.MapKd,
47+
Color: material.Kd,
48+
},
49+
Specular: TextureColor{
50+
Texture: material.MapKs,
51+
Color: material.Ks,
52+
},
53+
Emissive: TextureColor{
54+
Texture: material.MapKe,
55+
},
56+
TextureBump: material.Bump,
57+
}
58+
59+
if mat.Ambient.Texture != "" {
60+
asset.AddNext(mat.Ambient.Texture, true)
61+
}
62+
if mat.Specular.Texture != "" {
63+
asset.AddNext(mat.Specular.Texture, true)
64+
}
65+
if mat.Diffuse.Texture != "" {
66+
asset.AddNext(mat.Diffuse.Texture, true)
67+
}
68+
if mat.Emissive.Texture != "" {
69+
asset.AddNext(mat.Emissive.Texture, true)
70+
}
71+
if mat.TextureBump != "" {
72+
asset.AddNext(mat.TextureBump, true)
73+
}
74+
75+
materials[materialName] = mat
76+
}
77+
78+
asset.Data = materials
79+
asset.LoadStatus.Success()
80+
81+
return nil
82+
}
83+
84+
func (format *MtlFormat) Unload(asset *Asset) error {
85+
asset.LoadStatus.Reset()
86+
return nil
87+
}
88+
func (format *MtlFormat) Activate(asset *Asset) error {
89+
asset.ActivateStatus.Success()
90+
return nil
91+
}
92+
func (format *MtlFormat) Deactivate(asset *Asset) error {
93+
asset.ActivateStatus.Reset()
94+
return nil
95+
}

pkg/asset-obj.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package axe
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/udhos/gwob"
7+
)
8+
9+
var _ AssetFormat = &ObjFormat{}
10+
var objFormatRegex, _ = regexp.Compile(`\.obj$`)
11+
12+
type ObjFormat struct {
13+
}
14+
15+
func (format *ObjFormat) Handles(ref AssetRef) bool {
16+
return objFormatRegex.MatchString(ref.URI)
17+
}
18+
19+
func (format *ObjFormat) Types() []AssetType {
20+
return []AssetType{AssetTypeModel}
21+
}
22+
23+
func (format *ObjFormat) Load(asset *Asset) error {
24+
obj, err := gwob.NewObjFromReader(asset.Ref.Name, asset.SourceReader, nil)
25+
if err != nil {
26+
asset.LoadStatus.Fail(err)
27+
return err
28+
}
29+
30+
strideElements := obj.StrideSize >> 2
31+
offsetVertex := obj.StrideOffsetPosition >> 2
32+
offsetNormal := obj.StrideOffsetNormal >> 2
33+
offsetTexCoord := obj.StrideOffsetTexture >> 2
34+
35+
countVertex := obj.NumberOfElements()
36+
countNormals := 0
37+
if obj.NormCoordFound {
38+
countNormals = countVertex
39+
}
40+
countTexCoords := 0
41+
if obj.TextCoordFound {
42+
countTexCoords = countVertex
43+
}
44+
45+
mesh := MeshData{
46+
Name: asset.Ref.Name,
47+
Vertices: make([][3]float32, countVertex),
48+
Normals: make([][3]float32, countNormals),
49+
Uvs: make([][2]float32, countTexCoords),
50+
Groups: make([]MeshFaceGroup, len(obj.Groups)),
51+
Materials: obj.Mtllib,
52+
}
53+
54+
for i := range mesh.Vertices {
55+
off := i * strideElements
56+
offVertex := off + offsetVertex
57+
58+
mesh.Vertices[i] = [3]float32{
59+
obj.Coord[offVertex+0],
60+
obj.Coord[offVertex+1],
61+
obj.Coord[offVertex+2],
62+
}
63+
64+
if obj.NormCoordFound {
65+
offNormal := off + offsetNormal
66+
67+
mesh.Normals[i] = [3]float32{
68+
obj.Coord[offNormal+0],
69+
obj.Coord[offNormal+1],
70+
obj.Coord[offNormal+2],
71+
}
72+
}
73+
74+
if obj.TextCoordFound {
75+
offTexCoord := off + offsetTexCoord
76+
77+
mesh.Uvs[i] = [2]float32{
78+
obj.Coord[offTexCoord+0],
79+
obj.Coord[offTexCoord+1],
80+
}
81+
}
82+
}
83+
84+
for _, group := range obj.Groups {
85+
meshGroup := MeshFaceGroup{
86+
Material: group.Usemtl,
87+
Faces: make([]Face, 0),
88+
}
89+
90+
s := group.IndexBegin
91+
e := s + group.IndexCount
92+
for i := s; i < e; i += 3 {
93+
a := obj.Indices[i]
94+
b := obj.Indices[i+1]
95+
c := obj.Indices[i+2]
96+
97+
face := Face{
98+
Vertices: []int{a, b, c},
99+
}
100+
if obj.NormCoordFound {
101+
face.Normals = []int{a, b, c}
102+
}
103+
if obj.TextCoordFound {
104+
face.Uvs = []int{a, b, c}
105+
}
106+
107+
meshGroup.Faces = append(meshGroup.Faces, face)
108+
}
109+
mesh.Groups = append(mesh.Groups, meshGroup)
110+
}
111+
112+
asset.Data = mesh
113+
asset.LoadStatus.Success()
114+
asset.AddNext(obj.Mtllib, true)
115+
116+
return nil
117+
}
118+
119+
func (format *ObjFormat) Unload(asset *Asset) error {
120+
asset.LoadStatus.Reset()
121+
return nil
122+
}
123+
func (format *ObjFormat) Activate(asset *Asset) error {
124+
asset.ActivateStatus.Success()
125+
return nil
126+
}
127+
func (format *ObjFormat) Deactivate(asset *Asset) error {
128+
asset.ActivateStatus.Reset()
129+
return nil
130+
}

pkg/asset-sources.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package axe
2+
3+
import (
4+
"io"
5+
"net/http"
6+
"os"
7+
"regexp"
8+
)
9+
10+
var relativeRegex, _ = regexp.Compile(`[^/]+$`)
11+
var previousRegex, _ = regexp.Compile(`[^/]+/\.\./`)
12+
13+
type LocalAssetSource struct{}
14+
15+
var _ AssetSource = &LocalAssetSource{}
16+
var localAssetSourceRegex, _ = regexp.Compile(`^(/|\./|[a-zA-Z]:)`)
17+
18+
func (local *LocalAssetSource) Handles(ref AssetRef) bool {
19+
return localAssetSourceRegex.MatchString(ref.URI)
20+
}
21+
func (local *LocalAssetSource) Read(ref AssetRef) (io.Reader, error) {
22+
return os.Open(ref.URI)
23+
}
24+
func (local *LocalAssetSource) Relative(uri string, relative string) string {
25+
uri = relativeRegex.ReplaceAllString(uri, relative)
26+
uri = previousRegex.ReplaceAllString(uri, "")
27+
return uri
28+
}
29+
30+
type WebAssetSource struct{}
31+
32+
var _ AssetSource = &WebAssetSource{}
33+
var webAssetSourceRegex, _ = regexp.Compile("^https?:")
34+
35+
func (local *WebAssetSource) Handles(ref AssetRef) bool {
36+
return webAssetSourceRegex.MatchString(ref.URI)
37+
}
38+
func (local *WebAssetSource) Read(ref AssetRef) (io.Reader, error) {
39+
client := http.Client{
40+
CheckRedirect: func(r *http.Request, via []*http.Request) error {
41+
r.URL.Opaque = r.URL.Path
42+
return nil
43+
},
44+
}
45+
resp, err := client.Get(ref.URI)
46+
return resp.Body, err
47+
}
48+
func (local *WebAssetSource) Relative(uri string, relative string) string {
49+
uri = relativeRegex.ReplaceAllString(uri, relative)
50+
uri = previousRegex.ReplaceAllString(uri, "")
51+
return uri
52+
}

0 commit comments

Comments
 (0)