Toll Tax Collection Computer Graphics Project in OpenGL

Rajeev

The Toll Tax Collection Booth Simulation is a popular Computer Graphics mini-project developed using C/C++ with the GLUT/OpenGL library. The project creates a simple interactive highway environment where vehicles move through toll booths, stop at barriers, and continue once the gates are opened.


Toll Tax Collection Computer Graphics Project in OpenGL


Key Project Features

  • Welcome Screen: Displays the project title, student details, and basic instructions for operating the simulation.

  • Dual-Lane Highway: Represents a two-way highway with separate lanes and toll booths for vehicles traveling in opposite directions.

  • Interactive Barriers: Vehicles automatically stop when they reach a toll booth. The user can manually raise the barrier to allow the vehicle to pass.

  • Dynamic Vehicle Control: Users can adjust vehicle speed and manage traffic using predefined keyboard controls.

  • Interactive Environment: Keyboard inputs make the simulation more engaging by allowing the user to control vehicle movement and toll barriers in real time.

Overall, the project demonstrates fundamental computer graphics concepts such as 2D object drawing, animation, transformations, user interaction, and keyboard-based controls in an easy-to-understand highway toll booth simulation.

🚧 Toll Tax Collection Computer Graphics Project User Controls

Action Left / Top Lane Right / Bottom Lane
Open Toll Barrier L / l R / r
Increase Speed Z / z V / v


Complete Source Code Implementation
You can compile and run this full working program using CodeBlocks, Visual Studio, or a standard GCC/Clang compiler with GLUT linked.
Step-by-Step Execution Guide
To run this project seamlessly on your computer, follow these setup stages:
  1. Setup IDE: Open CodeBlocks or your environment of choice and start a new GLUT project.
  2. Library Linking: Ensure your compiler options are linking the required dependencies: -lglut -lGLU -lGL.
  3. Overwrite Main File: Replace all auto-generated code inside main.cpp with the script written above.
  4. Compile: Hit Build and Run. Tap ENTER on the welcome screen to launch into the toll layout!
 
Toll Tax Collection Computer Graphics Project in OpenGL

Full Source Code 

#include 
#include 
#include 

// Global State Variables
int screenMode = 0; // 0 = Welcome Screen, 1 = Toll Simulation
float car1X = -250.0f, car2X = 250.0f; 
float speed1 = 2.0f, speed2 = 2.0f;
bool gate1Open = false, gate2Open = false;
float gate1Angle = 0.0f, gate2Angle = 0.0f;

// Utility to render text strings
void renderText(float x, float y, void* font, const char* string) {
    glRasterPos2f(x, y);
    for (int i = 0; i < strlen(string); i++) {
        glutBitmapCharacter(font, string[i]);
    }
}

// Renders the initial introduction screen
void drawWelcomeScreen() {
    glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0f, 1.0f, 1.0f);
    renderText(-120, 150, GLUT_BITMAP_HELVETICA_18, "COMPUTER GRAPHICS MINI PROJECT");
    
    glColor3f(0.4f, 1.0f, 0.4f);
    renderText(-110, 100, GLUT_BITMAP_TIMES_ROMAN_24, "TOLL COLLECTING BOOTH SIMULATION");

    glColor3f(0.9f, 0.9f, 0.3f);
    renderText(-200, 20, GLUT_BITMAP_HELVETICA_12, "CONTROLS:");
    renderText(-200, 0, GLUT_BITMAP_HELVETICA_12, "  'L' / 'l' : Open Left Gate Barrier");
    renderText(-200, -20, GLUT_BITMAP_HELVETICA_12, "  'R' / 'r' : Open Right Gate Barrier");
    renderText(-200, -40, GLUT_BITMAP_HELVETICA_12, "  'Z' / 'z' : Speed up Left Car");
    renderText(-200, -60, GLUT_BITMAP_HELVETICA_12, "  'V' / 'v' : Speed up Right Car");

    glColor3f(0.3f, 0.8f, 1.0f);
    renderText(-90, -130, GLUT_BITMAP_HELVETICA_14, "Press [ENTER] to Launch Simulation");
    
    glutSwapBuffers();
}

// Renders the main simulation scene
void drawSimulationScene() {
    glClearColor(0.2f, 0.5f, 0.2f, 1.0f); // Green background for fields
    glClear(GL_COLOR_BUFFER_BIT);

    // 1. Draw Two-Way Road Asphalt
    glColor3f(0.2f, 0.2f, 0.2f);
    glBegin(GL_QUADS);
        glVertex2f(-300, 80);
        glVertex2f(300, 80);
        glVertex2f(300, -80);
        glVertex2f(-300, -80);
    glEnd();

    // 2. Draw Yellow Center Divider Line
    glColor3f(1.0f, 1.0f, 0.0f);
    glLineWidth(3.0);
    glBegin(GL_LINES);
        glVertex2f(-300, 0);
        glVertex2f(300, 0);
    glEnd();

    // 3. Draw Toll Cabins
    // Left Cabin
    glColor3f(0.7f, 0.1f, 0.1f);
    glBegin(GL_QUADS);
        glVertex2f(-60, 120); glVertex2f(-20, 120);
        glVertex2f(-20, 80);  glVertex2f(-60, 80);
    glEnd();
    // Right Cabin
    glBegin(GL_QUADS);
        glVertex2f(20, -80);  glVertex2f(60, -80);
        glVertex2f(60, -120); glVertex2f(20, -120);
    glEnd();

    // 4. Draw Left Lane Gate Barrier
    glPushMatrix();
    glTranslatef(-20, 40, 0);
    glRotatef(gate1Angle, 0, 0, 1);
    glColor3f(1.0f, 0.5f, 0.0f);
    glLineWidth(6.0);
    glBegin(GL_LINES);
        glVertex2f(0, 0); glVertex2f(0, 40);
    glEnd();
    glPopMatrix();

    // 5. Draw Right Lane Gate Barrier
    glPushMatrix();
    glTranslatef(20, -40, 0);
    glRotatef(-gate2Angle, 0, 0, 1);
    glColor3f(1.0f, 0.5f, 0.0f);
    glLineWidth(6.0);
    glBegin(GL_LINES);
        glVertex2f(0, 0); glVertex2f(0, 40);
    glEnd();
    glPopMatrix();

    // 6. Render Car 1 (Moving Left to Right)
    glColor3f(0.1f, 0.4f, 0.8f);
    glBegin(GL_QUADS);
        glVertex2f(car1X, 60);  glVertex2f(car1X + 40, 60);
        glVertex2f(car1X + 40, 20); glVertex2f(car1X, 20);
    glEnd();

    // 7. Render Car 2 (Moving Right to Left)
    glColor3f(0.8f, 0.1f, 0.2f);
    glBegin(GL_QUADS);
        glVertex2f(car2X, -20); glVertex2f(car2X - 40, -20);
        glVertex2f(car2X - 40, -60); glVertex2f(car2X, -60);
    glEnd();

    glutSwapBuffers();
}

// Master display routing
void display() {
    if (screenMode == 0) {
        drawWelcomeScreen();
    } else {
        drawSimulationScene();
    }
}

// Handles frame state updates and simulation arithmetic
void update(int value) {
    if (screenMode == 1) {
        // Handle Left Car Stopping / Moving Logic
        if (!gate1Open && (car1X + 40 >= -60) && (car1X < -60)) {
            car1X = -100; // Force full stop at booth entry boundary
        } else {
            car1X += speed1;
            if (car1X > 300) { // Loop back around
                car1X = -300;
                gate1Open = false;
            }
        }

        // Handle Right Car Stopping / Moving Logic
        if (!gate2Open && (car2X - 40 <= 60) && (car2X > 60)) {
            car2X = 100; // Force full stop at booth entry boundary
        } else {
            car2X -= speed2;
            if (car2X < -300) { // Loop back around
                car2X = 300;
                gate2Open = false;
            }
        }

        // Smoothly animate gate positions
        if (gate1Open && gate1Angle < 90.0f) gate1Angle += 5.0f;
        if (!gate1Open && gate1Angle > 0.0f) gate1Angle -= 5.0f;
        
        if (gate2Open && gate2Angle < 90.0f) gate2Angle += 5.0f;
        if (!gate2Open && gate2Angle > 0.0f) gate2Angle -= 5.0f;
    }

    glutPostRedisplay();
    glutTimerFunc(16, update, 0); // Roughly 60 Frames Per Second
}

// Configures coordinate projections
void reshape(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-300, 300, -200, 200);
    glMatrixMode(GL_MODELVIEW);
}

// User Input Evaluator
void keyboard(unsigned char key, int x, int y) {
    switch (key) {
        case 13: // ENTER key
            screenMode = 1;
            break;
        case 'L': case 'l':
            gate1Open = true;
            break;
        case 'R': case 'r':
            gate2Open = true;
            break;
        case 'Z': case 'z':
            speed1 += 0.5f;
            break;
        case 'V': case 'v':
            speed2 += 0.5f;
            break;
        case 27: // ESC key
            exit(0);
            break;
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("OpenGL Toll Booth Simulation");
    
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutTimerFunc(0, update, 0);
    
    glutMainLoop();
    return 0;
}


1 comment

  1. Anonymous
    post

Join the conversation