Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: How to show float numbers?

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2002
    Posts
    18

    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!

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: How to show float numbers?

    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
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2002
    Posts
    18

    Re: How to show float numbers?

    [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.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Mar 2002
    Posts
    102

    Re: How to show float numbers?

    sprintf

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Jul 2001
    Posts
    533

    Re: How to show float numbers?

    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).

  6. #6
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Hannover, Germany
    Posts
    1,258

    Re: How to show float numbers?

    As you can use %5.5 you can avoid the crash.
    - Michael Steinberg

  7. #7
    Junior Member Regular Contributor
    Join Date
    Feb 2002
    Posts
    124

    Re: How to show float numbers?

    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

  8. #8
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    3,032

    Re: How to show float numbers?

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

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    3,115

    Re: How to show float numbers?

    Code :
    #include <sstream>
    using namespace std;
    ...
    {
        ostringstream s;
        s << myFloat;
        // the number as a string is now available in s.str()
    }
    ... to avoid buffer overruns.

  10. #10
    Member Regular Contributor nickels's Avatar
    Join Date
    Feb 2000
    Location
    Colorado
    Posts
    298

    Re: How to show float numbers?

    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •