绘制线条时关闭 openGL 中的颜色插值

Turn off color interpolation in openGL when drawing lines

如何简单地修改下面的代码,使绘制的线不在顶点之间插入颜色,我想要从顶点[i]到顶点[i+1]的每条线段的颜色只是 vertex[i].

的颜色
#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b;
};
std::vector< Point > points;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 3, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glLineWidth( 3.0 );
    glPointSize(3.0);
    glDrawArrays( GL_LINE_STRIP, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Random Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    for( size_t i = 0; i < 4; ++i )
    {
        Point pt;
        pt.x = 0;
        pt.y = 0;
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        //pt.a = 255;
        points.push_back(pt);
    }

    points[1].x=20;
    points[1].y=20;
    points[2].x=10;
    points[2].y=40;
    points[3].x=20;
    points[3].y=40;

    glutMainLoop();
    return 0;
}

当前输出:

这是因为 OpenGL 默认使用平滑着色。

glShadeModel(GL_SMOOTH)

简单地指定在绘制之前必须使用平面着色。

glShadeModel(GL_FLAT);

您的固定密码。

#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b;
};
std::vector< Point > points;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glShadeModel(GL_FLAT); // Here we specify to use flat shading.

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 3, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glLineWidth( 3.0 );
    glPointSize(3.0);
    glDrawArrays( GL_LINE_STRIP, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Random Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    for( size_t i = 0; i < 4; ++i )
    {
        Point pt;
        pt.x = 0;
        pt.y = 0;
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        //pt.a = 255;
        points.push_back(pt);
    }

    points[1].x=20;
    points[1].y=20;
    points[2].x=10;
    points[2].y=40;
    points[3].x=20;
    points[3].y=40;

    glutMainLoop();
    return 0;
}

结果就出来了。