#### Archived

This topic is now archived and is closed to further replies.

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

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

## Recommended Posts

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.

##### Share on other sites
This is probably due to floating point inaccuracy. Best to use < or >, or just count with integer arithmetic.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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).

##### Share on other sites
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

##### Share on other sites
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 ! ! !

##### Share on other sites
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

##### Share on other sites
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......

##### Share on other sites
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!

##### Share on other sites
I''ve been doing OpenGL for about a week and C++ for about 2 weeks (talk about rushing it) but the prototypes thing is new to me.
In Nehe''s tutorials all his functions come before the main loop, (WinMain), but I presume that these functions could be put after it as long as I put a prototype for each above WinMain. So when you say put all the prototypes, do you mean just the prototypes of any functions in the second.cpp.

ie-will this work:
-Put my 3D engine in 1st.cpp
-Put my 3D fonts into my 2nd.cpp
-Make prototypes of all functions in 2nd.cpp and put them into a 3rd file with extension ''.h''
-Include this ''.h'' at top of 1st.cpp

Thanks for the help by the way.

##### Share on other sites
That is correct, mate!

##### Share on other sites
quote:
Original post by PCI
I ain''t using numbers that go forever, i was trying to get 0.025

That''s the problem, though, 0.025 does go on forever in binary representation (which is the only representation your computer understands internally). Instead of 0.025, it stores it as 0.0249996 (approximately)

##### Share on other sites
I''ve gotten into the practice of writing non-trivial answers as pages on my web site so here''s the link...

function prototypes and header files

Please notify me of any mistakes I may have made.
http://tannara.2y.net/

##### Share on other sites
What about variables ?
Using just the OpenGL window, I took DrawGLscene into a second file and doing all the header things it worked,.....But when I tried it with my 3D world, putting DrawGLscene into a second file, it told me that the variables in DrawGLscene were undefined, (they are in the main file), so I copied all the variables into the 2nd file as well, but not it tells me that they can''t be defined twice? How do I do this when the same variables are required in 2 different files

eg- In WinMain is were I ''watch'' for key presses, when a key is pressed it alters a variable and this variable will be used in DrawGLscene to move the polygons.

Thanks for the help.

##### Share on other sites
Dunno how that''s supposed to be done. You could do this, though:

class MyGlobals {  public:    static int MyVar;};int MyGlobals::MyVar = 0 // This MUST be declared also!!!

Now, elsewhere in the program, you can access the variable using:

MyGlobals::MyVar;

Note that you don''t need an instance of the class!

##### Share on other sites
You can only define global variables once. So, if you''re declaring all your variable in main.cpp: eg.

int gdone;
float happystuff;

Then you want to access those variables in your other files, such as second.cpp, you use the extern keyword:

extern int gdone;
extern float happystuff;

This will instead of creating a whole new variable, simply say that gdone is defined somewhere else, and all should be good.

##### Share on other sites
In my previous comment, also note this. If you choose to give a variable an initial value when declaring it:

int gdone = false;

Then you CANNOT assign values to these variables in your other files by saying:

extern int gdone = true;

Simply because, what should gdone equal? true or false?
Nor can you assign a value to an extern if it ISN''T assigned a value when it''s first declared:

int gdone;

I recommend before working with OpenGL, that you familiarize yourself more with the basics of C/C++. Such as making simple ANSI programs that span between more than one cpp file. You''ll see that your OpenGL code will greatly improve in it''s design (which may not mean much to you now... but trust me, it will)

##### Share on other sites
emmm 0.025 = double
0.025f = float

##### Share on other sites
Try this:

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 way it will work if the floating point numbers are a little off.

Zack

A moment enjoyed is not wasted.
-Gamers.com

##### Share on other sites
A couple of people need to start reading more of the post before replying! That problem was already solved.

I posted an update to the q&a page I wrote (link above) to include variables in multiple source files.

Anyone that's interested in writing any quick tutorials or a q&a page on c/c++, OpenGL or anything else please feel free to submit them to me and I'll add them to my site.

Edited by - avianRR on February 23, 2001 1:27:05 AM

##### Share on other sites
Why would anyone want to use more than one source file? Sorry if you already answered this in one of the other million posts in this thread In my game I have about 30,000 lines in my one main.cpp and it runs fine

##### Share on other sites
Why would anyone want to use more than one source file? Sorry if you already answered this in one of the other million posts in this thread In my game I have about 30,000 lines in my one main.cpp and it runs fine

##### Share on other sites
well, more than one source file makes the program easier to read and debug i suppose

##### Share on other sites
The project I''m currently working on consist''s of 27 *.cpp files and 22 *.h files. In total there are about 4500 lines of code in the cpp files alone and more in the headers. Now try to keep track of where in one file where a specific function that''s say 10 lines long is! It comes out to be about .2% of the file, the phrase "needle in a haystack" comes to mind when I think of doing that! Not only is it usefull to break the code into seprate files it also makes it nice for reusable code. like my vector, matrix, quaternion and ray classes. each has it''s own file and can be added to any project I want without haveing to rewrite or cut and paste the code.