0% found this document useful (0 votes)
28 views7 pages

Complete Football Assignment

The document describes code for drawing a football game scene using OpenGL. It contains functions for drawing rectangles, circles, text and other shapes to display a football field, scoreboard and ball using OpenGL commands.

Uploaded by

Minahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Complete Football Assignment

The document describes code for drawing a football game scene using OpenGL. It contains functions for drawing rectangles, circles, text and other shapes to display a football field, scoreboard and ball using OpenGL commands.

Uploaded by

Minahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//Minahil Shah

#include <GL/glut.h>
#include <corecrt_math.h>

//For the Football Game text color


void drawText(const char* text, float x, float y, float r, float g, float b) {
glColor3f(r, g, b); // Set color to the specified RGB values
glRasterPos2f(x, y);
for (const char* c = text; *c != '\0'; ++c) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

// Main container
glColor3f(1.0, 1.0, 1.0); // Set color to white
glBegin(GL_QUADS);
glVertex2f(-0.9, -0.7); // Bottom-left vertex
glVertex2f(0.9, -0.7); // Bottom-right vertex
glVertex2f(0.9, 0.7); // Top-right vertex
glVertex2f(-0.9, 0.7); // Top-left vertex
glEnd();

// Rectangle on the top side (color: blue)


glColor3f(0.0, 0.0, 1.0); // blue
glBegin(GL_QUADS);
glVertex2f(-0.9, 0.5); // Bottom-left vertex
glVertex2f(0.9, 0.5); // Bottom-right vertex
glVertex2f(0.9, 0.7); // Top-right vertex
glVertex2f(-0.9, 0.7); // Top-left vertex
glEnd();

// Rectangle on the bottom side (color: light brown)


glColor3f(0.8, 0.5, 0.2); // light brown
glBegin(GL_QUADS);
glVertex2f(-0.9, -0.7); // Bottom-left vertex
glVertex2f(0.9, -0.7); // Bottom-right vertex
glVertex2f(0.9, -0.5); // Top-right vertex
glVertex2f(-0.9, -0.5); // Top-left vertex
glEnd();

// Rectangle on the left side (color: red)


glColor3f(1.0, 0.0, 0.0); // Red
glBegin(GL_QUADS);
glVertex2f(-0.9, -0.5); // Bottom-left vertex
glVertex2f(-0.7, -0.5); // Bottom-right vertex
glVertex2f(-0.7, 0.5); // Top-right vertex
glVertex2f(-0.9, 0.5); // Top-left vertex
glEnd();

// Rectangle on the right side (color: yellow)


glColor3f(1.0, 1.0, 0.0); // yellow
glBegin(GL_QUADS);
glVertex2f(0.7, -0.5); // Bottom-left vertex
glVertex2f(0.9, -0.5); // Bottom-right vertex
glVertex2f(0.9, 0.5); // Top-right vertex
glVertex2f(0.7, 0.5); // Top-left vertex
glEnd();

// Central rectangle (color: brown)


//Between the 4 rectangles
glColor3f(0.5, 0.2, 0.0); // Brown
glBegin(GL_QUADS);
glVertex2f(-0.7, -0.5); // Bottom-left vertex
glVertex2f(0.7, -0.5); // Bottom-right vertex
glVertex2f(0.7, 0.5); // Top-right vertex
glVertex2f(-0.7, 0.5); // Top-left vertex
glEnd();

// Inner rectangle (color: green)


glColor3f(0.0, 1.0, 0.0); // green
glBegin(GL_QUADS);
glVertex2f(-0.65, -0.35); // Bottom-left vertex
glVertex2f(0.65, -0.35); // Bottom-right vertex
glVertex2f(0.65, 0.35); // Top-right vertex
glVertex2f(-0.65, 0.35); // Top-left vertex
glEnd();

// White rectangles on the right and left sides of the green rectangle
glColor3f(1.0, 1.0, 1.0); // white

// Left rectangle
glBegin(GL_QUADS);
glVertex2f(-0.55, -0.1); // Bottom-left vertex
glVertex2f(-0.65, -0.1); // Bottom-right vertex
glVertex2f(-0.65, 0.1); // Top-right vertex
glVertex2f(-0.55, 0.1); // Top-left vertex
glEnd();

// Right rectangle
glBegin(GL_QUADS);
glVertex2f(0.65, -0.1); // Bottom-left vertex
glVertex2f(0.55, -0.1); // Bottom-right vertex
glVertex2f(0.55, 0.1); // Top-right vertex
glVertex2f(0.65, 0.1); // Top-left vertex
glEnd();
// Black borders for the white rectangles

// Left rectangle border


glColor3f(0.0, 0.0, 0.0); // black
glBegin(GL_LINE_LOOP);
glVertex2f(-0.55, -0.1); // Bottom-left vertex
glVertex2f(-0.65, -0.1); // Bottom-right vertex
glVertex2f(-0.65, 0.1); // Top-right vertex
glVertex2f(-0.55, 0.1); // Top-left vertex
glEnd();

// Right rectangle border


glBegin(GL_LINE_LOOP);
glVertex2f(0.65, -0.1); // Bottom-left vertex
glVertex2f(0.55, -0.1); // Bottom-right vertex
glVertex2f(0.55, 0.1); // Top-right vertex
glVertex2f(0.65, 0.1); // Top-left vertex
glEnd();

// Outer black rectangles surrounding the white ones

// Left rectangle border


glBegin(GL_LINE_LOOP);
glVertex2f(-0.65, -0.25); // Bottom-left vertex
glVertex2f(-0.4, -0.25); // Bottom-right vertex
glVertex2f(-0.4, 0.25); // Top-right vertex
glVertex2f(-0.65, 0.25); // Top-left vertex
glEnd();

// Right rectangle border


glBegin(GL_LINE_LOOP);
glVertex2f(0.65, -0.25); // Bottom-left vertex
glVertex2f(0.4, -0.25); // Bottom-right vertex
glVertex2f(0.4, 0.25); // Top-right vertex
glVertex2f(0.65, 0.25); // Top-left vertex
glEnd();

// y-coordinate of the circles


float circleY = 0.0; // Adjust as needed

// Circle radius
float circleRadius = 0.02; // Adjust as needed

// x-coordinate of the left circle


float leftCircleX = -0.5;
// Calculate the x-coordinate of the right circle
float rightCircleX = 0.5;

// Left circle
glBegin(GL_POLYGON);
for (int i = 0; i < 360; i++) {
float theta = i * 3.14159 / 180;
glVertex2f(leftCircleX - circleRadius * sin(theta), circleY + circleRadius
* cos(theta));
}
glEnd();

// Right circle
glBegin(GL_POLYGON);
for (int i = 0; i < 360; i++) {
float theta = i * 3.14159 / 180;
glVertex2f(rightCircleX + circleRadius * sin(theta), circleY + circleRadius
* cos(theta));
}
glEnd();

glLineWidth(3.0);
glColor3f(0.0, 0.0, 0.0); // green
glBegin(GL_LINE_LOOP);
glVertex2f(-0.65, -0.35); // Bottom-left vertex
glVertex2f(0.65, -0.35); // Bottom-right vertex
glVertex2f(0.65, 0.35); // Top-right vertex
glVertex2f(-0.65, 0.35); // Top-left vertex
glEnd();

glLineWidth(3.0);

// Draw a black line in the center of the green rectangle


glColor3f(0.0, 0.0, 0.0); // black
glBegin(GL_LINES);
glVertex2f(0.0, 0.35); // Bottom-left vertex
glVertex2f(0.0, 0.1); // Bottom-right vertex
glVertex2f(0.0, -0.35);
glVertex2f(0.0, -0.1);
glEnd();
// Calculating the midpoint of the vertical line

float midY = (0.45 + (-0.45)) / 2.0;

// Circle at the midpoint of the vertical line

int numSegments = 100;


float radius = 0.1; // radius
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < numSegments; i++) {
float theta = 2.0f * 3.1415926f * float(i) / float(numSegments); // Angle
between segments
float x = 0.0 + radius * cosf(theta); // Center of circle is at x = 0
float y = midY + radius * sinf(theta); // y-coordinate with midpoint
glVertex2f(x, y);
}
glEnd();

// Inner circle

float innerRadius = 0.07; // inner circle radius


glColor3f(0.7, 1.0, 0.7);
glBegin(GL_POLYGON);
for (int i = 0; i < numSegments; i++) {
float theta = 2.0f * 3.1415926f * float(i) / float(numSegments); // Angle
between segments
float x = 0.0 + innerRadius * cosf(theta); // Center of circle is at x = 0
float y = midY + innerRadius * sinf(theta); // y-coordinate with midpoint
glVertex2f(x, y);
}
glEnd();

// center points and radii for the semicircles


float leftSemiCircleCenterX = -0.4;
float leftSemiCircleCenterY = 0.01;
float leftSemiCircleRadius = 0.08;

float rightSemiCircleCenterX = 0.4;


float rightSemiCircleCenterY = 0.01;
float rightSemiCircleRadius = 0.08;

// Rotate angles for the semicircles


float leftSemiCircleAngle = -90.0; // Degrees to rotate
float rightSemiCircleAngle = 90.0; // Degrees to rotate

// left semicircle
glPushMatrix();
glTranslatef(leftSemiCircleCenterX, leftSemiCircleCenterY, 0.0);
glRotatef(leftSemiCircleAngle, 0.0, 0.0, 1.0); // Rotate around the z-axis
glTranslatef(-leftSemiCircleCenterX, -leftSemiCircleCenterY, 0.0);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 180; ++i) {
float angle = i * 3.14159 / 180;
float x = leftSemiCircleCenterX + leftSemiCircleRadius * cos(angle);
float y = leftSemiCircleCenterY + leftSemiCircleRadius * sin(angle);
glVertex2f(x, y);
}
glEnd();
glPopMatrix(); // Restore the previous transformation matrix

// Draw right semicircle


glPushMatrix(); // Save the current transformation matrix
glTranslatef(rightSemiCircleCenterX, rightSemiCircleCenterY, 0.0); // Translate
to the center of the semicircle
glRotatef(rightSemiCircleAngle, 0.0, 0.0, 1.0); // Rotate around the z-axis
glTranslatef(-rightSemiCircleCenterX, -rightSemiCircleCenterY, 0.0); //
Translate back to the original position
glColor3f(0.0, 0.0, 0.0); // Set color to black
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 180; ++i) {
float angle = i * 3.14159 / 180;
float x = rightSemiCircleCenterX + rightSemiCircleRadius * cos(angle);
float y = rightSemiCircleCenterY + rightSemiCircleRadius * sin(angle);
glVertex2f(x, y);
}
glEnd();
glPopMatrix();

// Small rectangle of off-white color


glColor3f(0.9, 0.9, 0.9); // off-white
glBegin(GL_QUADS);
glVertex2f(-0.31, -0.69); // Bottom-left vertex
glVertex2f(0.31, -0.69); // Bottom-right vertex
glVertex2f(0.31, -0.55); // Top-right vertex
glVertex2f(-0.31, -0.55); // Top-left vertex
glEnd();

drawText("Football Game", -0.2, 0.55, 1.0, 1.0, 1.0); // white color values
drawText("Football Match", -0.31, -0.65, 0.0, 0.0, 0.0); // White color,
slightly offset

glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); //rthographic projection
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Minahil Shah (21-ARID-442)");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

You might also like