Graphical Display of Wi-Fi Connectivity with OpenGL
Code to Graphically Display the Wi-Fi Connectivity (Code Runs in OpenGL)
This code provides a graphical representation of Wi-Fi connectivity using OpenGL. It simulates a scenario where multiple laptops are within the range of a Wi-Fi router and visualizes the connection process.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#define PI 3.1415
float r = 0;
int x, y, xx1 = 0, fl2 = 0, yy1 = 0, f13 = 0,
xx2 = 0, yy2 = 2, fl4 = 0, xx3 = 0, yy3 = 0, xx4 = 0, yy4 = 0, fl5 = 0, f14 = 0, xx5 = 0, yy5 = 0, fl6 = 0, xx6 = 0, yy6 = 0, fl7 = 0;
int fl = 0, f2 = 0, f3 = 0, lc = 0, rc = 0;
float spin = 0.0;
float col = 0.0;
void text(char *, int, int);
void Circle(float X, float Y, float Radius)
{
glColor3f(0, 0, 0);
float DEG2RAD = 2 * PI / 180;
glBegin(GL_LINE_LOOP);
for (float i = 0; i < 360; i++)
{
float DEGINRAD = i * DEG2RAD;
glVertex3f(X + cos(DEGINRAD) * Radius, Y + sin(DEGINRAD) * Radius, 0);
}
glEnd();
}
void comp()
{
glColor3f(col, col, col);
/*laptop screen*/
glBegin(GL_POLYGON);
glVertex2f(100.0, 700.0);
glVertex2f(150.0, 700.0);
glVertex2f(150.0, 750.0);
glVertex2f(100.0, 750.0);
glEnd();
/*slant screen*/
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex2f(75.0, 650.0);
glVertex2f(100.0, 697.0);
glVertex2f(150.0, 697.0);
glVertex2f(125.0, 650.0);
glEnd();
/*slant outer edge*/
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(75.0, 640.0);
glVertex2f(75.0, 650.0);
glVertex2f(125.0, 650.0);
glVertex2f(125.0, 640.0);
glEnd();
/* side edge of the slant screen*/
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(125.0, 640.0);
glVertex2f(150.0, 687.0);
glVertex2f(150.0, 697.0);
glVertex2f(125.0, 650.0);
glEnd();
/* keypad*/
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(82.0, 662.0);
glVertex2f(100.0, 690.0);
glVertex2f(143.0, 690.0);
glVertex2f(127.0, 662.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(91.0, 662.0);
glVertex2f(109.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(100.0, 662.0);
glVertex2f(118.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(109.0, 662.0);
glVertex2f(127.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(118.0, 662.0);
glVertex2f(136.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(89.0, 671.0);
glVertex2f(133.0, 671.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(94.0, 682.0);
glVertex2f(138.0, 682.0);
glEnd();
/* touch pad*/
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_POLYGON);
glVertex2f(98.0, 652.0);
glVertex2f(110.0, 652.0);
glVertex2f(112.0, 660.0);
glVertex2f(100.0, 660.0);
glEnd();
}
void router()
{
// base
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(350.0, 150.0);
glVertex2f(330.0, 180.0);
glVertex2f(330.0, 200.0);
glVertex2f(350.0, 170.0);
glVertex2f(350.0, 150.0);
glVertex2f(410.0, 155.0);
glVertex2f(410.0, 175.0);
glVertex2f(350.0, 170.0);
glEnd();
//upper part
glColor3f(0.0, 0.3, 0.6);
glBegin(GL_POLYGON);
glVertex2f(350.0, 170.0);
glVertex2f(330.0, 200.0);
glVertex2f(385.0, 207.0);
glVertex2f(410.0, 175.0);
glEnd();
//antenna
glColor3f(0.2, 0.4, 0.7);
glBegin(GL_POLYGON);
glVertex2f(350.0, 200.0);
glVertex2f(350.0, 280.0);
glVertex2f(355.0, 280.0);
glVertex2f(355.0, 200.0);
glEnd();
}
void comp2(int x)
{
glColor3f(x, x, x);
glBegin(GL_POLYGON);
glVertex2f(100.0, 700.0);
glVertex2f(150.0, 700.0);
glVertex2f(150.0, 750.0);
glVertex2f(100.0, 750.0);
glEnd();
/*slant screen*/
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex2f(75.0, 650.0);
glVertex2f(100.0, 697.0);
glVertex2f(150.0, 697.0);
glVertex2f(125.0, 650.0);
glEnd();
/*slant outer edge*/
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(75.0, 640.0);
glVertex2f(75.0, 650.0);
glVertex2f(125.0, 650.0);
glVertex2f(125.0, 640.0);
glEnd();
/* side edge of the slant screen*/
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(125.0, 640.0);
glVertex2f(150.0, 687.0);
glVertex2f(150.0, 697.0);
glVertex2f(125.0, 650.0);
glEnd();
/* keypad*/
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(82.0, 662.0);
glVertex2f(100.0, 690.0);
glVertex2f(143.0, 690.0);
glVertex2f(127.0, 662.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(91.0, 662.0);
glVertex2f(109.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(100.0, 662.0);
glVertex2f(118.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(109.0, 662.0);
glVertex2f(127.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(118.0, 662.0);
glVertex2f(136.0, 690.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(89.0, 671.0);
glVertex2f(133.0, 671.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(94.0, 682.0);
glVertex2f(138.0, 682.0);
glEnd();
/* touch pad*/
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_POLYGON);
glVertex2f(98.0, 652.0);
glVertex2f(110.0, 652.0);
glVertex2f(112.0, 660.0);
glVertex2f(100.0, 660.0);
glEnd();
glFlush();
}
void arrow()
{
//arrow
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
glVertex2f(450.0, 200.0);
glVertex2f(600.0, 200.0);
glVertex2f(600.0, 225.0);
glVertex2f(650.0, 170.0);
glVertex2f(600.0, 125.0);
glVertex2f(600.0, 150.0);
glVertex2f(450.0, 150.0);
glVertex2f(450.0, 125.0);
glVertex2f(400.0, 170.0);
glVertex2f(450.0, 225.0);
glVertex2f(450.0, 200.0);
glFlush();
glEnd();
}
void DisplayPoint(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(0.2, 0, 0.3);
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
glColor3f(0.7, 0.3, 0.6);
glVertex2f(0.0, 1000.0);
glColor3f(0.2, 0, 0.3);
glVertex2f(1000.0, 1000.0);
glColor3f(0.7, 0.3, 0.6);
glVertex2f(1000.0, 0.0);
glColor3f(0.3, 0.1, 0.2);
glEnd();
comp();
glPushMatrix();
glTranslatef(500, 0, 0);
comp();
glPopMatrix();
glPushMatrix();
glTranslatef(700, 0, 0);
comp();
glColor3f(0, 0, 0);
glBegin(GL_POLYGON);
glVertex2f(100.0, 700.0);
glVertex2f(150.0, 700.0);
glVertex2f(150.0, 750.0);
glVertex2f(100.0, 750.0);
glEnd();
glPopMatrix();
glColor3f(col, col, col);
comp();
glPushMatrix();
glTranslatef(350, 200, 0);
comp();
glPopMatrix();
comp();
glPushMatrix();
glTranslatef(175, 200, 0);
comp();
glPopMatrix();
comp();
glPushMatrix();
glColor3f(col, col, col);
glTranslatef(100, -250, 0);
comp();
glPopMatrix();
glColor3f(0, 0, 0);
glBegin(GL_POLYGON);
glVertex2f(100.0, 700.0);
glVertex2f(150.0, 700.0);
glVertex2f(150.0, 750.0);
glVertex2f(100.0, 750.0);
glEnd();
glPopMatrix();
comp();
glPushMatrix();
glTranslatef(400, -250, 0);
comp();
glPopMatrix();
comp();
if (f2 == 0)
{
glPushMatrix();
glTranslatef(800, -525, 0);
comp2(0);
glPopMatrix();
}
else
{
glPushMatrix();
glTranslatef(800, -525, 0);
comp2(1);
glPopMatrix();
}
glPushMatrix();
glTranslatef(150, -30, 0);
router();
glPopMatrix();
glPushMatrix();
glTranslatef(0, 350, 0);
router();
glPopMatrix();
glPushMatrix();
glTranslatef(-200, 0, 0);
arrow();
glPopMatrix();
if (fl2 == 1)
{
glPushMatrix();
glTranslatef(0 + xx1, 0 + yy1, 0);
text("Ask for connection?", 750, 235);
if (xx1 > -300)
xx1 -= 01;
glPopMatrix();
glFlush();
}
if (f13 == 1)
{
glPushMatrix();
glTranslatef(0 + xx2, 0 + yy2, 0);
text("Request for password!", 550, 250);
if (xx2 < 300)
xx2 += 01;
glPopMatrix();
glutPostRedisplay();
glFlush();
}
if (fl4 == 1)
{
glPushMatrix();
glTranslatef(0 - xx3, 0 + yy3, 0);
text("***password***", 800, 200);
if (xx3 < 300)
xx3 += 01;
glPopMatrix();
glutPostRedisplay();
glFlush();
}
if (fl5 == 1)
{
glPushMatrix();
glTranslatef(0 + xx4, 0 + yy4, 0);
text("CHECK FOR PASSWORD", 450, 235);
if (xx4 > -320)
xx4 -= 01;
glPopMatrix();
glutPostRedisplay();
glFlush();
text("Press left for correct", 200, 300);
text("Press middle for incorrect", 600, 300);
if (lc == 1)
{
glPushMatrix();
glTranslatef(0 + xx5, 0 + yy5, 0);
text(" password is correct", 350, 235);
if (xx5 < 300)
xx5 += 01;
f2 = 1;
glPopMatrix();
glutPostRedisplay();
glFlush();
glPushMatrix();
glTranslatef(200, 0, 0);
arrow();
glPopMatrix();
text(" CONNECTION ESTABLISHED!", 640, 160);
}
if (rc == 1)
{
glPushMatrix();
text("NO CONNECTION ESTABLISHED!", 625, 160);
lc = 0;
f2 = 0;
glPopMatrix();
glutPostRedisplay();
glFlush();
}
}
glutPostRedisplay();
glFlush();
text("Out of Range", 750, 605);
text("LAPTOPS WITHIN THE RANGE", 250, 655);
glPushMatrix();
glTranslatef(100, 150, 0);
glRotatef(90, 1, 0, 0);
glRotatef(spin, 0, 0, 1);
glutWireSphere(80, 15, 15);
glPopMatrix();
glPushMatrix();
glTranslatef(20, 0, 0);
glPopMatrix();
glFlush();
Circle(350, 630, r);
glutSwapBuffers();
}
void anim()
{
spin += 0.5;
if (r < 300)
{
col += .003;
r += 1;
}
if (r >= 300)
{
col = 0.0;
r -= 300;
}
glutPostRedisplay();
}
void text(char *a, int x, int y)
{
//text
if (fl < 300)
{
glColor3f(1.0, 0.7, 0.0);
int len = strlen(a);
if (len == 1)
glRasterPos2i(x + 8, y + 8);
else
glRasterPos2i(x + 6, y + 8);
for (int i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, a[i]);
}
}
glEnd();
}
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
lc = 1;
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
rc = 1;
}
void mypoint()
{
glLineWidth(3.0);
glPointSize(20.0);
glOrtho(0.0, 1000.0, 0.0, 1000.0, -100, 100);
}
void menu1(int d)
{
switch (d)
{
case 0:
fl2 = 1;
break;
case 1:
fl2 = 0;
f13 = 1;
break;
case 2:
fl2 = 0;
f13 = 0;
fl4 = 1;
break;
case 3:
fl2 = 0;
f13 = 0;
fl4 = 0;
fl5 = 1;
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(50, 50);
glutCreateWindow("wifi");
glutCreateMenu(menu1);
glutAddMenuEntry("ask for connection", 0);
glutAddMenuEntry("request for password", 1);
glutAddMenuEntry("password", 2);
glutAddMenuEntry("check for password", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(DisplayPoint);
mypoint();
glutIdleFunc(anim);
glutMouseFunc(mouse);
glutMainLoop();
}
Features:
- Laptop Representation: Multiple laptops are displayed on the screen.
- Wi-Fi Router: A Wi-Fi router is positioned to indicate the source of the Wi-Fi signal.
- Range Visualization: A growing circle represents the expanding range of the Wi-Fi signal.
- Connection Request: When a laptop is within range, it can request a connection.
- Password Prompt: Upon connection request, a password prompt appears.
- Password Check: The user can simulate entering the correct or incorrect password.
- Connection Status: The program displays whether the connection was established or not.
- Animation: The Wi-Fi range and connection process are animated for better visualization.
Instructions:
- Right-click to access the menu options.
- Select “ask for connection” to initiate a connection request.
- Select “request for password” to simulate a password prompt.
- Select “password” to simulate entering a password.
- Select “check for password” to validate the password.
- Press the left mouse button to simulate entering the correct password.
- Press the middle mouse button to simulate entering an incorrect password.