PDA

View Full Version : How to Draw A filled Circles in the Field? plz help



Ryan4441
01-04-2016, 09:39 AM
Hey Sir/Maam,
I am a beginner at openGL and have facing a problem in my project.Project is about to make a Football Ground in OpenGL.after a very hardworking i just made 80% of it.
Center Circle in the field and center filled circles in front of each goals is missing.Kindly help me to draw these stuff.Bundles Of Thanks!
Here is the Code:

==> check this is the Project


#include <GL/glut.h>

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.33, 1.0, 0.50);
glBegin(GL_QUAD_STRIP);
glVertex3f(0.1, 0.1, 0.0);
glVertex3f(0.1, 0.9, 0.0);
glVertex3f(0.9, 0.1, 0.0); //GreenYard Vertex
glVertex3f(0.9, 0.9, 0.0);
glEnd();
glFlush();

glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.1, 0.0); //Mid Line ( Ground Divider )
glVertex3f(0.5, 0.9, 0.0);
glEnd();
glFlush();


// left side of the Ground

glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.25, 0.25, 0.0); // Goal keeper Front line
glVertex3f(0.25, 0.75, 0.0);

glEnd();
glFlush();

glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.1, 0.75, 0.0); //Goal Keeper left Line
glVertex3f(0.25, 0.75, 0.0);

glEnd();
glFlush();

glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.1, 0.25, 0.0); //Goal Keeper Right Line
glVertex3f(0.25, 0.25, 0.0);

glEnd();
glFlush();

glBegin(GL_QUAD_STRIP);
glColor3f(1, 1, 1);
glVertex3f(0.1, 0.35, 0.0);
glVertex3f(0.1, 0.65, 0.0); //Inner White Quad
glVertex3f(0.17, 0.35, 0.0);
glVertex3f(0.17, 0.65, 0.0);
glEnd();
glFlush();

// Right Side of the Ground
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.75, 0.25, 0.0); //Goal Keeper front line
glVertex3f(0.75, 0.75, 0.0);

glEnd();
glFlush();

glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.75, 0.25, 0.0); //Goal Keeper Left Line
glVertex3f(0.9, 0.25, 0.0);

glEnd();
glFlush();

glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.75, 0.75, 0.0); //Goal Keeper ki Right wali line
glVertex3f(0.9, 0.75, 0.0);

glEnd();
glFlush();

glBegin(GL_QUAD_STRIP);
glColor3f(1, 1, 1);
glVertex3f(0.9, 0.35, 0.0);
glVertex3f(0.9, 0.65, 0.0);
glVertex3f(0.83, 0.35, 0.0); //Inner White Quad
glVertex3f(0.83, 0.65, 0.0);
glEnd();
glFlush();

}

void Initialize()
{
glClearColor(0.60, 0.40, 0.12, 0.20);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int iArgc, char** cppArgv)
{
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(900, 450);
glutInitWindowPosition(200, 200);
glutCreateWindow("Football Ground OpenGL");
Initialize();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}

Carmine
01-05-2016, 02:49 AM
Hello Ryan -
I tried running your code and it came close to working. Had to change the 'GL_QUAD_STRIPS' to 'GL_QUADS' and reorder a few of the glVertex commands.

Some suggestions to make your code more concise:

--- You don't need the glFlush statements.
--- All vertices can be defined with glVertex2f commands. This way you don't have to bother setting z = 0.0.
--- All 3 quads can be put between one glBegin/glEnd pair.
--- All of the line segments can be put between between one glBegin/glEnd pair.

This is shown in my version of the code below. A picture of the output is attached.
Post code between [ code] and [ /code] tags (without the spaces), with indentation to show the logic more clearly.
You'll get more responses from people on the forum doing it that way.



void Soccer_Field (void)
{
float x, y, ang, radius = 0.05; // Not sure what the radius of the center circle should be?

static float RAD_DEG = 57.296;

glBegin (GL_QUADS);
glColor3f (0.20, 0.60, 0.20); // GreenYard
glVertex2f (0.10, 0.10); glVertex2f (0.90, 0.10);
glVertex2f (0.90, 0.90); glVertex2f (0.10, 0.90);
glColor3f (1.0, 1.0, 1.0);
glVertex2f (0.90, 0.35); glVertex2f (0.83, 0.35); // Inner White Quad - Right
glVertex2f (0.83, 0.65); glVertex2f (0.90, 0.65);
glVertex2f (0.10, 0.35); glVertex2f (0.17, 0.35); // Inner White Quad - Left
glVertex2f (0.17, 0.65); glVertex2f (0.10, 0.65);
glEnd ();

glColor3f (0.0, 0.0, 0.0); // Change color to black

glBegin (GL_LINES);
glVertex2f (0.50, 0.10); glVertex2f (0.50, 0.90); // Mid Line

// Left side of the Ground
glVertex2f (0.25, 0.25); glVertex2f (0.25, 0.75); // Goal keeper front line
glVertex2f (0.10, 0.75); glVertex2f (0.25, 0.75); // Goal keeper left line
glVertex2f (0.10, 0.25); glVertex2f (0.25, 0.25); // Goal keeper right line

// Right Side of the Ground
glVertex2f (0.75, 0.25); glVertex2f (0.75, 0.75); // Goal keeper front line
glVertex2f (0.75, 0.25); glVertex2f (0.90, 0.25); // Goal keeper left Line
glVertex2f (0.75, 0.75); glVertex2f (0.90, 0.75); // Goal keeper right line
glEnd ();

glBegin (GL_LINE_LOOP); // Circle at center of field
for (ang = 0.0; ang < 360.0; ang += 10.0) {
x = radius * cos(ang/RAD_DEG) + 1.0;
y = radius * sin(ang/RAD_DEG) + 0.5;
glVertex2f (x/2.0, y);
}
glEnd ();
}


2214

Ryan4441
01-05-2016, 09:24 AM
Thankeew So much <3 Carmine <3
you just made my Day <3 You are Simply Awsome <3 just Simply <3
further i will work on that project and i will draw further the D and Some Filled Dots :) and if i needed ur help again :) I will disturb you for that again <3 Bundle of Thanks <3 May God Help U Sir <3
MoreEver Can i contact u at ur email or Facebook If u want :)
In last thankyou so much again :)

Carmine
01-05-2016, 04:04 PM
You're welcome. A good way to contact me personally is to left click on my name and send me a private message.
The code I posted takes your code and modifies it. If I were starting from scratch I'd make a few basic changes.
For example you could have used actual soccer field dimensions (meters, feet, etc) in the glVertex statements.
You didn't have to use small fractional numbers like 0.35. To make it all fit into the graphics window you would
change the projection statement to something like glOrtho (-100, 100, -50, 50, -1, 1). This would put the center
of the field at the origin. You are currently using the glutInitWindowSize command to 'stretch' a square into a
rectangle. This is awkward because it forces you to figure out coordinates of the field markings as if it was shaped
like a square. Using glOrtho as above, with a field centered at the origin would make it MUCH easier to add more
markings to the field.

Read up on the options for glBegin. Seems like you are not clear on those. It will make things easier for you.
For example the goal keeper lines could be specified more cleanly using glBegin (GL_LINE_STRIP).

You realize that you are using 'Classic' or 'Fixed Function' GL. It is the old way of doing things. In 'Modern' GL
glBegin and glEnd are not used. I still use 'Classic' because it's simpler. It's a good way to learn about 3D,
interactive, computer graphics. It takes care of most of the underlying math of computer graphics for you.

Good luck.

Carmine
01-05-2016, 09:08 PM
BTW - if U want a filled circle instead of a line circle, use GL_POLYGON instead of GL_LINE_LOOP.

Ryan4441
01-05-2016, 10:43 PM
Sir there is an error in compiling the code provided by you sir.Will u please send me a message in private because i have searched for the message option on the forum but didnt find it.Kindly text me so i get notification about ur text and will start conversation.
Bundle of Thanks :)
#ryan