-
Notifications
You must be signed in to change notification settings - Fork 11
/
draw_and_create_model_no_pipeline.py
211 lines (166 loc) · 5.64 KB
/
draw_and_create_model_no_pipeline.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Create and draw models without a pipeline and using ModelBuilder
import harfang as hg
hg.InputInit()
hg.WindowSystemInit()
res_x, res_y = 1280, 720
win = hg.RenderInit('Harfang - Draw and Create Models using ModelBuilder - no Pipeline',
res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X)
# vertex layout, materials and models
vtx_layout = hg.VertexLayoutPosFloatNormUInt8()
mdl_builder = hg.ModelBuilder()
# Below are the 6 faces of the cube model, with 4 vertices declared for each face, that are added as 2 triangles to make a complete quad face
# -
vertex0 = hg.Vertex() # Vertex constructor
vertex0.pos = hg.Vec3(-0.5, -0.5, -0.5) # Declaring the vertex position
vertex0.normal = hg.Vec3(0, 0, -1) # Declaring the normal
vertex0.uv0 = hg.Vec2(0, 0) # Declaring the UV
a = mdl_builder.AddVertex(vertex0) # Adding the vertex to the ModelBuilder
vertex1 = hg.Vertex()
vertex1.pos = hg.Vec3(-0.5, 0.5, -0.5)
vertex1.normal = hg.Vec3(0, 0, -1)
vertex1.uv0 = hg.Vec2(0, 1)
b = mdl_builder.AddVertex(vertex1)
vertex2 = hg.Vertex()
vertex2.pos = hg.Vec3(0.5, 0.5, -0.5)
vertex2.normal = hg.Vec3(0, 0, -1)
vertex2.uv0 = hg.Vec2(1, 1)
c = mdl_builder.AddVertex(vertex2)
vertex3 = hg.Vertex()
vertex3.pos = hg.Vec3(0.5, -0.5, -0.5)
vertex3.normal = hg.Vec3(0, 0, -1)
vertex3.uv0 = hg.Vec2(1, 0)
d = mdl_builder.AddVertex(vertex3)
mdl_builder.AddTriangle(d, c, b) # Adding the first triangle of the current face
mdl_builder.AddTriangle(b, a, d) # Second triangle
# +
vertex0 = hg.Vertex()
vertex0.pos = hg.Vec3(-0.5, -0.5, 0.5)
vertex0.normal = hg.Vec3(0, 0, 1)
vertex0.uv0 = hg.Vec2(0, 0)
a = mdl_builder.AddVertex(vertex0)
vertex1 = hg.Vertex()
vertex1.pos = hg.Vec3(-0.5,0.5, 0.5)
vertex1.normal = hg.Vec3(0, 0, 1)
vertex1.uv0 = hg.Vec2(0, 1)
b = mdl_builder.AddVertex(vertex1)
vertex2 = hg.Vertex()
vertex2.pos = hg.Vec3(0.5, 0.5, 0.5)
vertex2.normal = hg.Vec3(0, 0, 1)
vertex2.uv0 = hg.Vec2(1, 1)
c = mdl_builder.AddVertex(vertex2)
vertex3 = hg.Vertex()
vertex3.pos = hg.Vec3(0.5, -0.5, 0.5)
vertex3.normal = hg.Vec3(0, 0, 1)
vertex3.uv0 = hg.Vec2(1, 0)
d = mdl_builder.AddVertex(vertex3)
mdl_builder.AddTriangle(a, b, c)
mdl_builder.AddTriangle(a, c, d)
# -
vertex0 = hg.Vertex()
vertex0.pos = hg.Vec3(-0.5, -0.5, -0.5)
vertex0.normal = hg.Vec3(0, -1, 0)
vertex0.uv0 = hg.Vec2(0, 0)
a = mdl_builder.AddVertex(vertex0)
vertex1 = hg.Vertex()
vertex1.pos = hg.Vec3(-0.5, -0.5, 0.5)
vertex1.normal = hg.Vec3(0, -1, 0)
vertex1.uv0 = hg.Vec2(0, 1)
b = mdl_builder.AddVertex(vertex1)
vertex2 = hg.Vertex()
vertex2.pos = hg.Vec3(0.5, -0.5, 0.5)
vertex2.normal = hg.Vec3(0, -1, 0)
vertex2.uv0 = hg.Vec2(1, 1)
c = mdl_builder.AddVertex(vertex2)
vertex3 = hg.Vertex()
vertex3.pos = hg.Vec3(0.5, -0.5, -0.5)
vertex3.normal = hg.Vec3(0, -1, 0)
vertex3.uv0 = hg.Vec2(1, 0)
d = mdl_builder.AddVertex(vertex3)
mdl_builder.AddTriangle(a, b, c)
mdl_builder.AddTriangle(a, c, d)
# +
vertex0 = hg.Vertex()
vertex0.pos = hg.Vec3(-0.5, 0.5, -0.5)
vertex0.normal = hg.Vec3(0, 1, 0)
vertex0.uv0 = hg.Vec2(0, 0)
a = mdl_builder.AddVertex(vertex0)
vertex1 = hg.Vertex()
vertex1.pos = hg.Vec3(-0.5, 0.5, 0.5)
vertex1.normal = hg.Vec3(0, 1, 0)
vertex1.uv0 = hg.Vec2(0, 1)
b = mdl_builder.AddVertex(vertex1)
vertex2 = hg.Vertex()
vertex2.pos = hg.Vec3(0.5, 0.5, 0.5)
vertex2.normal = hg.Vec3(0, 1, 0)
vertex2.uv0 = hg.Vec2(1, 1)
c = mdl_builder.AddVertex(vertex2)
vertex3 = hg.Vertex()
vertex3.pos = hg.Vec3(0.5, 0.5, -0.5)
vertex3.normal = hg.Vec3(0, 1, 0)
vertex3.uv0 = hg.Vec2(1, 0)
d = mdl_builder.AddVertex(vertex3)
mdl_builder.AddTriangle(d, c, b)
mdl_builder.AddTriangle(b, a, d)
# -
vertex0 = hg.Vertex()
vertex0.pos = hg.Vec3(-0.5, -0.5, -0.5)
vertex0.normal = hg.Vec3(-1, 0, 0)
vertex0.uv0 = hg.Vec2(0, 0)
a = mdl_builder.AddVertex(vertex0)
vertex1 = hg.Vertex()
vertex1.pos = hg.Vec3(-0.5, -0.5, 0.5)
vertex1.normal = hg.Vec3(-1, 0, 0)
vertex1.uv0 = hg.Vec2(0, 1)
b = mdl_builder.AddVertex(vertex1)
vertex2 = hg.Vertex()
vertex2.pos = hg.Vec3(-0.5, 0.5, 0.5)
vertex2.normal = hg.Vec3(-1, 0, 0)
vertex2.uv0 = hg.Vec2(1, 1)
c = mdl_builder.AddVertex(vertex2)
vertex3 = hg.Vertex()
vertex3.pos = hg.Vec3(-0.5, 0.5, -0.5)
vertex3.normal = hg.Vec3(-1, 0, 0)
vertex3.uv0 = hg.Vec2(1, 0)
d = mdl_builder.AddVertex(vertex3)
mdl_builder.AddTriangle(d, c, b)
mdl_builder.AddTriangle(b, a, d)
# +
vertex0 = hg.Vertex()
vertex0.pos = hg.Vec3(0.5, -0.5, -0.5)
vertex0.normal = hg.Vec3(1, 0, 0)
vertex0.uv0 = hg.Vec2(0, 0)
a = mdl_builder.AddVertex(vertex0)
vertex1 = hg.Vertex()
vertex1.pos = hg.Vec3(0.5, -0.5, 0.5)
vertex1.normal = hg.Vec3(1, 0, 0)
vertex1.uv0 = hg.Vec2(0, 1)
b = mdl_builder.AddVertex(vertex1)
vertex2 = hg.Vertex()
vertex2.pos = hg.Vec3(0.5, 0.5, 0.5)
vertex2.normal = hg.Vec3(1, 0, 0)
vertex2.uv0 = hg.Vec2(1, 1)
c = mdl_builder.AddVertex(vertex2)
vertex3 = hg.Vertex()
vertex3.pos = hg.Vec3(0.5, 0.5, -0.5)
vertex3.normal = hg.Vec3(1, 0, 0)
vertex3.uv0 = hg.Vec2(1, 0)
d = mdl_builder.AddVertex(vertex3)
mdl_builder.AddTriangle(a, b, c)
mdl_builder.AddTriangle(a, c, d)
cube_mdl = mdl_builder.MakeModel(vtx_layout) # Create the actual cube model
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)
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)
hg.RenderShutdown()