0

我遇到了渲染 3D 纹理伪影的问题,如下所示:

渲染伪影(从某个角度观看时,纹理会被隐藏)

我已经在网上搜索以找到该问题的解决方案,并且大多数答案都指向了与深度缓冲区位有关的问题。虽然我试图将深度缓冲区位从 更改为 24 位GL_DEPTHGL_STENCIL in GLUT但结果仍然与从某个角度查看时隐藏的纹理(或几何形状 - 不太确定)相同。

那么,我能知道导致这种工件的究竟是什么问题吗?

下面是片段着色器代码片段(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;
}

仅供参考,下面是顶点着色器片段:

#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

}

已编辑:尤其是在相对边缘进行查看时出现的伪影。

供参考,glm::perspective(45.0f,(float)w/h, 1.0f,10.0f);

4

0 回答 0