2016-04-27 90 views
2

我在一個球體(行星上的邊框)上渲染一些LineLoops,並且希望將它們縮短成較短的行以防止將長線剪切到球體中。 實施例: enter image description here在OpenGL/GLSL中繪製tessallated Lineloo

這是我的當前源代碼: c + +繪製調用:

void Border::Draw() const 
{ 
    glBindVertexArray(_vao); 

    glPatchParameteri(GL_PATCH_VERTICES, 2); 
    glDrawArrays(GL_PATCHES, 0, _size); 

    glBindVertexArray(0); 
} 

頂點着色器:

#version 400 

in layout(location = 0) vec3 position; 

void main() 
{ 
    gl_Position = vec4(position, 1.0); 
} 

Tessallation控制着色器:

#version 400 

layout (vertices = 2) out; 

void main() 
{ 
    if(gl_InvocationID == 0) 
    { 
     //get the distance between two points 
     float dist = length(gl_in[0].gl_Position - gl_in[1].gl_Position); 
     gl_TessLevelOuter[0] = 1; 
     //divide line in segments of size 1.0 
     gl_TessLevelOuter[1] = abs(dist); 
    } 

    gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; 
} 

Tessallati在評估着色

#version 400 

layout (isolines, equal_spacing) in; 

uniform float planetRadius; 
uniform mat4 view; 
uniform mat4 projection; 

void main() 
{ 
    vec4 p = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x); 

    //offset point so it is on the spheres surface 
    p = normalize(p); 
    p *= planetRadius; 

    gl_Position = projection * view * p; 
} 

片段着色器:

#version 400 

out vec4 outColor; 

void main() 
{ 
    outColor = vec4(1.0, 1.0, 1.0, 1.0); 
} 

的tessallation似乎不工作像我期望它的工作(在等值線文檔也很糟糕)。

這是怎麼看的時候我LineLoops只有頂點/片段着色器繪製它:

enter image description here

,這是它的樣子後,我實現了我的tessallation:

enter image description here

線條也看起來不tessallated和具有和以前一樣的長度。

問候的Jhonny

編輯: 找到了解決辦法嘍。看看下面的答案部分來看看它。

+1

你想你肯定規範化整個'vec4 p'而不僅僅是'.xyz'部分?你也許會得到等值線用法錯誤,這裏有一個很好的來源:[http://www.informit.com/articles/article.aspx?p=2120983](http://www.informit.com/articles/article。 aspx?p = 2120983) – aslg

+0

謝謝標準化是問題的一部分。 – Jhonny007

回答

1

我找到了解決方案。問題在於tessallation着色器無法繪製線條。所以,如果你哈瓦由點{p1, p2, p3, p4, p5, p6}它繪製行之間所形成的直線條:
P1 - > P2
P3 - > P4
P5 - > P6

我的每一點給着色器的2倍固定它,所以我的名單看起來是這樣的:
{p1, p2, p2, p3, p3, p4, p4, p5, p5, p6, p6, p1}
的tessallation着色器創建以下行:
P1 - > P2
P2 - > P3
P3 - > P4
P4 - > P5
P5 - > P6
P6 - > P1

我着色器現在這個樣子:

TessControl:

#version 430 

layout (vertices = 2) out; 

void main() 
{ 
    if(gl_InvocationID == 0) 
    { 
     float dist = length(gl_in[0].gl_Position.xyz - gl_in[1].gl_Position.xyz); 

     gl_TessLevelOuter[0] = 1; 
     gl_TessLevelOuter[1] = dist; 
    } 

    gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; 
} 

TessEval:

#version 430 

layout (isolines, equal_spacing) in; 

uniform float planetRadius; 
uniform mat4 view; 
uniform mat4 projection; 

void main() 
{ 
    //only interpolate and normalize xyz coordinates. Thanks to @aslg 
    vec3 p = mix(gl_in[0].gl_Position.xyz, gl_in[1].gl_Position.xyz, gl_TessCoord.x); 

    p = normalize(p); 
    p *= planetRadius; 

    gl_Position = projection * view * vec4(p, 1.0); 
}