Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Learn how to create a colorful Symmetrical Phyllotaxis Flower Burst animation using OpenGL and GLUT in C++. Includes Ubuntu installation, compile command, source code, explanation, and output.
๐ธ Symmetrical Phyllotaxis Flower Burst using OpenGL in C++ (Ubuntu)
๐ Introduction:
This OpenGL project creates a beautiful Symmetrical Phyllotaxis Flower Burst animation using C++ and GLUT. The program arranges hundreds of colorful circles in a spiral pattern using the famous Golden Angle (137.5°). Each circle is assigned a different color using the HSV color model, producing a visually appealing flower-like animation similar to sunflower seed arrangements found in nature. This project is an excellent example for learning OpenGL graphics, animation, mathematical visualization, and procedural art.
๐ Ubuntu Installation:
Before compiling this program, install the required OpenGL and GLUT development libraries.
sudo apt update sudo apt install freeglut3-dev
The above command installs: • OpenGL Library • GLU Library • GLUT Library These libraries are required for compiling and running OpenGL applications.
⚙ Compile and Run in Ubuntu:
Step 1: Save the program as flower.cpp
Step 2: Compile the program
g++ flower.cpp -o flower -lGL -lGLU -lglut
Step 3: Run the executable
./flower
Compile Command Explanation
- g++ → GNU C++ Compiler
- flower.cpp → Source File
- -o flower → Creates executable file named flower
- -lGL → Links OpenGL Library
- -lGLU → Links OpenGL Utility Library
- -lglut → Links GLUT Library
๐ป Complete C++ Program:
#include <GL/glut.h>
#include <cmath>
#include <unistd.h>
#define PI 3.14159265358979323846
int point = 0;
// HSV to RGB conversion
void HSVtoRGB(float h, float s, float v, float &r, float &g, float &b)
{
int i = int(h * 6);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
case 5: r = v; g = p; b = q; break;
}
}
// Draw circle
void drawCircle(float cx, float cy, float radius)
{
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 100; i++)
{
float angle = 2 * PI * i / 100.0f;
glVertex2f(
cx + radius * cos(angle),
cy + radius * sin(angle));
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < point; i++)
{
float angle = i * 137.5f * PI / 180.0f;
float distance = i * 0.8f;
float x = cos(angle) * distance;
float y = sin(angle) * distance;
float r, g, b;
HSVtoRGB(fmod(i * 0.003f, 1.0f),
1.0f,
1.0f,
r,
g,
b);
glColor3f(r, g, b);
drawCircle(x, y, i * 0.1f);
}
glutSwapBuffers();
}
void timer(int)
{
if (point < 500)
{
point++;
}
glutPostRedisplay();
usleep(10000);
glutTimerFunc(1, timer, 0);
}
void init()
{
glClearColor(0,0,0,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-450,450,-450,450);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(900,900);
glutCreateWindow("Symmetrical Phyllotaxis Flower Burst");
init();
glutDisplayFunc(display);
glutTimerFunc(0,timer,0);
glutMainLoop();
return 0;
}
๐ Explanation:
This program creates a colorful Symmetrical Phyllotaxis Flower Burst animation using OpenGL and GLUT. It places circles in a spiral pattern using the Golden Angle (137.5°). Every circle is assigned a different color using the HSV color model, creating a beautiful flower-like design.
1. Header Files
- #include <GL/glut.h> → Provides OpenGL and GLUT functions for graphics programming.
- #include <cmath> → Used for mathematical functions like
sin(),cos(), andfmod(). - #include <unistd.h> → Used for the
usleep()function to slow down the animation.
2. Global Variable
int point = 0;
The variable point stores the number of circles to draw. Initially, its value is zero. The timer function increases this value continuously until it reaches 500.
3. HSVtoRGB()
This function converts colors from the HSV (Hue, Saturation, Value) model into the RGB (Red, Green, Blue) model because OpenGL uses RGB colors for drawing.
4. drawCircle()
The drawCircle() function draws one circle using 100 small line segments. The coordinates are calculated using the sine and cosine functions.
5. display()
The display() function is responsible for drawing the animation.
- Clears the screen.
- Calculates the spiral angle using the Golden Angle (137.5°).
- Calculates the position of every circle.
- Generates a different color for every circle.
- Draws circles with increasing radius.
- Displays the completed frame.
6. timer()
The timer function increases the value of point by one every few milliseconds. After increasing the value, it redraws the screen to create a smooth animation.
7. init()
This function initializes the OpenGL environment by setting the background color to black and defining a 2D coordinate system.
8. main()
The main() function initializes GLUT, creates the OpenGL window, registers the display and timer functions, and starts the GLUT event loop.
Working Principle
- Create the OpenGL window.
- Initialize the graphics environment.
- Start the timer.
- Increase the number of circles.
- Calculate spiral positions using the Golden Angle.
- Generate different colors using HSV.
- Draw colorful circles.
- Repeat until 500 circles are displayed.
๐ฅ Sample Output:
Symmetrical Phyllotaxis Flower Burst • Opens a 900 × 900 OpenGL window. • Displays a black background. • Hundreds of colorful circles appear gradually. • Circles form a beautiful spiral flower pattern. • Animation stops after drawing 500 circles.
๐ Keywords:
OpenGL C++ Program, GLUT Graphics, Computer Graphics using C++, OpenGL Ubuntu Tutorial, Phyllotaxis Flower Burst, Golden Angle Animation, OpenGL Animation, Graphics Programming, C++ OpenGL Example, Ubuntu OpenGL Project
๐ Search Description:
Learn how to create a Symmetrical Phyllotaxis Flower Burst animation using OpenGL and GLUT in C++. This tutorial includes Ubuntu installation, compile commands, complete source code, explanation, and sample output.
๐ Hashtags:
#OpenGL #GLUT #ComputerGraphics #CPlusPlus #Ubuntu #Animation #GraphicsProgramming #Phyllotaxis #FlowerBurst #1printf
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment