forked from kmkolasinski/AwesomeBump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonObjects.h
539 lines (461 loc) · 15.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#ifndef COMMONOBJECTS_H
#define COMMONOBJECTS_H
#include <QtOpenGL>
#include <QImage>
#include <cstdio>
#include <iostream>
#include "qopenglerrorcheck.h"
#include <QOpenGLFunctions_3_3_Core>
#include "properties/ImageProperties.peg.h"
#define TAB_SETTINGS 9
#define TAB_TILING 10
#ifdef Q_OS_MAC
# define AB_INI "AwesomeBump.ini"
# define AB_LOG "AwesomeBump.log"
#else
# define AB_INI "config.ini"
# define AB_LOG "log.txt"
#endif
//#define TEXTURE_FORMAT GL_RGB16F
#define TEXTURE_FORMAT GL_RGB16F
#define TEXTURE_3DRENDER_FORMAT GL_RGB16F
#define KEY_SHOW_MATERIALS Qt::Key_S
//#define USE_OPENGL_330
#ifdef USE_OPENGL_330
#define AWESOME_BUMP_VERSION "AwesomeBump " VERSION_STRING " (2016) (openGL 330 release)"
#else
#define AWESOME_BUMP_VERSION "AwesomeBump " VERSION_STRING " (2016)"
#endif
using namespace std;
enum TextureTypes{
DIFFUSE_TEXTURE = 0,
NORMAL_TEXTURE ,
SPECULAR_TEXTURE,
HEIGHT_TEXTURE,
OCCLUSION_TEXTURE,
ROUGHNESS_TEXTURE,
METALLIC_TEXTURE,
MATERIAL_TEXTURE,
GRUNGE_TEXTURE,
MAX_TEXTURES_TYPE
};
enum ConversionType{
CONVERT_NONE = 0,
CONVERT_FROM_H_TO_N,
CONVERT_FROM_N_TO_H,
CONVERT_FROM_D_TO_O, // diffuse to others
CONVERT_FROM_HN_TO_OC,
CONVERT_RESIZE
};
enum UVManipulationMethods{
UV_TRANSLATE = 0,
UV_GRAB_CORNERS,
UV_SCALE_XY
};
enum ShadingType{
SHADING_RELIEF_MAPPING = 0,
SHADING_PARALLAX_NORMAL_MAPPING,
SHADING_TESSELATION
};
enum ShadingModel{
SHADING_MODEL_PBR = 0,
SHADING_MODEL_BUMP_MAPPING
};
// Methods of making the texture seamless
enum SeamlessMode{
SEAMLESS_NONE = 0,
SEAMLESS_SIMPLE,
SEAMLESS_MIRROR,
SEAMLESS_RANDOM
};
// Compressed texture type
enum CompressedFromTypes{
H_TO_D_AND_S_TO_N = 0,
S_TO_D_AND_H_TO_N = 1
};
// Selective blur methods
enum SelectiveBlurType{
SELECTIVE_BLUR_LEVELS = 0,
SELECTIVE_BLUR_DIFFERENCE_OF_GAUSSIANS
};
enum TargaColorFormat{
TARGA_BGR=0,
TARGA_BGRA,
TARGA_LUMINANCE
};
enum SourceImageType{
INPUT_NONE = 0,
INPUT_FROM_HEIGHT_INPUT,
INPUT_FROM_HEIGHT_OUTPUT,
INPUT_FROM_NORMAL_INPUT,
INPUT_FROM_NORMAL_OUTPUT,
INPUT_FROM_SPECULAR_INPUT,
INPUT_FROM_SPECULAR_OUTPUT,
INPUT_FROM_DIFFUSE_INPUT,
INPUT_FROM_DIFFUSE_OUTPUT,
INPUT_FROM_OCCLUSION_INPUT,
INPUT_FROM_ROUGHNESS_INPUT,
INPUT_FROM_ROUGHNESS_OUTPUT,
INPUT_FROM_METALLIC_INPUT,
INPUT_FROM_METALLIC_OUTPUT,
INPUT_FROM_HI_NI,
INPUT_FROM_HO_NO
};
enum ColorPickerMethod{
COLOR_PICKER_METHOD_A = 0,
COLOR_PICKER_METHOD_B ,
};
enum MaterialIndicesType{
MATERIALS_DISABLED = -10,
MATERIALS_ENABLED = -1
};
#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 roughnessName;
static QString metallicName;
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;
case(ROUGHNESS_TEXTURE ):
return roughnessName;
break;
case(METALLIC_TEXTURE ):
return metallicName;
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;
case(ROUGHNESS_TEXTURE ):
return "roughness";
break;
case(METALLIC_TEXTURE ):
return "metallic";
break;
case(MATERIAL_TEXTURE ):
return "material";
case(GRUNGE_TEXTURE ):
return "grunge";
break;
default: return "default-diffuse";
}
}
};
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(){
static int seed = 312;
qsrand(seed);
seed = qrand() % 41211; // fake seed
for(int i = 0; i < 9 ; i++){
angles[i] = 2 * 3.1415269 * qrand() / (RAND_MAX + 0.0);
}
}
};
/**
* All settings for the 3D widget
*/
class Display3DSettings{
public:
float depthScale;
float uvScale;
QVector2D uvOffset;
float specularIntensity;
float diffuseIntensity;
float lightPower ;
float lightRadius;
ShadingType shadingType;
ShadingModel shadingModel;
// rendering quality settings
bool bUseCullFace;
bool bUseSimplePBR;
int noTessSubdivision;
int noPBRRays;
bool bBloomEffect;
bool bDofEffect;
bool bShowTriangleEdges;
bool bLensFlares;
static float openGLVersion;
Display3DSettings(){
depthScale = 0.1;
uvScale = 1.0;
uvOffset = QVector2D(0.0,0.0);
specularIntensity = 1.0;
diffuseIntensity = 1.0;
lightPower = 0.1;
lightRadius = 0.1;
shadingType = SHADING_RELIEF_MAPPING;
shadingModel = SHADING_MODEL_PBR;
bUseCullFace = false;
bUseSimplePBR = false;
noTessSubdivision = 16;
noPBRRays = 15;
bBloomEffect = true;
bDofEffect = true;
bShowTriangleEdges = false;
bLensFlares = true;
}
};
// Wrapper for FBO initialization.
class FBOImages {
public:
static void create(QGLFramebufferObject *&fbo,int width,int height,GLuint internal_format = TEXTURE_FORMAT){
if(fbo)
{
fbo->release();
delete fbo;
}
QGLFramebufferObjectFormat format;
format.setInternalTextureFormat(internal_format);
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);
if(FBOImages::bUseLinearInterpolation){
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}else{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
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::creating new FBO(" << width << "," << height << ") with id=" << fbo->texture() ;
}
static void resize(QGLFramebufferObject *&src,QGLFramebufferObject *&ref,GLuint internal_format = TEXTURE_FORMAT){
if(src == NULL){
GLCHK(FBOImages::create(src ,ref->width(),ref->height(),internal_format));
}else if( ref->width() == src->width() &&
ref->height() == src->height() ){}else{
GLCHK(FBOImages::create(src ,ref->width(),ref->height(),internal_format));
}
}
static void resize(QGLFramebufferObject *&src,int width, int height,GLuint internal_format = TEXTURE_FORMAT){
if(!src){
GLCHK(FBOImages::create(src ,width,height,internal_format));
}else if( width == src->width() &&
height == src->height() ){}else{
GLCHK(FBOImages::create(src ,width,height,internal_format));
}
}
public:
static bool bUseLinearInterpolation;
};
struct BaseMapConvLevelProperties{
float conversionBaseMapAmplitude;
float conversionBaseMapFlatness;
int conversionBaseMapNoIters;
float conversionBaseMapFilterRadius;
float conversionBaseMapMixNormals;
float conversionBaseMapPreSmoothRadius;
float conversionBaseMapBlending;
BaseMapConvLevelProperties(){
conversionBaseMapAmplitude = 0;
conversionBaseMapFlatness = 0.5;
conversionBaseMapNoIters = 0;
conversionBaseMapFilterRadius = 3;
conversionBaseMapMixNormals = 1.0;
conversionBaseMapPreSmoothRadius= 0;
conversionBaseMapBlending = 1.0;
}
void fromProperty(QtnPropertySetConvertsionBaseMapLevelProperty& level){
conversionBaseMapAmplitude = level.Amplitude;
conversionBaseMapFlatness = level.Flatness;
conversionBaseMapNoIters = level.NumIters;
conversionBaseMapFilterRadius = level.FilterRadius;
conversionBaseMapMixNormals = level.Edges;
conversionBaseMapPreSmoothRadius= level.PreSmoothRadius;
conversionBaseMapBlending = level.Blending;
}
};
// Main object. Contains information about Image and the post process parameters
class FBOImageProporties{
public:
QtnPropertySetFormImageProp* properties;
bool bSkipProcessing;
QGLFramebufferObject *fbo ; // output image
GLuint scr_tex_id; // Id of texture loaded from image, from loaded file
GLuint normalMixerInputTexId; // Used only by normal texture
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
bool bFirstDraw;
// Conversion settings
float conversionHNDepth;
// Base to others settings
static bool bConversionBaseMap;
static bool bConversionBaseMapShowHeightTexture;
BaseMapConvLevelProperties baseMapConvLevels[4];
// Input image type
SourceImageType inputImageType;
static SeamlessMode seamlessMode;
static float seamlessSimpleModeRadius;
static int seamlessMirroModeType; // values: 2 - x repear, 1 - y repeat, 0 - xy repeat
static RandomTilingMode seamlessRandomTiling;
static float seamlessContrastStrenght;
static float seamlessContrastPower;
static int seamlessSimpleModeDirection;
static SourceImageType seamlessContrastInputType;
static bool bSeamlessTranslationsFirst;
static int currentMaterialIndeks;
FBOImageProporties(){
bSkipProcessing = false;
properties = NULL;
fbo = NULL;
normalMixerInputTexId = 0;
glWidget_ptr = NULL;
bFirstDraw = true;
scr_tex_id = 0;
conversionHNDepth = 2.0;
bConversionBaseMap = false;
inputImageType = INPUT_NONE;
seamlessMode = SEAMLESS_NONE;
properties = new QtnPropertySetFormImageProp;
}
void copySettings(FBOImageProporties &src){
bFirstDraw = src.bFirstDraw;
conversionHNDepth = src.conversionHNDepth;
bConversionBaseMap = src.bConversionBaseMap;
inputImageType = src.inputImageType;
if(properties != NULL && src.properties != NULL ) properties->copyValues(src.properties);
}
void init(QImage& image){
qDebug() << Q_FUNC_INFO;
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;
/*
switch(imageType){
case(HEIGHT_TEXTURE):
case(OCCLUSION_TEXTURE):
GLCHK(FBOImages::create(fbo ,image.width(),image.height(),GL_R16F));
break;
default:
GLCHK(FBOImages::create(fbo ,image.width(),image.height()));
break;
}
*/
GLuint internal_format = TEXTURE_FORMAT;
if(imageType == HEIGHT_TEXTURE) internal_format = TEXTURE_3DRENDER_FORMAT;
GLCHK(FBOImages::create(fbo , image.width(), image.height(),internal_format));
}
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){
GLuint internal_format = TEXTURE_FORMAT;
if(imageType == HEIGHT_TEXTURE) internal_format = TEXTURE_3DRENDER_FORMAT;
GLCHK(FBOImages::resize(fbo,width,height,internal_format));
bFirstDraw = true;
}
/**
* @brief getImage convert FBO image to QImage
* @return QImage
*/
QImage getImage(){
glWidget_ptr->makeCurrent();
return fbo->toImage();
}
~FBOImageProporties(){
if(glWidget_ptr != NULL){
qDebug() << Q_FUNC_INFO;
glWidget_ptr->makeCurrent();
if(glIsTexture(normalMixerInputTexId)) glWidget_ptr->deleteTexture(normalMixerInputTexId);
if(glIsTexture(scr_tex_id)) GLCHK(glWidget_ptr->deleteTexture(scr_tex_id));
normalMixerInputTexId = 0;
scr_tex_id = 0;
glWidget_ptr = NULL;
//qDebug() << "p=" << properties;
if(properties != NULL ) delete properties;
if(fbo != NULL ) delete fbo;
properties = NULL;
fbo = NULL;
}
}
};
#endif // COMMONOBJECTS_H