在OpenGL中如何将原语渲染为线框?


当前回答

最简单的方法是将原语绘制为GL_LINE_STRIP。

glBegin(GL_LINE_STRIP);
/* Draw vertices here */
glEnd();

其他回答

最简单的方法是将原语绘制为GL_LINE_STRIP。

glBegin(GL_LINE_STRIP);
/* Draw vertices here */
glEnd();

如果你处理的是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计数)调用。

使用这个函数:

void glPolygonMode(GLenum face, GLenum mode);

face:指定模式适用的多边形面。多边形的正面可以是GL_FRONT,背面可以是GL_BACK,两者都可以是GL_FRONT_AND_BACK。

mode:定义了三种模式。

GL_POINT:标记为边界边起点的多边形顶点被绘制为点。 GL_LINE:多边形的边界边被绘制为线段。(你的目标) GL_FILL:填充多边形内部。

glPolygonMode控制图形管道中栅格化多边形的解释。

要了解更多信息,请参阅khronos组中的OpenGL参考页。

在现代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); 阅读更多关于几何着色器

从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);