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版本並編寫你自己的着色器。 –