1
如何以简单的方式修改下面的代码,以便绘制的线条不会在顶点之间插入颜色,我希望每个线段的顶点颜色[i]到顶点[i + 1]仅仅是顶点[i]的颜色。在绘制线条时关闭openGL中的颜色插值
#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版本并编写你自己的着色器。 –