-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApus.Engine.PainterGL2.pas
326 lines (280 loc) · 11.1 KB
/
Apus.Engine.PainterGL2.pas
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
// Class for painting routines using OpenGL: programmable-function pipeline (GLES 2.0+)
//
// Copyright (C) 2011-2014 Ivan Polyacov, Apus Software ([email protected])
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
{$IFDEF IOS}{$DEFINE GLES} {$DEFINE GLES20} {$ENDIF}
{$IFDEF ANDROID}{$DEFINE GLES} {$DEFINE GLES20} {$ENDIF}
unit Apus.Engine.PainterGL2;
interface
uses Types, Apus.Engine.API, Apus.Engine.PainterBase, Apus.Engine.PainterGL;
type
{ TGLPainter2 }
TGLPainter2=class(TGLPainter)
constructor Create;
destructor Destroy; override;
procedure BeginPaint(target:TTexture); override;
// Режим работы
procedure SetMode(blend:TBlendingMode); override;
procedure SetTexMode(stage:byte;colorMode,alphaMode:TTexBlendingMode;filter:
TTexFilter=fltUndefined;intFactor:single=0.0); override; // Режим текстурирования
procedure UseCustomShader; override;
procedure ResetTexMode; override;
procedure Restore; override;
function TestTransformation(source:TPoint3):TPoint3;
function GetMVPMatrix:T3DMatrix; override;
protected
defaultShader:integer;
MVP:T3DMatrix;
uMVP,uTex1,uTexmode:integer; // uniform locations
curTexMode:int64; // описание режима текстурирования, установленного клиентским кодом
actualTexMode:int64; // фактически установленный режим текстурирования
actualShader:byte; // тип текущего шейдера
actualAttribArrays:shortint; // кол-во включенных аттрибутов (-1 - неизвестно, 2+кол-во текстур)
function SetStates(state:byte;primRect:TRect;tex:TTexture=nil):boolean; override; // возвращает false если примитив полностью отсекается
procedure renderDevice.Draw(primType,primCount:integer;vertices:pointer;stride:integer); override;
procedure DrawPrimitivesMulti(primType,primCount:integer;vertices:pointer;stride:integer;stages:integer); override;
procedure DrawPrimitivesFromBuf(primType,primCount,vrtStart:integer;
vertexBuf:TPainterBuffer;stride:integer); override;
procedure DrawIndexedPrimitives(primType:integer;vertexBuf,indBuf:TPainterBuffer;
stride:integer;vrtStart,vrtCount:integer; indStart,primCount:integer); override;
procedure DrawIndexedPrimitivesDirectly(primType:integer;vertexBuf:PVertex;indBuf:PWord;
stride:integer;vrtStart,vrtCount:integer; indStart,primCount:integer); override;
procedure SetGLMatrix(mType:TMatrixType;mat:PDouble); override;
function SetCustomProgram(mode:int64):integer;
end;
implementation
uses Apus.Common, SysUtils, Apus.Structs,
{$IFDEF GLES}gles20,
{$ELSE}dglOpenGL,{$ENDIF}
Apus.Images, Apus.Engine.GLImages, Apus.Geom2D, Apus.Geom3D;
{$IFNDEF GLES}
const
GL_FALSE=false;
GL_TRUE=true;
{$ENDIF}
const
AS_DEFAULT = 0; // Стандартный шейдер (для основных режимов блендинга)
AS_CUSTOMIZED = 1; // Специальный шейдер, созданный под выбранные параметры блендинга
AS_OWN = 2; // Внешний (клиентский) шейдер
DEFAULT_TEX_MODE = ord(tblModulate2X)+ord(tblModulate) shl 4; // стандартный режим блендинга
type
TTexMode=array[0..3] of word;
PTexMode=^TTexMode;
var
customShaders:TSimpleHash;
{ TGLPainter2 }
procedure TGLPainter2.SetGLMatrix(mType: TMatrixType; mat: PDouble);
var
tmp:T3DMatrix;
m:T3DMatrixS;
begin
MultMat4(objMatrix,viewMatrix,tmp);
MultMat4(tmp,projMatrix,MVP);
m:=Matrix4s(MVP);
if actualShader<>AS_OWN then
glUniformMatrix4fv(uMVP,1,GL_FALSE,@m);
end;
procedure TGLPainter2.SetTexMode(stage: byte; colorMode, alphaMode: TTexBlendingMode;
filter: TTexFilter=fltUndefined; intFactor:single=0.0);
var
i:integer;
tm:PTexMode;
b:byte;
begin
ASSERT(stage in [0..3]);
if filter<>fltUndefined then curFilters[stage]:=filter;
tm:=@curTexMode;
if colorMode=tblDisable then begin
for i:=stage to 3 do tm[i]:=0;
exit;
end;
b:=byte(tm[stage]);
if colorMode<>tblNone then b:=(b and $F0)+ord(colorMode);
if alphaMode<>tblNone then b:=(b and $0F)+ord(alphaMode) shl 4;
tm[stage]:=word(b)+round(intFactor*255) shl 8;
end;
procedure TGLPainter2.UseCustomShader;
begin
actualShader:=AS_OWN;
end;
function TGLPainter2.SetStates(state: byte; primRect: TRect; tex: TTexture): boolean;
var
op:TPoint;
i,n,prog:integer;
tm:int64;
m:TMatrix4s;
begin
// Override color blending mode for alpha only textures
if (tex<>nil) and (state=STATE_TEXTURED2X) then
if (tex.PixelFormat in [ipfA8,ipfA4]) then state:=STATE_COLORED2X;
// Setup shader
if (actualShader<>AS_OWN) and (curTexMode<>actualTexMode) then begin
actualTexMode:=curTexMode;
if curTexMode=DEFAULT_TEX_MODE then begin
glUseProgram(defaultShader);
m:=Matrix4s(MVP);
glUniformMatrix4fv(uMVP,1,GL_FALSE,@m);
actualShader:=AS_DEFAULT;
end else begin
prog:=SetCustomProgram(curTexMode);
actualShader:=AS_CUSTOMIZED;
// Interpolation factors and MVP matrix
m:=Matrix4s(MVP);
glUniformMatrix4fv(glGetUniformLocation(prog,'uMVP'),1,GL_FALSE,@m);
tm:=actualTexMode;
for i:=1 to 4 do begin
if (tm and $0f=ord(tblInterpolate)) or
(tm and $f0=ord(tblInterpolate) shl 4) then
glUniform1f(glGetUniformLocation(prog,PAnsiChar(AnsiString('uFactor'+inttostr(i)))),1-1/255*((tm shr 8) and $FF));
tm:=tm shr 16;
end;
end;
end;
// Blending settings
if curstate<>state then begin // Update 1) number of vertex arrays, 2) texMode for defaultShader
// number of vertex attrib arrays
case state of
STATE_TEXTURED2X,STATE_COLORED2X: n:=3;
STATE_COLORED: n:=2;
STATE_MULTITEX: begin
n:=3;
if actualTexMode and $FF0000>0 then inc(n);
if actualTexMode and $FF00000000>0 then inc(n);
if actualTexMode and $FF000000000000>0 then inc(n);
end;
end;
if n<>actualAttribArrays then begin
if actualAttribArrays>n then
while actualAttribArrays>n do begin
dec(actualAttribArrays);
glDisableVertexAttribArray(actualAttribArrays);
end else
while actualAttribArrays<n do begin
glEnableVertexAttribArray(actualAttribArrays);
inc(actualAttribArrays);
end;
end;
// Set blending mode for default shader
if actualShader=AS_DEFAULT then glUniform1i(uTexmode,state);
curstate:=state;
end;
end;
procedure TGLPainter2.ResetTexMode;
begin
actualShader:=AS_DEFAULT;
glUseProgram(defaultShader);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glDisableVertexAttribArray(3);
SetTexMode(0,tblModulate2X,tblModulate);
SetTexMode(1,tblDisable,tblDisable);
curState:=0;
end;
procedure TGLPainter2.Restore;
begin
inherited;
ResetTexMode;
end;
procedure TGLPainter2.DrawPrimitivesFromBuf(primType, primCount,
vrtStart: integer; vertexBuf: TPainterBuffer; stride: integer);
begin
end;
procedure TGLPainter2.DrawIndexedPrimitives(primType: integer; vertexBuf,
indBuf: TPainterBuffer; stride: integer; vrtStart, vrtCount: integer;
indStart, primCount: integer);
var
vrt:PVertex;
ind:Pointer;
begin
case vertexBuf of
vertBuf:vrt:=@partBuf[0];
textVertBuf:vrt:=@txtBuf[0];
else raise EWarning.Create('DIP: Wrong vertbuf');
end;
case indBuf of
partIndBuf:ind:=@partind[indStart];
bandIndBuf:ind:=@bandInd[indStart];
else raise EWarning.Create('DIP: Wrong indbuf');
end;
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,stride,@vrt.x);
glVertexAttribPointer(1,4,GL_UNSIGNED_BYTE,GL_TRUE,stride,@vrt.color);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,stride,@vrt.u);
case primtype of
LINE_LIST:glDrawElements(GL_LINES,primCount*2,GL_UNSIGNED_SHORT,ind);
LINE_STRIP:glDrawElements(GL_LINE_STRIP,primCount+1,GL_UNSIGNED_SHORT,ind);
TRG_LIST:glDrawElements(GL_TRIANGLES,primCount*3,GL_UNSIGNED_SHORT,ind);
TRG_FAN:glDrawElements(GL_TRIANGLE_FAN,primCount+2,GL_UNSIGNED_SHORT,ind);
TRG_STRIP:glDrawElements(GL_TRIANGLE_STRIP,primCount+2,GL_UNSIGNED_SHORT,ind);
end;
end;
procedure TGLPainter2.DrawIndexedPrimitivesDirectly(primType: integer;
vertexBuf: PVertex; indBuf: PWord; stride: integer; vrtStart,
vrtCount: integer; indStart, primCount: integer);
begin
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,stride,@vertexbuf.x);
glVertexAttribPointer(1,4,GL_UNSIGNED_BYTE,GL_TRUE,stride,@vertexbuf.color);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,stride,@vertexbuf.u);
case primtype of
LINE_LIST:glDrawElements(GL_LINES,primCount*2,GL_UNSIGNED_SHORT,indBuf);
LINE_STRIP:glDrawElements(GL_LINE_STRIP,primCount+1,GL_UNSIGNED_SHORT,indBuf);
TRG_LIST:glDrawElements(GL_TRIANGLES,primCount*3,GL_UNSIGNED_SHORT,indBuf);
TRG_FAN:glDrawElements(GL_TRIANGLE_FAN,primCount+2,GL_UNSIGNED_SHORT,indBuf);
TRG_STRIP:glDrawElements(GL_TRIANGLE_STRIP,primCount+2,GL_UNSIGNED_SHORT,indBuf);
end;
end;
const
mainVertexShader=
'uniform mat4 uMVP; '#13#10+
'attribute vec3 aPosition; '#13#10+
'attribute vec4 aColor; '#13#10+
'attribute vec2 aTexcoord; '#13#10+
'varying vec2 vTexcoord; '#13#10+
'varying vec4 vColor; '#13#10+
'void main() '#13#10+
'{ '#13#10+
' vTexcoord = aTexcoord; '#13#10+
' vColor = aColor; '#13#10+
' gl_Position = uMVP * vec4(aPosition, 1.0); '#13#10+
'}';
mainFragmentShader=
'#version 130'#13#10+
'precision mediump float; '#13#10+
'uniform sampler2D tex1; '#13#10+
'uniform int texmode; '#13#10+
'varying vec2 vTexcoord; '#13#10+
'varying vec4 vColor; '#13#10+
'void main() '#13#10+
'{ '#13#10+
' if (texmode==1) { gl_FragColor = vec4(2.0, 2.0, 2.0, 1.0)*vColor*texture2D(tex1,vTexcoord); } else '#13#10+
' if (texmode==2) { gl_FragColor = vColor; } else '#13#10+
' if (texmode==4) { vec4 value=vColor; value.a=value.a*texture2D(tex1,vTexcoord).a; gl_FragColor = value; }; '#13#10+
'}';
constructor TGLPainter2.Create;
begin
inherited;
customShaders.Init(20);
defaultShader:=BuildShaderProgram(mainVertexShader,mainFragmentShader,'aPosition,aColor,aTexcoord');
if defaultShader>0 then begin
uMVP:=glGetUniformLocation(defaultShader,'uMVP');
uTex1:=glGetUniformLocation(defaultShader,'tex1');
uTexMode:=glGetUniformLocation(defaultShader,'texmode');
end;
CheckForGLError;
actualTexMode:=0;
SetTexMode(0,tblModulate2X,tblModulate,fltBilinear);
glUseProgram(defaultShader);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
actualAttribArrays:=3;
CheckForGLError;
ForceLogMessage('PainterGL2 created, DSh='+inttostr(defaultShader));
end;
destructor TGLPainter2.Destroy;
begin
glDeleteProgram(defaultShader);
inherited;
end;
end.