-
-
Notifications
You must be signed in to change notification settings - Fork 365
Display depth image #2795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AoGao-Kedoka
wants to merge
11
commits into
f3d-app:master
Choose a base branch
from
AoGao-Kedoka:display-depth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Display depth image #2795
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bfb042e
FEAT: Display depth map
AoGao-Kedoka 4d2c9c6
FEAT: Control depth coloring with scalar-coloring option
AoGao-Kedoka ce7c6ff
CHORE: Update comments
AoGao-Kedoka 68c11f5
CHORE: Update coloring enable flag
AoGao-Kedoka f9d2393
CHORE: Use depth value directly for background color mapping
AoGao-Kedoka 95c1701
REFACTORE: Structural changes to the display depth pass
AoGao-Kedoka 0f6ae74
Remove debug includes
AoGao-Kedoka 2202a96
CHORE: Small changes, add tests and docs
AoGao-Kedoka 726202f
DOC: Update doc mistake
AoGao-Kedoka e53a0be
FIX: Fix colormap file by name
AoGao-Kedoka f4c4653
Add function seperator
AoGao-Kedoka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| #include "vtkF3DDisplayDepthRenderPass.h" | ||
|
|
||
| #include "vtkObjectFactory.h" | ||
| #include "vtkOpenGLError.h" | ||
| #include "vtkOpenGLFramebufferObject.h" | ||
| #include "vtkOpenGLQuadHelper.h" | ||
| #include "vtkOpenGLRenderUtilities.h" | ||
| #include "vtkOpenGLRenderWindow.h" | ||
| #include "vtkOpenGLShaderCache.h" | ||
| #include "vtkOpenGLState.h" | ||
| #include "vtkRenderState.h" | ||
| #include "vtkRenderer.h" | ||
| #include "vtkShaderProgram.h" | ||
| #include "vtkTextureObject.h" | ||
| #include <vtkOpenGLRenderer.h> | ||
|
|
||
| vtkStandardNewMacro(vtkF3DDisplayDepthRenderPass); | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| void vtkF3DDisplayDepthRenderPass::Render(const vtkRenderState* state) | ||
| { | ||
| vtkOpenGLClearErrorMacro(); | ||
| this->NumberOfRenderedProps = 0; | ||
|
|
||
| vtkRenderer* renderer = state->GetRenderer(); | ||
| vtkOpenGLRenderWindow* renWin = vtkOpenGLRenderWindow::SafeDownCast(renderer->GetRenderWindow()); | ||
| vtkOpenGLState* ostate = renWin->GetState(); | ||
|
|
||
| vtkOpenGLState::ScopedglEnableDisable bsaver(ostate, GL_BLEND); | ||
| vtkOpenGLState::ScopedglEnableDisable dsaver(ostate, GL_DEPTH_TEST); | ||
|
|
||
| assert(this->DelegatePass != nullptr); | ||
|
|
||
| int pos[2]; | ||
| int size[2]; | ||
| renderer->GetTiledSizeAndOrigin(&size[0], &size[1], &pos[0], &pos[1]); | ||
|
|
||
| // initialize resources | ||
| if (this->DepthTexture == nullptr) | ||
| { | ||
| this->DepthTexture = vtkSmartPointer<vtkTextureObject>::New(); | ||
| this->DepthTexture->SetContext(renWin); | ||
| this->DepthTexture->SetFormat(GL_RGBA); | ||
| this->DepthTexture->SetInternalFormat(GL_RGBA16F); | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this->DepthTexture->SetDataType(GL_HALF_FLOAT); | ||
| this->DepthTexture->SetMinificationFilter(vtkTextureObject::Linear); | ||
| this->DepthTexture->SetMagnificationFilter(vtkTextureObject::Linear); | ||
| this->DepthTexture->SetWrapS(vtkTextureObject::ClampToEdge); | ||
| this->DepthTexture->SetWrapT(vtkTextureObject::ClampToEdge); | ||
| this->DepthTexture->Allocate2D(size[0], size[1], 4, VTK_FLOAT); | ||
| } | ||
| this->DepthTexture->Resize(size[0], size[1]); | ||
|
|
||
| if (this->ColorMapTexture == nullptr) | ||
| { | ||
| this->ColorMapTexture = vtkSmartPointer<vtkTextureObject>::New(); | ||
| this->ColorMapTexture->SetContext(renWin); | ||
| this->ColorMapTexture->Allocate1D(256, 3, VTK_FLOAT); | ||
| this->ColorMapTexture->SetWrapS(vtkTextureObject::ClampToEdge); | ||
| this->ColorMapTexture->SetWrapT(vtkTextureObject::ClampToEdge); | ||
| this->ColorMapTexture->SetMinificationFilter(vtkTextureObject::Linear); | ||
| this->ColorMapTexture->SetMagnificationFilter(vtkTextureObject::Linear); | ||
|
|
||
| this->ColorMapTexture->SetFormat(GL_RGB); | ||
| this->ColorMapTexture->SetInternalFormat(GL_RGB16F); | ||
| this->ColorMapTexture->SetDataType(GL_FLOAT); | ||
|
|
||
| if (ColorMapDirty && this->ColorMap) | ||
| { | ||
| int tableSize = 256; | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this->ColorMap->SetNumberOfValues(tableSize); | ||
| this->ColorMap->Build(); | ||
| double range[2]; | ||
| this->ColorMap->GetRange(range); | ||
|
|
||
| std::vector<float> table(tableSize * 3); | ||
| this->ColorMap->GetTable(range[0], range[1], tableSize, table.data()); | ||
|
|
||
| this->ColorMapTexture->Create1DFromRaw(tableSize, 3, VTK_FLOAT, table.data()); | ||
| ColorMapDirty = false; | ||
| } | ||
| } | ||
|
|
||
| if (this->FrameBufferObject == nullptr) | ||
| { | ||
| this->FrameBufferObject = vtkSmartPointer<vtkOpenGLFramebufferObject>::New(); | ||
| this->FrameBufferObject->SetContext(renWin); | ||
| } | ||
|
|
||
| // render to texture | ||
| this->PreRender(state); | ||
| renWin->GetState()->PushFramebufferBindings(); | ||
| this->RenderDelegate( | ||
| state, size[0], size[1], size[0], size[1], this->FrameBufferObject, this->DepthTexture); | ||
| renWin->GetState()->PopFramebufferBindings(); | ||
| this->PostRender(state); | ||
|
|
||
| // depth displaying pass | ||
| if (!this->QuadHelper) | ||
| { | ||
| std::string depthDisplayingFS = | ||
| vtkOpenGLRenderUtilities::GetFullScreenQuadFragmentShaderTemplate(); | ||
| vtkShaderProgram::Substitute(depthDisplayingFS, "//VTK::FSQ::Decl", | ||
| "uniform sampler2D depthTexture;\n" | ||
| "uniform bool useColorMap;\n" | ||
| "uniform sampler1D colorMapTexture;\n" | ||
| "//VTK::FSQ::Decl"); | ||
|
|
||
| vtkShaderProgram::Substitute(depthDisplayingFS, "//VTK::FSQ::Impl", | ||
| "float depth = texture2D(depthTexture, texCoord).r;\n" | ||
| "if (useColorMap)\n" | ||
| "{\n" | ||
| " vec4 colorMapped = texture1D(colorMapTexture, depth);\n" | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| " gl_FragData[0] = vec4(colorMapped.rgb, 1.0);\n" | ||
| " return;\n" | ||
| "}\n" | ||
| "else {" | ||
| "if (depth <= 1e-6) depth = 1.0;\n" | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "}" | ||
| "gl_FragData[0] = vec4(depth, depth, depth, 1.0);\n" | ||
| "//VTK::FSQ::Impl"); | ||
| this->QuadHelper = | ||
| std::make_shared<vtkOpenGLQuadHelper>(renWin, nullptr, depthDisplayingFS.c_str(), nullptr); | ||
| this->QuadHelper->ShaderChangeValue = this->GetMTime(); | ||
| } | ||
| else | ||
| { | ||
| renWin->GetShaderCache()->ReadyShaderProgram(this->QuadHelper->Program); | ||
| } | ||
|
|
||
| assert(this->QuadHelper->Program && this->QuadHelper->Program->GetCompiled()); | ||
|
|
||
| this->DepthTexture->Activate(); | ||
| this->ColorMapTexture->Activate(); | ||
|
|
||
| this->QuadHelper->Program->SetUniformi("depthTexture", this->DepthTexture->GetTextureUnit()); | ||
| this->QuadHelper->Program->SetUniformi("useColorMap", this->ColorMap != nullptr ? 1 : 0); | ||
| this->QuadHelper->Program->SetUniformi( | ||
| "colorMapTexture", this->ColorMapTexture->GetTextureUnit()); | ||
|
|
||
| ostate->vtkglDisable(GL_BLEND); | ||
| ostate->vtkglDisable(GL_DEPTH_TEST); | ||
| ostate->vtkglViewport(pos[0], pos[1], size[0], size[1]); | ||
| ostate->vtkglScissor(pos[0], pos[1], size[0], size[1]); | ||
|
|
||
| this->QuadHelper->Render(); | ||
|
|
||
| this->DepthTexture->Deactivate(); | ||
| this->ColorMapTexture->Deactivate(); | ||
| vtkOpenGLCheckErrorMacro("failed after Render"); | ||
| } | ||
|
|
||
| void vtkF3DDisplayDepthRenderPass::ReleaseGraphicsResources(vtkWindow* window) | ||
| { | ||
| this->Superclass::ReleaseGraphicsResources(window); | ||
|
|
||
| if (this->FrameBufferObject) | ||
| { | ||
| this->FrameBufferObject->ReleaseGraphicsResources(window); | ||
| } | ||
| if (this->DepthTexture) | ||
| { | ||
| this->DepthTexture->ReleaseGraphicsResources(window); | ||
| } | ||
| if (this->ColorMapTexture) | ||
| { | ||
| this->ColorMapTexture->ReleaseGraphicsResources(window); | ||
| } | ||
| } | ||
|
|
||
| bool vtkF3DDisplayDepthRenderPass::PreReplaceShaderValues(std::string& vertexShader, | ||
| std::string& geometryShader, std::string& fragmentShader, vtkAbstractMapper* mapper, | ||
| vtkProp* prop) | ||
| { | ||
| vtkShaderProgram::Substitute( | ||
| fragmentShader, "//VTK::Light::Impl", " gl_FragData[0] = vec4(vec3(gl_FragCoord.z), 1.0);\n"); | ||
| return true; | ||
| } | ||
|
|
||
| void vtkF3DDisplayDepthRenderPass::SetColorMap(vtkDiscretizableColorTransferFunction* colorMap) | ||
| { | ||
| this->ColorMap = colorMap; | ||
| ColorMapDirty = true; | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #ifndef vtkF3DDepthRenderPass_h | ||
| #define vtkF3DDepthRenderPass_h | ||
|
|
||
| #include "vtkImageProcessingPass.h" | ||
|
|
||
| #include <vtkDiscretizableColorTransferFunction.h> | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include <vtkSmartPointer.h> | ||
|
|
||
| #include <memory> | ||
|
|
||
| class vtkOpenGLFramebufferObject; | ||
| class vtkOpenGLQuadHelper; | ||
| class vtkTextureObject; | ||
|
|
||
| class vtkF3DDisplayDepthRenderPass : public vtkImageProcessingPass | ||
| { | ||
| public: | ||
| static vtkF3DDisplayDepthRenderPass* New(); | ||
| vtkTypeMacro(vtkF3DDisplayDepthRenderPass, vtkImageProcessingPass); | ||
| void Render(const vtkRenderState* state) override; | ||
| void ReleaseGraphicsResources(vtkWindow* window) override; | ||
| bool PreReplaceShaderValues(std::string& vertexShader, std::string& geometryShader, | ||
| std::string& fragmentShader, vtkAbstractMapper* mapper, vtkProp* prop) override; | ||
| void SetColorMap(vtkDiscretizableColorTransferFunction* colorMap); | ||
|
|
||
| private: | ||
| vtkF3DDisplayDepthRenderPass() = default; | ||
| ~vtkF3DDisplayDepthRenderPass() override = default; | ||
|
|
||
| vtkDiscretizableColorTransferFunction* ColorMap = nullptr; | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bool ColorMapDirty = true; | ||
|
|
||
| vtkSmartPointer<vtkTextureObject> DepthTexture; | ||
| vtkSmartPointer<vtkTextureObject> ColorMapTexture; | ||
| vtkSmartPointer<vtkOpenGLFramebufferObject> FrameBufferObject; | ||
| std::shared_ptr<vtkOpenGLQuadHelper> QuadHelper; | ||
| }; | ||
| #endif | ||
AoGao-Kedoka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.