我想实现单通线框,但我有几个在这个过程中的问题。
问题1
对于一些原因,我没有得到仅线框(像glPolygoneMode
-线)充满几何后,我的几何着色器的工作。
)
但是,如果我禁用几何着色器让我的几何形状:
我真正想实现的是几何和它的线框。
问题2
其实,我的原语三角形带。 我用它们来避免使用四边形来提高性能。 我怎么能跳过线框的边缘,而在画什么? (我已经看到了后如何可以做,但它仍然是不明确我。)
这里是我的着色器:
顶点着色器:
#version 330
//
// (C) Copyright 2010 Patrick Cozzi and Deron Ohlarik
//
// Distributed under the MIT License.
// See License.txt or http://www.opensource.org/licenses/mit-license.php.
//
layout(location = 0) in vec3 in_Position;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 og_viewportTransformationMatrix;
out vec2 windowPosition;
vec4 og_ClipToWindowCoordinates(vec4 v, mat4 viewportTransformationMatrix);
void main()
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
windowPosition = og_ClipToWindowCoordinates(gl_Position, og_viewportTransformationMatrix).xy;
}
vec4 og_ClipToWindowCoordinates(vec4 v, mat4 viewportTransformationMatrix)
{
v.xyz /= v.w; // normalized device coordinates
v.xyz = (viewportTransformationMatrix * vec4(v.xyz, 1.0)).xyz; // window coordinates
return v;
}
Geomerty着色器:
#version 330
//
// (C) Copyright 2010 Patrick Cozzi and Deron Ohlarik
//
// Distributed under the MIT License.
// See License.txt or http://www.opensource.org/licenses/mit-license.php.
//
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec2 windowPosition[];
noperspective out vec3 distanceToEdges;
float og_distanceToLine(vec2 f, vec2 p0, vec2 p1);
void main()
{
vec2 p0 = windowPosition[0];
vec2 p1 = windowPosition[1];
vec2 p2 = windowPosition[2];
gl_Position = gl_in[0].gl_Position;
distanceToEdges = vec3(og_distanceToLine(p0, p1, p2), 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[1].gl_Position;
distanceToEdges = vec3(0.0, og_distanceToLine(p1, p2, p0), 0.0);
EmitVertex();
gl_Position = gl_in[2].gl_Position;
distanceToEdges = vec3(0.0, 0.0, og_distanceToLine(p2, p0, p1));
EmitVertex();
}
float og_distanceToLine(vec2 f, vec2 p0, vec2 p1)
{
vec2 l = f - p0;
vec2 d = p1 - p0;
//
// Closed point on line to f
//
vec2 p = p0 + (d * (dot(l, d) / dot(d, d)));
return distance(f, p);
}
片段着色器:
#version 330
//
// (C) Copyright 2010 Patrick Cozzi and Deron Ohlarik
//
// Distributed under the MIT License.
// See License.txt or http://www.opensource.org/licenses/mit-license.php.
//
uniform float u_halfLineWidth;
uniform vec3 u_color;
noperspective in vec3 distanceToEdges;
out vec4 fragmentColor;
void main()
{
float d = min(distanceToEdges.x, min(distanceToEdges.y, distanceToEdges.z));
if (d > u_halfLineWidth + 1.0)
{
discard;
}
d = clamp(d - (u_halfLineWidth - 1.0), 0.0, 2.0);
fragmentColor = vec4(u_color, exp2(-2.0 * d * d));
}
帮我找出我的位置弄错了。
更新:
我已经更新Fragment shader
和Geometry shader
在他的回答如上面所提到的Andon M.科尔曼,但在第二个问题中所述问题仍然没有解决。
更新2
我刚才做一些小的修改在fragmet着色器,并且解决了我的问题。
更新片段着色器
#version 330
//
// (C) Copyright 2010 Patrick Cozzi and Deron Ohlarik
//
// Distributed under the MIT License.
// See License.txt or http://www.opensource.org/licenses/mit-license.php.
//
uniform float u_halfLineWidth;
uniform vec3 u_color;
noperspective in vec3 distanceToEdges;
out vec4 fragmentColor;
void main()
{
float d = min(distanceToEdges.y, max(distanceToEdges.x, distanceToEdges.z));
if (d > u_halfLineWidth + 1.0)
{
fragmentColor = vec4(u_color, 1);
return;
}
fragmentColor = vec4(vec3(0,0,0), 1);
}