-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrender.cxs
816 lines (528 loc) · 18.2 KB
/
trender.cxs
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
Import minib3d
''
''-- also create this function if extending trender
''
'Function SetRender(flags:Int=0)
' TRender.render = New OpenglES11
' TRender.render.Graphicsinit(flags)
'End
''Method DeleteTexture(glid:Int[]) Abstract '' **** DEPRECATE THIS ****** (use DeleteTexture(tx:Texture))
'' and update BindTextureStack()
Const RENDERFLAG_DISABLEVBO:Int = 1
Const RENDERFLAG_PERPIXELLIGHTING:Int = 2
'' Interface IRenderUpdate
'' -- used for sprites and sprite batching, forces TRender to call Update when in camera view.
Interface IRenderUpdate
Method Update( cam:TCamera )
End
Class TRender
#If CONFIG="debug"
Const DEBUG:Int=1
#else
Const DEBUG:Int=0
#endif
Global render:TRender
Global vbo_enabled:Bool=False ' this is set in GraphicsInit - will be set to true if USE_VBO is true and the hardware supports vbos
Global MAX_TEXTURES:Int = 3
Global shader_enabled:Bool = False
Global width:Int,height:Int,mode:Int,depth:Int,rate:Int
Global wireframe:Int=False ''draw meshes as lines or filled
Global disable_lighting:Bool = False ''turns off light shading, renders full color
Global alpha_pass:Int = 0 ''for optimizing the TMesh render routine
Global shader2D:IShader2D = New BlankShader
Global camera2D:TCamera = New TCamera '' do not add to cam_list
Global draw_list:List<TMesh> = New List<TMesh> ''immediate mode drawing for overlay, text
Global tris_rendered:int
Private
Global render_list:RenderAlphaList = New RenderAlphaList
Global render_alpha_list:RenderAlphaList = New RenderAlphaList
Global temp_shader:TShader
Public
Method ContextReady:Bool() Abstract ''must return true when ready to render
Method GetVersion:Float() Abstract ''returns version of graphics platform being used
Method Reset:Void() Abstract ''reset, called before render for each camera
Method Finish:Void() Abstract ''finish pass for each camera
Method GraphicsInit:Int(flags:Int=0) Abstract ''init during SetRender()
Method Render:Void(ent:TEntity, cam:TCamera = Null) Abstract ''render per mesh
Method BindTexture:TTexture(tex:TTexture,flags:Int) Abstract
'Method DeleteTexture(glid:Int[]) '' **** DEPRECATE THIS ******
Method DeleteTexture(tex:TTexture) Abstract
Method UpdateLight(cam:TCamera, light:TLight) Abstract
Method DisableLight(light:TLight) Abstract
Method UpdateCamera(cam:TCamera) Abstract
Method UpdateVBO(surface:TSurface) Abstract
Method FreeVBO(surface:TSurface) Abstract
''-------------------------------------------------------
Function ClearWorld(entities=True,brushes=True,textures=True)
render_list.Clear()
render_alpha_list.Clear()
If entities
'Print TEntity.entity_list.Count()
For Local ent:TEntity=Eachin TEntity.entity_list
'Print ent.classname
ent.FreeEntity()
ent=Null
Next
'TEntity.entity_list.Clear()
ClearCollisions()
TPick.ent_list.Clear()
TPick.picked_ent=Null
TPick.picked_surface=0
Endif
If textures
For Local tex:TTexture=Eachin TTexture.tex_list
tex.FreeTexture()
Next
Endif
End
Function UpdateWorld(anim_speed#=1.0)
' collision
CollisionInfo.UpdateCollisions()
' anim
Local first:Int
Local last:Int
''-- this touches every entity, using for cull tests
For Local mesh:TEntity=Eachin TEntity.entity_list
If mesh.anim And mesh.anim_update=True
Local culltest:Bool =TMesh(mesh).GetCulled()
first=mesh.anim_seqs_first[mesh.anim_seq]
last=mesh.anim_seqs_last[mesh.anim_seq]
Local anim_start=False
If mesh.anim_trans>0
mesh.anim_trans=mesh.anim_trans-1
If mesh.anim_trans=1 Then anim_start=True
Endif
If mesh.anim_trans>0
Local r#=1.0-mesh.anim_time
r=r/mesh.anim_trans
mesh.anim_time=mesh.anim_time+r
If mesh.anim = 1 And culltest = false
TAnimation.AnimateMesh2(mesh,mesh.anim_time,first,last)
Elseif mesh.anim = 2 And culltest= false
TAnimation.AnimateVertex(mesh,mesh.anim_time,first,last)
Endif
If anim_start=True Then mesh.anim_time=first
Else
If mesh.anim = 1 And culltest= false
TAnimation.AnimateMesh(mesh,mesh.anim_time,first,last)
Elseif mesh.anim = 2 And culltest= false
TAnimation.AnimateVertex(mesh,mesh.anim_time,first,last)
Elseif mesh.anim =4
TBone.UpdateBoneChildren( mesh)
If culltest= false Then TAnimation.VertexDeform(TMesh(mesh))
Endif
If mesh.anim_mode=0 ''stop animation
mesh.anim_update=False ' after updating animation so that animation is in final 'stop' pose - don't update again
Elseif mesh.anim_mode=1 ''loop animation (default)
mesh.anim_time=mesh.anim_time+(mesh.anim_speed*anim_speed)
If mesh.anim_time>last
mesh.anim_time=first+(mesh.anim_time-last)
Endif
Elseif mesh.anim_mode=2 ''ping pong
If mesh.anim_dir=1
mesh.anim_time=mesh.anim_time+(mesh.anim_speed*anim_speed)
If mesh.anim_time>last
mesh.anim_time=mesh.anim_time-(mesh.anim_speed*anim_speed)
mesh.anim_dir=-1
Endif
Endif
If mesh.anim_dir=-1
mesh.anim_time=mesh.anim_time-(mesh.anim_speed*anim_speed)
If mesh.anim_time<first
mesh.anim_time=mesh.anim_time+(mesh.anim_speed*anim_speed)
mesh.anim_dir=1
Endif
Endif
Elseif mesh.anim_mode=3 ''one-shot, hold at end
mesh.anim_time=mesh.anim_time+(mesh.anim_speed*anim_speed)
If mesh.anim_time>last
'mesh.anim_time = last
mesh.anim_time = first + (mesh.anim_time - last)
mesh.anim_mode=0
Endif
Endif
Endif
Endif
'' Update mesh culled status
If TMesh(mesh) Then TMesh(mesh).culled = True
Next
End
Function RenderWorld:Void()
'Print "begin renderworld"
tris_rendered=0
''confirm rendering context
If TRender.render = Null Or Not TRender.render.ContextReady() Then Return
''process texture binds & framebuffer binds
If TTexture.tex_bind_stack.Length >0 Then TRender.render.BindTextureStack()
For Local cam:TCamera=Eachin TCamera.cam_list
'If cam.parent_hidden=True Or cam.hidden=True Then Continue
If cam.Hidden()=True Then Continue
TShader.PreProcess(cam)
''override the default render routine for custom shader
If IShaderRender(TShader.g_shader)=Null
TRender.render.RenderCamera(cam)
Else
temp_shader = TShader.g_shader ''to keep custom shader on return
IShaderRender(TShader.g_shader).Render(cam)
TShader.g_shader = temp_shader
Endif
TShader.PostProcess(cam)
Next
RenderDrawList()
render.RenderWorldFinish()
End
Function RenderDrawList:Void()
If draw_list.IsEmpty Or Not TRender.render.ContextReady() Then Return
TRender.render.Reset()
shader2D.SetShader2D() ''multi-shader is automatic
'camera2D.CameraClsMode(False,False) ''moved this to the end of method to allow mojo CLS
''*** for mojoemulation ***
MojoEmulationDevice._quadCache.FlushCache()
alpha_pass=1
Local wireframeIsEnabled:= wireframe
wireframe = False
'camera2D.ExtractFrustum()
'camera2D.CameraProjMode(3)
TRender.render.UpdateCamera(camera2D)
For Local mesh:TMesh = Eachin draw_list
If Not mesh Then Continue
If IRenderUpdate( mesh )
IRenderUpdate(mesh).Update(camera2D ) ' rotate sprites with respect to current cam
Endif
''auto-scaling for sprites and ttext
Local sp:TSprite = TSprite(mesh)
If mesh.is_sprite
sp.mat_sp.Scale( (sp.pixel_scale[0]) , (sp.pixel_scale[1]), 1.0)
mesh.EntityFX FXFLAG_DISABLE_DEPTH ''is this needed?
Endif
If mesh.Alpha() Then mesh.alpha_order=1.0 ' test for alpha in surface
TRender.render.Render(mesh,camera2D)
tris_rendered += mesh.total_tris
Next
wireframe = wireframeIsEnabled
TRender.render.Finish()
draw_list.Clear()
''update at end to allow mojo commands
camera2D.CameraClsMode(False,False)
camera2D.CameraViewport(0,0,TRender.width,TRender.height)
camera2D.SetPixelCamera
End
Method RenderCamera:Void(cam:TCamera, skip:Int=0)
TRender.render.Reset() ''reset render pass
'' use skip to render without updating camera
'' would be useful for FSAA, FBOs, or a render layer system
If (Not skip)
cam.Update(cam)
UpdateCamera( cam ) ''update Render driver
Endif
For Local light:TLight=Eachin TLight.light_list
UpdateLight(cam,light) ' EntityHidden code inside Update
Next
'Print "cam:"+cam.name
render_list.Clear()
render_alpha_list.Clear()
''Perform camera clipping, alpha ordering, and entity sort
Local mesh:TMesh
Local alpha_count:Int=0
Local wireFrameIsEnabled:= wireframe
For Local ent:TEntity=Eachin TEntity.entity_list
'' reject non-mesh
mesh = TMesh( ent )
If mesh
'If mesh.parent_hidden=True Or mesh.hidden=True Or mesh.brush.alpha=0.0 Then Continue
If mesh.Hidden()=True Or mesh.brush.alpha=0.0 Then Continue
''cam layer mode
If (mesh.use_cam_layer And mesh.cam_layer <> cam) Or (cam.use_cam_layer And mesh.cam_layer <> cam) Then Continue
' get new bounds
mesh.GetBounds()
' Perform frustum cull
Local inview:Float =cam.EntityInFrustum(mesh)
mesh.distance_nearplane = inview
'Print "// mesh "+mesh.classname+" "+inview
'Print "// center "+mesh.center_x+" "+mesh.center_y+" "+mesh.center_z
If inview > 0.00001
mesh.culled = false
wireframe = wireframe | mesh.wireframe
If mesh.auto_fade=True Then mesh.AutoFade(cam)
If IRenderUpdate(mesh)
IRenderUpdate(mesh).Update(cam ) ' update sprites, batchsprites
Endif
If mesh.Alpha()
''alpha entities are drawn last, sorted, without depth test
mesh.alpha_order = mesh.distance_nearplane
render_alpha_list.AddLast(mesh)
Else
TRender.render.Render(mesh,cam)
'mesh.alpha_order=-cam.EntityDistanceSquared(mesh)
'render_list.AddLast(mesh)
tris_rendered += mesh.total_tris
Endif
wireframe = wireFrameIsEnabled
Endif
Endif
Next
'' can't draw front to back for opaque, then "entity.order" is messed up
' Draw front to back for opaque
'render_list.Sort()
' Draw back to front, alpha render list
render_alpha_list.Sort() ''sorting alpha_order
'TRender.alpha_pass = 1 ''skip non-alpha surface pass ''ACTUALLY this may help hardware z-ordering
For mesh = Eachin render_alpha_list
wireframe = wireframe | mesh.wireframe
TRender.render.Render(mesh,cam)
wireframe = wireFrameIsEnabled
tris_rendered += mesh.total_tris
Next
Finish() ''end render pass
End
Function Wireframe(b:Bool=True)
wireframe = b
End
Method ReloadSurfaces:Int()
Error "**Reload not implemented"
End
''
''due to openGL context begin available only guaranteed in OnRender(), use this to queue texture binds
''
Method BindTextureStack()
For Local tex:TTexture = Eachin TTexture.tex_bind_stack
If tex.bind_flags = -255
''remove texture
TRender.render.DeleteTexture(tex)
tex.FreeTexture_()
Else
TRender.render.BindTexture(tex,tex.bind_flags)
Endif
tex.bind_flags = -1
Next
'' allow all the textures to bind, then free memory
For Local tex:TTexture = Eachin TTexture.tex_bind_stack
If tex.freeMemoryAfterBind
tex.pixmap.FreePixmap()
If DEBUG Then Print "..freeTextureMemory:"+tex.file
Endif
Next
TTexture.tex_bind_stack.Clear()
End
Method ClearErrors:Int()
Return 1
End
Method RenderWorldFinish:Void()
''optional
End
End
''
'' EffectState
''
'' -- unifies render behavior
Class EffectState
''should be on(1)/off(0)/reset(-1) -- no bool, use int
Field use_full_bright:Int=0
Field use_vertex_colors:Int=0
Field use_flatshade:Int=0
Field use_fog:Int=1
Field use_depth_test:int
Field use_depth_write:int
Field use_backface_culling:Int
Field use_alpha_test:Int
Field use_perpixellighting:int
Field ambient#[]=[1.0,1.0,1.0,1.0]
Field no_mat#[]=[0.0,0.0,0.0,0.0]
Field mat_ambient#[]=[1.0,1.0,1.0,1.0]
Field diffuse#[]=[1.0,1.0,1.0,1.0]
Field specular#[]=[1.0,1.0,1.0,1.0]
Field shininess#[]=[100.0] ' upto 128
Field shine#
Field red#,green#,blue#
Field alpha#
Field blend:Int
Field fx:Int
Field num_tex:Int
Field tex:TTexture[]
Method Overwrite:Void(e:EffectState)
use_full_bright = e.use_full_bright
use_vertex_colors=e.use_vertex_colors
use_flatshade=e.use_flatshade
use_fog=e.use_fog
ambient=[e.ambient[0],e.ambient[1],e.ambient[2],e.ambient[3]]
'no_mat#[]=[0.0,0.0,0.0,0.0]
'mat_ambient#[]=[1.0,1.0,1.0,1.0]
diffuse=[e.diffuse[0],e.diffuse[1],e.diffuse[2],e.diffuse[3]]
specular=[e.specular[0],e.specular[1],e.specular[2],e.specular[3]]
shininess=[e.shininess[0]]
use_depth_test=e.use_depth_test
use_depth_write = e.use_depth_write
use_backface_culling=e.use_backface_culling
red=e.red ; green=e.green ; blue=e.blue ; alpha=e.alpha
shine=e.shine ; blend=e.blend ; fx=e.fx
use_alpha_test = e.use_alpha_test
use_perpixellighting = e.use_perpixellighting
num_tex = e.num_tex
End
Method SetNull:Void()
use_full_bright = -1
use_vertex_colors= -1
use_flatshade= -1
use_fog= -1
ambient=[-1.0,-1.0,-1.0,1.0]
'no_mat#[]=[0.0,0.0,0.0,0.0]
'mat_ambient#[]=[1.0,1.0,1.0,1.0]
diffuse=[-1.0,-1.0,-1.0,1.0]
specular=[-1.0,-1.0,-1.0,1.0]
shininess=[-1.0]
use_depth_test= -1
use_depth_write = -1
use_backface_culling= -1
use_alpha_test=0
use_perpixellighting = 0
red=-1.0 ; green=-1.0 ; blue=-1.0 ; alpha=-1.0
shine=-1.0 ; blend=99999 ; fx=99999
num_tex=-1
End
Method UpdateEffect:Void(surf:TSurface, ent:TEntity, cam:TCamera = Null)
'Local red#,green#,blue#,alpha#,shine#,blend:Int,fx:Int
Local ambient_red#,ambient_green#,ambient_blue#
' get main brush values
red =ent.brush.red
green=ent.brush.green
blue =ent.brush.blue
alpha=ent.brush.alpha
shine=ent.brush.shine
blend =ent.brush.blend
fx =ent.brush.fx
num_tex = ent.brush.no_texs
tex = ent.brush.tex
' combine surface brush values with main brush values
If surf.brush
Local shine2#=0.0
red =red *surf.brush.red
green =green*surf.brush.green
blue =blue *surf.brush.blue
alpha =alpha *surf.brush.alpha
shine2=surf.brush.shine
If shine=0.0 Then shine=shine2
If shine<>0.0 And shine2<>0.0 Then shine=shine*shine2
If blend=0 Then blend=surf.brush.blend ' overwrite master brush if master brush blend=0
fx=fx|surf.brush.fx
If (num_tex < surf.brush.no_texs)
num_tex = surf.brush.no_texs
tex = surf.brush.tex
endif
Endif
' take into account auto fade alpha
alpha=alpha-ent.fade_alpha
use_depth_test = 1
use_depth_write = 1
' if surface contains alpha info, enable blending
Local enable_blend:Int=0
If ent.using_alpha = true
If ent.brush.alpha<1.0
''the entire entity
enable_blend=1
'depth_test = 0
use_depth_write = 0
Elseif surf.alpha_enable=True
''just one surface
enable_blend=1
'depth_test = 0
use_depth_write = 0
Else
''entity flagged for alpha, but not this surface
enable_blend=0
use_depth_test = 1
use_depth_write = 1
Endif
Else
enable_blend=0
Endif
If enable_blend = 0 Then blend = -1
use_vertex_colors=0
use_flatshade=0
use_fog=Int(cam.fog_mode >0)
' fx flag 1 - full bright
If fx& FXFLAG_FULLBRIGHT
ambient_red =0.0; ambient_green=0.0; ambient_blue =0.0
use_full_bright=1
Else
ambient_red =TLight.ambient_red
ambient_green=TLight.ambient_green
ambient_blue =TLight.ambient_blue
use_full_bright=0
Endif
' fx flag 2 - vertex colors ***todo*** disable all lights?
If fx& FXFLAG_VERTEXCOLORS
use_vertex_colors = 1
red=1.0; green=1.0; blue=1.0; alpha=1.0
Endif
' fx flag 4 - flatshaded
If fx& FXFLAG_FLATSHADE
use_flatshade=1
Endif
' fx flag 8 - disable fog
If fx& FXFLAG_DISABLE_FOG
use_fog=0
Endif
' fx flag 16 - disable backface culling
If fx& FXFLAG_DISABLE_CULLING
use_backface_culling = 0
'driver.SetCulling(DRIVER_NONE)
Else
use_backface_culling = 1
'driver.SetCulling(DRIVER_FRONT) '' minib3d is -z, which is opposite flash
Endif
'' fx flag 32 - force alpha, implemented TMesh.Alpha() called in TRender
'' fx flag 64 - disable depth testing, overrides other settings
If fx&64
use_depth_test = 0
use_depth_write = 0
Endif
use_alpha_test = 0
'' also look for alphatest flag in textures
Local tex_alphatest:Int=0
If num_tex>0
For Local i:Int =0 To num_tex-1
If tex[i] Then tex_alphatest |= (tex[i].flags&4)
Next
Endif
If (fx& FXFLAG_ALPHA_TESTING>0) Or (tex_alphatest>0)
use_alpha_test = 1
use_depth_test = 1
use_depth_write = 1
Endif
If (fx& FXFLAG_PERPIXEL_LIGHTING )
use_perpixellighting = 1
Endif
' material color + specular
ambient=[ambient_red,ambient_green,ambient_blue,0.0]
'mat_ambient=[red,green,blue,alpha]
diffuse=[red,green,blue,alpha]
specular=[shine,shine,shine,1.0]
shininess=[100.0] ' upto 128
If cam.draw2D
'glDisable(GL_DEPTH_TEST)
If fx&64 = 0 Then use_depth_test = 0; use_depth_write = 0
'glDisable(GL_FOG)
use_fog = 0
'glDisable(GL_LIGHTING)
use_full_bright = 1
Endif
End
End
''
''helper classes
''
Class RenderAlphaList Extends List<TMesh>
''draw furthest (highest alpha_order) first, so sort from great to least
Method Compare( left:TMesh,right:TMesh )
If left.alpha_order > right.alpha_order Return -1
Return left.alpha_order < right.alpha_order
End
End
Class SurfaceAlphaList Extends List<TSurface>
Method Compare( left:TSurface,right:TSurface)
''---- how do we compare surface alphas in the correct order to draw? leave to user to order?
'If left.alpha_order < right.alpha_order Return -1
'Return left.alpha_order > right.alpha_order
End
End
''----------------------------------------------------