//function for the action of drawing cones void DrawConeAtWithColor( double x, double y, double r, double g, double b) const { glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_LINE_BIT); glDisable(GL_LIGHTING); //disable lighting, so that we can see the color of the object glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //确保坐标变换不起作用,比如不能平移,旋转等操作 glTranslated(x, y, 0);//locate the position of cones glColor3d(r, g, b); //designate the color of cones glutSolidCone(5, 5, 500, 500); //draw glPopMatrix(); glPopAttrib(); //draw the top point of the cone glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslated(x, y, 5); glutSolidSphere(0.007, 500, 500); glPopMatrix(); } 加权voronoi图,这里的加权只是将圆锥的底圆和高度加了一个随机的扰动;如图示 //for weighted/additive voronoi void DrawConeAtWithColor( double x, double y, double r, double g, double b) const { double epsilon = 10 * rand() / (double)RAND_MAX; glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_LINE_BIT); glDisable(GL_LIGHTING); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //确保坐标变换不起作用,比如不能平移,旋转等操作 glTranslated(x, y, 0); glColor3d(r, g, b); glutSolidCone(5+epsilon, 5+epsilon, 500, 500); glPopMatrix(); glPopAttrib(); ////////////////////////////////////////////////////////////////////////// glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslated(x, y, 5+epsilon); glutSolidSphere(0.007, 500, 500); glPopMatrix(); } //function for rendering void TheOnlyRender() { srand(0); for (int i = 0; i < 100; ++i) { double random_x = 2 * rand() / (double)RAND_MAX - 1;//确保值在【-1,1】间 double random_y = 2 * rand() / (double)RAND_MAX - 1; double random_r = rand() / (double)RAND_MAX; double random_g = rand() / (double)RAND_MAX; double random_b = rand() / (double)RAND_MAX; DrawConeAtWithColor(random_x, random_y, random_r, random_g, random_b); } }