-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathharfang3d.py
171 lines (122 loc) · 4.14 KB
/
harfang3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
import sys
import platform
import asyncio
preload = []
def FS(tree, base=".", silent=False,debug=False):
global preload
path = [base]
last = 0
trail = False
for l in map(str.rstrip, tree.split('\n')):
if not l: continue
if l=='.': continue
pos, elem = l.rsplit(' ', 1)
current = (1+len(pos))//4
if not silent:
print(l[4:])
if current <= last :
preload.append( [ '/'.join(path) , '/'.join(path[1:])] )
if debug: print(preload[-1],'write', current, last)
while len(path)>current:
path.pop()
else:
trail = True
#print(pos, elem, current, path, last )
if len(path)<current+1:
path.append(elem)
path[current] = elem
last = current
if trail:
preload.append( [ '/'.join(path) , '/'.join(path[1:])] )
async def preload_fetch(silent=False,debug=False):
global preload
from pathlib import Path
while len(preload):
url, strfilename = preload.pop(0)
filename = Path(strfilename)
if debug:
print(f"{url} => {Path.cwd() / filename}")
if not filename.is_file():
filename.parent.mkdir(parents=True, exist_ok=True)
async with platform.fopen(url,"rb") as source:
with open(filename,"wb") as target:
target.write(source.read())
if not silent:
print("FS:", filename)
# ../../prebuilt/assetc -api GLES resources resources_gles -platform linux
FS("""
.
└── resources_compiled
├── core
│ └── shader
│ ├── font.fsb
│ ├── font.vsb
│ ├── imgui.fsb
│ ├── imgui_image.fsb
│ ├── imgui_image.vsb
│ └── imgui.vsb
├── font
│ └── default.ttf
└── shaders
├── mdl.fsb
└── mdl.vsb
""", "webgl")
print("===================================")
import harfang as hg
hg.InputInit()
hg.WindowSystemInit()
# initialize ImGui
if 0:
# hg.AddAssetsFolder('resources_compiled')
imgui_prg = hg.LoadProgramFromAssets('resources_compiled/core/shader/imgui')
imgui_img_prg = hg.LoadProgramFromAssets('resources_compiled/core/shader/imgui_image')
embed.webgl()
res_x, res_y = 1024, 600
win = hg.RenderInit("Harfang - Draw Models no Pipeline", res_x, res_y, hg.RT_OpenGLES)
print("===================================")
#hg.ImGuiInit(10, imgui_prg, imgui_img_prg)
# Draw models without a pipeline
async def main():
global preload
#return
print("X"*50)
await preload_fetch(debug=True)
print("X"*50)
# vertex layout and models
vtx_layout = hg.VertexLayoutPosFloatNormUInt8()
cube_mdl = hg.CreateCubeModel(vtx_layout, 1, 1, 1)
ground_mdl = hg.CreatePlaneModel(vtx_layout, 5, 5, 1, 1)
shader = hg.LoadProgramFromFile("resources_compiled/shaders/mdl")
# main loop
angle = 0
while not hg.ReadKeyboard().Key(hg.K_Escape): # and hg.IsWindowOpen(win):
dt = hg.TickClock()
angle = angle + hg.time_to_sec_f(dt)
if 0:
hg.ImGuiBeginFrame(res_x//2, res_y//2, hg.TickClock(), hg.ReadMouse(), hg.ReadKeyboard())
if hg.ImGuiBegin('Window'):
hg.ImGuiText('Hello World!')
hg.ImGuiEnd()
hg.SetView2D(0, 0, 0, res_x//2, res_y//2, -1, 1, hg.CF_Color | hg.CF_Depth, hg.Color.Red, 1, 0)
hg.ImGuiEndFrame(255)
if 1:
viewpoint = hg.TranslationMat4(hg.Vec3(0, 1, -3))
hg.SetViewPerspective(0, 0, 0, res_x, res_y, viewpoint)
hg.DrawModel(
0,
cube_mdl,
shader,
[],
[],
hg.TransformationMat4(hg.Vec3(0, 1, 0), hg.Vec3(angle, angle, angle)),
)
hg.DrawModel(
0, ground_mdl, shader, [], [], hg.TranslationMat4(hg.Vec3(0, 0, 0))
)
hg.Frame()
hg.UpdateWindow(win)
await asyncio.sleep(0)
hg.RenderShutdown()
asyncio.run(main())
# -->