0

I have encountered a problem of rendering artifacts of 3D texture as below:

Rendering artifacts(texture get hidden when viewed from certain angle)

I have searched on net as to find solution of this problem, and most answer pointed towards the problem in regards of depth buffer bit. While i have tried to change the depth buffer bit to 24 bit from GL_DEPTH to GL_STENCIL in GLUT, the result remains the same as the texture(or geometry-not really sure) get hidden when viewed from certain angle..

So, can i know what is exactly the problem that results in this kind of artifacts??

Below is the fragment shader code snippet(OpenGL Development Cookbook)

void main()
{ 
//get the 3D texture coordinates for lookup into the volume dataset
vec3 dataPos = vUV;

vec3 geomDir = normalize((vec3(0.556,0.614,0.201)*vUV-vec3(0.278,0.307,0.1005)) - camPos); 

vec3 dirStep = geomDir * step_size;     

//flag to indicate if the raymarch loop should terminate
bool stop = false; 

//for all samples along the ray
for (int i = 0; i < MAX_SAMPLES; i++) {
    // advance ray by dirstep
    dataPos = dataPos + dirStep;

    stop = dot(sign(dataPos-texMin),sign(texMax-dataPos)) < 3.0f;

    //if the stopping condition is true we brek out of the ray marching loop
    if (stop) 
        break;

    // data fetching from the red channel of volume texture
    float sample = texture(volume, dataPos).r;  


    float prev_alpha = sample - (sample * vFragColor.a);
    vFragColor.rgb = (prev_alpha) * vec3(sample) + vFragColor.rgb; 
    vFragColor.a += prev_alpha; 


    if( vFragColor.a>0.99)
        break;
}

FYI, below is the vertex shader snippet:

#version 330 core

layout(location = 0) in vec3 vVertex; //object space vertex position

//uniform
uniform mat4 MVP;   //combined modelview projection matrix

smooth out vec3 vUV; //3D texture coordinates for texture lookup in the    fragment shader

void main()
{  
//get the clipspace position 
gl_Position = MVP*vec4(vVertex.xyz,1);

//get the 3D texture coordinates by adding (0.5,0.5,0.5) to the object space 
//vertex position. Since the unit cube is at origin (min: (-0.5,-0.5,-0.5) and max: (0.5,0.5,0.5))
//adding (0.5,0.5,0.5) to the unit cube object space position gives us values from (0,0,0) to 
//(1,1,1)
//vUV = (vVertex + vec3(0.278,0.307,0.1005))/vec3(0.556,0.614,0.201);
vUV = vVertex/vec3(0.556,0.614,0.201);//after moving the cube to coordinates range of 0-1

}

EDITED: The artifacts present especially when viewing is done relatively at the edge.

FYI, glm::perspective(45.0f,(float)w/h, 1.0f,10.0f);

vincent911001
  • 523
  • 1
  • 6
  • 20
  • I suppose `normalize((vec3(0.556,0.614,0.201)*vUV-vec3(0.278,0.307,0.1005))` is used to orient the UVW coordinates evenly from one corner to the next in positive space by taking the texture size in account? – Dimo Markov Apr 08 '15 at 13:59
  • But what is your vUV range? Is it from -1 to 1 or from 0 to 1? These factors don't look adequate to me. – Dimo Markov Apr 08 '15 at 14:07
  • Have you checked the far plane on your camera? – Mokosha Apr 09 '15 at 00:39
  • @Mokosha, ya, the original far plane was 100.0f, as i have read somewhere that having a big value in term of far plane could possibly also lead to some sort of rendering defects, thus, i have changed the far plane to just 10.0f. But the problem is still the same, thanks btw. `glm::perspective(45.0f,(float)w/h, 1.0f,10.0f);` – vincent911001 Apr 09 '15 at 01:08
  • @DimoMarkov,ya, the operation is used to orient the primitive coordinates according to my texture coordinates as my cuboid is created according to the volume data size. My texture size is from 0 to 1 for all uvw. – vincent911001 Apr 09 '15 at 01:09
  • I was thinking that your near plane was too close rather than too far, which would clip the geometry. If your near plane is at 1.0f, then 1000.0f shouldn't be too bad for a far plane. That being said, if changing it from 100 to 10 didn't make the problem *worse* then that probably isn't the issue. – Mokosha Apr 09 '15 at 02:08
  • @Mokosha, ya, i thought so, the artifacts only appear when viewing is done relatively at the edge which left me wonder whether it is in regards of texture coordinates problem. Thanks a lot btw. – vincent911001 Apr 09 '15 at 02:14
  • Alright, the UVs seem to be in order if that is the case. However, why are you subtracting the camPos directly from the UVs to obtain the ray per pixel? Don't you think that the UVs should be transformed in world space before doing that? Otherwise in many cases your ray cast will be different (when the geometry is translated or rotated, for example) You can test that by putting your geometry exactly at 0,0,0 to 1,1,1 range, so it matches the UVW coords. If there's no mistake, then that is your problem. From what I can see, your mesh is centered around 0,0,0 – Dimo Markov Apr 09 '15 at 06:48
  • Ok, sure, thanks for your help, i will try your suggestion and see how it goes. Appreciate your help, cheers. Will keep you posted. – vincent911001 Apr 09 '15 at 07:05
  • @DimoMarkov, i have tried to put the geometry exactly at 0,0,0 to 1,1,1 range; however, the problem is still not resolved as there are still artifacts. BTW, can i know what do you mean by transforming the UVs to world space?? Coz i am not quite understand in regards of transforming UVs to world space. – vincent911001 Apr 09 '15 at 07:24
  • No worries. Can you share your vertex shader first, so I can directly show you and explain the mistake. Btw, does the error happen only when you move the camera? – Dimo Markov Apr 09 '15 at 07:33
  • @DimoMarkov, sure, will edit the post in a while, yes, the error only happen when i move the camera and the viewing is done at the edges of the geometry. Thanks a lot – vincent911001 Apr 09 '15 at 07:40
  • @DimoMarkov, FYI, the artifacts stay there even after moving the camera while viewing is done relatively at the edges of the geometry. – vincent911001 Apr 09 '15 at 07:49

0 Answers0