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

Football Game OpenGL Visualization

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)
140 views7 pages

Football Game OpenGL Visualization

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

//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;
}

Common questions

Powered by AI

Colors in the graphical layout serve both functional and aesthetic purposes. Functionally, different colors differentiate between segments, like the playing field, goals, and borders, improving user orientation and interaction with the interface. Aesthetically, using a palette that includes white, blue, red, yellow, green, and brown creates a visually appealing and dynamic environment, enhancing the user experience by mimicking real-world football fields through realistic color choices . The color schemes also help in highlighting critical areas, such as goals marking in bright colors to draw user attention .

Specifying vertex positions in OpenGL accurately is critical for defining geometric shapes' size, position, and orientation on the screen. Each vertex coordinate uses a 2D or 3D space notation, impacting how shapes are rendered. Errors in these specifications can lead to distorted shapes, misalignment, or even nothing showing on screen if vertices fall outside the viewport. Precise vertex positioning ensures that shapes are rendered as intended, maintaining the integrity of the graphical structure .

The combination of rectangles and circles in the OpenGL code creates a comprehensive representation of a football game's layout. Rectangles form the field's boundaries and distinct sections, indicative of zones and goals. Circles mark important areas such as central points or targets, enhancing the spatial awareness necessary for gameplay elements. This geometric combination emulates a real football field layout, providing clear demarcation of space and functional areas necessary for immersion and gameplay realism .

Geometric shapes like quads and polygons are essential for designing the football game's graphical interface as they form the primary structure of the layout. Rectangles, used for the main container, boundaries, and various segments, organize the space and add visual interest through color differentiation. Polygons, used for circles, are added to enhance interactivity and focus points, such as goals or important field positions. These shapes contribute to the clarity and aesthetic appeal of the interface, playing a crucial role in both functional layout and visual design .

Circles and semicircles are drawn in OpenGL using polygons constructed from multiple small line segments arranged in a circular manner. This is done by iterating through angles from 0 to 360 degrees for full circles or 180 degrees for semicircles and calculating vertex positions using trigonometric functions like cosine and sine. The implications for rendering performance include potentially higher computational load due to numerous iterations for accurate shape resolution, impacting performance especially if the complexity or number of segments is high. Optimizing the number of segments can balance quality and performance .

Adjusting the window size might distort aspect ratios because the coordinate system used for rendering is fixed while the window's dimensions change, potentially stretching or squashing the image. Maintaining aspect ratio requires recalibrating the viewport and projection matrix to adapt to the new aspect ratio. If not handled, objects could appear distorted, leading to visual inconsistencies in the game's graphical elements, which is critical to avoid in applications like football game rendering where proportions are important for accuracy .

The glPushMatrix and glPopMatrix functions are used to save and restore the current matrix state in OpenGL, allowing localized transformations. In the context of rendering semicircles, they enable specific rotations and translations necessary to position the semicircles without affecting other objects. glPushMatrix saves the current state, and transformations apply only to subsequent operations until glPopMatrix restores the saved state, thus keeping the rest of the scene unaltered by these transformations .

The use of orthographic projection in this OpenGL application allows for a consistent size and alignment of graphical elements regardless of their depth, which is critical for 2D rendering as in a football game layout. It ensures elements like quads and circles render at the same scale without distortion due to perspective. This approach simplifies calculations, provides a clear and predictable workspace, and ensures that spatial relationships on the field remain realistic and proportional, fostering a straightforward 2D graphical interface suitable for these types of applications .

To change the size of the inner green rectangle, you would need to adjust the coordinates of the vertices defined by glVertex2f calls. For example, if you want to change the width, adjust the x-coordinates of the current vertices (e.g., increasing from -0.65 and 0.65 to -0.7 and 0.7 respectively) to make the rectangle wider. Similarly, change the y-coordinates for height adjustment. It's important to maintain the relative positions to ensure the rectangle remains centered correctly within the layout .

Text color in the OpenGL rendering code is specified using the glColor3f function, which sets the current color with RGB values ranging from 0.0 to 1.0. For instance, text can be drawn in white color by specifying RGB values as glColor3f(1.0, 1.0, 1.0). This color setting affects the appearance of text when it is displayed on the screen, ensuring that the text stands out against the background with the exact color specified .

You might also like