在OpenGL中如何将原语渲染为线框?
当前回答
如果你处理的是OpenGL ES 2.0,你可以从中选择一个绘制模式常量
GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,来画线,
GL_POINTS(如果您只需要绘制顶点),或者
GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN和gl_triangle来绘制填充三角形
作为你的第一个论点
glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid * indices)
or
glDrawArrays(GLenum模式,GLint优先,GLsizei计数)调用。
其他回答
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
要打开电源,
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
回归正常。
注意,如果启用了纹理映射和照明,它们仍然会应用到线框线上,这看起来可能很奇怪。
从http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/ogladv/tut5
// Turn on wireframe mode
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
// Draw the box
DrawBox();
// Turn off wireframe mode
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
在非抗锯齿渲染目标上绘制抗锯齿线的一个好而简单的方法是绘制宽度为4像素的矩形,纹理为1x4, alpha通道值为{0,1,1,0。},并使用mip-mapping关闭的线性过滤。这将使线条2像素厚,但你可以改变不同厚度的纹理。 这比重力计算更快更简单。
在现代OpenGL(OpenGL 3.2及更高版本)中,你可以使用几何着色器:
#version 330
layout (triangles) in;
layout (line_strip /*for lines, use "points" for points*/, max_vertices=3) out;
in vec2 texcoords_pass[]; //Texcoords from Vertex Shader
in vec3 normals_pass[]; //Normals from Vertex Shader
out vec3 normals; //Normals for Fragment Shader
out vec2 texcoords; //Texcoords for Fragment Shader
void main(void)
{
int i;
for (i = 0; i < gl_in.length(); i++)
{
texcoords=texcoords_pass[i]; //Pass through
normals=normals_pass[i]; //Pass through
gl_Position = gl_in[i].gl_Position; //Pass through
EmitVertex();
}
EndPrimitive();
}
通知:
对于点,改变布局(line_strip, max_vertices=3);到布局(points, max_vertices=3); 阅读更多关于几何着色器
如果您正在使用固定的管道(OpenGL < 3.3)或兼容性配置文件,您可以使用
//Turn on wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//Draw the scene with polygons as lines (wireframe)
renderScene();
//Turn off wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
在这种情况下,你可以通过调用glLineWidth来改变行宽
否则,你需要在你的绘制方法(glDrawElements, glDrawArrays等)中改变多边形模式,你可能会得到一些粗略的结果,因为你的顶点数据是三角形的,你是输出线。为了获得最好的效果,可以考虑使用几何着色器或为线框创建新数据。