2012-11-15 105 views
0

OK二维网格所以我有,我已经决定了使用三角形一个真正的大广场(网格),然后应用高度图撞它。现在我试图做的是获取网格线。我已经找到了一种方法来做到这一点,但是这会导致在片段着色器中为33个if语句只为x,然后在另一个33中为y。我被告知我可以使用现在正在做的事情,并且稍微不同地使用它(使用一些GLSL函数),只需要1或2条if语句。这是我当前的代码(不是全部做完,但给你的是什么即时通讯做的想法。)创建使用OpenGL和顶点和片段着色器

#version 330 

uniform sampler2D texture; 

in vec2 texCoord; 

layout (location=0) out vec4 fragColour; 



void main(void) { 

vec4 newColor; 
vec2 line = texCoord * 32; // makes texCoords easier to number (as divided by 32 in the C++array) 


if(line.x > 0 && line.x < 0.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
else if(line.x > 1 && line.x < 1.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 2 && line.x < 2.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 3 && line.x < 3.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 4 && line.x < 4.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 5 && line.x < 5.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 6 && line.x < 6.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 7 && line.x < 7.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 8 && line.x < 8.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 9 && line.x < 9.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 10 && line.x < 10.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 11 && line.x < 11.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 12 && line.x < 12.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 13 && line.x < 13.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 14 && line.x < 14.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 15 && line.x < 15.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 16 && line.x < 16.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 17 && line.x < 17.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 18 && line.x < 18.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 19 && line.x < 19.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 20 && line.x < 20.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 21 && line.x < 21.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 22 && line.x < 22.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 23 && line.x < 23.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 
    else if(line.x > 24 && line.x < 24.9) 
{ 
    newColor = vec4(1.0,1.0,1.0,1.0); 
} 

else 
{ 
    newColor = vec4(0.0,0.0,0.0,1.0); 
} 

fragColour = newColor; 
} 
+0

您可能想要使用discard()函数 –

+1

@Mike:仅供参考,您不需要编辑问题以将* answers *添加到它们中。回答下面,问题在上面。你接受了一个答案,这意味着这个问题得到了满意答复。你不需要将他的解决方案放在你的问题上,因为那样会使人们疑惑开始时的问题。 –

+0

那么这只是浪费创建一个新的线程到相关的问题,但它现在无论如何 –

回答

6

你不应该使用的控制流(即depedns上比其他制服的东西),除非它是绝对必要(在至少这是我上次编写glsl着色器时的标准建议)。在你的情况下,不需要控制流程。使用stepsmoothstepmix和乘法以避免控制流。

你 “的if/else” 的代码段可以使用这样的事情(未测试的代码)来实现:

fragColor = mix(vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0), step(fract(line.x), 0.9));

或者用纹理查找。纹理查找可能比数值计算更快,具体取决于硬件(和纹理大小)

请注意,使用“step”可能会在远方表面上产生锯齿状的非反锯齿边缘和噪点/莫尔条纹图案。 “OpenGL橙皮书”(“OpenGL着色语言”)有一些例子应该解释如何处理它。但是,使用纹理查找可能更容易。首先,您可以尝试使用“smoothstep”而不是“step”。

,或者,你可以简单地重绘上的固体呈现景观顶部线框模式整个景观。

+0

非常感谢您的帮助。我用那条线和我得到了一个错误与压裂 错误C7506:OpenGL没有定义全球功能压裂 –

+0

我相信我已经解决了它使用分裂而不是压裂,非常感谢它的工作原理:D –

0

这很容易通过简单地扔下投影纹理来完成。它甚至不需要“投射”;只需根据您的line计算纹理坐标。实际上,这些看起来很像你的纹理坐标。因此,只需创建一个内部为白色,外部为黑色的纹理。你甚至可以使它成为单通道来节省内存。

当然,您需要在S和T方向上重复纹理。

+0

感谢您的反馈,我试图不使用这个练习的纹理,只是让它使用我在片段着色器中定义的颜色。不管怎么说,还是要谢谢你 :) –

相关问题