-
Notifications
You must be signed in to change notification settings - Fork 185
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
KDE Rendering Experimental Program #804
Open
JoaoDell
wants to merge
18
commits into
fury-gl:master
Choose a base branch
from
JoaoDell:kde
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
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ad3fda1
First experimental program for framebuffers
JoaoDell 41bf2b1
Updating branch with master's latest changes
JoaoDell 1a0ae7c
Shader changes
JoaoDell 3de5745
Codespell fixes
JoaoDell 3c5e4c0
Irrelevant changes
JoaoDell d08f137
FBO setup working
JoaoDell 0cc63f5
Updating branch with master latest changes.
JoaoDell ceb076f
Render structuring and cleanup
JoaoDell c848f56
Minor changes
JoaoDell 620060a
Code cleanup
JoaoDell 961dbd7
minor changes
JoaoDell dc989b7
New approach
JoaoDell 06ad2a8
Offscreen rendering option
JoaoDell 1d937dc
KDE demonstration refactoring
JoaoDell b611368
redirected function calling
JoaoDell 06dd4a1
fix: General changes and cleanups
JoaoDell 93ca6c2
fix: Adapting to FURY's standards and some fixes
JoaoDell bf2b91a
fix: Now using native fury.colormap native generator
JoaoDell 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 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,195 @@ | ||
import numpy as np | ||
from fury import window, actor, lib, shaders | ||
import vtk | ||
|
||
|
||
def shader_custom_uniforms(actor : actor.Actor, shader_type : str): | ||
"""Eases the passing of uniform values to the shaders by returning ``actor.GetShaderProperty().GetVertexCustomUniforms()``, | ||
that give access to the ``SetUniform`` methods. | ||
Parameters | ||
---------- | ||
actor : actor.Actor | ||
Actor which the uniform values will be passed to. | ||
shader_type : str | ||
Shader type of the uniform values to be passed. It can be: | ||
* "vertex" | ||
* "fragment" | ||
* "geometry" | ||
""" | ||
if shader_type == "vertex": | ||
return actor.GetShaderProperty().GetVertexCustomUniforms() | ||
elif shader_type == "fragment": | ||
return actor.GetShaderProperty().GetFragmentCustomUniforms() | ||
elif shader_type == "geometry": | ||
return actor.GetShaderProperty().GetGeometryCustomUniforms() | ||
else: raise ValueError("Shader type unknown.") | ||
|
||
|
||
|
||
# Don't mind these shaders, I just set them up to focus first on the framebuffer, then after on the shaders themselves | ||
billboard_vert_decl = """/* Billboard vertex shader declaration */ | ||
in vec3 center; | ||
in vec2 in_tex; | ||
|
||
out vec3 centerVertexMCVSOutput; | ||
out vec3 normalizedVertexMCVSOutput; | ||
varying vec2 out_tex;""" | ||
|
||
billboard_vert_impl = """/* Billboard vertex shader implementation */ | ||
centerVertexMCVSOutput = center; | ||
normalizedVertexMCVSOutput = vertexMC.xyz - center; // 1st Norm. [-scale, scale] | ||
float scalingFactor = 1. / abs(normalizedVertexMCVSOutput.x); | ||
float size = abs(normalizedVertexMCVSOutput.x) * 2; | ||
normalizedVertexMCVSOutput *= scalingFactor; // 2nd Norm. [-1, 1] | ||
vec2 billboardSize = vec2(size, size); // Fixes the scaling issue | ||
vec3 cameraRightMC = vec3(MCVCMatrix[0][0], MCVCMatrix[1][0], MCVCMatrix[2][0]); | ||
vec3 cameraUpMC = vec3(MCVCMatrix[0][1], MCVCMatrix[1][1], MCVCMatrix[2][1]); | ||
vec3 vertexPositionMC = center + | ||
cameraRightMC * billboardSize.x * normalizedVertexMCVSOutput.x + | ||
cameraUpMC * billboardSize.y * normalizedVertexMCVSOutput.y; | ||
out_tex = in_tex; | ||
gl_Position = MCDCMatrix * vec4(vertexPositionMC, 1.);""" | ||
|
||
|
||
frag_decl = """in vec3 normalizedVertexMCVSOutput; | ||
""" | ||
|
||
kde_func = """float kde(vec3 point, vec3 coord, float sigma){ | ||
return exp(-pow(length(abs(point - coord)), 2.0)/(2.0*sigma*sigma)); | ||
}; """ | ||
|
||
frag_impl = """ | ||
vec3 k = vec3(kde(point0, vec3(gl_FragCoord.xy/res0, 0.0), 10.0)); | ||
fragOutput0 = vec4(k, 0.5);""" | ||
|
||
|
||
frag = """ | ||
|
||
varying vec2 out_tex; | ||
void main(){ | ||
gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0); | ||
}""" | ||
|
||
|
||
frag_2 = """ | ||
|
||
varying vec2 out_tex; | ||
|
||
uniform sampler2D screenTexture; | ||
|
||
void main(){ | ||
vec4 texture = texture(screenTexture, out_tex); | ||
gl_FragColor = texture; | ||
//gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); | ||
}""" | ||
|
||
|
||
width, height = (1920, 1080) | ||
|
||
scene = window.Scene() | ||
|
||
scene.set_camera(position=(-6, 5, -10), | ||
focal_point=(0.0, | ||
0.0, | ||
0.0), | ||
view_up=(0.0, 0.0, 0.0)) | ||
|
||
|
||
|
||
scene.DebugOn() | ||
scene.GlobalWarningDisplayOn() | ||
|
||
manager = window.ShowManager( | ||
scene, | ||
"trees demo", | ||
(1920, | ||
1080), | ||
reset_camera=True, | ||
order_transparent=True) | ||
|
||
|
||
scale = np.array([[width, height, 0.0]]) | ||
|
||
# Actor setup | ||
billboard = actor.billboard(np.array([[0.0, 0.0, 0.0]]), (1.0, 0.0, 0.0), scales=scale) | ||
|
||
# Adding first the kde rendering shaders | ||
actor.shader_to_actor(billboard, "vertex", billboard_vert_impl, billboard_vert_decl) | ||
actor.replace_shader_in_actor(billboard, "fragment", frag) | ||
billboard_tex = np.array([[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]) | ||
actor.attribute_to_actor(billboard, billboard_tex, "in_tex") | ||
|
||
|
||
|
||
# Actor adding | ||
scene.add(billboard) | ||
|
||
|
||
|
||
scene.GetRenderWindow().DebugOn() | ||
scene.GetRenderWindow().GlobalWarningDisplayOn() | ||
|
||
|
||
|
||
# FBO setup | ||
|
||
# Useful links | ||
# https://vtk.org/doc/nightly/html/classvtkOpenGLFramebufferObject.html#details | ||
# https://gitlab.kitware.com/vtk/vtk/-/blob/v9.2.0/Rendering/OpenGL2/Testing/Cxx/TestWindowBlits.cxx | ||
# https://vtk.org/doc/nightly/html/classvtkTextureObject.html | ||
# https://vtk.org/doc/nightly/html/classvtkOpenGLRenderWindow.html | ||
# https://learnopengl.com/Advanced-OpenGL/Framebuffers | ||
# https://github.com/JoaoDell/Comp-Grafica/blob/main/shadow3d.c | ||
# https://github.com/Kitware/VTK/blob/8f88edb91d2efea2d9cef1a1399d7a856c47f3be/Rendering/OpenGL2/vtkOpenGLFramebufferObject.cxx | ||
# https://fury.gl/latest/auto_examples/04_demos/viz_fine_tuning_gl_context.html#sphx-glr-auto-examples-04-demos-viz-fine-tuning-gl-context-py | ||
|
||
FBO = vtk.vtkOpenGLFramebufferObject() | ||
|
||
manager.window.SetOffScreenRendering(True) | ||
manager.initialize() # sets everything for rendering | ||
|
||
FBO.DebugOn() | ||
FBO.GlobalWarningDisplayOn() | ||
|
||
FBO.SetContext(manager.window) # Sets the context for the FBO. | ||
FBO.SaveCurrentBindingsAndBuffers() | ||
FBO.PopulateFramebuffer(width, height, True, 1, vtk.VTK_UNSIGNED_CHAR, False, 24, 0) | ||
|
||
# Checking FBO status | ||
print("FBO of index:", FBO.GetFBOIndex()) | ||
print("Number of color attachments:", FBO.GetNumberOfColorAttachments()) | ||
FBO.RestorePreviousBindingsAndBuffers() | ||
|
||
|
||
|
||
# RENDER TIME | ||
# shaders.shader_apply_effects(manager.window, billboard, window.gl_disable_depth) | ||
# shaders.shader_apply_effects(manager.window, billboard, window.gl_set_normal_blending) | ||
|
||
FBO.SaveCurrentBindingsAndBuffers() | ||
FBO.Bind() | ||
FBO.ActivateBuffer(0) | ||
|
||
manager.render() | ||
|
||
FBO.RestorePreviousBindingsAndBuffers() | ||
|
||
|
||
|
||
color_texture = FBO.GetColorAttachmentAsTextureObject(0) # Gets the color texture for further rendering | ||
|
||
|
||
|
||
# WIP below | ||
manager.window.SetOffScreenRendering(False) # Then, after rendering everything in the offscreen FBO, time to render it to a simple billboard | ||
actor.replace_shader_in_actor(billboard, "fragment", frag_2) | ||
shaders.shader_apply_effects(manager.window, billboard, window.gl_disable_blend) | ||
|
||
color_texture.Activate() | ||
|
||
|
||
|
||
interactive = True | ||
|
||
if interactive: | ||
manager.start() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
texture should be 2D vec right?
try this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was passing out_tex as texture coordinates, but I will try what you recommended