Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried to continously decrease the sphere radius using a loop, but it does not seem to work.The animation for increasing sphere size is working fine.
Not able to understand why decreasing radius is not rendered.

I request anyone to share any sample code that can help to smoothly animate the same.

Thank You.

What I have tried:

C++
#include<windows.h>
 #include<GL/glut.h>

int r=1,flag=0;

void drawSphere(int r1)
{
    glutSolidSphere(r1,30,30);
    glFlush();
}

 void displaySolid()
 {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    drawSphere(100);

    glFlush();
 }

 void animate(int btn,int state,int x,int y)
 {
     if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
     {
                 for(r=200;r<400;r+=3)
                 {
                     drawSphere(r);
                     //glutPostRedisplay();
                     Sleep(100);
                     flag=1;
                 }
                 r=100;

         }




     if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
     {

         for(r=60;r>=30;r--)
         {
             
             drawSphere(r);
             Sleep(100);
         }
         
     }
 }


int main(int argc,char **argv)

{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(700,700);
    glutInitWindowPosition(0,0);
    glutCreateWindow("sphere test");
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-500,500,-500,500,-500,500);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glutDisplayFunc(displaySolid);
    glutMouseFunc(animate);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    return 0;
}
Posted
Updated 14-May-19 22:26pm
v3

1 solution

You are assigning double values to the int variable r (and apparently ignoring compiler warnings...).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900