Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 chris 1
unit app_glscene_blur;
2
 
3
interface
4
 
5
uses OpenGL, GLaux, glExt, sux_constant, sux_object, app_constant,
6
     resource_texture, resource_shader, resource_list;
7
 
8
type
9
  TSettings=record
10
    textures: SRList;
11
    currenttexture:SXInt;
12
    maxtexture:SXInt;
13
    timer:SXFloat;
14
    blurintensity:SXFloat;
15
    npottextures: SXBool;
16
  end;
17
  SAGLSBlur=class(SUXObject)
18
    settings:TSettings;
19
 
20
    procedure setBlurIntensity(intensity:SXFloat);
21
    procedure createTextureArray;
22
    procedure addFrameTexture;
23
    function getTexture(const index: SXInt): SRTexture;
24
 
25
    procedure renderTextures;
26
    procedure onTimer;
27
 
28
    procedure initialize;
29
    constructor Create(parent:TObject);
30
    destructor Destroy; override;
31
  end;
32
 
33
implementation
34
 
35
uses gl_main, main, gl_textureengine;
36
 
37
 
38
// --- SAGLSBlur
39
 
40
 
41
procedure SAGLSBlur.setBlurIntensity(intensity:SXFloat);
42
begin
43
  if (settings.blurintensity < intensity) then settings.blurintensity := intensity;
44
end;
45
 
46
 
47
procedure SAGLSBlur.createTextureArray;
48
var t: SXInt;
49
    texture: SRTexture;
50
begin
51
  with settings do
52
  begin
53
    for t := 0 to app.ini.settings.display.motionblurframes - 1 do
54
    begin
55
      if (npottextures) then
56
      begin
57
        texture := SRTexture.Create(self);
58
 
59
        glGenTextures(1, texture.texture.index);
60
        glBindTexture(GL_TEXTURE_2D, texture.texture.index);
61
        glTexImage2D(GL_TEXTURE_2D, 0, 4, gl.getCurrentRenderingContext.screen.width,
62
          gl.getCurrentRenderingContext.screen.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
63
 
64
        texture.texture.displayindex := texture.texture.index;
65
        texture.texture.width := gl.getCurrentRenderingContext.screen.width;
66
        texture.texture.height := gl.getCurrentRenderingContext.screen.height;
67
        texture.texture.format := SX_TEXTURE_FORMAT_RGBA;
68
        texture.setTextureClamp(true, true);
69
        texture.setTextureCompression(SX_GLTEXTURE_COMPRESSION_NONE);
70
        texture.setTextureFilter(SX_GLTEXTURE_FILTER_NONE);
71
      end else
72
      begin
73
        texture := nil;
74
        app.scene.r2t.generateScreenGrabTexture(texture);
75
      end;
76
 
77
      textures.addObject(texture);
78
    end;
79
 
80
    maxtexture := SX_NONE;
81
    currenttexture := SX_NONE;
82
  end;
83
end;
84
 
85
 
86
procedure SAGLSBlur.addFrameTexture;
87
begin
88
  with settings do
89
  begin
90
    inc(currenttexture);
91
    if (currenttexture >= app.ini.settings.display.motionblurframes) then currenttexture := 0;
92
    if (currenttexture > maxtexture) then maxtexture := currenttexture;
93
 
94
    // Now grab frame to current texture
95
    app.scene.r2t.grabScreenToTexture(getTexture(currenttexture));
96
  end;
97
end;
98
 
99
 
100
function SAGLSBlur.getTexture(const index: SXInt): SRTexture;
101
begin
102
  result := SRTexture(settings.textures.getObject(index));
103
end;
104
 
105
 
106
 
107
 
108
procedure SAGLSBlur.renderTextures;
109
var t,displayt:SXInt;
110
    intensity:SXFloat;
111
    width,height:SXFloat;
112
    incr, changeincr: SXFloat;
113
begin
114
  with settings do
115
  begin
116
    if (blurintensity<=0) and (not (app.game.settings.effects.slowmotion > 0)) then exit;
117
    if (currenttexture=SX_NONE) or (maxtexture<0) then exit;
118
    t := currenttexture - (app.ini.settings.display.motionblurframes - 1);
119
    intensity:=0;
120
 
121
    incr:=40;
122
    changeincr := -4;
123
    if (app.game.settings.effects.slowmotion > 0) then
124
    begin
125
      blurintensity := app.game.settings.effects.slowmotion;
126
      if (blurintensity > 1.1) then blurintensity := 1.1;
127
      incr := 1;
128
      changeincr := 0;
129
    end;
130
 
131
    if (settings.npottextures) then
132
    begin
133
      width := 1;
134
      height := 1;
135
    end else
136
    begin
137
      width:=gl.getCurrentRenderingContext.screen.width/app.scene.scene.framebuffer.texture.width;
138
      height:=gl.getCurrentRenderingContext.screen.height/app.scene.scene.framebuffer.texture.height;
139
    end;
140
 
141
    repeat
142
      inc(t);
143
      intensity := intensity + 0.02 * settings.blurintensity;
144
      displayt := t;
145
      while (displayt < 0) do displayt := displayt + app.ini.settings.display.motionblurframes;
146
 
147
      gl.material.resetMaterial;
148
      gl.material.setMaterial2D;
149
      gl.material.setMaterialBlending(SX_GL_BLEND_ADDITIVE,sx.convert.ColorRGBOf(1,1,1),intensity);
150
      gl.material.setMaterialTexture(getTexture(displayt));
151
      gl.material.renderMaterial;
152
 
153
      glBegin(GL_QUADS);
154
        glTexCoord2f(0,0); glVertex2f(0-incr,480+incr);
155
        glTexCoord2f(width,0); glVertex2f(640+incr,480+incr);
156
        glTexCoord2f(width,height); glVertex2f(640+incr,0-incr);
157
        glTexCoord2f(0, height); glVertex2f(0-incr,0-incr);
158
      glEnd;
159
      incr := incr + changeincr;
160
      //gl.gl2D.screen.renderFrameBufferTextureOnScreen(textures[displayt],SX_GL_BLEND_ADDITIVE,sx.convert.ColorRGBOf(1,1,1),intensity);
161
    until (t=currenttexture);
162
 
163
    sx.timer.decTimer(blurintensity);
164
  end;
165
end;
166
 
167
 
168
procedure SAGLSBlur.onTimer;
169
begin
170
  if (not app.ini.settings.display.motionblur) then exit;
171
  if (app.ini.settings.display.motionblurframes < 1) then exit;
172
  if (app.ini.settings.display.motionblurframes > 10) then app.ini.settings.display.motionblurframes := 10;
173
 
174
  // Add a new texture to the blur array
175
  sx.timer.incTimer(settings.timer);
176
  if (settings.timer>=SX_GLBLUR_FRAMEINTENSITY) then
177
  begin
178
    settings.timer:=settings.timer-SX_GLBLUR_FRAMEINTENSITY;
179
    addFrameTexture;
180
  end;
181
 
182
  // Blur all textures together to create motion blur effect
183
  renderTextures;
184
end;
185
 
186
 
187
 
188
 
189
procedure SAGLSBlur.initialize;
190
begin
191
  if (not app.ini.settings.display.motionblur) then exit;
192
  if (app.ini.settings.display.motionblurframes < 1) then exit;
193
 
194
  settings.npottextures := glExt.isSupported('GL_ARB_texture_non_power_of_two');
195
 
196
  createTextureArray;
197
end;
198
 
199
 
200
constructor SAGLSBlur.Create(parent:TObject);
201
begin
202
  inherited Create(parent,'GL Blur');
203
 
204
  settings.textures := SRList.Create(self, true, true, true);
205
  settings.blurintensity:=0;
206
end;
207
 
208
 
209
destructor SAGLSBlur.Destroy;
210
begin
211
  settings.textures.Free;
212
 
213
  inherited;
214
end;
215
 
216
 
217
end.