asdasc

ascdsac
mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.8 kB
2
Indexable
Never
#include <iostream>
#include <math.h>       /* sin */
#include <GL/glut.h>
#include <fstream>
const int g=30;
const double l = 1.5;
double u = 4;
double theta = 1;
const double h = 0.01;
double translatex=0;
double translatey=0;
GLuint texture_background;
using namespace std;

//simplified equations 1
double thetadot(double theta, double u)
{
    return u;
}

//simplified equations 2
double udot(double theta, double u)
{
    return (-g / l) * sin(theta);
}

/*---------------------------------------------*/
/* Runge kutta for the theta dot:              */
/* where thetadot = u                      */
/*                                             */
/*---------------------------------------------*/
double RKt (double u, double theta)
{
     double thetanext, k1, k2, k3, k4;   
     k1=thetadot(theta,u);
     k2=thetadot(theta+ 0.5 * h * k1, u + 0.5 * h * k1);
     k3=thetadot(theta+ 0.5 * h * k2, u + 0.5 * h * k2);
     k4=thetadot(theta+h * k3, u + h * k3);
     thetanext = theta + (h / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);    
     return thetanext;
}

/*---------------------------------------------*/
/* Runge kutta for the u     dot:              */
/* where udot = d theta/dt = -g/l sin(theta)   */
/*                                             */
/*---------------------------------------------*/
double RKu (double u, double theta)
{
     double unext, k1, k2, k3, k4;   
     k1=udot(theta,u);
     k2=udot(theta + 0.5 * h * k1, u + 0.5 * h * k1);
     k3=udot(theta + 0.5 * h * k2, u + 0.5 * h * k2);
     k4=udot(theta + h * k3, u + h * k3);
     unext = u + (h / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);    
     return unext;
}


static void display(void)
{

    const int width = glutGet(GLUT_WINDOW_WIDTH);
    const int height = glutGet(GLUT_WINDOW_HEIGHT);
    const float ar = ( float ) width / ( float ) height;

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


    glViewport( 0, 0, width, height );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho(-ar, ar, -1, 1, -1, 1);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
//Background
    if(texture_background) {
        glBindTexture( GL_TEXTURE_2D, texture_background );
        glEnable( GL_TEXTURE_2D );
        glBegin( GL_QUADS );
        glTexCoord2f( 1.0, 1.0 );
        glVertex2f( -1.0, 1.0 );
        glTexCoord2f( 0.0, 1.0 );
        glVertex2f( 1.0, 1.0 );
        glTexCoord2f( 0.0, 0.0 );
        glVertex2f( 1.0, -1.0 );
        glTexCoord2f( 1.0, 0.0 );
        glVertex2f( -1.0, -1.0 );
        glEnd();
        glDisable( GL_TEXTURE_2D );
    }

//TEXTURE 1


    glPushMatrix();


    glTranslatef(translatex, translatey, 0);
    glBegin( GL_TRIANGLE_FAN );

/*---------------------------------------------*/
/*                                             */
/* This is the only part i messed with for the */
/* size of the pendulum box.                   */
/*---------------------------------------------*/

    glVertex2f( 0.1f, 0.1f );  //center
    glVertex2f( -0.1f, 0.1f );  //center
    glVertex2f( -0.1f, -0.1f );  //center
    glVertex2f( 0.1f, -0.1f );  //center

    glEnd(  );
    glPopMatrix(  );
    glDisable( GL_TEXTURE_2D );

    glutSwapBuffers();

}

/*---------------------------------------------*/
/*                                             */
/* This is how opengl takes keyboard commands  */
/*      I don't need this, but i could \        */
/*      use this to change the speed           */
/*---------------------------------------------*/
static void key(
    unsigned char key,
    int a,
    int b )
{
    glutPostRedisplay();
}


/*---------------------------------------------*/
/*                                             */
/* This is the function that is run between  */
/*  in the loop. I put all the numerical        */
/*   estimatations here.                        */
/*---------------------------------------------*/
static void idle(
    void )
{
    double oldtheta = theta;
 u = RKu(u,theta);
 theta = RKt(u,theta);
 translatex=l*sin(theta)-sin(oldtheta);
 translatey=-l*cos(theta)+cos(oldtheta);
// cout<<translatex;
    glutPostRedisplay(  );
}


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


    glutInit( &argc, argv );

    glutInitWindowSize( 640, 640 );
    glutInitWindowPosition( 50, 50 );
    pre/* glutInitDisplayMode must be called before calling glutCreateWindow
     * GLUT, like OpenGL is stateful */
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
    glutCreateWindow( "Pendulum" );

    glutDisplayFunc( display );
    glutKeyboardFunc( key );
    glutIdleFunc( idle );


    glutMainLoop(  );
    return EXIT_SUCCESS;


}