-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl.bas
More file actions
executable file
·636 lines (512 loc) · 22.6 KB
/
gl.bas
File metadata and controls
executable file
·636 lines (512 loc) · 22.6 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
Attribute VB_Name = "basGL"
Option Explicit
Public Type GLVertex
x As Single
y As Single
z As Single
End Type
Public Type GLVTexture
u As Single
V As Single
w As Single
End Type
Public Type GLMaterial
Ambient(4) As Single
Diffuse(4) As Single
Specular(4) As Single
Emissive(4) As Single
SpecularExp As Single
Name As String
End Type
Public Type GLFace
VertexIdx() As Integer
VertexIdxSz As Integer
VNormalIdx() As Integer
VNormalIdxSz As Integer
VTextureIdx() As Integer
VTextureIdxSz As Integer
MaterialIdx As Integer
End Type
Public Type GLObj
Vertices() As GLVertex
VerticesSz As Integer
VNormals() As GLVertex
VNormalsSz As Integer
VTextures() As GLVTexture
VTexturesSz As Integer
Faces() As GLFace
FacesSz As Integer
Materials() As GLMaterial
MaterialsSz As Integer
End Type
Public Type PIXELFORMATDESCRIPTOR
nSize As Integer
nVersion As Integer
dwFlags As String * 1
iPixelType As String * 1
cColorBits As String * 1
cRedBits As String * 1
cRedShift As String * 1
cGreenBits As String * 1
cGreenShift As String * 1
cBlueBits As String * 1
cBlueShift As String * 1
cAlphaBits As String * 1
cAlphaShift As String * 1
cAccumBits As String * 1
cAccumRedBits As String * 1
cAccumGreenBits As String * 1
cAccumBlueBits As String * 1
cAccumAlphaBits As String * 1
cDepthBits As String * 1
cStencilBits As String * 1
cAuxBuffers As String * 1
iLayerType As String * 1
bReserved As String * 1
dwLayerMask As Long
dwVisibleMask As Long
dwDamageMask As Long
End Type
Public Const PFD_TYPE_RGBA = 0
Public Const PFD_MAIN_PLANE = 0
Public Const PFD_OVERLAY_PLANE = 1
Public Const PFD_UNDERLAY_PLANE = (-1)
Public Const PFD_DOUBLEBUFFER = &H1
Public Const PFD_STEREO = &H2
Public Const PFD_DRAW_TO_WINDOW = &H4
Public Const PFD_DRAW_TO_BITMAP = &H8
Public Const PFD_SUPPORT_GDI = &H10
Public Const PFD_SUPPORT_OPENGL = &H20
Public Const PFD_GENERIC_FORMAT = &H40
Public Const PFD_NEED_PALETTE = &H80
Public Const PFD_NEED_SYSTEM_PALETTE = &H100
Public Const PFD_SWAP_EXCHANGE = &H200
Public Const PFD_SWAP_COPY = &H400
Public Const PFD_SWAP_LAYER_BUFFERS = &H800
Public Const PFD_GENERIC_ACCELERATED = &H1000
Public Const PFD_SUPPORT_DIRECTDRAW = &H2000
Public Const PFD_DIRECT3D_ACCELERATED = &H4000
Public Const PFD_SUPPORT_COMPOSITION = &H8000
Public Const PFD_DEPTH_DONTCARE = &H20000000
Public Const PFD_DOUBLEBUFFER_DONTCARE = &H40000000
Public Const PFD_STEREO_DONTCARE = &H80000000
Public Const GL_PROJECTION = &H1701
Public Const GL_MODELVIEW = &H1700
Public Const GL_POINTS = &H0
Public Const GL_LINES = &H1
Public Const GL_LINE_LOOP = &H2
Public Const GL_LINE_STRIP = &H3
Public Const GL_TRIANGLES = &H4
Public Const GL_TRIANGLE_STRIP = &H5
Public Const GL_TRIANGLE_FAN = &H6
Public Const GL_QUADS = &H7
Public Const GL_QUAD_STRIP = &H8
Public Const GL_POLYGON = &H9
Public Const GL_NORMALIZE = &HBA1
Public Const GL_DEPTH_TEST = &HB71
Public Const GL_CULL_FACE = &HB44
Public Const GL_LIGHTING_BIT = &H40
Public Const GL_LIGHTING = &HB50
Public Const GL_LIGHT_MODEL_LOCAL_VIEWER = &HB51
Public Const GL_LIGHT_MODEL_AMBIENT = &HB53
Public Const GL_LIGHT0 = &H4000
Public Const GL_LIGHT1 = &H4001
Public Const GL_LIGHT2 = &H4002
Public Const GL_LIGHT3 = &H4003
Public Const GL_LIGHT4 = &H4004
Public Const GL_LIGHT5 = &H4005
Public Const GL_LIGHT6 = &H4006
Public Const GL_LIGHT7 = &H4007
Public Const GL_COLOR_MATERIAL = &HB57
Public Const GL_POSITION = &H1203
Public Const GL_SPOT_DIRECTION = &H1204
Public Const GL_SPOT_EXPONENT = &H1205
Public Const GL_SPOT_CUTOFF = &H1206
Public Const GL_TRUE = 1
Public Const GL_FALSE = 0
Public Const GL_NEVER = &H200
Public Const GL_LESS = &H201
Public Const GL_EQUAL = &H202
Public Const GL_LEQUAL = &H203
Public Const GL_GREATER = &H204
Public Const GL_NOTEQUAL = &H205
Public Const GL_GEQUAL = &H206
Public Const GL_ALWAYS = &H207
Public Const GL_DEPTH_BUFFER_BIT = &H100
Public Const GL_ACCUM_BUFFER_BIT = &H200
Public Const GL_STENCIL_BUFFER_BIT = &H400
Public Const GL_COLOR_BUFFER_BIT = &H4000
Public Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Public Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Public Const FORMAT_MESSAGE_FROM_STRING = &H400
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Public Const LANG_NEUTRAL = &H0
Public Declare Function ChoosePixelFormat Lib "Gdi32" (ByVal hdc As Long, ppfd As PIXELFORMATDESCRIPTOR) As Long
Public Declare Function SetPixelFormat Lib "Gdi32" (ByVal hdc As Long, ByVal format As Long, ppfd As PIXELFORMATDESCRIPTOR) As Boolean
Public Declare Function SwapBuffers Lib "Gdi32" (ByVal hdc As Long) As Boolean
Public Declare Function GetDC Lib "User32" (ByVal hwnd As Long) As Long
Public Declare Function wglCreateContext Lib "Opengl32" (ByVal hdc As Long) As Long
Public Declare Function wglMakeCurrent Lib "Opengl32" (ByVal hdc As Long, ByVal hrc As Long) As Boolean
Public Declare Function wglDeleteContext Lib "Opengl32" (ByVal hrc As Long) As Boolean
Public Declare Sub glClearColor Lib "Opengl32" (ByVal r As Single, ByVal g As Single, ByVal b As Single, ByVal a As Single)
Public Declare Sub glClear Lib "Opengl32" (ByVal b As Long)
Public Declare Sub glFlush Lib "Opengl32" ()
Public Declare Function glGetError Lib "Opengl32" () As Long
Public Declare Sub glViewport Lib "Opengl32" (ByVal x As Long, ByVal y As Long, ByVal w As Long, ByVal h As Long)
Public Declare Sub glFrustum Lib "Opengl32" (ByVal left As Double, ByVal right As Double, ByVal bottom As Double, ByVal top As Double, ByVal zNear As Double, ByVal zFar As Double)
Public Declare Sub glLoadIdentity Lib "Opengl32" ()
Public Declare Sub glMatrixMode Lib "Opengl32" (ByVal mode As Long)
Public Declare Sub glTranslatef Lib "Opengl32" (ByVal x As Single, ByVal y As Single, ByVal z As Single)
Public Declare Sub glRotatef Lib "Opengl32" (ByVal angle As Single, ByVal x As Single, ByVal y As Single, ByVal z As Single)
Public Declare Sub glNormal3f Lib "Opengl32" (ByVal r As Single, ByVal g As Single, ByVal b As Single)
Public Declare Sub glNormal3i Lib "Opengl32" (ByVal r As Long, ByVal g As Long, ByVal b As Long)
Public Declare Sub glVertex3f Lib "Opengl32" (ByVal x As Single, ByVal y As Single, ByVal z As Single)
Public Declare Sub glVertex3i Lib "Opengl32" (ByVal x As Long, ByVal y As Long, ByVal z As Long)
Public Declare Sub glColor3f Lib "Opengl32" (ByVal r As Single, ByVal g As Single, ByVal b As Single)
Public Declare Sub glColor3i Lib "Opengl32" (ByVal r As Long, ByVal g As Long, ByVal b As Long)
Public Declare Sub glBegin Lib "Opengl32" (ByVal poly As Long)
Public Declare Sub glEnd Lib "Opengl32" ()
Public Declare Sub glPushMatrix Lib "Opengl32" ()
Public Declare Sub glPopMatrix Lib "Opengl32" ()
Public Declare Sub glEnable Lib "Opengl32" (ByVal cap As Long)
Public Declare Sub glDisable Lib "Opengl32" (ByVal cap As Long)
Public Declare Sub glDepthMask Lib "Opengl32" (ByVal tf As Boolean)
Public Declare Sub glDepthFunc Lib "Opengl32" (ByVal tf As Boolean)
Public Declare Sub glDepthRange Lib "Opengl32" (ByVal near As Double, ByVal far As Double)
Public Declare Sub glLightfv Lib "Opengl32" (ByVal light As Long, ByVal pname As Long, ByRef fv() As Single)
Public Declare Function GetLastError Lib "kernel32" () As Long
Declare Function FormatMessageA Lib "kernel32" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Public Function GLDirName(ByVal FilePath As String)
Dim OutPathLen As Integer
Dim OutPath As String
OutPath = FilePath
OutPathLen = Len(FilePath)
While 0 < OutPathLen And "\" <> right(OutPath, 1)
OutPath = left(FilePath, OutPathLen)
OutPathLen = OutPathLen - 1
Wend
GLDirName = OutPath
End Function
Public Sub GLShowSystemError(ByVal ErrorPrefix As String, ByVal ErrorCode As Long, ByVal Silent As Boolean)
Dim ErrorBuffer As String * 200
FormatMessageA FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrorCode, LANG_NEUTRAL, ErrorBuffer, 200, ByVal 0&
If Silent Then
frmLog.LogLine ErrorPrefix & ": " & ErrorBuffer
Else
MsgBox ErrorPrefix & ": " & ErrorBuffer, vbCritical, "OpenGL Error"
End If
End Sub
Public Function GLLoadMtl(ByRef ObjIn As GLObj, ByVal MtlPath As String) As Boolean
Dim MtlFileNo As Integer
Dim Line As String
Dim LineArr() As String
Dim LineArrSz As Integer
MtlFileNo = FreeFile
Open MtlPath For Input As MtlFileNo
Do Until EOF(MtlFileNo)
Line Input #MtlFileNo, Line
If "newmtl" = left(Line, 6) Then
'Parse the line into an array.
LineArrSz = GLStrSplit(LineArr, Line, " ")
If 2 > LineArrSz Then frmLog.LogLine "Invalid array sz: " & LineArrSz
'Prepare and assign vertices.
ReDim Preserve ObjIn.Materials(ObjIn.MaterialsSz) As GLMaterial
ObjIn.MaterialsSz = ObjIn.MaterialsSz + 1
ObjIn.Materials(ObjIn.MaterialsSz - 1).Name = LineArr(1)
frmLog.LogLine "Material: " & ObjIn.Materials(ObjIn.MaterialsSz - 1).Name
ElseIf "Kd" = left(Line, 2) Then
'Parse the line into an array.
LineArrSz = GLStrSplit(LineArr, Line, " ")
If 4 > LineArrSz Then frmLog.LogLine "Invalid array sz: " & LineArrSz
ObjIn.Materials(ObjIn.MaterialsSz - 1).Diffuse(0) = Val(LineArr(1))
ObjIn.Materials(ObjIn.MaterialsSz - 1).Diffuse(1) = Val(LineArr(2))
ObjIn.Materials(ObjIn.MaterialsSz - 1).Diffuse(2) = Val(LineArr(3))
frmLog.LogLine "Material " & ObjIn.Materials(ObjIn.MaterialsSz - 1).Name & " Diffuse: " & _
ObjIn.Materials(ObjIn.MaterialsSz - 1).Diffuse(0) & ", " & _
ObjIn.Materials(ObjIn.MaterialsSz - 1).Diffuse(1) & ", " & _
ObjIn.Materials(ObjIn.MaterialsSz - 1).Diffuse(2)
End If
DoEvents
Loop
Close MtlFileNo
End Function
Public Sub GLParseObjFaces(ByRef ObjIn As GLObj, ByVal CurrentMtlIdx As Integer, ByVal Line As String)
Dim LineArr() As String
Dim LineArrSz As Integer
Dim FaceArr() As String
Dim FaceArrSz As Integer
Dim FaceVertexIdx As Integer
'Parse the line into an array.
LineArrSz = GLStrSplit(LineArr, Line, " ")
If 4 > LineArrSz Then frmLog.LogLine "Invalid array sz: " & LineArrSz
ReDim Preserve ObjIn.Faces(ObjIn.FacesSz) As GLFace
ObjIn.Faces(ObjIn.FacesSz).MaterialIdx = CurrentMtlIdx
'TODO: Handle >3 vertex indexes.
For FaceVertexIdx = 1 To 3
FaceArrSz = GLStrSplit(FaceArr, LineArr(FaceVertexIdx), "/")
'Parse vertex index.
ReDim Preserve ObjIn.Faces(ObjIn.FacesSz).VertexIdx(ObjIn.Faces(ObjIn.FacesSz).VertexIdxSz) As Integer
ObjIn.Faces(ObjIn.FacesSz).VertexIdx(ObjIn.Faces(ObjIn.FacesSz).VertexIdxSz) = _
Val(FaceArr(0)) - 1 'Vertex indexes are 1-indexed in obj format.
ObjIn.Faces(ObjIn.FacesSz).VertexIdxSz = ObjIn.Faces(ObjIn.FacesSz).VertexIdxSz + 1
'Parser normal index.
If 3 = FaceArrSz Then
ReDim Preserve ObjIn.Faces(ObjIn.FacesSz).VNormalIdx(ObjIn.Faces(ObjIn.FacesSz).VNormalIdxSz) As Integer
ObjIn.Faces(ObjIn.FacesSz).VNormalIdx(ObjIn.Faces(ObjIn.FacesSz).VNormalIdxSz) = _
Val(FaceArr(2)) - 1 'Vertex indexes are 1-indexed in obj format.
ObjIn.Faces(ObjIn.FacesSz).VNormalIdxSz = ObjIn.Faces(ObjIn.FacesSz).VNormalIdxSz + 1
End If
'TODO: Parse normal/texture indexes.
Next FaceVertexIdx
'Increment vertex count.
ObjIn.FacesSz = ObjIn.FacesSz + 1
End Sub
Public Sub GLViewObjTree(ByRef ObjIn As GLObj)
Dim FacesNode As Node
Dim FaceIdx As Integer
Dim FaceVertexIdx As Integer
Dim FaceNodeIter As Node
Dim VertexNodeIter As Node
Dim ObjVertexIdx As Integer
Dim ParentNodeIter As Node
Dim MaterialsNode As Node
Dim MaterialNodeIter As Node
Dim MaterialIdx As Integer
Dim DiffuseNodeIter As Node
Dim Kidx As Integer
Set FacesNode = frmObjTree.treeviewobj.Nodes.Add(, , , "Faces")
FacesNode.Image = 1
For FaceIdx = 0 To ObjIn.FacesSz - 1
Set FaceNodeIter = frmObjTree.treeviewobj.Nodes.Add(1, tvwChild, , "Face " & FaceIdx)
frmObjTree.treeviewobj.Nodes.Add FaceNodeIter, tvwChild, , _
"Material: " & ObjIn.Materials(ObjIn.Faces(FaceIdx).MaterialIdx).Name
'Add nodes for face vertices.
Set ParentNodeIter = frmObjTree.treeviewobj.Nodes.Add(FaceNodeIter, tvwChild, , "Vertices")
For FaceVertexIdx = 0 To ObjIn.Faces(FaceIdx).VertexIdxSz - 1
ObjVertexIdx = ObjIn.Faces(FaceIdx).VertexIdx(FaceVertexIdx)
Set VertexNodeIter = frmObjTree.treeviewobj.Nodes.Add( _
ParentNodeIter.Index, tvwChild, , "Vertex " & ObjVertexIdx)
frmObjTree.treeviewobj.Nodes.Add _
VertexNodeIter.Index, tvwChild, , "X: " & _
ObjIn.Vertices(ObjVertexIdx).x
frmObjTree.treeviewobj.Nodes.Add _
VertexNodeIter.Index, tvwChild, , "Y: " & _
ObjIn.Vertices(ObjVertexIdx).y
frmObjTree.treeviewobj.Nodes.Add _
VertexNodeIter.Index, tvwChild, , "Z: " & _
ObjIn.Vertices(ObjVertexIdx).z
Next FaceVertexIdx
'Add nodes for face normals.
Set ParentNodeIter = frmObjTree.treeviewobj.Nodes.Add(FaceNodeIter, tvwChild, , "Normals")
For FaceVertexIdx = 0 To ObjIn.Faces(FaceIdx).VNormalIdxSz - 1
ObjVertexIdx = ObjIn.Faces(FaceIdx).VNormalIdx(FaceVertexIdx)
Set VertexNodeIter = frmObjTree.treeviewobj.Nodes.Add( _
ParentNodeIter.Index, tvwChild, , "Normal " & ObjVertexIdx)
frmObjTree.treeviewobj.Nodes.Add _
VertexNodeIter.Index, tvwChild, , "X: " & _
ObjIn.VNormals(ObjVertexIdx).x
frmObjTree.treeviewobj.Nodes.Add _
VertexNodeIter.Index, tvwChild, , "Y: " & _
ObjIn.VNormals(ObjVertexIdx).y
frmObjTree.treeviewobj.Nodes.Add _
VertexNodeIter.Index, tvwChild, , "Z: " & _
ObjIn.VNormals(ObjVertexIdx).z
Next FaceVertexIdx
Next FaceIdx
Set MaterialsNode = frmObjTree.treeviewobj.Nodes.Add(, , , "Materials")
For MaterialIdx = 0 To ObjIn.MaterialsSz - 1
Set MaterialNodeIter = frmObjTree.treeviewobj.Nodes.Add( _
MaterialsNode, tvwChild, , ObjIn.Materials(MaterialIdx).Name)
'Add nodes for diffuse lighting.
Set DiffuseNodeIter = frmObjTree.treeviewobj.Nodes.Add( _
MaterialNodeIter, tvwChild, , "Diffuse")
For Kidx = 0 To 2
frmObjTree.treeviewobj.Nodes.Add _
DiffuseNodeIter, tvwChild, , "" & Kidx & ": " & ObjIn.Materials(MaterialIdx).Diffuse(Kidx)
Next Kidx
Next MaterialIdx
frmObjTree.Show
End Sub
Public Sub GLDrawCube()
'BACK
glBegin GL_TRIANGLES
glNormal3f 0, 0, 1#
glColor3f 1#, 1#, 1#
glVertex3f 1#, -1#, 1#
glVertex3f 1#, 1#, 1#
glVertex3f -1#, 1#, 1#
glVertex3f -1#, 1#, 1#
glVertex3f -1#, -1#, 1#
glVertex3f 1#, -1#, 1#
glEnd
'RIGHT
glBegin GL_TRIANGLES
glNormal3f 1#, 0, 0
glColor3f 0, 1#, 1#
glVertex3f 1#, -1#, -1#
glVertex3f 1#, 1#, -1#
glVertex3f 1#, 1#, 1#
glVertex3f 1#, 1#, 1#
glVertex3f 1#, -1#, 1#
glVertex3f 1#, -1#, -1#
glEnd
'LEFT
glBegin GL_TRIANGLES
glNormal3f -1#, 0, 0
glColor3f 1#, 1#, 0
glVertex3f -1#, -1#, 1#
glVertex3f -1#, 1#, 1#
glVertex3f -1#, 1#, -1#
glVertex3f -1#, 1#, -1#
glVertex3f -1#, -1#, -1#
glVertex3f -1#, -1#, 1#
glEnd
'FRONT
glBegin GL_TRIANGLES
glNormal3f 0, 0, -1#
glColor3f 0, 0, 1#
glVertex3f -1#, -1#, -1#
glVertex3f -1#, 1#, -1#
glVertex3f 1#, 1#, -1#
glVertex3f 1#, 1#, -1#
glVertex3f 1#, -1#, -1#
glVertex3f -1#, -1#, -1#
glEnd
'TOP
glBegin GL_TRIANGLES
glNormal3f 0, 1#, 0
glColor3f 0, 1#, 0
glVertex3f 1#, 1#, 1#
glVertex3f 1#, 1#, -1#
glVertex3f -1#, 1#, -1#
glVertex3f -1#, 1#, -1#
glVertex3f -1#, 1#, 1#
glVertex3f 1#, 1#, 1#
glEnd
'BOTTOM
glBegin GL_TRIANGLES
glNormal3f 0, -1#, 0
glColor3f 1#, 0, 0
glVertex3f 1#, -1#, -1#
glVertex3f 1#, -1#, 1#
glVertex3f -1#, -1#, 1#
glVertex3f -1#, -1#, 1#
glVertex3f -1#, -1#, -1#
glVertex3f 1#, -1#, -1#
glEnd
End Sub
Public Function GLLoadObj(ByRef ObjIn As GLObj, ByVal ObjPath As String) As Boolean
Dim ObjFileNo As Integer
Dim Line As String
Dim LineArr() As String
Dim LineArrSz As Integer
Dim CurrentMtl As Integer
Dim MtlIter As Integer
ObjFileNo = FreeFile
Open ObjPath For Input As ObjFileNo
Do Until EOF(ObjFileNo)
Line Input #ObjFileNo, Line
If "mtllib" = left(Line, 6) Then
LineArrSz = GLStrSplit(LineArr, Line, " ")
'Concat the path to the obj file with the material lib name.
GLLoadMtl ObjIn, GLDirName(ObjPath) & LineArr(1)
ElseIf "usemtl" = left(Line, 6) Then
LineArrSz = GLStrSplit(LineArr, Line, " ")
For MtlIter = 0 To ObjIn.MaterialsSz - 1
If LineArr(1) = ObjIn.Materials(MtlIter).Name Then
CurrentMtl = MtlIter
'frmLog.LogLine "Material Selected: " & _
ObjIn.Materials(CurrentMtl).Name & " (" & CurrentMtl & ")"
End If
Next MtlIter
ElseIf "f" = left(Line, 1) Then
GLParseObjFaces ObjIn, CurrentMtl, Line
ElseIf "vn" = left(Line, 2) Then
'Parse the line into an array.
LineArrSz = GLStrSplit(LineArr, Line, " ")
If 4 > LineArrSz Then frmLog.LogLine "Invalid array sz: " & LineArrSz
'Prepare and assign vertices.
ReDim Preserve ObjIn.VNormals(ObjIn.VNormalsSz) As GLVertex
ObjIn.VNormals(ObjIn.VNormalsSz).x = Val(LineArr(1))
ObjIn.VNormals(ObjIn.VNormalsSz).y = Val(LineArr(2))
ObjIn.VNormals(ObjIn.VNormalsSz).z = Val(LineArr(3))
'frmLog.LogLine "VNormal: " & ObjIn.VNormals(ObjIn.VNormalsSz).x & _
", " & ObjIn.VNormals(ObjIn.VNormalsSz).y & _
", " & ObjIn.VNormals(ObjIn.VNormalsSz).z
'Increment vertex count.
ObjIn.VNormalsSz = ObjIn.VNormalsSz + 1
ElseIf "vt" = left(Line, 2) Then
'Parse the line into an array.
LineArrSz = GLStrSplit(LineArr, Line, " ")
If 3 > LineArrSz Then frmLog.LogLine "Invalid array sz: " & LineArrSz
'Prepare and assign vertices.
ReDim Preserve ObjIn.VTextures(ObjIn.VTexturesSz) As GLVTexture
ObjIn.VTextures(ObjIn.VTexturesSz).u = Val(LineArr(1))
ObjIn.VTextures(ObjIn.VTexturesSz).V = Val(LineArr(2))
If 4 = LineArrSz Then ObjIn.VTextures(ObjIn.VTexturesSz).w = Val(LineArr(3))
'frmLog.LogLine "VTexture: " & ObjIn.VTextures(ObjIn.VTexturesSz).u & _
", " & ObjIn.VTextures(ObjIn.VTexturesSz).V & _
", " & ObjIn.VTextures(ObjIn.VTexturesSz).w
'Increment vertex count.
ObjIn.VTexturesSz = ObjIn.VTexturesSz + 1
ElseIf "v" = left(Line, 1) Then
'Parse the line into an array.
LineArrSz = GLStrSplit(LineArr, Line, " ")
If 4 > LineArrSz Then frmLog.LogLine "Invalid array sz: " & LineArrSz
'Prepare and assign vertices.
ReDim Preserve ObjIn.Vertices(ObjIn.VerticesSz) As GLVertex
ObjIn.Vertices(ObjIn.VerticesSz).x = Val(LineArr(1))
ObjIn.Vertices(ObjIn.VerticesSz).y = Val(LineArr(2))
ObjIn.Vertices(ObjIn.VerticesSz).z = Val(LineArr(3))
'frmLog.LogLine "Vertex: " & ObjIn.Vertices(ObjIn.VerticesSz).x & ", " & ObjIn.Vertices(ObjIn.VerticesSz).y & ", " & ObjIn.Vertices(ObjIn.VerticesSz).z
'Increment vertex count.
ObjIn.VerticesSz = ObjIn.VerticesSz + 1
Else
'frmLog.LogLine Line
End If
DoEvents
Loop
Close ObjFileNo
End Function
Public Function GLShowError() As Boolean
Dim ErrVal As Long
Dim ErrMsg As String
ErrVal = glGetError
Select Case ErrVal
Case 1280
ErrMsg = "Invalid enumerated argument."
Case 1281
ErrMsg = "Invalid value."
Case 1282
ErrMsg = "Invalid operation."
Case 1283
ErrMsg = "Stack overflow."
Case 1284
ErrMsg = "Stack underflow."
Case 1285
ErrMsg = "Out of memory."
End Select
If 0 <> ErrVal Then
MsgBox ErrMsg, vbCritical, "OpenGL Error"
GLShowError = True
End If
End Function
Public Function GLStrSplit(ByRef StrOut() As String, ByVal StrIn As String, ByVal Token As String) As Integer
Dim WordsOut As Integer
Dim CharIdx As Integer
ReDim StrOut(0) As String
For CharIdx = 1 To Len(StrIn)
If right(left(StrIn, CharIdx), 1) = Token Then
WordsOut = WordsOut + 1
'Realloc but preserve what's parsed so far!
ReDim Preserve StrOut(WordsOut) As String
StrOut(WordsOut) = ""
ElseIf Chr(13) <> right(left(StrIn, CharIdx), 1) And Chr(10) <> right(left(StrIn, CharIdx), 1) Then
StrOut(WordsOut) = StrOut(WordsOut) & right(left(StrIn, CharIdx), 1)
End If
Next
GLStrSplit = WordsOut + 1
End Function