Archived

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

YO_MAMA

Please explain this code to me

Recommended Posts

YO_MAMA    122
unsigned char g_letterA[] = { 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xDF, 0xFB, 0x7F, 0xFE, 0x60, 0x06, 0x30, 0x0C, 0x18, 0x18, 0x18, 0x18, 0x0C, 0x30, 0x0C, 0x30, 0x07, 0xE0, 0x07, 0xE0 }; .... ... void Display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glColor3f(1.0f, 1.0f, 1.0f); glRasterPos2f(50, 50); glBitmap(16, 16, 0.0f, 0.0f, 0.0f, 0.0f, g_letterA); glutSwapBuffers(); return; } ok, I understand that this will draw the letter ''A'' to the screen. I don''t understand what all the 0xC0 things are and how they translate into each bit being a 1 or 0. Could someone explain this to me? The other thing I don''t understand is what the glPixelStorei() function does.

Share this post


Link to post
Share on other sites
Kippesoep    892
They are simply hexadecimal numbers, which you can see by the "0x" prefix. Each hexadecimal digit (which is 0..9 or A..F) is called a nibble and represents four bits.


0 hex = 0000 binary = 0 decimal
1 = 0001 = 1
2 = 0010 = 2
3 = 0011 = 3
4 = 0100 = 4
5 = 0101 = 5
6 = 0110 = 6
7 = 0111 = 7
8 = 1000 = 8
9 = 1001 = 9
A = 1010 = 10
B = 1011 = 11
C = 1100 = 12
D = 1101 = 13
E = 1110 = 14
F = 1111 = 15


One of the values you find is 0xFB. This translates to binary as 11111011, which in decimal is 15 * 16 + 11 = 251

So there you have your ones and zeroes. This array is a sequence of 30 bytes, each storing 8 bits, so that''s 240 pixels. If you use the table I gave you above and convert it to a pattern of ones and zeroes, you''ll get the following:


11000000 00000011
11000000 00000011
11000000 00000011
11000000 00000011
11000000 00000011
11011111 11111011
01111111 11111110
01100000 00000110
00110000 00001100
00011000 00011000
00011000 00011000
00001100 00110000
00001100 00110000
00000111 11100000
00000111 11100000


As you can see, that''s an upside-down A. Do a Google search for some more info on binary arithmetic and you should find enough to give you a more in-depth explanation.
The glPixelStorei call tells OpenGL what kind of byte-alignment to use, which is a bit more complicated than I want to explain right now. Just look up the function in MSDN. They explain it nicely.

Share this post


Link to post
Share on other sites