
Back to Parallels
Back to original reference
C++ code for drawing a helix
The following code is taken from the NeHe OpenGl
tutorials, Radial Blur & Rendering To A Texture, written
by Dario Corno.
Note the complicated algorithm to draw something like this 3D image:
void ProcessHelix() // Draws A Helix
{
GLfloat x = 0; // Helix x Coordinate
GLfloat y = 0; // Helix y Coordinate
GLfloat z = 0; // Helix z Coordinate
GLfloat phi; // Angle
GLfloat theta; // Angle
GLfloat v, u; // Angles
int twists = 5; // 5 Twists
//GLfloat glfMaterialColor[] = {0.4f, 0.2f, 0.8f, 1.0f};// Set The Material Color
GLfloat glfMaterialColor[] = {0.1f, 0.6f, 0.6f, 1.0f};
//GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f}; // Sets Up Specular Lighting
glLoadIdentity(); // Reset The Modelview Matrix
gluLookAt(0, 5, 50, 0, 0, 0, 0, 1, 0); // Eye Position (0,5,50) Center Of Scene (0,0,0) Up On Y Axis.
glPushMatrix(); // Push The Modelview Matrix
glTranslatef(0, 0, -50); // Translate 50 Units Into The Screen
glRotatef(angle41 / 2.0f, 1, 0, 0); // Rotate By angle/2 On The X-Axis
glRotatef(angle41 / 3.0f, 0, 1, 0); // Rotate By angle/3 On The Y-Axis
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glfMaterialColor);
//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glBegin(GL_QUADS); // Begin Drawing Quads
for(phi = 0; phi <= 360; phi += helix_rotsteps) // 360 Degrees In Steps Of 20
{
for(theta = 0; theta <= 360 * twists; theta += helix_forwsteps) // 360 Degrees * Number Of Twists In Steps Of 20
{
if (default41_flag == 1)
{
if (p1_flag == TRUE)
{
v = (phi / 180.0f * PI); // Calculate Angle Of First Point ( 0 )
u = (theta / 180.0f * PI); // Calculate Angle Of First Point ( 0 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (1st Point)
y = float(sin(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate y Position (1st Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (1st Point)
vertexes41[0][0] = x; // Set x Value Of First Vertex
vertexes41[0][1] = y; // Set y Value Of First Vertex
vertexes41[0][2] = z; // Set z Value Of First Vertex
}
if (p2_flag == TRUE)
{
v = (phi / 180.0f * PI); // Calculate Angle Of Second Point ( 0 )
u = ((theta + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Second Point ( 20 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (2nd Point)
y = float(sin(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate y Position (2nd Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (2nd Point)
vertexes41[1][0] = x; // Set x Value Of Second Vertex
vertexes41[1][1] = y; // Set y Value Of Second Vertex
vertexes41[1][2] = z; // Set z Value Of Second Vertex
}
if (p3_flag == TRUE)
{
v = ((phi + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Third Point ( 20 )
u = ((theta + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Third Point ( 20 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (3rd Point)
y = float(sin(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate y Position (3rd Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (3rd Point)
vertexes41[2][0] = x; // Set x Value Of Third Vertex
vertexes41[2][1] = y; // Set y Value Of Third Vertex
vertexes41[2][2] = z; // Set z Value Of Third Vertex
}
if (p4_flag == TRUE)
{
v = ((phi + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Fourth Point ( 20 )
u = ((theta) / 180.0f * PI); // Calculate Angle Of Fourth Point ( 0 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (4th Point)
y = float(sin(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate y Position (4th Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (4th Point)
vertexes41[3][0] = x; // Set x Value Of Fourth Vertex
vertexes41[3][1] = y; // Set y Value Of Fourth Vertex
vertexes41[3][2] = z; // Set z Value Of Fourth Vertex
}
}
if (flip_sincos41_flag == 1)
{
if (p1_flag == TRUE)
{
v = (phi / 180.0f * PI); // Calculate Angle Of First Point ( 0 )
u = (theta / 180.0f * PI); // Calculate Angle Of First Point ( 0 )
x = float(sin(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate x Position (1st Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (1st Point)
z = float(((u - (helix_angle_fac * PI)) + cos(v)) * helix_radius); // Calculate z Position (1st Point)
vertexes41[0][0] = x; // Set x Value Of First Vertex
vertexes41[0][1] = y; // Set y Value Of First Vertex
vertexes41[0][2] = z; // Set z Value Of First Vertex
}
if (p2_flag == TRUE)
{
v = (phi / 180.0f * PI); // Calculate Angle Of Second Point ( 0 )
u = ((theta + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Second Point ( 20 )
x = float(sin(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate x Position (2nd Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (2nd Point)
z = float(((u - (helix_angle_fac * PI)) + cos(v)) * helix_radius); // Calculate z Position (2nd Point)
vertexes41[1][0] = x; // Set x Value Of Second Vertex
vertexes41[1][1] = y; // Set y Value Of Second Vertex
vertexes41[1][2] = z; // Set z Value Of Second Vertex
}
if (p3_flag == TRUE)
{
v = ((phi + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Third Point ( 20 )
u = ((theta + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Third Point ( 20 )
x = float(sin(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate x Position (3rd Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (3rd Point)
z = float(((u - (helix_angle_fac * PI)) + cos(v)) * helix_radius); // Calculate z Position (3rd Point)
vertexes41[2][0] = x; // Set x Value Of Third Vertex
vertexes41[2][1] = y; // Set y Value Of Third Vertex
vertexes41[2][2] = z; // Set z Value Of Third Vertex
}
if (p4_flag == TRUE)
{
v = ((phi + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Fourth Point ( 20 )
u = ((theta) / 180.0f * PI); // Calculate Angle Of Fourth Point ( 0 )
x = float(sin(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate x Position (4th Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (4th Point)
z = float(((u - (helix_angle_fac * PI)) + cos(v)) * helix_radius); // Calculate z Position (4th Point)
vertexes41[3][0] = x; // Set x Value Of Fourth Vertex
vertexes41[3][1] = y; // Set y Value Of Fourth Vertex
vertexes41[3][2] = z; // Set z Value Of Fourth Vertex
}
}
if (flip_leftright_sincos41_flag == 1)
{
if (p1_flag == TRUE)
{
v = (phi / 180.0f * PI); // Calculate Angle Of First Point ( 0 )
u = (theta / 180.0f * PI); // Calculate Angle Of First Point ( 0 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (1st Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (1st Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (1st Point)
vertexes41[0][0] = x; // Set x Value Of First Vertex
vertexes41[0][1] = y; // Set y Value Of First Vertex
vertexes41[0][2] = z; // Set z Value Of First Vertex
}
if (p2_flag == TRUE)
{
v = (phi / 180.0f * PI); // Calculate Angle Of Second Point ( 0 )
u = ((theta + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Second Point ( 20 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (2nd Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (2nd Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (2nd Point)
vertexes41[1][0] = x; // Set x Value Of Second Vertex
vertexes41[1][1] = y; // Set y Value Of Second Vertex
vertexes41[1][2] = z; // Set z Value Of Second Vertex
}
if (p3_flag == TRUE)
{
v = ((phi + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Third Point ( 20 )
u = ((theta + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Third Point ( 20 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (3rd Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (3rd Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (3rd Point)
vertexes41[2][0] = x; // Set x Value Of Third Vertex
vertexes41[2][1] = y; // Set y Value Of Third Vertex
vertexes41[2][2] = z; // Set z Value Of Third Vertex
}
if (p4_flag == TRUE)
{
v = ((phi + helix_angle_inc) / 180.0f * PI); // Calculate Angle Of Fourth Point ( 20 )
u = ((theta) / 180.0f * PI); // Calculate Angle Of Fourth Point ( 0 )
x = float(cos(u) * (helix_angle_fac + cos(v))) * helix_radius; // Calculate x Position (4th Point)
y = float(cos(u) * (helix_angle_fac + sin(v))) * helix_radius; // Calculate y Position (4th Point)
z = float(((u - (helix_angle_fac * PI)) + sin(v)) * helix_radius); // Calculate z Position (4th Point)
vertexes41[3][0] = x; // Set x Value Of Fourth Vertex
vertexes41[3][1] = y; // Set y Value Of Fourth Vertex
vertexes41[3][2] = z; // Set z Value Of Fourth Vertex
}
}
calcNormal(vertexes41, normal41); // Calculate The Quad Normal
glNormal3f(normal41[0], normal41[1], normal41[2]); // Set The Normal
// Render The Quad
glVertex3f(vertexes41[0][0], vertexes41[0][1], vertexes41[0][2]);
glVertex3f(vertexes41[1][0], vertexes41[1][1], vertexes41[1][2]);
glVertex3f(vertexes41[2][0], vertexes41[2][1], vertexes41[2][2]);
glVertex3f(vertexes41[3][0], vertexes41[3][1], vertexes41[3][2]);
}
}
glEnd(); // Done Rendering Quads
glPopMatrix(); // Pop The Matrix
}
© Martin Wurzinger - see Terms of Use