forked from adamredwoods/minib3d-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tshader.monkey
184 lines (112 loc) · 3.32 KB
/
tshader.monkey
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
''
''
'' TShader
''
Import tbrush
Import minib3d
Class TShader Extends TBrush
'' TBrush includes color, shininess, texture, etc.
Global process_list:List<TShader> = New List<TShader>
'Global layer_list:List<TShader> = New List<TShader>
Field list_link:list.Node<TShader>
Global g_shader:TShader ''current shader
Global default_shader:TShader ''fixed function equivalent
Field active:Int =0
Field shader_id:Int =0
Field fragment_id:Int =0
Field vertex_id:Int =0
Field override:Bool = False ''override all other shader brushes
''all driver implementations need this
'Method LoadShader:TShader(vp_file:String, fp_file:String) Abstract
Method CompileShader:Int(source:String, type:Int) Abstract
Method Copy:TBrush() Abstract
Function LoadDefaultShader:TShader(vp_file:String, fp_file:String) 'Abstract
''load default shader on init
''extend me
'default_shader = LoadShader(vp_file, fp_file)
End
Function LoadDefaultShader:Void(sh:TShader)
default_shader = sh
SetShader( default_shader )
End
Function LoadShader:TShader(vp_file:String, fp_file:String) 'Abstract
''extend me
End
Method IsValid:Int()
Return shader.active
End
Function PreProcess(cam:TCamera)
For Local sh:TShader = Eachin process_list
IShaderProcess(sh).PreProcess(cam)
Next
End
Function PostProcess(cam:TCamera)
For Local sh:TShader = Eachin process_list
IShaderProcess(sh).PostProcess(cam)
Next
End
''adds to TShaderProcess list in TShader
''
Function AddProcess(sh:TShader)
If IShaderProcess(sh) <> Null
sh.list_link = TShader.process_list.AddLast(sh)
Endif
End
''Custom Shader methods
''
Function SetShader(sh:TShader)
If g_shader Then g_shader.active = 0
g_shader = sh
g_shader.active = 1
End
Function DefaultShader:TShader()
If Not (g_shader) Then Return Null
g_shader.active = 0
g_shader = default_shader
g_shader.active = 1
Return default_shader
End
Method Override(i:Int)
If i Then override = True Else override = False
End
Method RenderCamera(cam:TCamera)
TRender.render.RenderCamera(cam)
End
Method Update()
''runs per each surface (if implemented in hardware render). ideal for setting uniforms
End
''-- draws entity with g_shader instead of brush shader
Method DrawEntity:Void(cam:TCamera, ent:TEntity)
''update camera, since this routine is used in shaders
Local temp1:Int = cam.cls_color
Local temp2:Int = cam.cls_zbuffer
cam.cls_color = False
cam.cls_zbuffer = False
TRender.render.UpdateCamera(cam)
cam.cls_color = temp1
cam.cls_zbuffer = temp2
Local temp:Int = ent.shader_brush.active
ent.shader_brush.active= 0
TRender.render.Render(ent,cam)
ent.shader_brush.active= temp
End
End
''
'' standard pre/post per camera
''
Interface IShaderProcess
Method PreProcess:Int(cam:TCamera) ''run before all rendering (ie. clear framebuffer)
Method PostProcess:Int(cam:TCamera) ''run after all rendering (ie. draw framebuffer, blur, etc)
End
''
''used to take control of the pipeline
''
Interface IShaderRender Extends IShaderProcess
Method Render:Int(cam:TCamera)
End
''
''used to process a pre/post shader per entity (used in brushes)
''
Interface IShaderEntity Extends IShaderProcess
Method RenderEntity:Void(cam:TCamera, ent:TEntity)
End