PLEASE IT IS IMPORTANT!
I have to create a revolution surface from a closed curve (rotating it around x-axis). I have chosen a circle. I have tried to apply the following code, but it works only for open curves, but not for closed ones; it’s the following:
float polyint ( float points[][3], float x, int N )
{
float y;
float num = 1.0, den = 1.0;
float sum = 0.0;
for ( int i = 0; i < N; ++i ) {
num = den = 1.0;
for ( int j = 0; j < N; ++j ) {
if ( j == i ) continue;
num = num * ( x - points[j][0] ); //x - xj
}
for ( int j = 0; j < N; ++j ) {
if ( j == i ) continue;
den = den * ( points[i][0] - points[j][0] ); //xi - xj
}
sum += num / den * points[i][1];
}
y = sum;
return y;
}
float aLine ( float x )
{
return x + 2.5;
}
void display(void)
{
int i, j;
float x, y, z, r; //current coordinates
float x1, y1, z1, r1; //next coordinates
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 1.0);
const float startx = cos(0), endx = cos(2*PI);
const int nx = 20; //number of slices along x-direction
const int ntheta = 20; //number of angular slices
const float dx = (endx - startx) / nx; //x step size
const float dtheta = 2*PI / ntheta; //angular step size
x = startx;
//r = aLine ( x );
r = polyint( ctrlpoints, x, 4);
glPushMatrix();
glRotatef( anglex, 1.0, 0.0, 0.0); //rotate the object about x-axis
glRotatef( angley, 0.0, 1.0, 0.0); //rotate about y-axis
glRotatef( anglez, 0.0, 0.0, 1.0); //rotate about z-axis
for ( i = 0; i < nx; ++i ) { //step through x
theta = 0;
x1 = x + dx; //next x
//r1 = aLine ( x1 ); //next f(x)
r1 = polyint( ctrlpoints, x1, 4); //next f(x)
//draw the surface composed of quadrilaterals by sweeping theta
glBegin( GL_QUAD_STRIP );
for ( j = 0; j <= ntheta; ++j ) {
theta += dtheta;
double cosa = cos( theta );
double sina = sin ( theta );
y = r * cosa; y1 = r1 * cosa; //current and next y
z = r * sina; z1 = r1 * sina; //current and next z
//edge from point at x to point at next x
glVertex3f (x, y, z);
glVertex3f (x1, y1, z1);
//forms quad with next pair of points with incremented theta value
}
glEnd();
x = x1;
r = r1;
} //for i
}
In case of closed curve I have used for circle:
crtlpoints[4][3]={cos(0.), sin(0.), 0.},{cos(2/3*PI), sin(2/3*PI), 0},{cos(5/3*PI), sin(5/3*PI), 0}, {cos(2*PI), sin(2*PI), 0}
.
polyint()
is instead a function for interpolation of N points. I have thought that the problem is on the fact that endx=startx for a closed curve…Please can you tell me how do I make it work also for closed curve?