绘制线条时,请在openGL中关闭颜色插值

时间:2015-11-08 11:02:17

标签: c++ opengl

我怎样才能以简单的方式修改下面的代码,使得绘制的线不会在顶点之间插入颜色,我希望每个线段的颜色从顶点[i]到顶点[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;
}

当前输出:enter image description here

1 个答案:

答案 0 :(得分:4)

这是因为默认情况下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;
}

结果将是。 enter image description here