• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

# andre_arjun

Member Since 19 Apr 2014
Offline Last Active Apr 19 2014 05:49 AM

### I need a C program using OpenGL to create a pendulum that rotates about its string on a...

19 April 2014 - 05:51 AM

I need it for my mini project. I havent understoods the basics for the rotation yet. I have the source code for creating the pendulum with the thread. I understand that I have to create a matrix for translating the tip of the string to the origin, then rotate the entire figure, and translate it back to the initial position; and multiply all these matrices. I dont understand how to create these matrices, please help me. To create the sphere in the pendulum, I've used recursive defintion of the sphere using tetrahedrons. Please help me, I need the source code. Thank you.
This is the source code so far:
#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);
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);
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);
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();
}

PARTNERS