PDA

View Full Version : Drawing multiple circles in a loop



Chidori
12-08-2009, 09:55 PM
I'd like to put my three rows of circles into three loops where 15 are drawn and translated over 28 units each time, but I've been running into some issues. Could I get ya'll to take a look at my code and explain what I'm doing wrong? Here's my code snippet I'm having issues with:



for
(Circle1=0; Circle1<15; Circle1++)
{

glPushMatrix();
glTranslatef(d+28,455,1);
random_generator(0,3);
if (randomValue == 0)
{
glColor3f(0,.5,0);

}
else if (randomValue == 1)
{
glColor3f(.5,0,0);

}
else
{
glColor3f(0,0,.5);

}
glLineWidth(1.0);
drawCircle(14,200);
glPopMatrix();
}



and here's my actual code:




int r;
int d = 1;
int aim = 0;
int randomValue;
int min;
int sec;
int Circle1 [20];
int Circle2 [20];
int Circle3 [20];
int d = 54;
void timer()
{
for (min = 5; min>0; min++)
{
for (sec=60; sec>0; sec++)
{

}
}
}

int random_generator(int min,int max)
{

randomValue = min + random()%(max - min);
return randomValue;

}

void drawCircle(float radius, int segments)
{

glBegin(GL_POLYGON);
for (r=0; r<segments; r++)
{
float x = radius * cos(r*2.0*M_PI/segments);
float y = radius * sin(r*2.0*M_PI/segments);
glVertex2f(x,y);
}
glEnd();
glFlush();
}



void drawFire(float radius, int segments)
{

glBegin(GL_POLYGON);
for (r=0; r<segments; r++)
{
float x = radius * cos(r*2.0*M_PI/segments);
float y = radius * sin(r*2.0*M_PI/segments);
glVertex2f(x,y);
}
glEnd();
glFlush();
}


void drawOutside()
{
glPushMatrix();
glColor4f(.3, .3, .3, 1);
glLineWidth(1.0);
glBegin(GL_POLYGON);
glVertex2i(0,500);
glVertex2i(0,0);
glVertex2i(500, 0);
glVertex2i(500, 500);

glEnd();
glPopMatrix();
glFlush();
}

void drawInside()
{
glPushMatrix();
glColor4f(.5, .5, .5, 1);
glLineWidth(1.0);
glBegin(GL_POLYGON);
glVertex2i(30,470);
glVertex2i(30,30);
glVertex2i(470, 30);
glVertex2i(470, 470);

glEnd();
glPopMatrix();
glFlush();
}

void drawCannon()
{
glPushMatrix();
glTranslatef(aim,0,1);
glColor3f(0, 0, 0);
glLineWidth(1.0);
glBegin(GL_POLYGON);
glVertex2i(230,20);
glVertex2i(230,100);
glVertex2i(270, 100);
glVertex2i(270, 20);

glEnd();
glPopMatrix();
glFlush();
}


void drawText(char * string)
{
int len = strlen(string);
int i;

for (i=0; i < len; i++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN,string[i]);
}
}

void ballPhysics()
{
}

void displayFunction(void)
{
switch (d)
{

case 1 :
glClear(GL_COLOR_BUFFER_BIT);

drawOutside();


glPushMatrix();
glTranslatef(180,455,1);
glColor3f(1,1,1);
glScalef(.2,.2,2);
glLineWidth(2.0);
drawText("Bubble Buster");
glPopMatrix();

glPushMatrix();
glTranslatef(160,425,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("Press s to start and r to restart");
glPopMatrix();

glPushMatrix();
glTranslatef(30,400,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText("Instructions:");
glPopMatrix();

glPushMatrix();
glTranslatef(30,375,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("Use the left and right arrow keys to aim the cannon.");
glPopMatrix();

glPushMatrix();
glTranslatef(30,350,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("Your goal is to hit balls of the same color. If you fail");
glPopMatrix();

glPushMatrix();
glTranslatef(30,325,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("to hit a ball of the same color, but another color, you ");
glPopMatrix();

glPushMatrix();
glTranslatef(30,300,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("will have an extra ball to get rid of, as it will stick. If");
glPopMatrix();

glPushMatrix();
glTranslatef(30,275,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("you do not destroy all the balls within the time limit, or");
glPopMatrix();

glPushMatrix();
glTranslatef(30,250,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("let your balls touch the bottom of the screen you will lose.");
glPopMatrix();


break;



case 2 :

glClear(GL_COLOR_BUFFER_BIT);



drawOutside();
drawInside();

glPushMatrix();
glTranslatef(250+aim,102,1);
random_generator(0,3);
if (randomValue == 0)
{
glColor3f(0,.5,0);


}
else if (randomValue == 1)
{
glColor3f(.5,0,0);


}
else
{
glColor3f(0,0,.5);

}

glLineWidth(1.0);
drawFire(14,200);
glPopMatrix();



for
(Circle1=0; Circle1<15; Circle1++)
{

glPushMatrix();
glTranslatef(d+28,455,1);
random_generator(0,3);
if (randomValue == 0)
{
glColor3f(0,.5,0);

}
else if (randomValue == 1)
{
glColor3f(.5,0,0);

}
else
{
glColor3f(0,0,.5);

}
glLineWidth(1.0);
drawCircle(14,200);
glPopMatrix();
}


drawCannon();


glPushMatrix();
glTranslatef(30,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText("Score:");
glPopMatrix();

glPushMatrix();
glTranslatef(90,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText("0");
glPopMatrix();

char Minutes [10];
sprintf(Minutes, "%d", min);
glPushMatrix();
glTranslatef(430,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText(Minutes);
glPopMatrix();

char Seconds [300];
sprintf(Seconds, "%d", sec);
glPushMatrix();
glTranslatef(450,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText(Seconds);
glPopMatrix();

break;

}

glFlush();
glutSwapBuffers();
}

void timerFunction(int value)
{

glutPostRedisplay();
glutTimerFunc(1,timerFunction,0);
}


void keyboardFunction(unsigned char key, int x, int y)
{
switch (key)
{
case 'r' : d=1; break;
case 's' : d=2; break;
case ' ' : break;

}
glutPostRedisplay();
}


void functionSpecial(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT :
if (aim >= -198)
{
aim -=5; //Defines the use of the right and left arrow keys
}
break;

case GLUT_KEY_RIGHT :
if (aim <= 198)

aim +=5; break;
}
glutPostRedisplay();

}

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


glutInit(&amp;argc, argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Bubble Buster");

glutDisplayFunc(displayFunction);
glutSpecialFunc(functionSpecial);
glutKeyboardFunc(keyboardFunction);
glutTimerFunc(1,timerFunction,0);


gluOrtho2D(0, WIDTH, 0, HEIGHT);

glClearColor(1, 1, 1, 0);

glutMainLoop();
return 0;


}

c0de
12-09-2009, 08:53 AM
I'd like to put my three rows of circles into three loops where 15 are drawn and translated over 28 units each time, but I've been running into some issues. Could I get ya'll to take a look at my code and explain what I'm doing wrong?

I dont know if I understand very clear what you want to do but if you want to draw 15 circles translated 28 units dont forget to increment the distance. your variable "d" is 54. For every step of the loop you are doing this:


glPushMatrix();
glTranslatef(d+28,455,1);
....
glPopMatrix();

Once you push the matrix your actual coordinates related to current position will be 0,0,0 and the current position will not be affected by any translation/rotation between PushMatrix and PopMatrix. In other words with PushMatrix you save the current coordinates and with PopMatrix you restore them. That means your circles are not drawn translated with 28 units but are drawn in the same place BECAUSE "d" has a fixed value. By doing this: glTranslatef(d+28,455,1); you always translate with 54+28 on X axis relative to current position (since the matrix is pushed).
In order to have your position incremented try to save the circles position. Declare a variable:
GLfloat circle_position;

and here:


circle_position=d+28;
for
(Circle1=0; Circle1<15; Circle1++)
{

glPushMatrix();
glTranslatef(circle_position,455,1);
circle_position=circle_position+28;
..............
}


I hope this is what you want.