bool createTexture(TGAimage *tgaimage)
{
unsigned int bytesPerPixel = tgaimage->bpp / 8;
glGenTextures(1,tgaimage->texID);
glBindTexture(GL_TEXTURE_2D, *tgaimage->texID);
if(bytesPerPixel==1 || bytesPerPixel== 3)
glTexImage2D(GL_TEXTURE_2D,0,3,tgaimage->width, tgaimage->height,0, GL_RGB, GL_UNSIGNED_BYTE,(BYTE*)tgaimage->image);
if(bytesPerPixel==2 || bytesPerPixel== 4)
glTexImage2D(GL_TEXTURE_2D,0,4,tgaimage->width, tgaimage->height,0, GL_RGBA, GL_UNSIGNED_BYTE,(BYTE*)tgaimage->imageA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return true;
}
//There is another function, which reads a tga file, and passes the data(and ect), to this function.
bool loadTGA(BYTE *DATA, int size, TGAimage *tgaimage, unsigned int *texID)
{
int i;
unsigned char temp;
u8 *stream = NULL;
u8 Desc = 0;
bool orgin = false;//If false, orgin is
//in lower-left corner, otherwise, it is in the top-left corner.
bool RLE = false;//Wether or
//not the image is RLE compressed.
bool Gray = false;//Wether or not the image is grayscale.
bool has_palette = false;//Wether or not the image has an color map.(palette)
if(DATA==NULL){return 0;}
stream = DATA;
tgaimage->texID = texID;
tgaimage->header.ID_Length = *stream;stream++;
tgaimage->header.colorMapType = *stream;stream++;
tgaimage->header.imageType = *stream;stream++;
for(int a = 0; a < 5; a++)
{
tgaimage->header.ColorMapSpecification[a] = *stream;stream++;
}
SCpy(stream,&tgaimage->header.xOrigin);stream+=2;
SCpy(stream,&tgaimage->header.yOrigin);stream+=2;
SCpy(stream,&tgaimage->header.imageWidth);stream+=2;
SCpy(stream,&tgaimage->header.imageHeight);stream+=2;
tgaimage->header.pixelDepth = *stream;stream++;
if(tgaimage->header.imageType !=2 &&
tgaimage->header.imageType != 10 &&
tgaimage->header.imageType != 3 &&
tgaimage->header.imageType != 11 ){
ERROR
}
if(tgaimage->header.imageType == 10 || tgaimage->header.imageType == 11){
RLE = true;//printf("RLE\n");
}
if(tgaimage->header.imageType != 10 && tgaimage->header.imageType != 11)
RLE = false;
if(tgaimage->header.imageType == 3 || tgaimage->header.imageType == 11){
Gray = true;//printf("Grayscale\n");
}
if(tgaimage->header.imageType != 3 && tgaimage->header.imageType != 11)
Gray = false;
tgaimage->width = (unsigned int)tgaimage->header.imageWidth;
tgaimage->height = (unsigned int)tgaimage->header.imageHeight;
tgaimage->bpp = tgaimage->header.pixelDepth;
if (tgaimage->width <= 0 || tgaimage->height <= 0 || (tgaimage->bpp!=24 && tgaimage->bpp!=32 && tgaimage->bpp!=8 && tgaimage->bpp!=16)){
ERROR
}
unsigned int bytesPerPixel = tgaimage->bpp / 8;
unsigned int imageSize = ((tgaimage->width * tgaimage->height) * bytesPerPixel);
tgaimage->type = GL_RGBA;
if(tgaimage->bpp==24)
{
tgaimage->type = GL_RGB;printf("RGB\n");
}
if(tgaimage->bpp==32)
{
//printf("RGBA\n");
}
if(tgaimage->bpp==8)
{
tgaimage->type = GL_RGB;printf("Grayscale\n");
}
if(tgaimage->bpp==16)
{
//printf("Grayscale with Alpha\n");
}
if(tgaimage->header.colorMapType==1)
{
has_palette = true;
}
else
{
has_palette = false;
}
if(has_palette){ERROR}
tgaimage->imageData = (BYTE*)malloc((size_t)((int)imageSize));
if(tgaimage->imageData==NULL)
{
ERROR
}
Desc = *stream;
tgaimage->header.ImageDesc = Desc;
stream++;
if(Desc & BIT(5))//Upper-left hand corner
{
orgin = true;
}
else
{
//lower-left hand corner
orgin = false;
}
if(!RLE)
{
for(int I = 0; I < (int)imageSize; I++)
{
tgaimage->imageData = *stream;
stream++;
}
}
if(RLE)
{
int Size;
Size = size - 18;
int pos = 0;
u8 temp = 0;
int impos = 0;
int repeats = 0;
u8 r, g, b, a, gvalue;
r=0;
g=0;
b=0;
a=0;
gvalue=0;//Grayscale value.
bool raw = false;
while(!((*(stream)) & BIT(7))){pos++;stream++;Size++;}
//Find the first packet of the actual data.
while(pos < Size)
{
temp = *(stream);
repeats = 0;
//if(!(temp & BIT(0)))
for(int i=0; i< 7; i++)
{
if(temp & BIT(i))
repeats |= BIT(i);
}
//repeats = (int)temp;
//repeats = repeats & ~BIT(7);
repeats++;//The value in the TGA is 1
//less than the actal pixels.
//So 0 would be 1 pixel, 1 would mean 2 pixels,
//ect.
#ifdef DEBUG2
//fprintf(log,"Runlength: %d\n",repeats);
#endif
raw = (!(*stream & BIT(7)))?true:false;
pos++;
stream++;
if(!raw)
{
if(bytesPerPixel>2)
{
b = *(stream);//B//
//In an TGA, the colors values are flipped.
//That is, it is BGR, instead of RGB
pos++;
stream++;
g = *(stream);//G
pos++;
stream++;
r = *(stream);//R
pos++;
stream++;
if(bytesPerPixel==4)
{
a = *(stream);//A
pos++;
stream++;
}
}
else
{
gvalue = *(stream);
pos++;
stream++;
if(bytesPerPixel==2)
{
a = *(stream);//A
pos++;
stream++;
}
}
if(bytesPerPixel>2)
{
for(int i=0; i<repeats; i++)
{
tgaimage->imageData[impos + 0] = b;
tgaimage->imageData[impos + 1] = g;
tgaimage->imageData[impos + 2] = r;
if(bytesPerPixel==4)
tgaimage->imageData[impos + 3] = a;
impos+=(int)bytesPerPixel;
}
}
else
{
for(int i=0; i<repeats; i++)
{
tgaimage->imageData[impos + 0] = gvalue;
if(bytesPerPixel==2)
tgaimage->imageData[impos + 1] = a;
impos+=(int)bytesPerPixel;
}
}
}
else
{
for(int i=0; i<repeats; i++)
{
if(bytesPerPixel>2)
{
b = *(stream);//B//
//In an TGA, the colors values are flipped.
//That is, it is BGR, instead of RGB
pos++;
stream++;
g = *(stream);//G
pos++;
stream++;
r = *(stream);//R
pos++;
stream++;
if(bytesPerPixel==4)
{
a = *(stream);//A
pos++;
stream++;
}
tgaimage->imageData[impos + 0] = b;
tgaimage->imageData[impos + 1] = g;
tgaimage->imageData[impos + 2] = r;
if(bytesPerPixel==4)
tgaimage->imageData[impos + 3] = a;
impos+=(int)bytesPerPixel;
}
else
{
gvalue = *(stream);
pos++;
stream++;
if(bytesPerPixel==2)
{
a = *(stream);//A
pos++;
stream++;
}
tgaimage->imageData[impos + 0] = gvalue;
if(bytesPerPixel==2)
tgaimage->imageData[impos + 1] = a;
impos+=(int)bytesPerPixel;
}
}
}
if((int)impos > (int)imageSize)
break;//TGAs have
//some extra data at the end of it,
//after the image data.
//This code is there so it dosen't
//think that data is image data.
}
}
//printf("Image data\n\n");
//Convert BGR to RGB
if(!Gray)
{
for (i =0; i <(int)imageSize; i+=(int)bytesPerPixel)
{
temp = tgaimage->imageData;
tgaimage->imageData = tgaimage->imageData;
tgaimage->imageData = temp;
temp = tgaimage->imageData;
tgaimage->imageData = tgaimage->imageData;
tgaimage->imageData = temp;
}
}
<span class="cpp-keyword">if</span>(bytesPerPixel != <span class="cpp-number">2</span> && bytesPerPixel != <span class="cpp-number">4</span>)
{
tgaimage->image = (color*)malloc(<span class="cpp-keyword">sizeof</span>(color)*(tgaimage->width*tgaimage->height));
}
<span class="cpp-keyword">else</span>
{
tgaimage->imageA = (colorA*)malloc(<span class="cpp-keyword">sizeof</span>(colorA)*(tgaimage->width*tgaimage->height));
}
<span class="cpp-keyword">int</span> I = <span class="cpp-number">0</span>;
color Temp;
colorA Temp2;
<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i < (<span class="cpp-keyword">int</span>)(tgaimage->width*tgaimage->height); i++)
{
<span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">1</span>){
Temp.r = tgaimage->imageData;
Temp.g = tgaimage->imageData;
Temp.b = tgaimage->imageData;
}
<span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">2</span>){
Temp2.r = tgaimage->imageData;
Temp2.g = tgaimage->imageData;
Temp2.b = tgaimage->imageData;
Temp2.a = tgaimage->imageData;
}
<span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">3</span>){
Temp.r = tgaimage->imageData;
Temp.g = tgaimage->imageData;
Temp.b = tgaimage->imageData;
}
<span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">4</span>)
{
Temp2.r = tgaimage->imageData;
Temp2.g = tgaimage->imageData;
Temp2.b = tgaimage->imageData;
Temp2.a = tgaimage->imageData;
}
I+=bytesPerPixel;
<span class="cpp-keyword">if</span>(bytesPerPixel != <span class="cpp-number">2</span> && bytesPerPixel != <span class="cpp-number">4</span>)
tgaimage->image<span style="font-weight:bold;"> = Temp;
<span class="cpp-keyword">if</span>(bytesPerPixel == <span class="cpp-number">2</span> || bytesPerPixel == <span class="cpp-number">4</span>)
tgaimage->imageA<span style="font-weight:bold;"> = Temp2;
}
<span class="cpp-keyword">if</span>(!orgin)
{
color temp;
colorA temp2;
<span class="cpp-keyword">int</span> y1, y2;
y1 = <span class="cpp-number">0</span>;
y2 = (<span class="cpp-keyword">int</span>)tgaimage->height-<span class="cpp-number">1</span>;
<span class="cpp-keyword">if</span>(bytesPerPixel != <span class="cpp-number">2</span> && bytesPerPixel != <span class="cpp-number">4</span>)
{
<span class="cpp-keyword">while</span>((y1 != y2) && (y1 < y2))
{
<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> w=<span class="cpp-number">0</span>; w< (<span class="cpp-keyword">int</span>)tgaimage->width; w++)
{
temp = tgaimage->image[(y1 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w];
tgaimage->image[(y1 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w] =
tgaimage->image[(y2 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w];
tgaimage->image[(y2 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w] = temp;
}
y1++;
y2–;
}
}
<span class="cpp-keyword">else</span>
{
<span class="cpp-keyword">while</span>((y1 != y2) && (y1 < y2))
{
<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> w=<span class="cpp-number">0</span>; w< (<span class="cpp-keyword">int</span>)tgaimage->width; w++)
{
temp2 = tgaimage->imageA[(y1 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w];
tgaimage->imageA[(y1 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w] =
tgaimage->imageA[(y2 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w];
tgaimage->imageA[(y2 * (<span class="cpp-keyword">int</span>)tgaimage->width) + w] = temp2;
}
y1++;
y2–;
}
}
}
createTexture(tgaimage);
<span class="cpp-keyword">return</span> <span class="cpp-number">1</span>;
}
</pre></div><!–ENDSCRIPT–>
<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - yellowstar on October 19, 2007 4:24:54 PM]<!–EDIT–></span><!–/EDIT–>