如何在 OpenGL 中绘制锯齿状像素线

How to draw jagged pixellated line in OpenGL

我可以使用 OpenGL 画线,结果如下:

但是,与我游戏的像素化风格相比,这条线看起来很流畅而且不合时宜。我更喜欢这样的结果:

有人可以给我一些提示,告诉我如何让 OpenGL 绘制这样的像素化线条吗?

这是我的 renderLine() 方法(恐怕使用即时模式):

public static void renderLine(float x1, float y1, float x2, float y2,
        float thickness, float[] colour) {

    // Store model matrix to prevent contamination
    glPushMatrix();

    // Set colour and thickness
    glColor4f(colour[0], colour[1], colour[2], colour[3]);
    glLineWidth(thickness);

    // Draw line
    glBegin(GL_LINES);
    {
        glVertex2f(x1, y1);
        glVertex2f(x2, y2);
    }
    glEnd();

    // Restore previous state
    glColor4f(1, 1, 1, 1);
    glLineWidth(1.0f);
    glPopMatrix();
}

OpenGL 中确实没有内置任何东西来绘制这样的线。

您可以绘制一个包含直线中像素的紧凑四边形,并使用片段着色器评估哪些像素要留在其中,哪些像素要排除在外。

但是,考虑到您希望所有游戏的外观都像素化,最好的解决方案是渲染到较小的纹理,然后使用 x4(或任何因子)最近邻插值将其 blit 到屏幕。然后绘制这样的线减少到只是一个普通的 GL_LINE 没有 GL_LINE_SMOOTHing.