-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_postprocess.c
359 lines (278 loc) · 11.9 KB
/
gl_postprocess.c
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
/*
Copyright (C) 2001-2002 Charles Hollemeersch
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
PENTA: the whole file is freakin penta...
Generic post processing
Please give us decent rtt on opengl!!
Post processing is in two fases for tenebrae
Fase1:
* Effects like heat haze, gravity warps, refractions, ... they are normal polygons
rendered after all other polygons, they get acess to the rendered framebuffer
for stuff like distortions, decolorations...
Fase2:
* Effects like glare, ... these get applied after the heat haze effects etc...
*/
#include "quakedef.h"
#include "gl_arbprograms.h"
static int frameBufferTexture; // Full screen image of the current rendered view (next pow of 2)
static int smallBufferTexture1; // Textures used for downsample post process effects
static int smallBufferTexture2;
static int frameBufferWidth;
static int frameBufferHeight;
static int smallBufferWidth;
static int smallBufferHeight;
static int smallWidth;
static int smallHeight;
typedef struct {
int vp;
int fp;
} glProgram_t;
static glProgram_t downSample;
static glProgram_t blur;
static glProgram_t finalCompose;
static cvar_t gl_lumiMin = {"gl_lumiMin","0.2"};
static cvar_t gl_lumiMax = {"gl_lumiMax","0.6"};
static cvar_t gl_glareScale = {"gl_glareScale","1"};
static cvar_t gl_sceneScale = {"gl_sceneScale","1"};
cvar_t gl_postprocess = {"gl_postprocess","2"};
cvar_t post_brightness = {"post_brightness","1.1", true};
cvar_t post_contrast = {"post_contrast","1.1", true};
cvar_t post_saturation = {"post_saturation","1.2", true};
qboolean R_CardCanDoPostprocess( void ) {
return (gl_cardtype == ARB);
}
int R_UsePostprocess( void ) {
if ( !R_CardCanDoPostprocess() ) return 0;
return gl_postprocess.value;
}
void R_InitPostProcess( void ) {
void *rubbish;
// These always need to be registered because they are used by the options menu
Cvar_RegisterVariable (&post_brightness);
Cvar_RegisterVariable (&post_saturation);
Cvar_RegisterVariable (&post_contrast);
//We will never do postprocessing, save some texture memory
if ( !R_CardCanDoPostprocess() ) {
gl_postprocess.value = 0.0f;
return;
}
//Allocate textures
frameBufferTexture = texture_extension_number;
texture_extension_number++;
smallBufferTexture1 = texture_extension_number;
texture_extension_number++;
smallBufferTexture2 = texture_extension_number;
texture_extension_number++;
for (frameBufferWidth = 1; frameBufferWidth < vid.width; frameBufferWidth<<=1);
for (frameBufferHeight = 1; frameBufferHeight < vid.height; frameBufferHeight<<=1);
smallWidth = vid.width/4;
smallHeight = vid.height/4;
for (smallBufferWidth = 1; smallBufferWidth < smallWidth; smallBufferWidth<<=1);
for (smallBufferHeight = 1; smallBufferHeight < smallHeight; smallBufferHeight<<=1);
//Upoad rubbish
rubbish = malloc( frameBufferWidth* frameBufferHeight * 4 );
memset( rubbish, 0, frameBufferWidth* frameBufferHeight * 4 );
GL_Bind ( frameBufferTexture );
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, frameBufferWidth, frameBufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, rubbish);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
GL_Bind ( smallBufferTexture1 );
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, smallBufferWidth, smallBufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, rubbish);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
GL_Bind ( smallBufferTexture2 );
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, smallBufferWidth, smallBufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, rubbish);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
//Load shaders
downSample.vp = GL_LoadShader( GL_VERTEX_PROGRAM_ARB, "glprogs/downsample_vp.txt" );
downSample.fp = GL_LoadShader( GL_FRAGMENT_PROGRAM_ARB, "glprogs/downsample_fp.txt" );
blur.vp = GL_LoadShader( GL_VERTEX_PROGRAM_ARB, "glprogs/blur_vp.txt" );
blur.fp = GL_LoadShader( GL_FRAGMENT_PROGRAM_ARB, "glprogs/blur_fp.txt" );
finalCompose.vp = GL_LoadShader( GL_VERTEX_PROGRAM_ARB, "glprogs/final_vp.txt" );
finalCompose.fp = GL_LoadShader( GL_FRAGMENT_PROGRAM_ARB, "glprogs/final_fp.txt" );
Cvar_RegisterVariable (&gl_lumiMin);
Cvar_RegisterVariable (&gl_lumiMax);
Cvar_RegisterVariable (&gl_sceneScale);
Cvar_RegisterVariable (&gl_glareScale);
Cvar_RegisterVariable (&gl_postprocess);
}
void R_PostProcessFase1( void ) {
if (R_UsePostprocess() != 2) return;
GL_Bind( frameBufferTexture );
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, frameBufferWidth, frameBufferHeight );
}
static void FullScreenQuad() {
float texX = (float)vid.width / (float)frameBufferWidth;
float texY = (float)vid.height / (float)frameBufferHeight;
glBegin (GL_QUADS);
glTexCoord2f(0,1);
glVertex3f (0.1f, 1, 1);
glTexCoord2f(1,1);
glVertex3f (0.1f, -1, 1);
glTexCoord2f(1,0);
glVertex3f (0.1f, -1, -1);
glTexCoord2f(0,0);
glVertex3f (0.1f, 1, -1);
glEnd ();
}
extern cshift_t cshift_slime;
extern cshift_t cshift_lava;
float SetupForScreenFlash( void ) {
float blur = 1.0; // We modulate this down then invert at the end
//No flashing
if (!v_blend[3]) {
glColor4f ( 1.0f, 1.0f, 1.0f, 1.0f );
return 0.0f;
}
//Some fluids and
if ( memcmp( &cl.cshifts[CSHIFT_CONTENTS], &cshift_slime, sizeof(cshift_t) ) == 0 ) {
blur *= 0.25;
} else if ( memcmp( &cl.cshifts[CSHIFT_CONTENTS], &cshift_lava, sizeof(cshift_t) ) == 0 ) {
blur *= 0.5;
}
//Damage blurs view
blur *= (1.0f-(cl.cshifts[CSHIFT_DAMAGE].percent / 150.0f));
blur *= (1.0f-(cl.cshifts[CSHIFT_DAMAGE].percent / 150.0f));
glColor4fv ( v_blend );
return 1.0f-blur;
}
void R_PostProcessFase2( void ) {
float blurFactor;
if (!R_UsePostprocess()) return;
GL_Bind( frameBufferTexture );
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, frameBufferWidth, frameBufferHeight );
//Setup opengl state
glDisable (GL_BLEND);
glDisable (GL_DEPTH_TEST);
glLoadIdentity ();
glRotatef (-90, 1, 0, 0); // put Z going up
glRotatef (90, 0, 0, 1); // put Z going up
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity ();
glMatrixMode(GL_MODELVIEW);
glEnable( GL_VERTEX_PROGRAM_ARB );
glEnable( GL_FRAGMENT_PROGRAM_ARB );
//Blurring is easy by just playing with the values a bit (they can go over 1.0)
blurFactor = SetupForScreenFlash();
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 0,
1.0f / (float)frameBufferWidth, 1.0f / (float)frameBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 1,
1.0f / (float)smallBufferWidth, 1.0f / (float)smallBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 2,
(float)vid.width / (float)frameBufferWidth, (float)vid.height / (float)frameBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 3,
(float)smallWidth / (float)smallBufferWidth, (float)smallHeight / (float)smallBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_FRAGMENT_PROGRAM_ARB, 0,
gl_lumiMin.value, 1.0f / ( gl_lumiMax.value - gl_lumiMin.value ) , gl_sceneScale.value * (1-blurFactor), gl_glareScale.value );
// Downscale and other fancy stuff here
GL_Bind( frameBufferTexture );
qglBindProgramARB( GL_VERTEX_PROGRAM_ARB, downSample.vp );
qglBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, downSample.fp );
FullScreenQuad();
GL_Bind( smallBufferTexture1 );
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, smallBufferWidth, smallBufferHeight );
// Blur
GL_Bind( smallBufferTexture1 );
qglBindProgramARB( GL_VERTEX_PROGRAM_ARB, blur.vp );
qglBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, blur.fp );
qglProgramLocalParameter4fARB( GL_VERTEX_PROGRAM_ARB, 0, 0.0f, 1.0f, 0.0f, 0.0f );
FullScreenQuad();
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, smallBufferWidth, smallBufferHeight );
qglProgramLocalParameter4fARB( GL_VERTEX_PROGRAM_ARB, 0, 1.0f, 0.0f, 0.0f, 0.0f );
FullScreenQuad();
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, smallBufferWidth, smallBufferHeight );
// Final compositing pass
GL_Bind( frameBufferTexture );
GL_SelectTexture(GL_TEXTURE1_ARB);
GL_Bind( smallBufferTexture1 );
GL_SelectTexture(GL_TEXTURE0_ARB);
qglBindProgramARB( GL_VERTEX_PROGRAM_ARB, finalCompose.vp );
qglBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, finalCompose.fp );
qglProgramLocalParameter4fARB( GL_FRAGMENT_PROGRAM_ARB, 0,
post_saturation.value, post_contrast.value, post_brightness.value, blurFactor * gl_sceneScale.value );
FullScreenQuad();
// Cleanup opengl state
glDisable( GL_VERTEX_PROGRAM_ARB );
glDisable( GL_FRAGMENT_PROGRAM_ARB );
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_ALPHA_TEST);
}
void R_DrawMenuPostProcess( void ) {
float blurFactor;
if (!R_UsePostprocess()) return;
//Setup opengl state
glDisable ( GL_ALPHA_TEST );
glPushMatrix();
glLoadIdentity ();
glRotatef (-90, 1, 0, 0); // put Z going up
glRotatef (90, 0, 0, 1); // put Z going up
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity ();
glEnable( GL_VERTEX_PROGRAM_ARB );
glEnable( GL_FRAGMENT_PROGRAM_ARB );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 0,
1.0f / (float)frameBufferWidth, 1.0f / (float)frameBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 1,
1.0f / (float)smallBufferWidth, 1.0f / (float)smallBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 2,
(float)vid.width / (float)frameBufferWidth, (float)vid.height / (float)frameBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 3,
(float)smallWidth / (float)smallBufferWidth, (float)smallHeight / (float)smallBufferHeight, 0, 0 );
qglProgramEnvParameter4fARB( GL_FRAGMENT_PROGRAM_ARB, 0,
gl_lumiMin.value, 1.0f / ( gl_lumiMax.value - gl_lumiMin.value ) , 0.2 , gl_glareScale.value );
// Final compositing pass
GL_Bind( frameBufferTexture );
GL_SelectTexture(GL_TEXTURE1_ARB);
GL_Bind( smallBufferTexture1 );
GL_SelectTexture(GL_TEXTURE0_ARB);
qglBindProgramARB( GL_VERTEX_PROGRAM_ARB, finalCompose.vp );
qglBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, finalCompose.fp );
qglProgramLocalParameter4fARB( GL_FRAGMENT_PROGRAM_ARB, 0,
post_saturation.value, post_contrast.value, post_brightness.value, gl_sceneScale.value*0.8 );
FullScreenQuad();
// Cleanup opengl state
glDisable( GL_VERTEX_PROGRAM_ARB );
glDisable( GL_FRAGMENT_PROGRAM_ARB );
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glColor4f(1.0,1.0,1.0,1.0);
glEnable (GL_ALPHA_TEST);
}
int R_GetPostProcessTexture( void ) {
return frameBufferTexture;
}
static float scale_matrix[16] = {
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
float * R_GetPostProcessMatrix( void ) {
scale_matrix[0] = (float)vid.width / (float)frameBufferWidth;
scale_matrix[5] = (float)vid.height / (float)frameBufferHeight;
return scale_matrix;
}