-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathnormalmapgenerator.cpp
More file actions
179 lines (147 loc) · 8.7 KB
/
Copy pathnormalmapgenerator.cpp
File metadata and controls
179 lines (147 loc) · 8.7 KB
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
/********************************************************************************
* Copyright (C) 2015 by Simon Wendsche *
* *
* This file is part of NormalmapGenerator. *
* *
* NormalmapGenerator 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 3 of the License, or *
* (at your option) any later version. *
* *
* NormalmapGenerator 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, see <http://www.gnu.org/licenses/>. *
* *
* Sourcecode: https://github.com/Theverat/NormalmapGenerator *
********************************************************************************/
#include "normalmapgenerator.h"
#include <QVector3D>
#include <QColor>
NormalmapGenerator::NormalmapGenerator(IntensityMap::Mode mode, double redMultiplier, double greenMultiplier, double blueMultiplier, double alphaMultiplier)
: tileable(false), redMultiplier(redMultiplier), greenMultiplier(greenMultiplier), blueMultiplier(blueMultiplier), alphaMultiplier(alphaMultiplier), mode(mode)
{}
const IntensityMap& NormalmapGenerator::getIntensityMap() const {
return this->intensity;
}
QImage NormalmapGenerator::calculateNormalmap(const QImage& input, Kernel kernel, double strength, bool invert, bool tileable,
bool keepLargeDetail, int largeDetailScale, double largeDetailHeight) {
this->tileable = tileable;
this->intensity = IntensityMap(input, mode, redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier);
if(!invert) {
// The default "non-inverted" normalmap looks wrong in renderers,
// so I use inversion by default
intensity.invert();
}
QImage result(input.width(), input.height(), QImage::Format_ARGB32);
// optimization
double strengthInv = 1.0 / strength;
#pragma omp parallel for // OpenMP
//code from http://stackoverflow.com/a/2368794
for(int y = 0; y < input.height(); y++) {
QRgb *scanline = (QRgb*) result.scanLine(y);
for(int x = 0; x < input.width(); x++) {
const double topLeft = intensity.at(handleEdges(x - 1, input.width()), handleEdges(y - 1, input.height()));
const double top = intensity.at(handleEdges(x - 1, input.width()), handleEdges(y, input.height()));
const double topRight = intensity.at(handleEdges(x - 1, input.width()), handleEdges(y + 1, input.height()));
const double right = intensity.at(handleEdges(x, input.width()), handleEdges(y + 1, input.height()));
const double bottomRight = intensity.at(handleEdges(x + 1, input.width()), handleEdges(y + 1, input.height()));
const double bottom = intensity.at(handleEdges(x + 1, input.width()), handleEdges(y, input.height()));
const double bottomLeft = intensity.at(handleEdges(x + 1, input.width()), handleEdges(y - 1, input.height()));
const double left = intensity.at(handleEdges(x, input.width()), handleEdges(y - 1, input.height()));
const double convolution_kernel[3][3] = {{topLeft, top, topRight},
{left, 0.0, right},
{bottomLeft, bottom, bottomRight}};
QVector3D normal(0, 0, 0);
if(kernel == SOBEL)
normal = sobel(convolution_kernel, strengthInv);
else if(kernel == PREWITT)
normal = prewitt(convolution_kernel, strengthInv);
scanline[x] = qRgb(mapComponent(normal.x()), mapComponent(normal.y()), mapComponent(normal.z()));
}
}
if(keepLargeDetail) {
//generate a second normalmap from a downscaled input image, then mix both normalmaps
int largeDetailMapWidth = (int) (((double)input.width() / 100.0) * largeDetailScale);
int largeDetailMapHeight = (int) (((double)input.height() / 100.0) * largeDetailScale);
//create downscaled version of input
QImage inputScaled = input.scaled(largeDetailMapWidth, largeDetailMapHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//compute downscaled normalmap
QImage largeDetailMap = calculateNormalmap(inputScaled, kernel, largeDetailHeight, invert, tileable, false, 0, 0.0);
//scale map up
largeDetailMap = largeDetailMap.scaled(input.width(), input.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
#pragma omp parallel for // OpenMP
//mix the normalmaps
for(int y = 0; y < input.height(); y++) {
QRgb *scanlineResult = (QRgb*) result.scanLine(y);
QRgb *scanlineLargeDetail = (QRgb*) largeDetailMap.scanLine(y);
for(int x = 0; x < input.width(); x++) {
const QRgb colorResult = scanlineResult[x];
const QRgb colorLargeDetail = scanlineLargeDetail[x];
const int r = blendSoftLight(qRed(colorResult), qRed(colorLargeDetail));
const int g = blendSoftLight(qGreen(colorResult), qGreen(colorLargeDetail));
const int b = blendSoftLight(qBlue(colorResult), qBlue(colorLargeDetail));
scanlineResult[x] = qRgb(r, g, b);
}
}
}
return result;
}
QVector3D NormalmapGenerator::sobel(const double convolution_kernel[3][3], double strengthInv) const {
const double top_side = convolution_kernel[0][0] + 2.0 * convolution_kernel[0][1] + convolution_kernel[0][2];
const double bottom_side = convolution_kernel[2][0] + 2.0 * convolution_kernel[2][1] + convolution_kernel[2][2];
const double right_side = convolution_kernel[0][2] + 2.0 * convolution_kernel[1][2] + convolution_kernel[2][2];
const double left_side = convolution_kernel[0][0] + 2.0 * convolution_kernel[1][0] + convolution_kernel[2][0];
const double dY = right_side - left_side;
const double dX = bottom_side - top_side;
const double dZ = strengthInv;
return QVector3D(dX, dY, dZ).normalized();
}
QVector3D NormalmapGenerator::prewitt(const double convolution_kernel[3][3], double strengthInv) const {
const double top_side = convolution_kernel[0][0] + convolution_kernel[0][1] + convolution_kernel[0][2];
const double bottom_side = convolution_kernel[2][0] + convolution_kernel[2][1] + convolution_kernel[2][2];
const double right_side = convolution_kernel[0][2] + convolution_kernel[1][2] + convolution_kernel[2][2];
const double left_side = convolution_kernel[0][0] + convolution_kernel[1][0] + convolution_kernel[2][0];
const double dY = right_side - left_side;
const double dX = top_side - bottom_side;
const double dZ = strengthInv;
return QVector3D(dX, dY, dZ).normalized();
}
int NormalmapGenerator::handleEdges(int iterator, int maxValue) const {
if(iterator >= maxValue) {
//move iterator from end to beginning + overhead
if(tileable)
return maxValue - iterator;
else
return maxValue - 1;
}
else if(iterator < 0) {
//move iterator from beginning to end - overhead
if(tileable)
return maxValue + iterator;
else
return 0;
}
else {
return iterator;
}
}
//transform -1..1 to 0..255
int NormalmapGenerator::mapComponent(double value) const {
return (value + 1.0) * (255.0 / 2.0);
}
//uses a similar algorithm like "soft light" in PS,
//see http://www.michael-kreil.de/algorithmen/photoshop-layer-blending-equations/index.php
int NormalmapGenerator::blendSoftLight(int color1, int color2) const {
const float a = color1;
const float b = color2;
if(2.0f * b < 255.0f) {
return (int) (((a + 127.5f) * b) / 255.0f);
}
else {
return (int) (255.0f - (((382.5f - a) * (255.0f - b)) / 255.0f));
}
}