PDA

View Full Version : Animation with glut



debonair
09-02-2013, 02:24 PM
I am trying to do animation using glut. I have 2 squares overlapping to each other and i want other square to rotate around this central object without rotating themselves. I want to rotate in orbit manner. I am calculating circular path using parametric equation and translating that one of the square to that distance. i am calling this function in a loop

Here is my code which is not working as per expected. Let me know where it is going wrong.
I have added expected image as well



void DrawShapes()
{
static float angle = 0;

glClear(GL_COLOR_BUFFER_BIT);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0f, 0.3f, 0.2f);


glBegin(GL_POLYGON);
glVertex2f(-0.2, -0.2);
glVertex2f(0.2, -0.2);
glVertex2f(0.2, 0.2);
glVertex2f(-0.2, 0.2);
glEnd();

static int i = 0;
distance_x = 0.3 * cos(i*3.14 / 180);
distance_y = 0.3* sin(i*3.14 / 180);
i++;
if (i > 360)
i = 0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(distance_x, distance_x, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.2, -0.2);
glVertex2f(0.2, -0.2);
glVertex2f(0.2, 0.2);
glVertex2f(-0.2, 0.2);
glEnd();

glPopMatrix();
glutSwapBuffers();

}


1143

GClements
09-02-2013, 05:36 PM
I am trying to do animation using glut. I have 2 squares overlapping to each other and i want other square to rotate around this central object without rotating themselves. I want to rotate in orbit manner. I am calculating circular path using parametric equation and translating that one of the square to that distance. i am calling this function in a loop
Your display function should render one frame then return. If you want animation, register a timer function which calls glutPostRedisplay().

debonair
09-02-2013, 06:05 PM
Your display function should render one frame then return. If you want animation, register a timer function which calls glutPostRedisplay().

problem i am facing is not about the frames, i am able to get the animation, only thing is that the output is not as per expected.

debonair
09-02-2013, 10:48 PM
Guys help me out please..

Alfonse Reinheart
09-02-2013, 11:44 PM
Here is my code which is not working as per expected.

In what way is it "not working as per expected"?

debonair
09-03-2013, 01:05 AM
In what way is it "not working as per expected"?

the motion of other square is not in a orbit instead it moves along a diagonal of middle one.

Alfonse Reinheart
09-03-2013, 01:10 AM
So it moves in a diagonal. Almost as if you passed the same value for the X and Y components of the position...

debonair
09-03-2013, 01:23 AM
So it moves in a diagonal. Almost as if you passed the same value for the X and Y components of the position...

:( I dont know how did i write same parameters for both axes .. Thanks a lot.

Carmine
09-03-2013, 09:48 AM
// glTranslatef (distance_x, distance_x, 0.0);
glTranslatef (distance_x, distance_y, 0.0);

debonair
09-04-2013, 02:06 PM
how can i achieve the same effect with glRotatef() without using above manipulation?

debonair
09-06-2013, 11:21 PM
Now i am able to rotate it with glrotatef. But if i have the wall of objects around the central object which i want to rotate around fixed axis but it seems that the axis is not constant. here is my code



static int i = 0;

static int angle = 0;
float distance_x, distance_y;
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 7 * width / 8, 7 * height / 8);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(-0.25f, 0.1f, 0.0f);
DrawHouse();
glPopMatrix();
distance_x = 0.2 * cos(i*3.14 / 180);
distance_y = 0.2 * sin(i*3.14 / 180);

glPushMatrix();
glRotatef(i, 0.0f, 0.0f, 1.0f);
glTranslatef(0.5f, 0.0f, 0.0f);
glRotatef(-i, 0.0f, 0.0f, 1.0f);
//DrawTriangle();
int angle1 =0 ;
for (int k = 0; k < 20; k++)
{
distance_x = 0.5 * cos(angle1*3.14 / 180);
distance_y = 0.5 * sin(angle1*3.14 / 180);
angle1 += 20;
glPushMatrix();
glTranslatef(distance_x, distance_y, 0.0f);
DrawTriangle();
glPopMatrix();
}

distance_x = 0.3 * cos(i*3.14 / 180);
distance_y = 0.3 * sin(i*3.14 / 180);

glRotatef(i, 0.0f, 0.0f, 1.0f);
glTranslatef(0.3f, 0.0f, 0.0f);
glRotatef(-i, 0.0f, 0.0f, 1.0f);

i++;

i %= 360;
angle++;

glPopMatrix();

glutSwapBuffers();
glutTimerFunc(10, animation, 0);


The video is available at http://www.youtube.com/watch?v=d3-sAnrG8pE&feature=em-upload_owner

please correct me where i am going wrong