git-svn-id: svn://db.shs.com.ru/libs@702 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -301,7 +301,93 @@ void RendererBase::renderQuad(QOpenGLShaderProgram * prog, Mesh * mesh, Camera *
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
float RadicalInverse_VdC(uint bits) {
|
||||
bits = (bits << 16u) | (bits >> 16u);
|
||||
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
|
||||
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
|
||||
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
|
||||
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
|
||||
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
|
||||
}
|
||||
// ----------------------------------------------------------------------------
|
||||
QVector2D Hammersley(uint i, uint N) {
|
||||
return QVector2D(float(i)/float(N), RadicalInverse_VdC(i));
|
||||
}
|
||||
QVector3D ImportanceSampleGGX(QVector2D Xi, QVector3D N, float roughness) {
|
||||
float a = roughness*roughness;
|
||||
float phi = 2.0 * M_PI * Xi[0];
|
||||
float cosTheta = sqrt((1.0 - Xi[1]) / (1.0 + (a*a - 1.0) * Xi[1]));
|
||||
float sinTheta = sqrt(1.0 - cosTheta*cosTheta);
|
||||
// преобразование из сферических в декартовы координаты
|
||||
QVector3D H;
|
||||
H[0] = cos(phi) * sinTheta;
|
||||
H[1] = sin(phi) * sinTheta;
|
||||
H[2] = cosTheta;
|
||||
// преобразование из касательного пространства в мировые координаты
|
||||
QVector3D up = qAbs(N[2]) < 0.999 ? QVector3D(0.0, 0.0, 1.0) : QVector3D(1.0, 0.0, 0.0);
|
||||
QVector3D tangent = QVector3D::crossProduct(up, N).normalized();
|
||||
QVector3D bitangent = QVector3D::crossProduct(N, tangent);
|
||||
QVector3D sampleVec = tangent * H[0] + bitangent * H[1] + N * H[2];
|
||||
return sampleVec.normalized();
|
||||
}
|
||||
float GeometrySchlickGGX(float NdotV, float roughness) {
|
||||
float k = (roughness * roughness) / 2.0;
|
||||
float nom = NdotV;
|
||||
float denom = NdotV * (1.0 - k) + k;
|
||||
return nom / denom;
|
||||
}
|
||||
float GeometrySmith(QVector3D N, QVector3D V, QVector3D L, float roughness) {
|
||||
float NdotV = piMax(QVector3D::dotProduct(N, V), 0.f);
|
||||
float NdotL = piMax(QVector3D::dotProduct(N, L), 0.f);
|
||||
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||
return ggx1 * ggx2;
|
||||
}
|
||||
QVector2D IntegrateBRDF(float NdotV, float roughness) {
|
||||
QVector3D V;
|
||||
V[0] = sqrt(1.f - NdotV*NdotV);
|
||||
V[1] = 0.f;
|
||||
V[2] = NdotV;
|
||||
float A = 0.f;
|
||||
float B = 0.f;
|
||||
QVector3D N = QVector3D(0.f, 0.f, 1.f);
|
||||
const uint SAMPLE_COUNT = 256u;
|
||||
for(uint i = 0u; i < SAMPLE_COUNT; ++i) {
|
||||
QVector2D Xi = Hammersley(i, SAMPLE_COUNT);
|
||||
QVector3D H = ImportanceSampleGGX(Xi, N, roughness);
|
||||
QVector3D L = (2.f * QVector3D::dotProduct(V, H) * H - V).normalized();
|
||||
float NdotL = piMax(L[2], 0.f);
|
||||
float NdotH = piMax(H[2], 0.f);
|
||||
float VdotH = piMax(QVector3D::dotProduct(V, H), 0.f);
|
||||
if(NdotL > 0.f) {
|
||||
float G = GeometrySmith(N, V, L, roughness);
|
||||
float G_Vis = (G * VdotH) / (NdotH * NdotV);
|
||||
float Fc = pow(1.f - VdotH, 5.f);
|
||||
A += (1.f - Fc) * G_Vis;
|
||||
B += Fc * G_Vis;
|
||||
}
|
||||
}
|
||||
A /= float(SAMPLE_COUNT);
|
||||
B /= float(SAMPLE_COUNT);
|
||||
return QVector2D(A, B);
|
||||
}
|
||||
|
||||
void RendererBase::initCoeffTextures() {
|
||||
QImage im = QImage(":/coeffs_brdf.png").mirrored();
|
||||
int size = im.width();
|
||||
QVector<QVector2D> data(size*size);
|
||||
int ind = -1;
|
||||
for (int x = 0; x < size; ++x) {
|
||||
//float c = x / double(size - 1) + 1.E-3;
|
||||
for (int y = 0; y < size; ++y) {
|
||||
//float r = y / double(size - 1);
|
||||
QColor p = im.pixelColor(x, y);
|
||||
data[++ind] = QVector2D(p.redF(), p.greenF());//IntegrateBRDF(c, 1.f - r + 1.E-3);
|
||||
}
|
||||
}
|
||||
createCoeffTexture(tex_coeff[0], data.constData(), size, 2);
|
||||
/*const int size = 512;
|
||||
QVector<float> data_diff(size*size), data_spec(size*size);
|
||||
double r, c, c2;
|
||||
@@ -334,15 +420,19 @@ void RendererBase::initCoeffTextures() {
|
||||
}
|
||||
|
||||
|
||||
void RendererBase::createCoeffTexture(GLuint & id, const QVector<float> & data, int size) {
|
||||
void RendererBase::createCoeffTexture(GLuint & id, const void * data, int size, int channels) {
|
||||
QOpenGLExtraFunctions * f = view;
|
||||
deleteGLTexture(f, id);
|
||||
f->glGenTextures(1, &id);
|
||||
f->glBindTexture(GL_TEXTURE_2D, id);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
f->glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, size, size, 0, GL_RED, GL_FLOAT, data.constData());
|
||||
GLenum iformat = GL_R16F, format = GL_RED;
|
||||
if (channels == 2) {iformat = GL_RG16F; format = GL_RG;}
|
||||
if (channels == 3) {iformat = GL_RGB16F; format = GL_RGB;}
|
||||
if (channels == 4) {iformat = GL_RGBA16F; format = GL_RGBA;}
|
||||
f->glTexImage2D(GL_TEXTURE_2D, 0, iformat, size, size, 0, format, GL_FLOAT, data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user