2
Copyright (c) 1999 Nate Miller
4
Notice: Usage of any code in this file is subject to the rules
5
described in the LICENSE.TXT file included in this directory.
6
Reading, compiling, or otherwise using this code constitutes
7
automatic acceptance of the rules in said text file.
11
Author -- Nate 'm|d' Miller
12
Contact -- vandals1@home.com
13
Web -- http://members.home.com/vandals1
17
6/11/99 - added support for 8bit images, changed commenting
18
5/30/99 - original file
20
#include "glTexFont.h"
21
#include "glTexFontTGA.h"
22
#include "glTexFontInclude.h"
30
Make sure its a power of 2.
33
int fontCheckSize (int x)
35
if (x == 2 || x == 4 ||
38
x == 128 || x == 256 || x == 512)
46
Reads in RGBA data for a 32bit image.
49
unsigned char *fontGetRGBA (FILE *s, int size)
56
rgba = malloc (size * 4);
61
bread = fread (rgba, sizeof (unsigned char), size * 4, s);
63
/* TGA is stored in BGRA, make it RGBA */
64
if (bread != size * 4)
70
for (i = 0; i < size * 4; i += 4 )
73
rgba[i] = rgba[i + 2];
84
Reads in RGB data for a 24bit image.
87
unsigned char *fontGetRGB (FILE *s, int size)
94
rgb = malloc (size * 3);
99
bread = fread (rgb, sizeof (unsigned char), size * 3, s);
101
if (bread != size * 3)
107
/* TGA is stored in BGR, make it RGB */
108
for (i = 0; i < size * 3; i += 3)
123
Reads a gray scale image.
126
unsigned char *fontGetGray (FILE *s, int size)
128
unsigned char *grayData;
131
grayData = malloc (size);
133
if (grayData == NULL)
136
bread = fread (grayData, sizeof (unsigned char), size, s);
144
texFormat = GL_ALPHA;
152
Gets the image data for the specified bit depth.
155
unsigned char *fontGetData (FILE *s, int sz, int iBits)
158
return fontGetRGBA (s, sz);
159
else if (iBits == 24)
160
return fontGetRGB (s, sz);
162
return fontGetGray (s, sz);
170
Loads up a targa file. Supported types are 8,24 and 32 uncompressed images.
173
int fontLoadTGA (char *name, int id)
175
unsigned char type[4];
176
unsigned char info[7];
177
unsigned char *imageData = NULL;
178
int imageWidth, imageHeight;
183
if (!(s = fopen (name, "r+bt")))
185
// FIXME - the file exists but I still error on this fopen() call
186
fprintf(stderr,"font: %s\n", name);
187
if (!(s = fopen (name, "rb")))
189
return FONT_FILE_NOT_FOUND;
191
fread (&type, sizeof (char), 3, s); // read in colormap info and image type, byte 0 ignored
192
fseek (s, 12, SEEK_SET); // seek past the header and useless info
193
fread (&info, sizeof (char), 6, s);
195
if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
196
return FONT_BAD_IMAGE_TYPE;
198
imageWidth = info[0] + info[1] * 256;
199
imageHeight = info[2] + info[3] * 256;
202
size = imageWidth * imageHeight;
204
/* make sure dimension is a power of 2 */
205
if (!fontCheckSize (imageWidth) || !fontCheckSize (imageHeight))
206
return FONT_BAD_DIMENSION;
208
/* make sure we are loading a supported type */
209
if (imageBits != 32 && imageBits != 24 && imageBits != 8)
210
return FONT_BAD_BITS;
212
imageData = fontGetData (s, size, imageBits);
217
if (imageData == NULL)
218
return FONT_BAD_DATA;
220
glBindTexture (GL_TEXTURE_2D, id);
221
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
222
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
223
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
224
/* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); */
225
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
226
/* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */
227
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
228
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
229
glTexImage2D (GL_TEXTURE_2D, 0, texFormat, imageWidth, imageHeight, 0, texFormat, GL_UNSIGNED_BYTE, imageData);
231
/* release data, its been uploaded */