Part of the Graphics Log series:
- Intro into Computer Graphics
- Animated Clock using 2D Graphics
- Creating an Animated Barber Pole with OpenGLThis post!
- Basic Texturing
- Creating a flag using Bezier Curves
- Understanding Subdivision Surfaces in Computer Graphics
- Lighting in Computer Graphics : A Practical Guide with OpenGL
- Techniques for Object Creation
- Practical tips for efficient development with OpenGL
- Computer Graphics: Top Learning Resources for beginners
Introduction
In this post, we’ll explore the creation of an animated barber pole with OpenGL. The project involves combining graphics programming concepts with animation techniques to bring a traditional barber pole to life.
Drawing a Cylinder
The drawCylinder
function is responsible for rendering a cylindrical shape using quad strips. This function is crucial for creating the main pole of the barber pole in the project.
1void drawCylinder(float radius, float height) {
2 // Initialize angle and the number of divisions (n) for creating quad strips
3 float angle = 0;
4 int n = 30;
5
6 // Begin drawing with quad strips
7 glBegin(GL_QUAD_STRIP);
8
9 // Iterate through the quad strips
10 for (int c = 0; c <= n; c++) {
11 // Calculate x and z coordinates based on the current angle
12 double x = radius * cos(angle);
13 double z = radius * sin(angle);
14
15 // Draw the vertices for the current quad strip
16 glVertex3f(x, 0, z);
17 glVertex3f(x, height, z);
18
19 // Increment the angle for the next iteration
20 angle = angle + ((2 * PI) / n);
21 }
22
23 // End the drawing of quad strips
24 glEnd();
25}
Drawing a Helix
The drawHelix
function is responsible for rendering a helical structure, following a similar approach as the cylinder but gradually incrementing the y
coordinate.
1void drawHelix(float radius, float height, float gap, float thickness, float startHeight) {
2 // Initialize angle, y-coordinate, and the number of divisions (n) for creating quad strips
3 float angle = 0;
4 float y = startHeight;
5 int n = 30;
6
7 // Begin drawing with quad strips
8 glBegin(GL_QUAD_STRIP);
9
10 // Draw the helix loops while incrementing the y-coordinate
11 while (y <= height) {
12 // Calculate x and z coordinates based on the current angle
13 double x = radius * cos(angle);
14 double z = radius * sin(angle);
15
16 // Draw the vertices for the current quad strip
17 glVertex3f(x, y, z);
18 glVertex3f(x, y + thickness, z);
19
20 // Increment the angle and y-coordinate for the next iteration
21 angle = angle + ((2 * PI) / n);
22 y = y + gap;
23 }
24
25 // End the drawing of quad strips
26 glEnd();
27}
Drawing the Barber Pole
The drawPole
function is responsible for rendering the complete structure of the barber pole. This function combines the main cylindrical pole with additional decorative elements.
1void drawPole(float radius, float height) {
2 // Save the current transformation state
3 glPushMatrix();
4
5 // Rotate the entire structure based on the scene rotation for animation
6 glRotatef(sceneRotation, 0.0, 1.0, 0.0);
7
8 // Save the current transformation state for the main pole
9 glPushMatrix();
10 glRotatef(0, 0, 1, 0);
11 glColor3f(1, 1, 1);
12
13 // Draw the main cylinder (Pole)
14 drawCylinder(radius, height);
15
16 glColor3f(0, 0, 0.61);
17
18 // Draw the first radius (blue helix)
19 drawHelix(radius + 0.05, height, 0.05, 0.2, 0);
20
21 glColor3f(1, 0, 0);
22
23 // Draw the second radius (red helix)
24 drawHelix(radius + 0.05, height, 0.05, 0.2, 0.3);
25
26 // Restore the transformation state for the main pole
27 glPopMatrix();
28
29 // Restore the original transformation state
30 glPopMatrix();
31
32 // Draw a sphere at the top
33 glPushMatrix();
34 glColor3f(0.67, 0.66, 0.66);
35 glTranslatef(0, height, 0);
36 glutSolidSphere(radius, 30, 30);
37 glPopMatrix();
38
39 // Draw a larger torus at the top
40 glPushMatrix();
41 glColor3f(0.77, 0.76, 0.76);
42 glTranslatef(0, height, 0);
43 glRotatef(-90, 1, 0, 0);
44 glutSolidTorus(radius / 4 * 0.9, radius, 25, 25);
45 glPopMatrix();
46
47 // Draw a smaller torus at the top
48 glPushMatrix();
49 glColor3f(.8314, .6863, .2157);
50 glTranslatef(0, height - radius / 4, 0);
51 glRotatef(-90, 1, 0, 0);
52 glutSolidTorus(radius / 5, radius, 25, 25);
53 glPopMatrix();
54
55 // Draw a sphere at the bottom
56 glPushMatrix();
57 glColor3f(0.67, 0.66, 0.66);
58 glutSolidSphere(radius, 30, 30);
59 glPopMatrix();
60
61 // Draw a larger torus at the bottom
62 glPushMatrix();
63 glColor3f(0.77, 0.76, 0.76);
64 glRotatef(-90, 1, 0, 0);
65 glutSolidTorus(radius / 4, radius, 25, 25);
66 glPopMatrix();
67
68 // Draw a smaller torus at the bottom
69 glPushMatrix();
70 glColor3f(.8314, .6863, .2157);
71 glTranslatef(0, radius / 4, 0);
72 glRotatef(-90, 1, 0, 0);
73 glutSolidTorus(radius / 5, radius, 25, 25);
74 glPopMatrix();
75}
Output
Conclusion
In this project, we delved into the world of graphics programming using GLUT and OpenGL to create a visually appealing animated barber pole.
Feel free to explore the complete source code here and experiment with different parameters to customize the barber pole further. I hope this project serves as an educational and inspiring resource for fellow graphics enthusiasts!