From d950026038fa5769ec30ad8488271cc55f989a0e Mon Sep 17 00:00:00 2001 From: Cyrille Pierre Henri Favreau Date: Fri, 29 Sep 2023 17:29:30 +0200 Subject: [PATCH] Fixed clipping planes in OSPRay Advanced renderer --- .../widgets/BioExplorer_widgets.ipynb | 92 ++++++++++- platform/core/common/Properties.h | 2 + platform/core/engineapi/Scene.cpp | 2 +- platform/engines/ospray/OSPRayEngine.cpp | 7 +- platform/engines/ospray/OSPRayMaterial.cpp | 5 + platform/engines/ospray/OSPRayRenderer.cpp | 16 ++ .../ospray/ispc/render/AdvancedRenderer.cpp | 13 +- .../ospray/ispc/render/AdvancedRenderer.h | 1 + .../ospray/ispc/render/AdvancedRenderer.ih | 1 + .../ospray/ispc/render/AdvancedRenderer.ispc | 143 +++++++++--------- .../ospray/ispc/render/BasicRenderer.ispc | 2 +- .../ispc/render/utils/AdvancedMaterial.cpp | 8 +- 12 files changed, 196 insertions(+), 96 deletions(-) diff --git a/bioexplorer/pythonsdk/notebooks/widgets/BioExplorer_widgets.ipynb b/bioexplorer/pythonsdk/notebooks/widgets/BioExplorer_widgets.ipynb index 3440b9805..796c8c803 100644 --- a/bioexplorer/pythonsdk/notebooks/widgets/BioExplorer_widgets.ipynb +++ b/bioexplorer/pythonsdk/notebooks/widgets/BioExplorer_widgets.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ @@ -24,7 +24,65 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "# w.display_clipping_planes([-50, 50])" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "core = be.core_api()\n", + "core.set_environment_map('/media/favreau/medias/hdri/0000.jpg')\n", + "status = core.set_renderer(\n", + " background_color=[0.2, 0.15, 0.3], head_light=False,\n", + " current='advanced',subsampling=4, max_accum_frames=64)\n", + "params = core.AdvancedRendererParams()\n", + "params.gi_samples = 1\n", + "params.gi_strength = 0.5\n", + "params.gi_ray_length = 100.0\n", + "params.shadow_intensity = 1.0\n", + "params.soft_shadow_strength = 0.0\n", + "params.epsilon_multiplier = 100.0\n", + "params.max_ray_depth = 10\n", + "params.show_background = True\n", + "params.main_exposure = 1.0\n", + "status = core.set_renderer_params(params)\n", + "status = core.set_renderer()" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b19eccf2f90247b2924291e07390e801", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HBox(children=(Select(description='Models:', options=('Demo scene',), value='Demo scene'), Sele…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "w.display_palette_for_models()" + ] + }, + { + "cell_type": "code", + "execution_count": 59, "metadata": { "scrolled": false }, @@ -32,7 +90,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "d2fb0c7746f2465ca9f32a5b92ea3cf3", + "model_id": "020d85543ca745d9a35b0ca411b07846", "version_major": 2, "version_minor": 0 }, @@ -50,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 60, "metadata": { "scrolled": false }, @@ -58,12 +116,12 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "f913a91cfdbc44299a7626165a4a1e23", + "model_id": "73eca5277dfa467fa9815b7340dd308c", "version_major": 2, "version_minor": 0 }, "text/plain": [ - "VBox(children=(HBox(children=(Select(description='Models:', options=('layer00001',), value='layer00001'), Sele…" + "VBox(children=(HBox(children=(Select(description='Models:', options=('Demo scene',), value='Demo scene'), Sele…" ] }, "metadata": {}, @@ -76,13 +134,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'DEFAULT_GRID_LAYOUT' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/favreau/code/favreau/BioExplorer/bioexplorer/pythonsdk/notebooks/widgets/BioExplorer_widgets.ipynb Cell 8\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m w\u001b[39m.\u001b[39;49mdisplay_model_visibility()\n", + "File \u001b[0;32m~/Notebooks/env/lib/python3.10/site-packages/bioexplorer/notebook_widgets.py:638\u001b[0m, in \u001b[0;36mWidgets.display_model_visibility\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 635\u001b[0m hide_aabb_btn\u001b[39m.\u001b[39mon_click(hide_aabbs)\n\u001b[1;32m 636\u001b[0m adjust_camera_btn\u001b[39m.\u001b[39mon_click(adjust_camera)\n\u001b[0;32m--> 638\u001b[0m hbox \u001b[39m=\u001b[39m HBox([model_select, hbox_params], layout\u001b[39m=\u001b[39mDEFAULT_GRID_LAYOUT)\n\u001b[1;32m 639\u001b[0m display(hbox)\n", + "\u001b[0;31mNameError\u001b[0m: name 'DEFAULT_GRID_LAYOUT' is not defined" + ] + } + ], "source": [ "w.display_model_visibility()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "code", "execution_count": null, diff --git a/platform/core/common/Properties.h b/platform/core/common/Properties.h index e37fc778c..6982a0e35 100644 --- a/platform/core/common/Properties.h +++ b/platform/core/common/Properties.h @@ -82,6 +82,7 @@ static const char* RENDERER_PROPERTY_BACKGROUND_MATERIAL = "bgMaterial"; static const char* RENDERER_PROPERTY_LIGHTS = "lights"; static const char* RENDERER_PROPERTY_RANDOM_NUMBER = "randomNumber"; +static constexpr bool DEFAULT_RENDERER_FAST_PREVIEW = false; static constexpr bool DEFAULT_RENDERER_SHOW_BACKGROUND = true; static constexpr int DEFAULT_RENDERER_MAX_RAY_DEPTH = 3; static constexpr double DEFAULT_RENDERER_SHADOW_INTENSITY = 0.0; @@ -130,6 +131,7 @@ static const Property RENDERER_PROPERTY_MAX_DISTANCE_TO_SECONDARY_MODEL = { 0.1, 100.0, {"Maximum distance to secondary model"}}; +static const Property RENDERER_PROPERTY_FAST_PREVIEW = {"fastPreview", DEFAULT_RENDERER_FAST_PREVIEW, {"Fast preview"}}; /* Material properties diff --git a/platform/core/engineapi/Scene.cpp b/platform/core/engineapi/Scene.cpp index c0d8f6c53..dc0156537 100644 --- a/platform/core/engineapi/Scene.cpp +++ b/platform/core/engineapi/Scene.cpp @@ -251,7 +251,7 @@ void Scene::buildDefault() CORE_INFO("Building default Cornell Box scene"); auto model = createModel(); -#if 1 +#if 0 const Vector3f WHITE = {1.f, 1.f, 1.f}; const Vector3f positions[8] = { diff --git a/platform/engines/ospray/OSPRayEngine.cpp b/platform/engines/ospray/OSPRayEngine.cpp index 4ef0b6495..94e0fcfa8 100644 --- a/platform/engines/ospray/OSPRayEngine.cpp +++ b/platform/engines/ospray/OSPRayEngine.cpp @@ -145,6 +145,7 @@ void OSPRayEngine::_createRenderers() { PLUGIN_INFO("Registering '" << RENDERER_PROPERTY_TYPE_ADVANCED << "' renderer"); PropertyMap properties; + properties.setProperty(RENDERER_PROPERTY_FAST_PREVIEW); properties.setProperty(RENDERER_PROPERTY_ALPHA_CORRECTION); properties.setProperty(RENDERER_PROPERTY_MAX_DISTANCE_TO_SECONDARY_MODEL); properties.setProperty(RENDERER_PROPERTY_GLOBAL_ILLUMINATION_RAY_LENGTH); @@ -177,8 +178,10 @@ void OSPRayEngine::_createRenderers() properties.setProperty(OSPRAY_RENDERER_SHADOW_ENABLED); addRendererType(RENDERER_PROPERTY_TYPE_SCIVIS, properties); } - PLUGIN_INFO("Registering '" << RENDERER_PROPERTY_TYPE_BASIC << "' renderer"); - addRendererType(RENDERER_PROPERTY_TYPE_BASIC); + { + PLUGIN_INFO("Registering '" << RENDERER_PROPERTY_TYPE_BASIC << "' renderer"); + addRendererType(RENDERER_PROPERTY_TYPE_BASIC); + } } FrameBufferPtr OSPRayEngine::createFrameBuffer(const std::string& name, const Vector2ui& frameSize, diff --git a/platform/engines/ospray/OSPRayMaterial.cpp b/platform/engines/ospray/OSPRayMaterial.cpp index ebc38dd56..633270224 100644 --- a/platform/engines/ospray/OSPRayMaterial.cpp +++ b/platform/engines/ospray/OSPRayMaterial.cpp @@ -67,7 +67,12 @@ void OSPRayMaterial::commit() osphelper::set(_ospMaterial, MATERIAL_PROPERTY_DIFFUSE_COLOR, Vector3f(_diffuseColor)); osphelper::set(_ospMaterial, MATERIAL_PROPERTY_SPECULAR_COLOR, Vector3f(_specularColor)); osphelper::set(_ospMaterial, MATERIAL_PROPERTY_SPECULAR_INDEX, static_cast(_specularExponent)); +#if 0 + // For some unknown reason, this simply does not work!?! osphelper::set(_ospMaterial, MATERIAL_PROPERTY_OPACITY, static_cast(_opacity)); +#else + osphelper::set(_ospMaterial, "opacity", static_cast(_opacity)); +#endif osphelper::set(_ospMaterial, MATERIAL_PROPERTY_REFRACTION, static_cast(_refractionIndex)); osphelper::set(_ospMaterial, MATERIAL_PROPERTY_REFLECTION, static_cast(_reflectionIndex)); osphelper::set(_ospMaterial, MATERIAL_PROPERTY_EMISSION, static_cast(_emission)); diff --git a/platform/engines/ospray/OSPRayRenderer.cpp b/platform/engines/ospray/OSPRayRenderer.cpp index 8efe8c9ca..98db07920 100644 --- a/platform/engines/ospray/OSPRayRenderer.cpp +++ b/platform/engines/ospray/OSPRayRenderer.cpp @@ -138,6 +138,22 @@ void OSPRayRenderer::commit() ospSetObject(_renderer, OSPRAY_RENDERER_PROPERTY_CAMERA, _camera->impl()); ospSetObject(_renderer, OSPRAY_RENDERER_PROPERTY_WORLD, scene->getModel()); + + // Clip planes + if (!_clipPlanes.empty()) + { + const auto clipPlanes = convertVectorToFloat(_clipPlanes); + auto clipPlaneData = ospNewData(clipPlanes.size(), OSP_FLOAT4, clipPlanes.data()); + ospSetData(_renderer, CAMERA_PROPERTY_CLIPPING_PLANES, clipPlaneData); + ospRelease(clipPlaneData); + } + else + { + // ospRemoveParam leaks objects, so we set it to null first + ospSetData(_renderer, CAMERA_PROPERTY_CLIPPING_PLANES, nullptr); + ospRemoveParam(_renderer, CAMERA_PROPERTY_CLIPPING_PLANES); + } + ospCommit(_renderer); } diff --git a/platform/engines/ospray/ispc/render/AdvancedRenderer.cpp b/platform/engines/ospray/ispc/render/AdvancedRenderer.cpp index 47cdecadb..dabe8f269 100644 --- a/platform/engines/ospray/ispc/render/AdvancedRenderer.cpp +++ b/platform/engines/ospray/ispc/render/AdvancedRenderer.cpp @@ -70,16 +70,8 @@ void AdvancedRenderer::commit() { SimulationRenderer::commit(); + _fastPreview = getParam(RENDERER_PROPERTY_FAST_PREVIEW.name.c_str(), DEFAULT_RENDERER_FAST_PREVIEW); _alphaCorrection = getParam1f(RENDERER_PROPERTY_ALPHA_CORRECTION.name.c_str(), DEFAULT_RENDERER_ALPHA_CORRECTION); - _fogStart = getParam1f(RENDERER_PROPERTY_FOG_START.name.c_str(), DEFAULT_RENDERER_FOG_START); - _fogThickness = getParam1f(RENDERER_PROPERTY_FOG_THICKNESS.name.c_str(), DEFAULT_RENDERER_FOG_THICKNESS); - _exposure = getParam1f(COMMON_PROPERTY_EXPOSURE.name.c_str(), DEFAULT_COMMON_EXPOSURE); - _epsilonFactor = getParam1f(RENDERER_PROPERTY_EPSILON_MULTIPLIER.name.c_str(), DEFAULT_RENDERER_EPSILON_MULTIPLIER); - _maxRayDepth = getParam1i(RENDERER_PROPERTY_MAX_RAY_DEPTH.name.c_str(), DEFAULT_RENDERER_MAX_RAY_DEPTH); - _randomNumber = rand() % 1000; - _useHardwareRandomizer = getParam(COMMON_PROPERTY_USE_HARDWARE_RANDOMIZER.name.c_str(), - static_cast(DEFAULT_COMMON_USE_HARDWARE_RANDOMIZER)); - _showBackground = getParam(RENDERER_PROPERTY_SHOW_BACKGROUND.name.c_str(), DEFAULT_RENDERER_SHOW_BACKGROUND); _shadows = getParam1f(RENDERER_PROPERTY_SHADOW_INTENSITY.name.c_str(), DEFAULT_RENDERER_SHADOW_INTENSITY); _softShadows = getParam1f(RENDERER_PROPERTY_SOFT_SHADOW_STRENGTH.name.c_str(), DEFAULT_RENDERER_SOFT_SHADOW_STRENGTH); @@ -101,7 +93,6 @@ void AdvancedRenderer::commit() clipPlanes = getParamData(CAMERA_PROPERTY_CLIPPING_PLANES, nullptr); const auto clipPlaneData = clipPlanes ? clipPlanes->data : nullptr; const uint32 numClipPlanes = clipPlanes ? clipPlanes->numItems : 0; - ::ispc::AdvancedRenderer_set(getIE(), (_bgMaterial ? _bgMaterial->getIE() : nullptr), _shadows, _softShadows, _softShadowsSamples, _giStrength, _giDistance, _giSamples, _randomNumber, _timestamp, spp, _lightPtr, _lightArray.size(), _exposure, _epsilonFactor, _fogThickness, @@ -109,7 +100,7 @@ void AdvancedRenderer::commit() _userData ? (float*)_userData->data : nullptr, _simulationDataSize, _volumeSamplingThreshold, _volumeSpecularExponent, _volumeAlphaCorrection, (const ::ispc::vec4f*)clipPlaneData, numClipPlanes, _anaglyphEnabled, - (ispc::vec3f&)_anaglyphIpdOffset); + (ispc::vec3f&)_anaglyphIpdOffset, _fastPreview); } AdvancedRenderer::AdvancedRenderer() diff --git a/platform/engines/ospray/ispc/render/AdvancedRenderer.h b/platform/engines/ospray/ispc/render/AdvancedRenderer.h index 9fbf9ece5..63636960f 100644 --- a/platform/engines/ospray/ispc/render/AdvancedRenderer.h +++ b/platform/engines/ospray/ispc/render/AdvancedRenderer.h @@ -60,6 +60,7 @@ class AdvancedRenderer : public SimulationRenderer private: // Shading + bool _fastPreview{false}; double _shadows{0.f}; double _softShadows{0.f}; ::ospray::uint32 _softShadowsSamples{1}; diff --git a/platform/engines/ospray/ispc/render/AdvancedRenderer.ih b/platform/engines/ospray/ispc/render/AdvancedRenderer.ih index 4aea966b2..2b1642865 100644 --- a/platform/engines/ospray/ispc/render/AdvancedRenderer.ih +++ b/platform/engines/ospray/ispc/render/AdvancedRenderer.ih @@ -28,6 +28,7 @@ struct AdvancedRenderer SimulationRenderer super; // Shading + bool fastPreview; float shadowIntensity; float softShadowStrength; uint32 shadowSamples; diff --git a/platform/engines/ospray/ispc/render/AdvancedRenderer.ispc b/platform/engines/ospray/ispc/render/AdvancedRenderer.ispc index 701664d4f..08a60456f 100644 --- a/platform/engines/ospray/ispc/render/AdvancedRenderer.ispc +++ b/platform/engines/ospray/ispc/render/AdvancedRenderer.ispc @@ -58,8 +58,8 @@ struct ShadingAttributes MaterialClippingMode clippingMode; vec3f indirectColor; float indirectIntensity; - vec3f simulationColor; - float simulationIntensity; + vec3f userDataColor; + float userDataIntensity; vec3f volumeColor; float volumeIntensity; vec3f shadingContribution; @@ -106,8 +106,8 @@ inline void initializeShadingAttributes(const uniform AdvancedRenderer* uniform // Simulation contribution attributes.castUserData = false; - attributes.simulationColor = make_vec3f(0.f); - attributes.simulationIntensity = 0.f; + attributes.userDataColor = make_vec3f(0.f); + attributes.userDataIntensity = 0.f; // Volume attributes.volumeColor = make_vec3f(0.f); @@ -167,6 +167,9 @@ inline void setGeometryShadingAttributes(const uniform AdvancedRenderer* uniform // Model Id attributes.nodeId = 0; + // Update reflected shading normal + attributes.reflectedNormal = ray.dir - 2.f * dot(ray.dir, attributes.normal) * attributes.normal; + // Process material attributes const uniform Material* material = dg.material; const uniform AdvancedMaterial* objMaterial = (const uniform AdvancedMaterial*)material; @@ -175,9 +178,7 @@ inline void setGeometryShadingAttributes(const uniform AdvancedRenderer* uniform // Diffuse color attributes.diffuseColor = make_vec3f(dg.color); - // if no material is defined, localOpacity is stored in the - // alpha - // component of the RGBA color + // if no material is defined, localOpacity is stored in the alpha component of the RGBA color attributes.opacity = dg.color.w; return; } @@ -263,9 +264,6 @@ inline void setGeometryShadingAttributes(const uniform AdvancedRenderer* uniform // Clipping planes attributes.clippingMode = mat->clippingMode; } - - // Update reflected shading normal - attributes.reflectedNormal = ray.dir - 2.f * dot(ray.dir, attributes.normal) * attributes.normal; } inline bool launchRandomRay(const uniform AdvancedRenderer* uniform self, varying ScreenSample& sample, @@ -282,11 +280,12 @@ inline bool launchRandomRay(const uniform AdvancedRenderer* uniform self, varyin // Invert direction of random ray direction is opposite to surface normal randomDirection = neg(randomDirection); + float nearClip = epsilon; + float farClip = self->giRayLength; + Ray randomRay; randomRay.org = intersection; randomRay.dir = randomDirection; - randomRay.t0 = epsilon; - randomRay.t = self->giRayLength; randomRay.time = inf; randomRay.primID = -1; randomRay.geomID = -1; @@ -296,6 +295,9 @@ inline bool launchRandomRay(const uniform AdvancedRenderer* uniform self, varyin bool hit = false; while (moreRebounds) { + clipRay(self->clipPlanes, self->numClipPlanes, intersection, randomDirection, nearClip, farClip); + randomRay.t0 = nearClip; + randomRay.t = farClip; traceRay(self->super.super.super.model, randomRay); if (randomRay.geomID < 0) @@ -342,8 +344,8 @@ inline bool launchRandomRay(const uniform AdvancedRenderer* uniform self, varyin else moreRebounds = false; - randomRay.t0 = randomRay.t + self->super.epsilonMultiplier * dg.epsilon; - randomRay.t = inf; + nearClip = randomRay.t + self->super.epsilonMultiplier * dg.epsilon; + farClip = self->giRayLength - randomRay.t; randomRay.primID = -1; randomRay.geomID = -1; randomRay.instID = -1; @@ -575,7 +577,6 @@ inline float shadedLightIntensity(varying ScreenSample& sample, const varying Ra for (uint32 s = 0; s < nbSamples; ++s) { float shadowIntensity = 0.f; - vec3f ld = lightDirection; if (attributes.self->softShadowStrength > 0.f) // Slightly alter light direction for Soft shadowIntensity @@ -587,34 +588,37 @@ inline float shadedLightIntensity(varying ScreenSample& sample, const varying Ra if (dot(ld, lightDirection) < 0.f) ld = neg(ld); + float nearClip = attributes.self->super.epsilonMultiplier * dg.epsilon; + float farClip = attributes.self->giRayLength; + Ray shadowRay; shadowRay.org = dg.P; shadowRay.dir = ld; - shadowRay.t0 = max(0.f, attributes.self->super.epsilonMultiplier * dg.epsilon); - shadowRay.t = attributes.self->giRayLength; - shadowRay.time = inf; + shadowRay.time = ray.time; shadowRay.geomID = -1; shadowRay.primID = -1; shadowRay.instID = -1; while (shadowIntensity < 1.f) { + clipRay(attributes.self->clipPlanes, attributes.self->numClipPlanes, shadowRay.org, shadowRay.dir, nearClip, + farClip); + + shadowRay.t0 = nearClip; + shadowRay.t = farClip; traceRay(attributes.self->super.super.super.model, shadowRay); - if (shadowRay.geomID == -1) + if (shadowRay.geomID < 0) break; DifferentialGeometry shadowingGeometry; - postIntersect(attributes.self->super.super.super.model, shadowingGeometry, shadowRay, - DG_NG | DG_NS | DG_NORMALIZE | DG_TANGENTS | DG_FACEFORWARD | DG_MATERIALID | DG_COLOR | - DG_TEXCOORD); + postIntersect(attributes.self->super.super.super.model, shadowingGeometry, shadowRay, DG_MATERIALID); if (!isClipped(attributes.self, shadowingGeometry.P, attributes.clippingMode)) { uniform AdvancedMaterial* objMaterial = (uniform AdvancedMaterial*)shadowingGeometry.material; float opacity = 1.f; - if (objMaterial == 0) opacity = 1.f - shadowingGeometry.color.w; else @@ -637,18 +641,16 @@ inline float shadedLightIntensity(varying ScreenSample& sample, const varying Ra } } } - shadowIntensity += opacity; } - shadowRay.t0 = shadowRay.t + shadowingGeometry.epsilon; - shadowRay.t = attributes.self->giRayLength - shadowRay.t0; - shadowRay.primID = -1; + // Prepare next ray + nearClip = shadowRay.t + shadowingGeometry.epsilon; + // farClip = attributes.self->giRayLength - shadowRay.t0; + shadowRay.geomID = -1; + shadowRay.primID = -1; shadowRay.instID = -1; - - if (shadowRay.t < 0.f) - break; } intensity += shadowIntensity; } @@ -914,8 +916,8 @@ inline void processGoodsellShading(const uniform AdvancedRenderer* uniform self, attributes.specularColor = make_vec3f(0.f); } -inline void processSimulationContribution(varying ScreenSample& sample, ShadingAttributes& attributes, - const uint32 materialID, const varying uint32 primID) +inline void processUserDataContribution(varying ScreenSample& sample, ShadingAttributes& attributes, + const uint32 materialID, const varying uint32 primID) { if (!attributes.castUserData) return; @@ -923,9 +925,9 @@ inline void processSimulationContribution(varying ScreenSample& sample, ShadingA if (!attributes.self->super.simulationData) return; - const vec4f simulationColor = getSimulationValue(&attributes.self->super, attributes.dg, primID); - attributes.simulationColor = make_vec3f(simulationColor); - attributes.simulationIntensity = simulationColor.w; + const vec4f userDataColor = getSimulationValue(&attributes.self->super, attributes.dg, primID); + attributes.userDataColor = make_vec3f(userDataColor); + attributes.userDataIntensity = userDataColor.w; } inline void processVolumeContribution(varying ScreenSample& sample, varying Ray& ray, ShadingAttributes& attributes, @@ -944,8 +946,8 @@ inline void processVolumeContribution(varying ScreenSample& sample, varying Ray& inline void processFinalContribution(ShadingAttributes& attributes) { // Surface shading - vec3f totalContributions = attributes.simulationColor * attributes.simulationIntensity + - attributes.shadingContribution * (1.f - attributes.simulationIntensity) + + vec3f totalContributions = attributes.userDataColor * attributes.userDataIntensity + + attributes.shadingContribution * (1.f - attributes.userDataIntensity) + attributes.specularColor; // Shadow contribution @@ -971,18 +973,17 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s { Ray ray = sample.ray; - float maxt = self->fogStart + self->fogThickness; - vec4f color = make_vec4f(0.f); vec3f bgColor = make_vec3f(0.f); if (self->super.super.showBackground) bgColor = make_vec3f(skyboxMapping((Renderer*)self, ray, (AdvancedMaterial*)self->super.super.bgMaterial)); - uint32 depth = 0; - float oldlocalRefraction = 1.f; + sample.z = inf; - sample.z = maxt; - ray.t = maxt; + uint32 depth = 0; + float oldRefraction = 1.f; + float nearClip = ray.t0; + float farClip = ray.t; bool moreRebounds = true; while (moreRebounds && depth < self->super.maxRayDepth && color.w < 1.f) @@ -996,6 +997,9 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s float firstIntersection = inf; // Trace ray + clipRay(attributes.self->clipPlanes, attributes.self->numClipPlanes, ray.org, ray.dir, nearClip, farClip); + ray.t0 = nearClip; + ray.t = farClip; traceRay(self->super.super.super.model, ray); if (ray.geomID < 0) @@ -1022,7 +1026,6 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s DG_NG | DG_NS | DG_NORMALIZE | DG_TANGENTS | DG_MATERIALID | DG_COLOR | DG_FACEFORWARD | DG_TEXCOORD); - // Get intersection information // Initialize geometry shading attributes setGeometryShadingAttributes(self, dg, sample, ray, attributes); @@ -1030,13 +1033,14 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s isClipped(self, dg.P, attributes.clippingMode) || attributes.chameleonMode == emitter; if (discardIntersection) - ray.t0 = ray.t + self->super.epsilonMultiplier * dg.epsilon; + nearClip = ray.t + self->super.epsilonMultiplier * dg.epsilon; else { - // Compute simulation contribution - processSimulationContribution(sample, attributes, dg.materialID, ray.primID); + // Compute user data contribution + processUserDataContribution(sample, attributes, dg.materialID, ray.primID); - if (sample.sampleID.z > 0) + const bool processIndirectLighting = (self->fastPreview ? (sample.sampleID.z > 0) : true); + if (processIndirectLighting) // Compute indirect lighting contribution computeIndirectShading(dg, sample, attributes); @@ -1059,7 +1063,7 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s processDiffuseShading(self, dg, sample, ray, attributes); // Compute shadowIntensity - if (sample.sampleID.z > 0 && self->shadowIntensity > 0.f) + if (processIndirectLighting && self->shadowIntensity > 0.f) processShadows(dg, sample, ray, attributes); // Compute volume contribution @@ -1069,7 +1073,7 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s if (depth == 0) sample.z = ray.t; - // Alpha + // Opacity totalOpacity += (1.f - attributes.reflection) * attributes.opacity; // Prepare ray for next iteration @@ -1086,8 +1090,8 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s if (doRefraction) { // Refraction - ray.dir = refractedVector(ray.dir, attributes.normal, oldlocalRefraction, attributes.refraction); - oldlocalRefraction = attributes.refraction; + ray.dir = refractedVector(ray.dir, attributes.normal, oldRefraction, attributes.refraction); + oldRefraction = attributes.refraction; } else if (doReflection) // Reflection @@ -1096,13 +1100,11 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s moreRebounds = false; // Prepare next ray - ray.org = dg.P + ray.dir * (self->super.epsilonMultiplier * dg.epsilon); - ray.t0 = max(0.f, self->super.epsilonMultiplier * dg.epsilon); + ray.org = dg.P + ray.dir * self->super.epsilonMultiplier * dg.epsilon; + nearClip = 0.f; + farClip = self->fogStart + self->fogThickness; ++depth; } - - maxt -= ray.t; - ray.t = maxt; ray.primID = -1; ray.geomID = -1; ray.instID = -1; @@ -1164,20 +1166,18 @@ export void* uniform AdvancedRenderer_create(void* uniform cppE) return self; } -export void AdvancedRenderer_set(void* uniform _self, void* uniform bgMaterial, const uniform float shadowIntensity, - const uniform float softShadowStrength, const uniform uint32 shadowSamples, - const uniform float giStrength, const uniform float giRayLength, - const uniform uint32 giSamples, const uniform uint32 randomNumber, - const uniform float timestamp, const uniform uint32 spp, void** uniform lights, - const uniform uint32 numLights, const uniform float exposure, - const uniform float epsilonMultiplier, const uniform float fogThickness, - const uniform float fogStart, const uniform bool useHardwareRandomizer, - const uniform uint32 maxRayDepth, const uniform bool showBackground, - const uniform bool matrixFilter, uniform float* uniform simulationData, - const uniform uint64 simulationDataSize, const uniform float volumeSamplingThreshold, - const uniform float volumeSpecularExponent, const uniform float volumeAlphaCorrection, - const uniform vec4f clipPlanes[], const uniform uint32 numClipPlanes, - const uniform bool anaglyphEnabled, const uniform vec3f& anaglyphIpdOffset) +export void AdvancedRenderer_set( + void* uniform _self, void* uniform bgMaterial, const uniform float shadowIntensity, + const uniform float softShadowStrength, const uniform uint32 shadowSamples, const uniform float giStrength, + const uniform float giRayLength, const uniform uint32 giSamples, const uniform uint32 randomNumber, + const uniform float timestamp, const uniform uint32 spp, void** uniform lights, const uniform uint32 numLights, + const uniform float exposure, const uniform float epsilonMultiplier, const uniform float fogThickness, + const uniform float fogStart, const uniform bool useHardwareRandomizer, const uniform uint32 maxRayDepth, + const uniform bool showBackground, const uniform bool matrixFilter, uniform float* uniform simulationData, + const uniform uint64 simulationDataSize, const uniform float volumeSamplingThreshold, + const uniform float volumeSpecularExponent, const uniform float volumeAlphaCorrection, + const uniform vec4f clipPlanes[], const uniform uint32 numClipPlanes, const uniform bool anaglyphEnabled, + const uniform vec3f& anaglyphIpdOffset, const uniform bool fastPreview) { uniform AdvancedRenderer* uniform self = (uniform AdvancedRenderer * uniform) _self; @@ -1198,6 +1198,7 @@ export void AdvancedRenderer_set(void* uniform _self, void* uniform bgMaterial, self->super.simulationData = (uniform float* uniform)simulationData; self->super.simulationDataSize = simulationDataSize; + self->fastPreview = fastPreview; self->fogThickness = fogThickness; self->fogStart = fogStart; self->shadowIntensity = shadowIntensity; diff --git a/platform/engines/ospray/ispc/render/BasicRenderer.ispc b/platform/engines/ospray/ispc/render/BasicRenderer.ispc index 757ffe20a..dd27fbcfa 100644 --- a/platform/engines/ospray/ispc/render/BasicRenderer.ispc +++ b/platform/engines/ospray/ispc/render/BasicRenderer.ispc @@ -45,7 +45,7 @@ inline vec3f BasicRenderer_shadeRay(const uniform BasicRenderer* uniform self, v if (ray.geomID < 0) { // No intersection, use environment map or background color - const vec4f bgSample = skyboxMapping((Renderer*)self, ray, self->abstract.bgMaterial); + vec4f bgSample = skyboxMapping((Renderer*)self, ray, self->abstract.bgMaterial); sample.alpha = bgSample.w; return make_vec3f(bgSample); } diff --git a/platform/engines/ospray/ispc/render/utils/AdvancedMaterial.cpp b/platform/engines/ospray/ispc/render/utils/AdvancedMaterial.cpp index 748da9f16..15efb57ae 100644 --- a/platform/engines/ospray/ispc/render/utils/AdvancedMaterial.cpp +++ b/platform/engines/ospray/ispc/render/utils/AdvancedMaterial.cpp @@ -39,11 +39,15 @@ namespace ospray { void AdvancedMaterial::commit() { - if (ispcEquivalent == nullptr) + if (!ispcEquivalent) ispcEquivalent = ::ispc::AdvancedMaterial_create(this); - // Opacity +// Opacity +#if 0 d = getParam1f(MATERIAL_PROPERTY_OPACITY, 1.f); +#else + d = getParam1f("opacity", 1.f); +#endif map_d = (::ospray::Texture2D*)getParamObject(MATERIAL_PROPERTY_MAP_OPACITY, nullptr); auto xform_d = getTextureTransform(MATERIAL_PROPERTY_MAP_OPACITY);