Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 11

Thread: c program using opengl for oscillating pendulum

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    6

    c program using opengl for oscillating pendulum

    need a program for an oscillating pendulum - a sphere suspended to the end of the thread that can oscillate about the fixed point at the other end of the thread. need the code. im creating the sphere by recursive subdivision of a tetrahedron. need help with attaching the string and making it oscillate. thank you.

  2. #2
    Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    259
    "need the code." Homework task? How exactely is it Windows(R) specific?

    How detailed does the simulation have to be? Is the thread supposed to be flexible or rigid?

    For the rigid case, I remember having solved the underlying differential equation in school some time. The result can be found here

    Regarding the code: If you understand the problem and your programming language (neither is hard), it is fairly easy to write the simulation code yourself (from the post I assume you already have the drawing code).

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    6
    Yes, its a homework task. I've just started working on the drawing code. I don't know much about OpenGL, just a beginner. If not the code for the entire program, I'd at least like an idea on how to go about with it. Like i said, i was able to create a sphere by recursive subdivision of a tetrahedron and the thread I created using GL_LINES from the centre of the sphere to a point outside. I need to get it to rotate about its axis now.

  4. #4
    Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    259
    You could aproach the rotation picewise: Write down a translation matrix that moves the tip of the line to the origin (and the rest of the pendulum accordingly), a rotation matrix that rotates the pendulum around one of the major axes, a translation matrix that moves it back to it is supposed to be at. Multiply those matrices and you end up with a single matrix that depends on the angle of rotation.

    Somewhere, you need a variable to store time passed since the beginning of the program. At the end of every frame, update that variable. At the beginning of the frame, this variable can be used to determine the angle from the formula on the Wikipedia page (or the solution of your differential equation). Once you have the angle, you can determine the value of the matrix and send it to the shader.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    6
    Well, I understand the multiplication of the 3 matrices to achieve the rotation but I dont understand how to construct a matrix for translation/rotation. Could you please help me with it? This is my code uptil now that just creates a stationary pendulum:
    #include<stdio.h>
    #include<msclr/glut.h>
    #include<math.h>
    int n=10;
    GLfloat v[4][3]={{0.0,0.0,1.0},{0.0,0.942809,-0.33333},{-0.816497,-0.471405,-0.33333},{0.816497,-0.471405,-0.333333}};
    GLfloat a[2][3]={{0.0,0.0,-0.333333},{0.0,10.0,-0.333333}};
    void normalize(GLfloat *p)
    {
    double d=0.0;
    int i;
    for(i=0;i<3;i++)
    d+=p[i]*p[i];
    d=sqrt(d);
    if(d>0.0)
    for(i=0;i<3;i++)
    p[i]/=d;
    }
    void triangle(GLfloat *a,GLfloat *b,GLfloat *c)
    {
    glBegin(GL_POLYGON);
    glNormal3fv(a);
    glVertex3fv(a);
    glVertex3fv(b);
    glVertex3fv(c);
    glEnd();
    }
    void divide_triangle(GLfloat *a,GLfloat *b,GLfloat *c,int n)
    {
    GLfloat v1[3],v2[3],v3[3];
    int j;
    if(n>0)
    {
    for(j=0;j<3;j++)
    v1[j]=a[j]+b[j];
    normalize(v1);
    for(j=0;j<3;j++)
    v2[j]=a[j]+c[j];
    normalize(v2);
    for(j=0;j<3;j++)
    v3[j]=c[j]+b[j];
    normalize(v3);
    divide_triangle(a,v2,v1,n-1);
    divide_triangle(c,v3,v2,n-1);
    divide_triangle(b,v1,v3,n-1);
    divide_triangle(v1,v2,v3,n-1);
    }
    else
    triangle(a,b,c);
    }
    void tetrahedron(int n)
    {
    divide_triangle(v[0],v[1],v[2],n);
    divide_triangle(v[3],v[2],v[1],n);
    divide_triangle(v[0],v[3],v[1],n);
    divide_triangle(v[0],v[2],v[3],n);
    }
    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glLoadIdentity();
    tetrahedron(n);
    glColor3f(0.0,0.0,1.0);
    glBegin(GL_LINES);
    glVertex3fv(a[0]);
    glVertex3fv(a[1]);
    glEnd();
    glFlush();
    }
    void myReshape(int w,int h)
    {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
    glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
    else
    glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    }
    void main(int argc,char** argv)
    {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutCreateWindow("Pendulum");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    }

  6. #6
    Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    259
    Quote Originally Posted by andre_arjun View Post
    Well, I understand the multiplication of the 3 matrices to achieve the rotation but I dont understand how to construct a matrix for translation/rotation. Could you please help me with it?
    Homogeneous coordinates
    You don't use 3x3 matrices and 3 component vectors, you use 4x4 matrices 4 component vectors. If the last component "happens to be" 1.0, it will result in the last row of the matrix being added to the vector.

    I suggest reading a tutorial related to the subject.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •