Jump to content
  • Advertisement
Sign in to follow this  
hervebags

OpenGL How do i increase the radius once per second

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I started learning OpenGL not too long ago from this website:
http://www.swiftless...opengltuts.html


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::itsSlices,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

Share this post


Link to post
Share on other sites
Advertisement
My guess is that your radius increase is too slow to be noticed. I mean it is ok when you keep pressed a key and it works, as you add 0.002 to your radius like every 15ms, but now you are trying to do that once a second. That means that it takes 500 seconds (that is about 8 minutes) to get a radius of 1.0f added to your sphere. Try increasing the 0.002 value to something like 0.5 and see how it works.

Share this post


Link to post
Share on other sites

Hi,

I started learning OpenGL not too long ago from this website:
http://www.swiftless...opengltuts.html


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::itsSlices,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


two things
1: can you confirm if(timeDifference != lastDifference) is ever true?
2. are you certain time_now() is in milliseconds(or is 1000th a microsecond?)

edit:

My guess is that your radius increase is too slow to be noticed. I mean it is ok when you keep pressed a key and it works, as you add 0.002 to your radius like every 15ms, but now you are trying to do that once a second. That means that it takes 500 seconds (that is about 8 minutes) to get a radius of 1.0f added to your sphere. Try increasing the 0.002 value to something like 0.5 and see how it works.

^^That too^^

Share this post


Link to post
Share on other sites

^^That too^^

I was thinking of that because he specified the time tricked worked for him in the past. And 8 minutes per 1 unit is like watching plants grow :P

Share this post


Link to post
Share on other sites
Scratch that. First off, post the definition of time_now. Secondly, you're using integer mathematics on your time values, which truncate values. This means that for time01 and time02 (very terrible names btw) until they have a difference greater than a thousand you will never see any increase in the radius of anything. Assuming that's in milliseconds then, theoretically, you might see it once per second...

Frankly, without the definition of time_now, its hard to say what's going wrong here. Also 0.002 is a VERY small amount... You might be growing it once per second as you expect, but only by a very tiny amount. Further, since we don't really know the starting radius of your sphere its possible that you hit floating point precision limits and aren't actually increasing the size of it at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!