Documentos de Académico
Documentos de Profesional
Documentos de Cultura
h>
#include <stdlib.h>
#ifdef _WIN32
#include "glut.h"
#elif defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
void
void
void
void
void
void
InitGL();
SetCameraGL();
OnRenderGL();
OnTimerGL(int id);
OnKey(unsigned char key,int x, int y);
OnReshapeWindow(int width, int height);
void safeExit();
void Init(float m0,float* X0,float* V0);
void DerivativeCalculation();
void SystemEvolution();
// Data
float f[6];
float df[6];
float V[3];
float X[3];
float t;
float it;
float
float
float
float
m;
g[3];
h;
Ks;
int WindowH;
int WindowW;
int main(int argc, char **argv)
{
float X0[] = {10.0f,10.0f,100.0f};
float V0[] = {10.0f,50.0f,30.0f};
Init(50.0f,X0,V0);
WindowH = 300;
WindowW = 400;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Ejemplo caida libre");
glutReshapeWindow(WindowW,WindowH);
glutDisplayFunc(OnRenderGL);
glutKeyboardFunc(OnKey);
glutReshapeFunc(OnReshapeWindow);
glutTimerFunc(1,OnTimerGL,1);
InitGL();
glutMainLoop();
}
return 0;
void safeExit()
{
exit(0);
}
void SetCameraGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(float)WindowW/(float)WindowH,0.01f,1000.0f);
glTranslatef(0.0f,0.0f,-500.0f);
glViewport(0,0,WindowW,WindowH);
}
void InitGL()
{
SetCameraGL();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OnRenderGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//El estado de fase tiene la posicin en las tres primeras componentes
glTranslatef(X[0],X[1],X[2]);
glutSolidSphere(5,50,50);
glutSwapBuffers();
}
void OnKey(unsigned char key,int x, int y)
{
if(key==27)
safeExit();
}
void OnTimerGL(int id)
{
if(id==1)
{
//Ciclo de simulacin
DerivativeCalculation();
SystemEvolution();
glutPostRedisplay();
glutTimerFunc(10,OnTimerGL,1);
}
}
void OnReshapeWindow(int width, int height)
{
WindowH = height;
WindowW = width;
}
SetCameraGL();
X[i] =
V[i] =
f[i]
f[i+3]
X0[i];
V0[i];
= X0[i]; // Las tres primeras componentes del estado de fase son la posicin
= V0[i]; // Las tres ltimas componentes del estado de fase son la velocidad
}
m = m0;
g[0] = 0.0f;
g[1] = -10.0f;
g[2] = 0.0f;
Ks = 5;
t = 0.0f;
h = 0.01f;
it= 1;
void DerivativeCalculation()
{
float F[3];
int i;
for(i=0;i<3;i++)
{
F[i] = 0;
}
// F = mg
for(i=0;i<3;i++)
{
F[i] += m*g[i];
}
// F = mg
for(i=0;i<3;i++)
{
F[i] += -Ks*V[i];
}
for(i=0;i<3;i++)
{
df[i]= V[i];//Las tres primeras componentes de la derivada del estado de fase son la velocidad
df[i+3]=F[i]/m;//Las tres ltimas componentes del estado de fase son fuerza sobre la masa
}
void SystemEvolution()
{
int i;
for(i=0;i<6;i++)
{
//Metodo de Euler
f[i] += (df[i]*h);
}
for(i=0;i<3;i++)
{
X[i]= f[i];//Las tres primeras componentes de la derivada del estado de fase son la
velocidad
V[i]= f[i+3];//Las tres ltimas componentes del estado de fase son fuerza sobre la masa
}
t += h;
it++;