PDA

View Full Version : How to quick get a color polygons!



spring
02-10-2007, 06:02 PM
Hi:
I plan to draw a color cube with following code. And then I want to use a clipplane to clip this color cube, I will get a polygons! Now I hope this polygons is also color, I mean that the color fill polygons according its vertices position in color cube.
Now my idea is compute the point of intersection which clipplane and color cube, and indicate the color to point. Color is easy indicate because color is vertices'x,y,z from [-1,+1] --> [0,+1].

But I think it will have better idea than above to avoid computing the point of intersection! For I have to use this way a lot of times, I want to have a better efficiency.

thanks good guy advanced!


/*----------------------------------------------------------------------*/
/*
** Draw the cube.
*/
GLfloat vertices[][3] = {
{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0},
{-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}
};

GLfloat colors[][3] = {
{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0},
{0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}
};


void polygon(int a, int b, int c , int d, int face)
{

/* draw a polygon via list of vertices */

glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[B]);
glVertex3fv(vertices[B]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}

void colorcube(void)
{

/* map vertices to faces */

polygon(1,0,3,2,0);
polygon(3,7,6,2,1);
polygon(7,3,0,4,2);
polygon(2,6,5,1,3);
polygon(4,5,6,7,4);
polygon(5,4,0,1,5);
}