Results 1 to 2 of 2

Thread: How do i increase the radius once per second

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2012
    Location
    South Africa
    Posts
    1

    How do i increase the radius once per second

    Hi,

    I started learning OpenGL not too long ago from this website:
    Swiftless
    I got only to point 5: OpenGL Color.

    What I want to do is:
    • Draw a sphere: done
    • Increase its radius once a second: NOT done
    I don’t want to use the keyboard to increase the radius. I already know how to do that.

    Here is what my code looks like:

    #include "TimeUtils.hpp"

    //More code

    int main(int argc, char *argv[])
    {
    //More code
    /*
    Increment Sphere radius once a second
    */
    long time01, time02;
    time01 = time_now();// Returns the time now

    long timeDifference = 0;
    long lastDifference = 0;
    long count = 0;

    while(1)
    {
    time02 = time_now(); // Returns the time now
    count = (time02-time01)/1000; // Number of seconds since “time01”
    timeDifference = count;

    if(timeDifference != lastDifference)
    {
    lastDifference = timeDifference;
    Sphere::itsRadius = Sphere::itsRadius + 0.002; //Does not work: Supposed to increase radius once per second
    }

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutIdleFunc(idle);

    //More code

    glutMainLoop();

    if( ((time02-time01)/1000) >= MAX) break;// Stop after MAX seconds
    }//End while loop

    return EXIT_SUCCESS;

    }//End main()

    //More code

    static void display(void)
    {
    const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double a = t*90.0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    glPushMatrix();
    glTranslated(0,0,-6);
    glutSolidSphere(Sphere::itsRadius,Sphere::itsSlice s,Sphere::itsStacks);
    glPopMatrix();

    glutSwapBuffers();
    }

    //More code


    The time trick I used above work on its own but, not when I draw my sphere.

    Thank you for your help.

    Regards,

    Herve

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: How do i increase the radius once per second

    Please use [ code]/[ /code] (no space after '[') around source snippets to make them easier to read.

    Code :
    while(1)
    {
        time02 = time_now(); // Returns the time now
        count = (time02-time01)/1000; // Number of seconds since “time01”
        timeDifference = count;
     
        if(timeDifference != lastDifference)
        {
            lastDifference = timeDifference;
            Sphere::itsRadius = Sphere::itsRadius + 0.002; //Does not work: Supposed to increase radius once per second
        }
     
        glutReshapeFunc(resize);
        glutDisplayFunc(display);
        glutIdleFunc(idle);
     
        //More code
     
        glutMainLoop();
     
        if( ((time02-time01)/1000) >= MAX) break;// Stop after MAX seconds
    }//End while loop

    Never call glutMainLoop() in a loop. This function implements internally an application event loop and only ever returns when your program exits (in some GLUT implementations glutMainLoop() actually never returns so any code after it up to the end of main() is never executed).

    To increase the radius once a second: in idle() examine the time elapsed since the last radius increase (or program start for the first one). If a second or more has elapsed, change the radius and call glutPostRedisplay().

Similar Threads

  1. N-dependent Radius?
    By atorres in forum OpenGL: General
    Replies: 2
    Last Post: 03-15-2004, 03:49 AM
  2. Light radius
    By Catman in forum OpenGL: Advanced Coding
    Replies: 8
    Last Post: 02-11-2003, 07:52 PM
  3. radius of gluSphere/gluDisk
    By shosking in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-05-2002, 05:32 PM
  4. radius of gluSphere/gluDisk
    By shosking in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 09-04-2002, 11:11 PM
  5. Light radius
    By blabla in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-07-2001, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean