texload.cpp

00001 
00002 #include <stdio.h>
00003 
00004 
00005 #ifndef WIN32 // unices
00006 #include <SDL/SDL.h>
00007 #include <SDL/SDL_opengl.h>
00008 #include <GL/gl.h>
00009 
00010 
00011 
00012 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
00013 {
00014         GLuint texture;
00015         int w, h;
00016         SDL_Surface *image;
00017         SDL_Rect area;
00018         Uint32 saved_flags;
00019         Uint8  saved_alpha;
00020 
00021         /* Use the surface width and height expanded to powers of 2 */
00022         w = power_of_two(surface->w);
00023         h = power_of_two(surface->h);
00024         texcoord[0] = 0.0f;                     /* Min X */
00025         texcoord[1] = 0.0f;                     /* Min Y */
00026         texcoord[2] = (GLfloat)surface->w / w;  /* Max X */
00027         texcoord[3] = (GLfloat)surface->h / h;  /* Max Y */
00028 
00029         image = SDL_CreateRGBSurface(
00030                         SDL_SWSURFACE,
00031                         w, h,
00032                         32,
00033 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
00034                         0x000000FF,
00035                         0x0000FF00,
00036                         0x00FF0000,
00037                         0xFF000000
00038 #else
00039                         0xFF000000,
00040                         0x00FF0000,
00041                         0x0000FF00,
00042                         0x000000FF
00043 #endif
00044                        );
00045         if ( image == NULL ) {
00046                 return 0;
00047         }
00048 
00049 
00050         /* Save the alpha blending attributes */
00051         saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
00052         saved_alpha = surface->format->alpha;
00053         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00054                 SDL_SetAlpha(surface, 0, 0);
00055         }
00056 
00057         /* Copy the surface into the GL texture image */
00058         area.x = 0;
00059         area.y = 0;
00060         area.w = surface->w;
00061         area.h = surface->h;
00062         SDL_BlitSurface(surface, &area, image, &area);
00063 
00064         /* Restore the alpha blending attributes */
00065         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00066                 SDL_SetAlpha(surface, saved_flags, saved_alpha);
00067         }
00068 
00069 
00070 
00071         /* Create an OpenGL texture for the image */
00072         glGenTextures(1, &texture);
00073         glBindTexture(GL_TEXTURE_2D, texture);
00074         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00075         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00076         glTexImage2D(GL_TEXTURE_2D,
00077                      0,
00078                      GL_RGBA,
00079                      w, h,
00080                      0,
00081                      GL_RGBA,
00082                      GL_UNSIGNED_BYTE,
00083                      image->pixels);
00084         SDL_FreeSurface(image); /* No longer needed */
00085 
00086         return texture;
00087 }
00088 
00089 
00090 
00091 
00092 int BitmapLoad(const char* Filename) {
00093         int w, h;
00094         SDL_Surface *image;
00095         GLfloat texcoord[4];
00096 
00097         /* Load the image (could use SDL_image library here) */
00098         image = SDL_LoadBMP(Filename);
00099         if ( image == NULL ) {
00100                         return 0;
00101         }
00102         w = image->w;
00103         h = image->h;
00104 
00105         /* Convert the image into an OpenGL texture */
00106         return SDL_GL_LoadTexture(image, texcoord);
00107 
00108 }
00109 #else
00110 #include <GL/glaux.h>
00111 int BitmapLoad(const char* Filename) // 0 if success, else fail
00112     {
00113       GLuint texid;
00114       AUX_RGBImageRec *Bitmap = NULL;     // Pointer To AUX_RGBImageRec
00115       FILE *FileHandle = NULL;            // File Handle To Use
00116       char *pod;
00117       int i, j;
00118 
00119       printf("Ucitavanje grafike iz datoteke %s...\n", Filename);
00120       FileHandle = fopen(Filename,"rb");  // Check To See If The File Exists
00121 
00122       if(FileHandle != NULL)              // If File Exists
00123       {
00124         Bitmap = auxDIBImageLoad(Filename);  // Use glaux.h To Load Image
00125       } else                              // Else If Error Loading File
00126       {
00127         //fclose(FileHandle);               // Close File Handle
00128         return(1);                    // Return FALSE
00129       }
00130 
00131       fclose(FileHandle);                 // Close File Handle
00132 
00133       glGenTextures(1,                    // # Of Texture Names/ID To Create
00134                     &texid);  // Generated Texture ID
00135 
00136       glBindTexture(GL_TEXTURE_2D,        // Bind A 2D Texture
00137                     texid); // Bind Using This Texture ID
00138 
00139       pod = (char*)malloc(Bitmap->sizeX * Bitmap->sizeY * 4);
00140 
00141       for (i = 0 ; i < Bitmap->sizeX ; i++) {
00142           for (j = 0 ; j < Bitmap->sizeY ; j++) {
00143               if (Bitmap->data[((Bitmap->sizeY-j-1)*Bitmap->sizeX + i)*3] == 255 &&
00144                   Bitmap->data[((Bitmap->sizeY-j-1)*Bitmap->sizeX + i)*3 + 2] == 255 &&
00145                   Bitmap->data[((Bitmap->sizeY-j-1)*Bitmap->sizeX + i)*3 + 1] == 0) {
00146                       pod[(j*Bitmap->sizeX + i)*4 + 0] = 0;
00147                       pod[(j*Bitmap->sizeX + i)*4 + 1] = 0;
00148                       pod[(j*Bitmap->sizeX + i)*4 + 2] = 0;
00149                       pod[(j*Bitmap->sizeX + i)*4 + 3] = 0;
00150                   } else {
00151                       pod[(j*Bitmap->sizeX + i)*4 + 0] = Bitmap->data[((Bitmap->sizeY-j-1)*Bitmap->sizeX + i)*3];
00152                       pod[(j*Bitmap->sizeX + i)*4 + 1] = Bitmap->data[((Bitmap->sizeY-j-1)*Bitmap->sizeX + i)*3+1];
00153                       pod[(j*Bitmap->sizeX + i)*4 + 2] = Bitmap->data[((Bitmap->sizeY-j-1)*Bitmap->sizeX + i)*3+2];
00154                       pod[(j*Bitmap->sizeX + i)*4 + 3] = 255;
00155                   }
00156           }
00157       }
00158 
00159 
00160       gluBuild2DMipmaps(GL_TEXTURE_2D,    // Texture Is A 2D Texture
00161                         4,                // 3 Means "RGB" Texture...=)
00162                         Bitmap->sizeX,    // Bitmap's Width
00163                         Bitmap->sizeY,    // Bitmap's Height
00164                         GL_RGBA,           // Bitmap Is RGB Format
00165                         GL_UNSIGNED_BYTE, // Type Of Data Each Pixel Is Stored
00166                         pod);    // Actual Bitmap Data
00167 
00168 
00169       glTexParameteri(GL_TEXTURE_2D,          // Set 2D Texture Parameter
00170                       GL_TEXTURE_MIN_FILTER,  // Select Minification Filter Type
00171                       GL_NEAREST);            // GL_NEAREST (Blocky & Fastest)
00172 
00173       glTexParameteri(GL_TEXTURE_2D,          // Set 2D Texture Parameter
00174                       GL_TEXTURE_MAG_FILTER,  // Select Magnification Filter Type
00175                       GL_NEAREST);            // GL_LINEAR (Smoothest & Slowest)
00176 
00177       if (Bitmap)
00178       {
00179         if (Bitmap->data)                 // If There Is Texture Data
00180         {
00181            free(Bitmap->data);            // Free Texture Data
00182         }
00183         free(Bitmap);                     // Free Structure
00184       }
00185       free(pod);
00186 //      printf("Teksturni id: %d\n", texid);
00187       return(texid);                       // Return TRUE
00188 }
00189 
00190 #endif

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007