Advertisement

Madness..

Started by April 05, 2001 06:16 AM
0 comments, last by Metus 23 years, 10 months ago
Ok. now I''m MAD (almose teared down a wall)! Can anyone of you get NeHe''s TGA-Loader to work? Well the, Lucky you, I can''t! I''ve COPIED the code NeHe used but i Can''t use the TGA as a Texture (I Guess there''s something wrong here ) so I''m asking you: 1. What should i Use for Textures; TGA, BMP, JPG, GIF .. .. .. 2. I''m relatively new to programming so if there''s one who knows of a real "beginners tutorial to load images" out there so..please?
Ethereal
Here''s my rewrite of his TGA loader, it''s basicly the same, but is a little more forgive (it can load compressed files also, and isn''t so strict about the header, which isn''t always a good thing):
  // arData is and unsigned char *// Size is a class that holds the width/height// I think you can guess what the rest of the things arebool riTextureImage::LoadTGA(char *filepath) {  unsigned char TGAheader[12];  unsigned char header[6];  unsigned int bytesPerPixel;  unsigned int imageSize;  unsigned int temp;  unsigned int type=GL_RGBA;  // Stuff for Run Length files:  unsigned char packetHead;  unsigned char chunk[4];  unsigned char *at;  FILE *file = fopen(filepath,"rb");  if(file==NULL ||    fread(TGAheader,1,sizeof(TGAheader),file)!=sizeof(TGAheader) ||    fread(header,1,sizeof(header),file)!=sizeof(header)) {    if(file==NULL) {      return false;    } else {      fclose(file);      return false;    }  }  Size.ukX = header[1] * 256 + header[0];  Size.ukY = header[3] * 256 + header[2];  if(Size.ukX<=0 || Size.ukY<=0 || (header[4]!=24 && header[4]!=32)) {    fclose(file);    return false;  }  bytesPerPixel = header[4]/8;  imageSize = Size.ukX*Size.ukY*bytesPerPixel;  arData = (unsigned char *) malloc(imageSize);  if(arData==NULL) {    fclose(file);    return false;  }  if(TGAheader[2]==2) {    if(fread(arData, 1, imageSize, file)!=imageSize) {      fclose(file);      return false;    }  } else {    at = arData;    for(unsigned int bytesDone=0; bytesDone<imageSize; ) {      packetHead = (unsigned char) fgetc(file);      if((packetHead & 0x80)) {        packetHead &= 0x7F;        packetHead++;        fread(chunk, bytesPerPixel, 1, file);        for(unsigned char b=0; b<packetHead; b++) {          memcpy(at,chunk,bytesPerPixel);          bytesDone += bytesPerPixel;          at += bytesPerPixel;        }      } else {        packetHead &= 0x7F;        packetHead++;        fread(at, bytesPerPixel, packetHead, file);        bytesDone += packetHead*bytesPerPixel;        at += packetHead*bytesPerPixel;      }    }  }  for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) {    temp = arData[i];    arData[i] = arData[i+2];    arData[i+2] = temp;  }  fclose(file);  glGenTextures(1,&uiTexID);  glBindTexture(GL_TEXTURE_2D, uiTexID);  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  if(header[4]==24) {    type = GL_RGB;  }  glTexImage2D(GL_TEXTURE_2D, 0, type, Size.ukX, Size.ukY, 0, type, GL_UNSIGNED_BYTE, arData);  return true;}  


"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/

This topic is closed to new replies.

Advertisement