How to show float numbers?

Hi, my teacher,

I can show texts of char type using
glListBase(1000);
glCallLists(Twidth,GL_UNSIGNED_BYTE,Text[i]);
But how to show float number in OpenGL?

thanks!

You use the same technic, but you convert the float number to a string. Use your favorite library or your own code to do that.

NAOQ - not a advanced opengl question

V-man

[QUOTE]Originally posted by V-man:
[b]You use the same technic, but you convert the float number to a string. Use your favorite library or your own code to do that.

I have convert it to a string. But how to show decimal’s position. I want to know whether to show float number directly,which needn’t convert or use own code.

sprintf

NEVER use sprintf with floating point numbers. Always a risk of a crash. Use snprintf instead to guard your buffer sizes.

!!! (speaking from experience of course).

As you can use %5.5 you can avoid the crash.

Originally posted by Michael Steinberg:
As you can use %5.5 you can avoid the crash.

I agree with mike, you simply have to make sure your buffer can accomidate the number of digits you are going to generate. In reality this isn’t a big deal. Just make your buffer a resonable size and its an issue that should never come up. I really don’t think that this borders on the line of a buffer overrun type of hack.

(:

Devulon

5.5 won’t limit you to 5 output characters.
It simply means miniumum of 5 characters, with a precision of 5 decimal places.

#include
using namespace std;

{
ostringstream s;
s << myFloat;
// the number as a string is now available in s.str()
}

… to avoid buffer overruns.

Paranoia! Will destroy ya!

(By the way, printf and sprintf are so incredible to use compared to C++ formatting statements. You want to talk about one major step backwards in the name of progress, this is it)

You mean a type-safe stream is a step backwards from a variable argument, buffer overflowing, type-ignorant, mark-up language?

– Zeno

[This message has been edited by Zeno (edited 05-03-2002).]

no he means that with all those new features added, simple formating of the output got lost (it IS still possible, but not at all that nice as before…)

that is true…

Exactly. printf and sprintf are so immensly easy and efficient to use.
I tried to learn how to use the C++ formatters once and just laughed. It would take 1000 characters of code to do what takes 20 with sprintf.
I had a job where they tried to make me use that style of output and I quit, got a new job (not just because of that, but because of general C++ overzealous religousness).

JESUS!!!

Sorry guys but you are really stressing a trivial issue.

Yes using streams is SAFE but you are buying it with a lot of overhead.

If you are doing a %1.5f sprintf on a char[256] buffer there is NO way you will EVER over run the buffer and its fast and easy too…

BTW: Knackered is right the width.precision only specifies the MININUM length of the result.

Regards,

LG

EDIT: Fixed some typo’s

[This message has been edited by lgrosshennig (edited 05-03-2002).]

my teachers,

Sorry!

I want to draw some objects with float numbers at (x,y,z). If using glListBase(1000); glRasterPos3f(x,y,z);glCallLists(Twidth,GL_UNSIGNED_BYTE,Text);,I have to use own code to convert it to a string with finding a decimal position (_fcvt(…)).If using sprintf/printf(), I can’t see the float numbers near objects drawed, namely I can’t show the float numbers at given (x,y,z).

How to directly show float numbers at (x,y,z) in OpenGL with drawing objects?

thanks

You’re misuberstanding the advice, ding

Used sprintf to format the output string (not printf!), then draw the formatted string using your existing calls.

Example:

char buffer[512];
sprintf (buffer, “%3.5f”, output_string);

YourNormalDrawTextFunction(buffer);

You don’t need to do anything special with OpenGL to draw floats. It’s all about formatting the output.

If you’re totally into safety then you might wanna use _ecvt or however it is called, it returns a pointer to a static char array filled with the floating point number.

>>(By the way, printf and sprintf are so incredible to use compared to C++ formatting statements. You want to talk about one major step backwards in the name of progress, this is it)<<

yeah i remember from when i started c++ in about 1996 (c in 1986/7) what struck me with c++.
the c++ way with formatting strings,
i thought it was a joke?
no i mean seriously!
i still think it nowadays, its a joke right?

btw i love c++

I am not sure that :

ostringstream os;
float t;

os << fixed() << setw(3) << setPrecision(2) << t;

is weirder or uglier than

char buffer[256];
sprintf( buffer, “%3.2f”, &t );

I will admit it is longer to type though.

[This message has been edited by Gorg (edited 05-05-2002).]

humm…I have something to say to this non-opengl debate hehehe

When I started learning c++ and used nothing of the printf family i thought that it was great.

But when I discovered that I could use sprintf and printf and all that stuff, I said to myself “f*ck the c++ text formatting method it’s ugly” ! hahaha

maybe(probably) it’s more safe though…
well…I love risks…


Evil-Dog
Let’s have a funny day