PDA

View Full Version : Difference between GLfloat and float?



sam02
07-24-2002, 03:49 AM
What's the difference between GLfloat and float?

I also have problem understanding the following line of code:

GLubyte yellow[] = {255, 255, 0, 255};

What is this line of code for, and why is yellow an array?

Andrea Rossini
07-24-2002, 03:52 AM
typedef float GLfloat;
typedef something GLsomething;

http://www.opengl.org/discussion_boards/ubb/smile.gif http://www.opengl.org/discussion_boards/ubb/smile.gif

yellow = rgb(255,255,0)

the ending 255 means "full color non translucent"

ramo
07-25-2002, 08:06 AM
it is 32-bit color.
it is array because one byte is 8 bit you need 4 bytes to store it

red green blue alpha
255 , 255 , 0 , 255

GLfloat is for cross-platform (to prevent conflicts with platforms)
in windows,unix ... they are totally same

nexusone
07-25-2002, 09:41 AM
Because of diffrences between some systems in which a float could be 16, 32 or 64 bit.
So when gl.h defines GLfloat, it defines it a set size that does not change per system. This helps keeps openGL crossplatform compilant.

openGL uses RGB values to define a color.
(Red, Green, Blue), also your can add a transparency value to a color also, which is the fourth value.

GLubyte yellow[] = {255, 255, 0, 255}; // Define the RGB values for the color yellow with no transparency.

GLubyte white[] = {255, 255, 255, 255};

GLubyte reb[] = {255, 0, 0, 255};

The reason we do this is to cut down on code and having to remember numbers.

Both of the folowing does the same thing, set's current color to draw with.

glColor__( 255, 255, 0, 255);
or
glColor__( yellow );

Just easy to remember yellow then a string of numbers, and less typing.

http://www.opengl.org/discussion_boards/ubb/smile.gif


Originally posted by sam02:
What's the difference between GLfloat and float?

I also have problem understanding the following line of code:

GLubyte yellow[] = {255, 255, 0, 255};

What is this line of code for, and why is yellow an array?

ndj55
07-25-2002, 10:06 AM
I would add that using GLfloat (or GLwhatever) can make easier the reading of your code,because of the prefix GL and the usage you make of the variable... GL variable for a GL usage...

Bob
07-25-2002, 10:14 AM
So when gl.h defines GLfloat, it defines it a set size that does not change per system.

As with most, if not all features in OpenGL, the spec defines a lower limit, but not an upper. Same with the GL data types. Table 2.2 in the OpenGL 1.3 specification states the minimum length of GLfloat to 32 bit, but also says that an implementation may use more bits.

So GL-datatypes can make portability easier, yes. But portable? Don't be so sure about that.

Miguel_dup1
07-25-2002, 10:50 AM
You can avoid the size of byte changes from platform to platform by simply specifying it manually.

float a:4;
the :4 represents four bytes...

Deiussum
07-25-2002, 11:19 AM
Originally posted by mancha:
You can avoid the size of byte changes from platform to platform by simply specifying it manually.

float a:4;
the :4 represents four bytes...

Huh? What language are you working with?

Test.cpp:



void main()
{
float a:4;
}


Cygwin:
$ g++ -o test test.cpp
test.cpp: In function `int main(...)':
test.cpp:3: parse error before `:'

VC++:
Compiling...
test.cpp
f:\cygwin\home\administrator\test.cpp(3) : error C2601: 'a' : local function definitions are illegal
f:\cygwin\home\administrator\test.cpp(3) : error C2063: 'a' : not a function
f:\cygwin\home\administrator\test.cpp(3) : error C2969: syntax error : ';' : expected member function definition to end with '}'
Error executing cl.exe.

test.exe - 3 error(s), 0 warning(s)

Mezz
07-25-2002, 11:23 AM
That's legal syntax for bit fields, but AFAIK you can only use 'unsigned' or 'int' types (not float) with them, and the number specifies the number of BITS, not the number of BYTES.

-Mezz

Deiussum
07-25-2002, 11:33 AM
Originally posted by Mezz:
That's legal syntax for bit fields, but AFAIK you can only use 'unsigned' or 'int' types (not float) with them, and the number specifies the number of BITS, not the number of BYTES.

-Mezz

Ummm... yeah... changed float a:4; to unsigned int a:32;. Same errors. Not sure where you guys are reading this stuff, but I've never heard of that in any ANSI standard stuff I've ever seen. Maybe Delphi allows that, but for C/C++ most compilers usually have types like __int32, etc. That's not even an ANSI standard, though.

Miguel_dup1
07-25-2002, 11:38 AM
Originally posted by Mezz:
That's legal syntax for bit fields, but AFAIK you can only use 'unsigned' or 'int' types (not float) with them, and the number specifies the number of BITS, not the number of BYTES.

-Mezz

That is right, you do it with ints. You also do the declaration in struct, classes...
I dont remember well, 'cause I have not used it in a LOOONG time... But it goes by bytes, not bits; I might be wrong though... So you would declare it as int a:4; in your struct or class...

There are functions to manipulate your EPSILON, but I forgot.


[This message has been edited by mancha (edited 07-25-2002).]

[This message has been edited by mancha (edited 07-25-2002).]

Deiussum
07-25-2002, 08:17 PM
Ahhh... yes... I seem to vaguely recall the bitmask thingy. Not really meant for making your own custom size types, though. A quick test shows that different compiler's handles that differently too.




// Make sure struct is tightly packed
#pragma pack(1)
struct Blah
{
int a:8;
};

sizeof(Blah); // 4 with VC++ 1 with Cygwin.


If I recall, that is mainly meant for something like so...




#pragma pack(1)
struct Blah
{
int highWord:16;
int lowWord:16;
};

sizeof(Blah); // VC++ & Cygwin both == 4


A quick search on Google revealed that the ANSI standard doesn't actually specify how the memory has to be used, so that there is the possibility for padding as seen in the first example of struct Blah.

Anyway, I thought that was interesting. Always fun to learn new things about my favorite language just as I think I know it all... http://www.opengl.org/discussion_boards/ubb/smile.gif

I'll stop rambling now. http://www.opengl.org/discussion_boards/ubb/smile.gif

Edit:
A couple other quick notes.

1) If you try and do




struct Blah
{
int a:33;
};


Both Cygwin and VC++ give an error saying that the type is too small.


2) Just to get this back to the original topic. It was my understanding that the GL header would define GLfloat to be a 32-bit float based on whatever OS that header was meant for. So say hypothetically that you are on some platform where a float is 64 bits, but the compiler also has a __float32 type that can be used. In that case the GL.h for that particular platform would be:

typedef __float32 GLfloat;

Ok... NOW I'll stop rambling. http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by Deiussum (edited 07-25-2002).]

Mezz
07-25-2002, 11:53 PM
Yeah, I did look in my C book before I posted that, I forgot they had to be in a struct though http://www.opengl.org/discussion_boards/ubb/wink.gif

Of course, I don't think I've ever actually had cause to use them anywhere ever, but I did see them in "Game Programming Gems" once...

-Mezz

Robbo
07-26-2002, 12:32 AM
I have to add that I've written modules using `float' and then made the **assumption** that those variables are GLfloat when they hit the rendering pipeline. This is BAD.

See my post on templatization - Makes things slightly easier.

Bob
07-26-2002, 01:15 AM
It was my understanding that the GL header would define GLfloat to be a 32-bit float based on whatever OS that header was meant for. So say hypothetically that you are on some platform where a float is 64 bits, but the compiler also has a __float32 type that can be used. In that case the GL.h for that particular platform would be:

typedef __float32 GLfloat;

I wouldn't be so sure about that. The float would be as valid to represent GLfloat as __float32. Both float and __float32 meet the requirement of the spec; 32 bits or longer.

ramo
07-31-2002, 05:25 AM
hi everybody,

visual C++ gives 4 (while cygwin gives 1)
because "#pragma pack(1)" means nothing for Visual C++
i dont know its equvalent... http://www.opengl.org/discussion_boards/ubb/smile.gif
and it cant be bigger than 32 because you choose int (int is 32-bit http://www.opengl.org/discussion_boards/ubb/smile.gif
you cant exceed its max size...

Deiussum
07-31-2002, 07:23 AM
struct s1
{
char ch;
int n1;
int n2;
};

#pragma pack(1)
// You can also use #pragma pack(push, 1)
// then #pragam pack(pop) at the end of the struct
// The push/pop doesn't work for GNU compilers, though
struct s2
{
char ch;
int n1;
int n2;
};



Becuase sizeof returned 4 for VC++, that shows that VC++ was using the full size of the int, not just a single bit. Also, I know what the reason was for the 32-bit error.

I was merely pointing out that using the bitfield thing doesn't allow you to set fields to whatever size you want, as was previously implied.




[This message has been edited by Deiussum (edited 07-31-2002).]

Rob The Bloke
07-31-2002, 08:41 AM
Packed bit fields only work on unsigned data types. Generally they are quite slow compared to using bitmasks with the & and | operators. They will always use up memory to the next byte boundary, ie, specifying 9 bits would give you a 2byte data element. I've only ever found a use for them when reading/writing compressed binary files. You can do some fairly pointless things with them and unions though :




struct bit
{
bit()
{
b = 0x01;
}
union
{
struct
{
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
};
unsigned char b;
};
};


Fairly Pointless I think you'll agree.