ボリューム レンダリング アプリケーション (C# + OpenTK) に取り組んでいます。ボリュームはレイキャスティングを使用してレンダリングされています。このサイトで多くのインスピレーションを見つけました: http://graphicsrunner.blogspot.sk/2009/01/volume-rendering-101.html。私のアプリケーションは OpenGL で動作しますが、 3D テクスチャやその他のものを使用する主なアイデアは同じです。アプリケーションは正常に動作しますが、「ボリュームに流れ込む」(バウンディング ボックス内を意味する) 後、すべてが消えてしまうので、これを防ぎたいと思います。それで、これを行う簡単な方法はありますか?--> ボリューム内を流れたり移動したりできるようになります。
フラグメントシェーダーのコードは次のとおりです。
#version 330
in vec3 EntryPoint;
in vec4 ExitPointCoord;
uniform sampler2D ExitPoints;
uniform sampler3D VolumeTex;
uniform sampler1D TransferFunc;
uniform float StepSize;
uniform float AlphaReduce;
uniform vec2 ScreenSize;
layout (location = 0) out vec4 FragColor;
void main()
{
//gl_FragCoord --> http://www.txutxi.com/?p=182
vec3 exitPoint = texture(ExitPoints, gl_FragCoord.st/ScreenSize).xyz;
//background need no raycasting
if (EntryPoint == exitPoint)
discard;
vec3 rayDirection = normalize(exitPoint - EntryPoint);
vec4 currentPosition = vec4(EntryPoint, 0.0f);
vec4 colorSum = vec4(.0f,.0f,.0f,.0f);
vec4 color = vec4(0.0f,0.0f,0.0f,0.0f);
vec4 value = vec4(0.0f);
vec3 Step = rayDirection * StepSize;
float stepLength= length(Step);
float LengthSum = 0.0f;
float Length = length(exitPoint - EntryPoint);
for(int i=0; i < 16000; i++)
{
currentPosition.w = 0.0f;
value = texture(VolumeTex, currentPosition.xyz);
color = texture(TransferFunc, value.a);
//reduce the alpha to have a more transparent result
color.a *= AlphaReduce;
//Front to back blending
color.rgb *= color.a;
colorSum = (1.0f - colorSum.a) * color + colorSum;
//accumulate length
LengthSum += stepLength;
//break from the loop when alpha gets high enough
if(colorSum.a >= .95f)
break;
//advance the current position
currentPosition.xyz += Step;
//break if the ray is outside of the bounding box
if(LengthSum >= Length)
break;
}
FragColor = colorSum;
}
以下のコードはhttps://github.com/toolchainX/Volume_Rendering_Using_GLSLに基づいてい ます
Display() 関数:
public void Display()
{
// the color of the vertex in the back face is also the location
// of the vertex
// save the back face to the user defined framebuffer bound
// with a 2D texture named `g_bfTexObj`
// draw the front face of the box
// in the rendering process, i.e. the ray marching process
// loading the volume `g_volTexObj` as well as the `g_bfTexObj`
// after vertex shader processing we got the color as well as the location of
// the vertex (in the object coordinates, before transformation).
// and the vertex assemblied into primitives before entering
// fragment shader processing stage.
// in fragment shader processing stage. we got `g_bfTexObj`
// (correspond to 'VolumeTex' in glsl)and `g_volTexObj`(correspond to 'ExitPoints')
// as well as the location of primitives.
// draw the back face of the box
GL.Enable(EnableCap.DepthTest);
//"vykreslim" front || back face objemu do framebuffru --> teda do 2D textury s ID bfTexID
//(pomocou backface.frag &.vert)
GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferID);
GL.Viewport(0, 0, width, height);
LinkShader(spMain.GetProgramHandle(), bfVertShader.GetShaderHandle(), bfFragShader.GetShaderHandle());
spMain.UseProgram();
//cull front face
Render(CullFaceMode.Front);
spMain.UseProgram(0);
//klasicky framebuffer --> "obrazovka"
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.Viewport(0, 0, width, height);
LinkShader(spMain.GetProgramHandle(), rcVertShader.GetShaderHandle(), rcFragShader.GetShaderHandle());
spMain.UseProgram();
SetUniforms();
Render(CullFaceMode.Back);
spMain.UseProgram(0);
GL.Disable(EnableCap.DepthTest);
}
private void DrawBox(CullFaceMode mode)
{
// --> Face culling allows non-visible triangles of closed surfaces to be culled before expensive Rasterization and Fragment Shader operations.
GL.Enable(EnableCap.CullFace);
GL.CullFace(mode);
GL.BindVertexArray(VAO);
GL.DrawElements(PrimitiveType.Triangles, 36, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
GL.Disable(EnableCap.CullFace);
spMain.UseProgram(0);//zapnuty bol v Render() ktora DrawBox zavolala
}
private void Render(CullFaceMode mode)
{
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
spMain.UseProgram();
spMain.SetUniform("modelViewMatrix", Current);
spMain.SetUniform("projectionMatrix", projectionMatrix);
DrawBox(mode);
}
問題は (私が思うに) ボリュームに向かって移動しているとき (カメラを動かさず、ボリュームをスケーリングするだけです)、スケール係数が 2.7 を超える場合、ボリューム内にいるということです。最終的な画像がレンダリングされる平面」なので、何も見えません。私が考えることができる解決策 (おそらく) は、次のようなものです: スケール係数 = 2.7 に達した場合:
1.) -> ボリュームをスケーリングしない
2.) -> 何らかの方法でフラグメント シェーダーに EntryPoint を RayDirection の方向に移動するように (おそらくスケール ファクターに基づいて) 指示します。
今、私はこの「方法」を試しましたが、うまくいくようです:
vec3 entryPoint = EntryPoint + some_value * rayDirection;
some_value は [0,1[ 間隔 (または [0,1]?) の間にクランプする必要がありますが、そのおかげで問題ないかもしれません:
if (EntryPoint == exitPoint)
discard;
だから今、おそらく(私の解決策がそれほど悪くない場合)、これに対する私の答えを変更できます:some_valueを計算する方法(フラグメントシェーダーに送信するスケールファクターに基づく)?
if(scale_factor < 2.7something)
work like before;
else
{
compute some_value; //(I need help with this part)
change entry point;
work like before;
}
(私は英語のネイティブスピーカーではないので、テキストに大きな間違いがあり、何か理解できない場合は、お知らせください。これらのバグを修正します)
ありがとう。