Question

I have just figured out the rotation but cannot limit the rotation to a particular angle. I'm using the idlefunc to increment an angle theta in the background. Please suggest some alternative or a solution to limit my rotation angle to, say, +90 and -90 degrees. Here's my code:

#include<stdio.h>
#include<GL/freeglut.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,3.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);
}
GLfloat theta=0.0;
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0,1.0,1.0,1.0);
    glBegin(GL_LINES);
        glVertex3fv(a[0]);
        glVertex3fv(a[1]);
    glEnd();
    glColor3f(1.0,0.0,0.0);
    tetrahedron(n);
    glColor3f(0.0,0.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0,+3.0,-0.333333);
    glRotatef(theta,0.0,0.0,1.0);
    glTranslatef(0.0,-3.0,+0.333333);   
    glFlush();
}
void myReshape(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
            glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-20.0,20.0);
    else
            glOrtho(-4.0*(GLfloat)w/(GLfloat)h,4.0*(GLfloat)w/(GLfloat)h,-4.0,4.0,-20.0,20.0);
    glMatrixMode(GL_MODELVIEW);
}
void oscillate()
{
theta+=2.0;
glutPostRedisplay()
}
void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(1366,768);
    glutCreateWindow("Pendulum");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glutIdleFunc(oscillate);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
} 
Was it helpful?

Solution

Quick 'n dirty:

void oscillate()
{
    static float direction = 1.0f;
    if( theta > 90.0f )
        direction = -1.0f;
    if( theta < -90.0f )
        direction = 1.0f;

    theta += 2.0 * direction;
    glutPostRedisplay()
}

OTHER TIPS

You may create a circle and ocsilatte it like below -

 float drawCircle(float segments,float radius,float sx,float sy)
{
    glBegin(GL_LINE_LOOP);
    for(int i=0;i<segments;i++)
    {
        float theta=2.0*3.142*float(i)/float(segments); //get the current angle
        float x=radius*cos(theta);
        float y=radius*sin(theta);
        glVertex2f(x+sx,y+sy);
    }
    glEnd();
}

void draw(float x1,float y1,float angle)
{
    float segments=100;
    float radius=6.0;

    //Drawing Clock main Circle
    glLineWidth(4);
    glColor3f(1,0,0);
    drawCircle(segments,radius,x1,y1);

    //Drawing Minute Line
    glColor3f(1,1,0);
    glLineWidth(2);
    glBegin(GL_LINES);
    glVertex2f(x1,y1);
    glVertex2f(x1,(radius/3.0)*2.0);
    glEnd();

    //Drawing Hour Line
  glColor3f(1,0,0);
    glLineWidth(2);
    glBegin(GL_LINES);
    glVertex2f(x1,y1);
    glVertex2f(radius/3.0,radius/3.0);
    glEnd();
    //Drawing Pendulum Circle
    double radians=angle*3.142/180;
    float x2=(radius*3.4)*cos(radians);
    float y2=(radius*3.4)*sin(radians);
    float radius2=radius/3.0;

    glColor3f(0,0,1);
    //glLineWidth(2);
    drawCircle(segments,radius2,x2,y2);

    glBegin(GL_LINES);
    glVertex2f(x1,-1*(radius)+0.2);
    glVertex2f(x2,y2);
    glEnd();
}


void display()
{
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-10,10,-30);
    glColor3f(1,1,1);
if(angle>315)
{
angle=315;
inc=-inc;
}
if(angle<225)
{
angle=225;
inc=-inc;
}
    angle += inc;
    draw(0,0,angle);
    glutSwapBuffers();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top