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

Hos*_*ag2 2 c++ opengl

我怎样才能以简单的方式修改下面的代码,以便绘制的线不会在顶点之间插入颜色,我希望从 vertex[i] 到 vertex[i+1] 的每个线段的颜色只是颜色的顶点[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;
}
Run Code Online (Sandbox Code Playgroud)

电流输出:在此输入图像描述

Myk*_*ola 5

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

glShadeModel(GL_SMOOTH)
Run Code Online (Sandbox Code Playgroud)

只需指定在绘图之前必须使用平面着色即可。

glShadeModel(GL_FLAT);
Run Code Online (Sandbox Code Playgroud)

你的固定代码。

#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;
}
Run Code Online (Sandbox Code Playgroud)

结果将会是。 在此输入图像描述