sRGBAfloat cRenderWorker::LightShading(const sShaderInputData &input, cLights::sLight light, int number, sRGBAfloat *outSpecular) { sRGBAfloat shading; CVector3 d = light.position - input.point; double distance = d.Length(); //angle of incidence CVector3 lightVector = d; lightVector.Normalize(); double intensity = 100.0 * light.intensity / (distance * distance) / number; double shade = input.normal.Dot(lightVector); if (shade < 0) shade = 0; shade = shade * intensity; if (shade > 500.0) shade = 500.0; //specular CVector3 half = lightVector - input.viewVector; half.Normalize(); double shade2 = input.normal.Dot(half); if (shade2 < 0.0) shade2 = 0.0; shade2 = pow(shade2, 30.0) * 1.0; shade2 *= intensity * params->specular; if (shade2 > 15.0) shade2 = 15.0; //calculate shadow if ((shade > 0.01 || shade2 > 0.01) && params->shadow) { double light = AuxShadow(input, distance, lightVector); shade *= light; shade2 *= light; } else { if (params->shadow) { shade = 0; shade2 = 0; } } shading.R = shade * light.colour.R / 65536.0; shading.G = shade * light.colour.G / 65536.0; shading.B = shade * light.colour.B / 65536.0; outSpecular->R = shade2 * light.colour.R / 65536.0; outSpecular->G = shade2 * light.colour.G / 65536.0; outSpecular->B = shade2 * light.colour.B / 65536.0; return shading; }
sRGBAfloat cRenderWorker::VolumetricShader( const sShaderInputData &input, sRGBAfloat oldPixel, sRGBAfloat *opacityOut) { sRGBAfloat output; float totalOpacity = 0.0; output.R = oldPixel.R; output.G = oldPixel.G; output.B = oldPixel.B; output.A = oldPixel.A; // volumetric fog init double colourThresh = params->volFogColour1Distance; double colourThresh2 = params->volFogColour2Distance; double fogReduce = params->volFogDistanceFactor; double fogIntensity = params->volFogDensity; // visible lights init int numberOfLights = data->lights.GetNumberOfLights(); if (numberOfLights < 4) numberOfLights = 4; // glow init double glow = input.stepCount * params->glowIntensity / 512.0 * params->DEFactor; double glowN = 1.0 - glow; if (glowN < 0.0) glowN = 0.0; double glowR = (params->glowColor1.R * glowN + params->glowColor2.R * glow) / 65536.0; double glowG = (params->glowColor1.G * glowN + params->glowColor2.G * glow) / 65536.0; double glowB = (params->glowColor1.B * glowN + params->glowColor2.B * glow) / 65536.0; double totalStep = 0.0; // qDebug() << "Start volumetric shader &&&&&&&&&&&&&&&&&&&&"; sShaderInputData input2 = input; for (int index = input.stepCount - 1; index > 0; index--) { double step = input.stepBuff[index].step; double distance = input.stepBuff[index].distance; CVector3 point = input.stepBuff[index].point; totalStep += step; input2.point = point; input2.distThresh = input.stepBuff[index].distThresh; // qDebug() << "i" << index << "dist" << distance << "iters" << input.stepBuff[index].iters << // "distThresh" << input2.distThresh << "step" << step << "point" << point.Debug(); if (totalStep < CalcDelta(point)) { continue; } step = totalStep; totalStep = 0.0; //------------------- glow if (params->glowEnabled) { double glowOpacity = glow / input.stepCount; if (glowOpacity > 1.0) glowOpacity = 1.0; output.R = glowOpacity * glowR + (1.0 - glowOpacity) * output.R; output.G = glowOpacity * glowG + (1.0 - glowOpacity) * output.G; output.B = glowOpacity * glowB + (1.0 - glowOpacity) * output.B; output.A += glowOpacity; } // qDebug() << "step" << step; //------------------ visible light if (params->auxLightVisibility > 0) { double miniStep = 0.0; double lastMiniSteps = -1.0; for (double miniSteps = 0.0; miniSteps < step; miniSteps += miniStep) { double lowestLightSize = 1e10; double lowestLightDist = 1e10; for (int i = 0; i < numberOfLights; ++i) { const cLights::sLight *light = data->lights.GetLight(i); if (light->enabled) { CVector3 lightDistVect = (point - input.viewVector * miniSteps) - light->position; double lightDist = lightDistVect.Length(); double lightSize = sqrt(light->intensity) * params->auxLightVisibilitySize; double distToLightSurface = lightDist - lightSize; if (distToLightSurface < 0.0) distToLightSurface = 0.0; if (distToLightSurface <= lowestLightDist) { if (lightSize < lowestLightSize) { lowestLightSize = lightSize; } lowestLightDist = distToLightSurface; } } } miniStep = 0.1 * (lowestLightDist + 0.1 * lowestLightSize); if (miniStep > step - miniSteps) miniStep = step - miniSteps; // qDebug() << "lowDist:" << lowestLightDist << "lowSize" << lowestLightSize << "miniStep" // << miniStep; for (int i = 0; i < numberOfLights; ++i) { const cLights::sLight *light = data->lights.GetLight(i); if (light->enabled) { CVector3 lightDistVect = (point - input.viewVector * miniSteps) - light->position; double lightDist = lightDistVect.Length(); double lightSize = sqrt(light->intensity) * params->auxLightVisibilitySize; double r2 = lightDist / lightSize; double bellFunction = 1.0 / (1.0 + pow(r2, 4.0)); double lightDensity = miniStep * bellFunction * params->auxLightVisibility / lightSize; output.R += lightDensity * light->colour.R / 65536.0; output.G += lightDensity * light->colour.G / 65536.0; output.B += lightDensity * light->colour.B / 65536.0; output.A += lightDensity; } } if (miniSteps == lastMiniSteps) { // qWarning() << "Dead computation\n" // << "\npoint:" << (point - input.viewVector * miniSteps).Debug(); break; } lastMiniSteps = miniSteps; } } // fake lights (orbit trap) if (params->fakeLightsEnabled) { sFractalIn fractIn(point, params->minN, params->N, params->common, -1); sFractalOut fractOut; Compute<fractal::calcModeOrbitTrap>(*fractal, fractIn, &fractOut); double r = fractOut.orbitTrapR; r = sqrt(1.0f / (r + 1.0e-30f)); double fakeLight = 1.0 / (pow(r, 10.0 / params->fakeLightsVisibilitySize) * pow(10.0, 10.0 / params->fakeLightsVisibilitySize) + 1e-100); output.R += fakeLight * step * params->fakeLightsVisibility; output.G += fakeLight * step * params->fakeLightsVisibility; output.B += fakeLight * step * params->fakeLightsVisibility; output.A += fakeLight * step * params->fakeLightsVisibility; } //---------------------- volumetric lights with shadows in fog for (int i = 0; i < 5; i++) { if (i == 0 && params->volumetricLightEnabled[0]) { sRGBAfloat shadowOutputTemp = MainShadow(input2); output.R += shadowOutputTemp.R * step * params->volumetricLightIntensity[0] * params->mainLightColour.R / 65536.0; output.G += shadowOutputTemp.G * step * params->volumetricLightIntensity[0] * params->mainLightColour.G / 65536.0; output.B += shadowOutputTemp.B * step * params->volumetricLightIntensity[0] * params->mainLightColour.B / 65536.0; output.A += (shadowOutputTemp.R + shadowOutputTemp.G + shadowOutputTemp.B) / 3.0 * step * params->volumetricLightIntensity[0]; } if (i > 0) { const cLights::sLight *light = data->lights.GetLight(i - 1); if (light->enabled && params->volumetricLightEnabled[i]) { CVector3 lightVectorTemp = light->position - point; double distanceLight = lightVectorTemp.Length(); double distanceLight2 = distanceLight * distanceLight; lightVectorTemp.Normalize(); double lightShadow = AuxShadow(input2, distanceLight, lightVectorTemp); output.R += lightShadow * light->colour.R / 65536.0 * params->volumetricLightIntensity[i] * step / distanceLight2; output.G += lightShadow * light->colour.G / 65536.0 * params->volumetricLightIntensity[i] * step / distanceLight2; output.B += lightShadow * light->colour.B / 65536.0 * params->volumetricLightIntensity[i] * step / distanceLight2; output.A += lightShadow * params->volumetricLightIntensity[i] * step / distanceLight2; } } } //----------------------- basic fog if (params->fogEnabled) { double fogDensity = step / params->fogVisibility; if (fogDensity > 1.0) fogDensity = 1.0; output.R = fogDensity * params->fogColor.R / 65536.0 + (1.0 - fogDensity) * output.R; output.G = fogDensity * params->fogColor.G / 65536.0 + (1.0 - fogDensity) * output.G; output.B = fogDensity * params->fogColor.B / 65536.0 + (1.0 - fogDensity) * output.B; totalOpacity = fogDensity + (1.0 - fogDensity) * totalOpacity; output.A = fogDensity + (1.0 - fogDensity) * output.A; } //-------------------- volumetric fog if (fogIntensity > 0.0 && params->volFogEnabled) { double densityTemp = (step * fogReduce) / (distance * distance + fogReduce * fogReduce); double k = distance / colourThresh; if (k > 1) k = 1.0; double kn = 1.0 - k; double fogRtemp = (params->volFogColour1.R * kn + params->volFogColour2.R * k); double fogGtemp = (params->volFogColour1.G * kn + params->volFogColour2.G * k); double fogBtemp = (params->volFogColour1.B * kn + params->volFogColour2.B * k); double k2 = distance / colourThresh2 * k; if (k2 > 1) k2 = 1.0; kn = 1.0 - k2; fogRtemp = (fogRtemp * kn + params->volFogColour3.R * k2); fogGtemp = (fogGtemp * kn + params->volFogColour3.G * k2); fogBtemp = (fogBtemp * kn + params->volFogColour3.B * k2); double fogDensity = 0.3 * fogIntensity * densityTemp / (1.0 + fogIntensity * densityTemp); if (fogDensity > 1) fogDensity = 1.0; output.R = fogDensity * fogRtemp / 65536.0 + (1.0 - fogDensity) * output.R; output.G = fogDensity * fogGtemp / 65536.0 + (1.0 - fogDensity) * output.G; output.B = fogDensity * fogBtemp / 65536.0 + (1.0 - fogDensity) * output.B; // qDebug() << "densityTemp " << densityTemp << "k" << k << "k2" << k2 << "fogTempR" << // fogRtemp << "fogDensity" << fogDensity << "output.R" << output.R; totalOpacity = fogDensity + (1.0 - fogDensity) * totalOpacity; output.A = fogDensity + (1.0 - fogDensity) * output.A; } // iter fog if (params->iterFogEnabled) { int L = input.stepBuff[index].iters; double opacity = IterOpacity(step, L, params->N, params->iterFogOpacityTrim, params->iterFogOpacity); sRGBAfloat newColour(0.0, 0.0, 0.0, 0.0); if (opacity > 0) { // fog colour double iterFactor1 = (L - params->iterFogOpacityTrim) / (params->iterFogColor1Maxiter - params->iterFogOpacityTrim); double k = iterFactor1; if (k > 1.0) k = 1.0; if (k < 0.0) k = 0.0; double kn = 1.0 - k; double fogColR = (params->iterFogColour1.R * kn + params->iterFogColour2.R * k); double fogColG = (params->iterFogColour1.G * kn + params->iterFogColour2.G * k); double fogColB = (params->iterFogColour1.B * kn + params->iterFogColour2.B * k); double iterFactor2 = (L - params->iterFogColor1Maxiter) / (params->iterFogColor2Maxiter - params->iterFogColor1Maxiter); double k2 = iterFactor2; if (k2 < 0.0) k2 = 0.0; if (k2 > 1.0) k2 = 1.0; kn = 1.0 - k2; fogColR = (fogColR * kn + params->iterFogColour3.R * k2); fogColG = (fogColG * kn + params->iterFogColour3.G * k2); fogColB = (fogColB * kn + params->iterFogColour3.B * k2); //---- for (int i = 0; i < 5; i++) { if (i == 0) { if (params->mainLightEnable && params->mainLightIntensity > 0.0) { sRGBAfloat shadowOutputTemp = MainShadow(input2); newColour.R += shadowOutputTemp.R * params->mainLightColour.R / 65536.0 * params->mainLightIntensity; newColour.G += shadowOutputTemp.G * params->mainLightColour.G / 65536.0 * params->mainLightIntensity; newColour.B += shadowOutputTemp.B * params->mainLightColour.B / 65536.0 * params->mainLightIntensity; } } if (i > 0) { const cLights::sLight *light = data->lights.GetLight(i - 1); if (light->enabled) { CVector3 lightVectorTemp = light->position - point; double distanceLight = lightVectorTemp.Length(); double distanceLight2 = distanceLight * distanceLight; lightVectorTemp.Normalize(); double lightShadow = AuxShadow(input2, distanceLight, lightVectorTemp); double intensity = light->intensity * 100.0; newColour.R += lightShadow * light->colour.R / 65536.0 / distanceLight2 * intensity; newColour.G += lightShadow * light->colour.G / 65536.0 / distanceLight2 * intensity; newColour.B += lightShadow * light->colour.B / 65536.0 / distanceLight2 * intensity; } } } if (params->ambientOcclusionEnabled && params->ambientOcclusionMode == params::AOmodeMultipeRays) { sRGBAfloat AO = AmbientOcclusion(input2); newColour.R += AO.R * params->ambientOcclusion; newColour.G += AO.G * params->ambientOcclusion; newColour.B += AO.B * params->ambientOcclusion; } if (opacity > 1.0) opacity = 1.0; output.R = output.R * (1.0 - opacity) + newColour.R * opacity * fogColR / 65536.0; output.G = output.G * (1.0 - opacity) + newColour.G * opacity * fogColG / 65536.0; output.B = output.B * (1.0 - opacity) + newColour.B * opacity * fogColB / 65536.0; totalOpacity = opacity + (1.0 - opacity) * totalOpacity; output.A = opacity + (1.0 - opacity) * output.A; } } if (totalOpacity > 1.0) totalOpacity = 1.0; if (output.A > 1.0) output.A = 1.0; (*opacityOut).R = totalOpacity; (*opacityOut).G = totalOpacity; (*opacityOut).B = totalOpacity; } // next stepCount return output; }
sRGBAfloat cRenderWorker::LightShading( const sShaderInputData &input, const cLights::sLight *light, int number, sRGBAfloat *outSpecular) { sRGBAfloat shading; CVector3 d = light->position - input.point; double distance = d.Length(); // angle of incidence CVector3 lightVector = d; lightVector.Normalize(); double intensity = 100.0 * light->intensity / (distance * distance) / number; double shade = input.normal.Dot(lightVector); if (shade < 0) shade = 0; shade = (1.0 - input.material->shading) + shade * input.material->shading; shade = shade * intensity; if (shade > 500.0) shade = 500.0; // specular CVector3 half = lightVector - input.viewVector; half.Normalize(); double shade2 = input.normal.Dot(half); if (shade2 < 0.0) shade2 = 0.0; double diffuse = 10.0 * (1.1 - input.material->diffussionTextureIntensity * (input.texDiffuse.R + input.texDiffuse.G + input.texDiffuse.B) / 3.0); shade2 = pow(shade2, 30.0 / input.material->specularWidth / diffuse) / diffuse; shade2 *= intensity * input.material->specular; if (shade2 > 15.0) shade2 = 15.0; // calculate shadow if ((shade > 0.01 || shade2 > 0.01) && params->shadow) { double auxShadow = AuxShadow(input, distance, lightVector); shade *= auxShadow; shade2 *= auxShadow; } else { if (params->shadow) { shade = 0; shade2 = 0; } } shading.R = shade * light->colour.R / 65536.0; shading.G = shade * light->colour.G / 65536.0; shading.B = shade * light->colour.B / 65536.0; outSpecular->R = shade2 * light->colour.R / 65536.0; outSpecular->G = shade2 * light->colour.G / 65536.0; outSpecular->B = shade2 * light->colour.B / 65536.0; return shading; }