/
CommonObjects.h
427 lines (378 loc) · 12.8 KB
/
CommonObjects.h
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
#ifndef COMMONOBJECTS_H
#define COMMONOBJECTS_H
#include <QtOpenGL>
#include <QImage>
#include <cstdio>
#include <iostream>
#include "qopenglerrorcheck.h"
#define PROGRAM_VERTEX_ATTRIBUTE 0
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
using namespace std;
enum TextureTypes{
DIFFUSE_TEXTURE = 0,
NORMAL_TEXTURE ,
SPECULAR_TEXTURE,
HEIGHT_TEXTURE,
OCCLUSION_TEXTURE
};
enum ConversionType{
CONVERT_NONE = 0,
CONVERT_FROM_H_TO_N,
CONVERT_FROM_N_TO_H,
CONVERT_FROM_D_TO_O, // diffuse to others
CONVERT_RESIZE
};
enum UVManipulationMethods{
uvTranslate = 0,
uvGrabCorners,
uvScaleXY
};
// Compressed texture type
enum CompressedFromTypes{
H_TO_D_AND_S_TO_N = 0,
S_TO_D_AND_H_TO_N = 1
};
enum TargaColorFormat{
TARGA_BGR=0,
TARGA_BGRA,
TARGA_LUMINANCE
};
#define TARGA_HEADER_SIZE 0x12
#define TARGA_UNCOMP_RGB_IMG 0x02
#define TARGA_UNCOMP_BW_IMG 0x03
// Reading and writing to file TGA image
class TargaImage{
public:
// write QImage to tga file
void write(QImage image, QString fileName);
// return QImage from readed tga file
QImage read(QString fileName);
private:
/**
Read tga image to data.
* @param filename - path to the image
* @param width (output) width of the image
* @param height (output) height of the image
* @param format (output) format of the image (RGB,RGBA,LUMINANCE)
* @param pixels (output) data of pixels
* @return returns true if image was loaded.
*/
bool load_targa (const char *filename, int &width, int &height,
TargaColorFormat &format, unsigned char *&pixels);
// The same as above but write image to file
bool save_targa (const char *filename, int width, int height,
TargaColorFormat format, unsigned char *pixels);
};
class PostfixNames{
public:
static QString diffuseName;
static QString normalName;
static QString specularName;
static QString heightName;
static QString occlusionName;
static QString outputFormat;
static QString getPostfix(TextureTypes tType){
switch(tType){
case(DIFFUSE_TEXTURE ):
return diffuseName;
break;
case(NORMAL_TEXTURE ):
return normalName;
break;
case(SPECULAR_TEXTURE):
return specularName;
break;
case(HEIGHT_TEXTURE ):
return heightName;
break;
case(OCCLUSION_TEXTURE ):
return occlusionName;
break;
default: return diffuseName;
}
}
static QString getTextureName(TextureTypes tType){
switch(tType){
case(DIFFUSE_TEXTURE ):
return "diffuse";
break;
case(NORMAL_TEXTURE ):
return "normal";
break;
case(SPECULAR_TEXTURE):
return "specular";
break;
case(HEIGHT_TEXTURE ):
return "height";
break;
case(OCCLUSION_TEXTURE ):
return "occlusion";
break;
default: return "default-diffuse";
}
}
};
struct GrayScalePreset{
float R;
float G;
float B;
int grayScaleMode;
void mode1(){
R = 1.0;
G = 1.0;
B = 1.0;
normalize();
grayScaleMode = 0;
}
void mode2(){
R = 0.3;
G = 0.59;
B = 0.11;
normalize();
grayScaleMode = 1;
}
void mode(){
if(grayScaleMode == 0) mode1();
if(grayScaleMode == 1) mode2();
}
QVector3D toQVector3D(){
return QVector3D(R,G,B);
}
void normalize(){
float sum = R + G + B;
if(fabs(sum) < 0.0001) sum = 1;
R /= sum;
G /= sum;
B /= sum;
}
};
// Methods of making the texture seamless
enum SeamlessMode{
SEAMLESS_NONE = 0,
SEAMLESS_SIMPLE,
SEAMLESS_MIRROR,
SEAMLESS_RANDOM
};
struct RandomTilingMode{
float angles[9];
float common_phase;
float inner_radius;
float outer_radius;
RandomTilingMode(){
inner_radius = 0.2;
outer_radius = 0.4;
common_phase = 0.0;
for(int i = 0; i < 9 ; i++){
angles[i] = 0;
}
}
// generate random angles
void randomize(){
for(int i = 0; i < 9 ; i++){
angles[i] = 2 * 3.1415269 * rand() / (RAND_MAX + 0.0);
}
}
};
// Wrapper for FBO initialization.
class FBOImages{
public:
static void create(QGLFramebufferObject *&fbo,int width,int height){
if(fbo !=NULL ){
fbo->release();
delete fbo;
}
QGLFramebufferObjectFormat format;
format.setInternalTextureFormat(GL_RGBA16F);
format.setTextureTarget(GL_TEXTURE_2D);
format.setMipmap(true);
fbo = new QGLFramebufferObject(width,height,format);
glBindTexture(GL_TEXTURE_2D, fbo->texture());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
float aniso = 0.0;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
GLCHK(glBindTexture(GL_TEXTURE_2D, 0));
qDebug() << "FBOImages::creatig new FBO(" << width << "," << height << ") with id=" << fbo->texture() ;
}
static void resize(QGLFramebufferObject *&src,QGLFramebufferObject *&ref){
if( ref->width() == src->width() &&
ref->height() == src->height() ){}else{
GLCHK(FBOImages::create(src ,ref->width(),ref->height()));
}
}
static void resize(QGLFramebufferObject *&src,int width, int height){
if( width == src->width() &&
height == src->height() ){}else{
GLCHK(FBOImages::create(src ,width,height));
}
}
};
// Main object. Contains information about Image and the post process parameters
class FBOImageProporties{
public:
QGLFramebufferObject *ref_fbo ; // reference image
QGLFramebufferObject *fbo ; // output image
QGLFramebufferObject *aux_fbo ; // aux image (used in post processing)
QGLFramebufferObject *aux2_fbo; // the same
GLuint scr_tex_id; // Id of texture loaded from image, from loaded file
int scr_tex_width; // width of the image loaded from file.
int scr_tex_height; // height ...
QGLWidget* glWidget_ptr; // pointer to GL context
TextureTypes imageType; // This will define what kind of preprocessing will be applied to image
// Variables used to control the image processing (most of them are controlled from GUI)
bool bFirstDraw;
bool bGrayScale;
GrayScalePreset grayScalePreset;
bool bInvertR,bInvertG,bInvertB;
bool bRemoveShading;
// Specular settings
int noRemoveShadingGaussIter;
int noBlurPasses;
bool bSpeclarControl;
int specularRadius;
float specularW1,specularW2,specularContrast,specularAmplifier;
float specularBrightness;
// General processing
float smallDetails;
float mediumDetails;
float detailDepth;
int sharpenBlurAmount;
float normalsStep;
// Conversion settings
float conversionHNDepth;
bool bConversionHN;
// Normal to Height conversion settings
bool bConversionNH;
int conversionNHItersHuge;
int conversionNHItersVeryLarge;
int conversionNHItersLarge;
int conversionNHItersMedium;
int conversionNHItersSmall;
int conversionNHItersVerySmall;
// Base to others settings
bool bConversionBaseMap;
float conversionBaseMapAmplitude;
float conversionBaseMapFlatness;
int conversionBaseMapNoIters;
int conversionBaseMapFilterRadius;
float conversionBaseMapMixNormals;
float conversionBaseMapPreSmoothRadius;
float conversionBaseMapBlending;
// ambient occlusion settings
int ssaoNoIters;
float ssaoIntensity;
float ssaoBias;
float ssaoDepth;
// height settings
float heightMinValue;
float heightMaxValue;
int heightAveragingRadius;
// global settings seamless parameters
static bool bAttachNormalToHeightMap;
static SeamlessMode seamlessMode;
static float seamlessSimpleModeRadius;
static int seamlessMirroModeType; // values: 2 - x repear, 1 - y repeat, 0 - xy repeat
static RandomTilingMode seamlessRandomTiling;
FBOImageProporties(){
ref_fbo = NULL;
fbo = NULL;
aux_fbo = NULL;
aux2_fbo = NULL;
glWidget_ptr = NULL;
bFirstDraw = true;
bGrayScale = false;
grayScalePreset.mode2();
bInvertR = bInvertG = bInvertB = false;
scr_tex_id = 0;
bRemoveShading = false;
noRemoveShadingGaussIter = 10;
bSpeclarControl = false;
specularRadius = 10;
specularW1 = 0.1;
specularW2 = 10.0;
specularContrast = 0.05;
specularAmplifier = 3.0;
specularBrightness = 0.0;
noBlurPasses = 0;
smallDetails = 0;
mediumDetails = 0;
detailDepth = 2.0;
sharpenBlurAmount = 0;
normalsStep = 0.0;
conversionHNDepth = 2.0;
bConversionHN = false;
bConversionNH = false;
conversionNHItersHuge = 10;
conversionNHItersVeryLarge = 10;
conversionNHItersLarge = 10;
conversionNHItersMedium = 10;
conversionNHItersSmall = 10;
conversionNHItersVerySmall = 10;
bConversionBaseMap = false;
conversionBaseMapAmplitude = 0;
conversionBaseMapFlatness = 0.5;
conversionBaseMapNoIters = 0;
conversionBaseMapFilterRadius = 3;
conversionBaseMapMixNormals = 1.0;
conversionBaseMapPreSmoothRadius= 0;
conversionBaseMapBlending = 1.0;
ssaoNoIters = 4;
ssaoIntensity = 1.0;
ssaoBias = 0.0;
ssaoDepth = 0.1;
heightMinValue = 0.0;
heightMaxValue = 1.0;
heightAveragingRadius = 1;
seamlessMode = SEAMLESS_NONE;
}
void init(QImage& image){
qDebug() << "<FBOImageProporties> init.";
glWidget_ptr->makeCurrent();
if(glIsTexture(scr_tex_id)) glWidget_ptr->deleteTexture(scr_tex_id);
scr_tex_id = glWidget_ptr->bindTexture(image,GL_TEXTURE_2D);
scr_tex_width = image.width();
scr_tex_height = image.height();
bFirstDraw = true;
GLCHK(FBOImages::create(ref_fbo ,image.width(),image.height()));
GLCHK(FBOImages::create(fbo ,image.width(),image.height()));
GLCHK(FBOImages::create(aux_fbo ,image.width(),image.height()));
GLCHK(FBOImages::create(aux2_fbo,image.width(),image.height()));
}
void updateSrcTexId(QGLFramebufferObject* in_ref_fbo){
glWidget_ptr->makeCurrent();
if(glIsTexture(scr_tex_id)) glWidget_ptr->deleteTexture(scr_tex_id);
QImage image = in_ref_fbo->toImage();
scr_tex_id = glWidget_ptr->bindTexture(image,GL_TEXTURE_2D);
}
void resizeFBO(int width, int height){
GLCHK(FBOImages::resize(ref_fbo ,width,height));
GLCHK(FBOImages::resize(fbo ,width,height));
GLCHK(FBOImages::resize(aux_fbo ,width,height));
GLCHK(FBOImages::resize(aux2_fbo,width,height));
bFirstDraw = true;
}
/**
* @brief getImage convert FBO image to QImage
* @return QImage
*/
QImage getImage(){
glWidget_ptr->makeCurrent();
return fbo->toImage();
}
~FBOImageProporties(){
qDebug() << "<FBOImageProporties> delete.";
glWidget_ptr->makeCurrent();
if(glIsTexture(scr_tex_id)) GLCHK(glWidget_ptr->deleteTexture(scr_tex_id));
glWidget_ptr = NULL;
if(ref_fbo != NULL ) delete ref_fbo;
if(fbo != NULL ) delete fbo;
if(aux_fbo != NULL ) delete aux_fbo;
if(aux2_fbo != NULL ) delete aux2_fbo;
}
};
#endif // COMMONOBJECTS_H