texload.c

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

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