00001 #include "general.h"
00002
00003 bool LoadTGA(TEXTURE *texture, char *filename)
00004 {
00005 GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
00006 GLubyte TGAcompare[12];
00007 GLubyte header[6];
00008 GLuint bytesPerPixel;
00009 GLuint imageSize;
00010 GLuint temp;
00011 GLuint type=GL_RGBA;
00012 FILE *file = fopen(filename, "rb");
00013 if( file==NULL ||
00014 fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||
00015 memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 ||
00016 fread(header,1,sizeof(header),file)!=sizeof(header))
00017 {
00018 if (file == NULL)
00019 {
00020 MessageBox(NULL,"Image file was not found","Error",MB_OK|MB_ICONERROR);
00021 return false;
00022 }
00023 if(TGAcompare[2] == 1)
00024 MessageBox(NULL,"Image cannot be indexed color. \r\n Convert the image to RGB or RGBA.","Error",MB_OK|MB_ICONERROR);
00025 if(TGAcompare[2] == 3)
00026 MessageBox(NULL,"Image cannot be greyscale color. \r\n Convert the image to RGB or RGBA.","Error",MB_OK|MB_ICONERROR);
00027 if(TGAcompare[2] == 9 || TGAcompare[2] == 10)
00028 MessageBox(NULL,"Image cannot be compressed. \r\n Convert the image to an uncompressed format.","Error",MB_OK|MB_ICONERROR);
00029 fclose(file);
00030 return false;
00031 }
00032 texture->Width = header[1] * 256 + header[0];
00033 texture->Height = header[3] * 256 + header[2];
00034 if( texture->Width <=0 ||
00035 texture->Height <=0 ||
00036 (header[4]!=24 && header[4]!=32))
00037 {
00038 fclose(file);
00039 if(texture->Width <=0 || texture->Height <=0)
00040 MessageBox(NULL,"Image must have a width and height greater than 0","Error",MB_OK|MB_ICONERROR);
00041 if(header[4]!=24 && header[4]!=32)
00042 MessageBox(NULL,"Image must be 24 or 32 bit","Error",MB_OK|MB_ICONERROR);
00043 return false;
00044 }
00045 texture->Bpp = header[4];
00046 bytesPerPixel = texture->Bpp/8;
00047 imageSize = texture->Width*texture->Height*bytesPerPixel;
00048 texture->ImageData=(GLubyte *)malloc(imageSize);
00049 if( texture->ImageData==NULL ||
00050 fread(texture->ImageData, 1, imageSize, file)!=imageSize)
00051 {
00052 if(texture->ImageData!=NULL)
00053 free(texture->ImageData);
00054 MessageBox(NULL,"Image load failed for unknown reason.","Error",MB_OK|MB_ICONERROR);
00055 fclose(file);
00056 return false;
00057 }
00058 for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
00059 {
00060 temp=texture->ImageData[i];
00061 texture->ImageData[i] = texture->ImageData[i + 2];
00062 texture->ImageData[i + 2] = temp;
00063 }
00064 fclose (file);
00065
00066
00067 glGenTextures(1, &texture[0].TexID);
00068 glBindTexture(GL_TEXTURE_2D, texture[0].TexID);
00069 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00070 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00071 if (texture[0].Bpp==24)
00072 {
00073 type=GL_RGB;
00074 }
00075 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00076 glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].Width, texture[0].Height, 0, type, GL_UNSIGNED_BYTE, texture[0].ImageData);
00077 gluBuild2DMipmaps(GL_TEXTURE_2D, type, texture[0].Width, texture[0].Height, type, GL_UNSIGNED_BYTE, texture[0].ImageData);
00078 free(texture->ImageData);
00079 return true;
00080 }
00081
00082
00083 void SetGLProperties()
00084 {
00085 glCullFace(GL_BACK);
00086 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00087 glClearDepth(1.0);
00088 glDepthFunc(GL_LESS);
00089 glEnable(GL_DEPTH_TEST);
00090 glShadeModel(GL_SMOOTH);
00091 glEnable(GL_NORMALIZE);
00092 glEnable(GL_CULL_FACE);
00093 }
00094
00095 void SetGLView(int Width, int Height)
00096 {
00097 if (Height==0)
00098 Height=1;
00099
00100 glViewport(0, 0, Width, Height);
00101 glMatrixMode(GL_PROJECTION);
00102 glLoadIdentity();
00103 gluPerspective(45.0,(float)Width/(float)Height,0.1,200.0);
00104 glMatrixMode(GL_MODELVIEW);
00105 glLoadIdentity();
00106 }
00107
00108 void SetGLMaterial()
00109 {
00110 float no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
00111 float mat_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
00112 float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00113 float mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
00114 float high_shininess[] = { 80.0 };
00115
00116 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00117 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00118 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00119 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00120 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
00121 }
00122