Al's Programming Resource Homepage  Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

general.cpp File Reference

#include "general.h"

Go to the source code of this file.

Functions

bool LoadTGA (TEXTURE *texture, char *filename)
void SetGLProperties ()
void SetGLView (int Width, int Height)
void SetGLMaterial ()


Function Documentation

bool LoadTGA TEXTURE   texture,
char *    filename
 

Definition at line 3 of file general.cpp.

References TEXTURE::Bpp, TEXTURE::Height, TEXTURE::ImageData, texture, and TEXTURE::Width.

00004 {
00005     GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};                        // Uncompressed TGA Header
00006     GLubyte TGAcompare[12];                                                 // Used To Compare TGA Header
00007     GLubyte header[6];                                                      // First 6 Useful Bytes From The Header
00008     GLuint  bytesPerPixel;                                                  // Holds Number Of Bytes Per Pixel Used In The TGA File
00009     GLuint  imageSize;                                                      // Used To Store The Image Size When Setting Aside Ram
00010     GLuint  temp;                                                           // Temporary Variable
00011     GLuint  type=GL_RGBA;                                                   // Set The Default GL Mode To RBGA (32 BPP)
00012     FILE *file = fopen(filename, "rb");                                     // Open The TGA Fi
00013     if( file==NULL ||                                                       // Does File Even Exist?
00014     fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||      // Are There 12 Bytes To Read?
00015     memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0               ||      // Does The Header Match What We Want?
00016     fread(header,1,sizeof(header),file)!=sizeof(header))                    // If So Read Next 6 Header Bytes
00017     {
00018         if (file == NULL)                                                   // Did The File Even Exist? *Added Jim Strong*
00019         {
00020             MessageBox(NULL,"Image file was not found","Error",MB_OK|MB_ICONERROR);
00021             return false;                                                   // 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);                                                   // If Anything Failed, Close The File
00030             return false;                                                   // Return False
00031         }
00032         texture->Width  = header[1] * 256 + header[0];                      // Determine The TGA Width      (highbyte*256+lowbyte)
00033         texture->Height = header[3] * 256 + header[2];                      // Determine The TGA Height     (highbyte*256+lowbyte)
00034         if( texture->Width  <=0     ||                                      // Is The Width Less Than Or Equal To Zero
00035         texture->Height <=0     ||                                          // Is The Height Less Than Or Equal To Zero
00036         (header[4]!=24 && header[4]!=32))                                   // Is The TGA 24 or 32 Bit?
00037         {
00038             fclose(file);                                                   // If Anything Failed, Close The 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;                                               // Return False
00044         }
00045         texture->Bpp    = header[4];                                        // Grab The TGA's Bits Per Pixel (24 or 32)
00046         bytesPerPixel   = texture->Bpp/8;                                   // Divide By 8 To Get The Bytes Per Pixel
00047         imageSize       = texture->Width*texture->Height*bytesPerPixel;     // Calculate The Memory Required For The TGA Data
00048         texture->ImageData=(GLubyte *)malloc(imageSize);                    // Reserve Memory To Hold The TGA Data
00049         if( texture->ImageData==NULL ||                                     // Does The Storage Memory Exist?
00050         fread(texture->ImageData, 1, imageSize, file)!=imageSize)           // Does The Image Size Match The Memory Reserved?
00051         {
00052             if(texture->ImageData!=NULL)                                    // Was Image Data Loaded
00053                 free(texture->ImageData);                                   // If So, Release The Image Data
00054             MessageBox(NULL,"Image load failed for unknown reason.","Error",MB_OK|MB_ICONERROR);
00055             fclose(file);                                                   // Close The File
00056             return false;                                                   // Return False
00057         }
00058         for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)                 // Loop Through The Image Data
00059         {                                                                   // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
00060             temp=texture->ImageData[i];                                     // Temporarily Store The Value At Image Data 'i'
00061             texture->ImageData[i] = texture->ImageData[i + 2];              // Set The 1st Byte To The Value Of The 3rd Byte
00062             texture->ImageData[i + 2] = temp;                               // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
00063         }
00064         fclose (file);                                                      // Close The File
00065 
00066         // Build A Texture From The Data
00067         glGenTextures(1, &texture[0].TexID);                                // Generate OpenGL texture IDs
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)                                             // Was The TGA 24 Bits
00072         {
00073             type=GL_RGB;                                                    // If So Set The 'type' To 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;                                                        // Texture Building Went Ok, Return True
00080 }

void SetGLMaterial  
 

Definition at line 108 of file general.cpp.

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 }

void SetGLProperties  
 

Definition at line 83 of file general.cpp.

00084 {
00085     glCullFace(GL_BACK);
00086     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    // This Will Clear The Background Color To Black
00087     glClearDepth(1.0);            // Enables Clearing Of The Depth Buffer
00088      glDepthFunc(GL_LESS);            // The Type Of Depth Test To Do
00089     glEnable(GL_DEPTH_TEST);        // Enables Depth Testing
00090     glShadeModel(GL_SMOOTH);        // Enables Smooth Color Shading
00091     glEnable(GL_NORMALIZE);
00092     glEnable(GL_CULL_FACE);
00093 }

void SetGLView int    Width,
int    Height
 

Definition at line 95 of file general.cpp.

00096 {
00097     if (Height==0)                // Prevent A Divide By Zero If The Window Is Too Small
00098         Height=1;
00099 
00100     glViewport(0, 0, Width, Height);    // Reset The Current Viewport And Perspective Transformation
00101     glMatrixMode(GL_PROJECTION);        // Select The Projection Matrix
00102     glLoadIdentity();            // Reset The Projection Matrix
00103       gluPerspective(45.0,(float)Width/(float)Height,0.1,200.0);    // Calculate The Aspect Ratio Of The Window
00104     glMatrixMode(GL_MODELVIEW);        // Select The Modelview Matrix
00105     glLoadIdentity();
00106 }


Generated on Fri Dec 23 05:19:34 2005 for OpenGL MDI 2 by doxygen1.2.15