Either I can't count or OpenGL can't count ! ! ! ! !

Started by
30 comments, last by PCI 23 years, 2 months ago
In my prog I display a 3D font and want it to fade in and out, so I am using blending and altering the alpha value. I have this in my prog: //At top ------ float alp =1.0; int cha = 1; //In DrawGLScene ---------------- if (alp == 0.0) {cha = 0;} if (alp == 1.0) {cha = 1;} if (cha == 0) {alp+=0.025;} if (cha == 1) {alp-=0.025;} This means when alp (as it is at the start) equals 1.0 it takes away 0.025 until it reaches zero, then cha changes and it adds again.It counts down to zero BUT WON''T INCREMENT BACK UP.....WHY. If I change the 0.025 to 0.5 it work but happens so fast and abruptly its useless. I was under the impression that 0.025 went into 1.0. Please someone tell me I''m stupid and tell me whats wrong cause I gonna cry,....in fact to late.I am crying.
Advertisement
This is probably due to floating point inaccuracy. Best to use < or >, or just count with integer arithmetic.
Gee Brain, what we gonna do tonight?
By Gum your right. How the......DAM COMPUTER. So basically I was right and he can''t count

*PCI beats the computer with a lead pipe*

But isn''t the computer like a calculator, how can there be inaccuracy? Anyways, it works and I have a major smile on me face.
Cheers
Floating point numbers are represented using binary arithmetic, i.e. one bit would represent 0.5, the next bit 0.25 and so on..
There are infinitely many real numbers, and infinitely many numbers between 0 and 1. There aren''t enough bits in the universe to hold the complete set of real numbers, so consequently there are small inaccuracies. The same applies to calculators, you just don''t notice it.
Gee Brain, what we gonna do tonight?
This is a shining example of why a broad education in computers is a Good ThingTM. Floating point numbers are, by nature, susceptable to inaccuracies on computers because they often require precision beyond what the finite storage allocated for them on computers.

Consider this example (I''ll use decimal rather than binary since it''s easier to think about). How is the value 1/9 represented in floating point? It''s 0.11111... the 1 repeats forever. On a machine, you have to limit that to some finite number of decimal places. How many decimal places depends on the architecture, whether you are using doubles or not, etc., but the point is, it will be represented by a number that is almost, but not quite, it''s real value. The real problem begins to reveal itself when you begin to use floating point values together (especially if the magnitude difference in the numbers is great). One example, and probably what you are experiencing, is due to accumulation errors. Getting back to the 1/9 example, let''s say we represent this on the computer with 0.111111. If you multiply 1/9th by 9, you''d expect it to be 1, but because of the finite representation on the computer, you get 0.999999 instead.

Anyway, floating point inprecision is something that you need to be aware of anytime you work with floats. Historically, failure to pay attention to this has led to the loss of billions of dollars and many human lives (not in games, of course, but it''s important nonetheless).
I would also like to say that when you think something should work, and it is not, then it is a good idea to have a few printf/cprintf or whatever so you can see the actual error in your thought process.

Now you can take the lead pipe and smack yourself a few times.

Just remember, 1.0!=1.00000025



But I can''t use printf (or I prefer cout) in opengl and when I do it in C++ it works ( I ain''t using numbers that go forever, i was trying to get 0.025 to go into 1.000.

Anyways, I still feel no guilt about giving my computer a thrashing ! ! !
I''m waiting for a new operator: abo == about....

sooo if(1 abo 1.0000025) it would go into the body ;o)

hehehehe... hmmmm... sounds like a cool idea to me.. lol...
j/k
cya,
Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )
This question is VERY offtopic to what this post was for but because that point seems to bo addressed ,thanks for the replies by the way, I wanted to ask another question without cluttering up the board with my curiousity.

Is it possible to link cpp files in a project together, so that....er.....i mean.
I have a very basic 3D engine/level that I am working on and I am trying to incorperate alittle of every/most of Nehe''s tutorials (blending,lighting,textures,datafiles etc) and now I want to display some 3D font text at the start so when the user starts the prog'' it will display this text before starting the 3D engine bit.
I could do it within'' the current code but it will really clutter it up and make it messy. Is there no way that I can create a 2nd cpp in the project, that is just the OpenGL window template with the 3D font routine that will after running switch to the 1st cpp and continue with the engine (so the 2nd cpp ain''t just like a function) or could I do it using a function?

Sorry if that makes no sense......
Had that prob'' too. Here''s what you do:

1. Make a .cpp as your main file. Let''s call it main.cpp
2. Put other stuff in another .cpp file. Let''s call it second.cpp
3. Now, make a new file and put in it the prototypes of all the stuff in second.cpp. Save it as a .h, say second.h.
4. At the top of main.cpp, add this: "#include"

You may need to add the path in #include.

nB: If you change something in the .cpp that changes the prototype, also change the .h, otherwise it won''t compile!

This topic is closed to new replies.

Advertisement