PDA

View Full Version : OpenGL Animation

mr07th
06-11-2014, 07:09 AM
Hi everyone! I wrote program that calculates angles of rotation and animates their but i have a 2 questions.
1) How to make limited in time animation and not infinity ?
2) Why with growing value of angle will grow speed of animation ?
if u need code

#include <GL/glut.h>
#include <math.h>
int a = 20, b = 30, c = 40;

void display()
{
double Gamma0 = 3.14 / 90000, Psi0 = 3.14 / 60000, Phi0 = 3.14 / 70000;
glClear(GL_COLOR_BUFFER_BIT);
glTranslated(0, 0, 0);

glPushMatrix();

glColor3f(1,0,0);
glRotated(Gamma0, 1, 0, 0);
glRotated(Psi0, 0, 1, 0);
glRotated(Phi0, 0, 0, 1);

gluCylinder (quadObj, 0.3, 0.3, 1, 24, 4);
glPolygonMode(GL_FRONT,GL_LINE);

glPopMatrix();
glutSwapBuffers();
}
void redisplay()
{
double x0 = 0.1, y0 = 0.2, z0 = 0.4, t0 = 0, T = 1, h = 0.001, Eps = 0.000001, Gamma0 = 3.14 / 90000, Psi0 = 3.14 / 60000, Phi0 = 3.14 / 70000, fx, fy, fz, fGamma, fPsi, fPhi, xx, yy, zz, GGamma, PPsi, PPhi, Ex, Ey, Ez, Eps1;
double x[4]; double y[4]; double z[4]; double Kx[4]; double Ky[4]; double Kz[4]; double Gamma[4]; double Psi[4]; double Phi[4]; double KGamma[4]; double KPsi[4]; double KPhi[4];
double divBy3 = 3.0;
double divBy8 = 8.0;
double divBy6 = 6.0;
x[0] = x0; y[0] = y0; z[0] = z0; Gamma[0] = Gamma0; Psi[0] = Psi0; Phi[0] = Phi0;
display();
while (t0 <= T)
{
Label:
for (int i = 0; i <= 3; i++)
{
fx = -y[i] - z[i];
fy = x[i] + 0.2 * y[i];
fz = 0.2 + (x[i] - 5.7) * z[i];
fGamma = x[i] * sin(Phi[i]) + y[i] * cos(Phi[i]);
fPsi = 1 / cos(Gamma[i]) * (x[i] * sin(Phi[i]) - y[i] * cos(Phi[i]));
fPhi = z[i] - (sin(Gamma[i]) / cos(Gamma[i])) * (x[i] * sin(Phi[i]) - y[i] * cos(Phi[i]));
Kx[i] = h * fx;
Ky[i] = h * fy;
Kz[i] = h * fz;
KGamma[i] = h * fGamma;
KPsi[i] = h * fPsi;
KPhi[i] = h * fPhi;
x[1] = x[0] + 0.5 * Kx[0];
x[2] = x[0] + 0.5 * Kx[1];
x[3] = x[0] + Kx[2];
y[1] = y[0] + 0.5 * Ky[0];
y[2] = y[0] + 0.5 * Ky[1];
y[3] = y[0] + Ky[2];
z[1] = z[0] + 0.5 * Kz[0];
z[2] = z[0] + 0.5 * Kz[1];
z[3] = z[0] + Kz[2];
Gamma[1] = Gamma[0] + 0.5 * KGamma[0];
Gamma[2] = Gamma[0] + 0.5 * KGamma[1];
Gamma[3] = Gamma[0] + KGamma[2];
Psi[1] = Psi[0] + 0.5 * KPsi[0];
Psi[2] = Psi[0] + 0.5 * KPsi[1];
Psi[3] = Psi[0] + KPsi[2];
Phi[1] = Phi[0] + 0.5 * KPhi[0];
Phi[2] = Phi[0] + 0.5 * KPhi[1];
Phi[3] = Phi[0] + KPhi[2];
}
Ex = fabs(2 * (Kx[0] - Kx[1] - Kx[2] + Kx[3]) * divBy3);
Ey = fabs(2 * (Kx[0] - Kx[1] - Kx[2] + Kx[3]) * divBy3);
Ez = fabs(2 * (Kx[0] - Kx[1] - Kx[2] + Kx[3]) * divBy3);
Eps1 = Eps * divBy8;
if (Ex > Eps | Ey > Eps | Ez > Eps)
{
h /= 2;
goto Label;
}
//расчет координат в следующей точке
xx = x[0] + (Kx[0] + 2 * Kx[1] + 2 * Kx[2] + Kx[3]) * divBy6;
yy = y[0] + (Ky[0] + 2 * Ky[1] + 2 * Ky[2] + Ky[3]) * divBy6;
zz = z[0] + (Kz[0] + 2 * Kz[1] + 2 * Kz[2] + Kz[3]) * divBy6;
//расчет углов в следующей точке
GGamma = Gamma[0] + (KGamma[0] + 2 * KGamma[1] + 2 * KGamma[2] + KGamma[3]) * divBy6;
PPsi = Psi[0] + (KPsi[0] + 2 * KPsi[1] + 2 * KPsi[2] + KPsi[3]) * divBy6;
PPhi = Phi[0] + (KPhi[0] + 2 * KPhi[1] + 2 * KPhi[2] + KPhi[3]) * divBy6;
//отображение углов на анимации
glRotated(Gamma[0] / 100000, 1, 0, 0);
glRotated(Psi[0] / 100000, 0, 1, 0);
glRotated(Phi[0] / 100000, 0, 0, 1);
//переприсваивание
x[0] = xx;
y[0] = yy;
z[0] = zz;
Gamma[0] = GGamma;
Psi[0] = PPsi;
Phi[0] = PPhi;
t0 += h;
}
glutPostRedisplay();

}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Test");
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);