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
00031 w = power_of_two(surface->w);
00032 h = power_of_two(surface->h);
00033 texcoord[0] = 0.0f;
00034 texcoord[1] = 0.0f;
00035 texcoord[2] = (GLfloat)surface->w / w;
00036 texcoord[3] = (GLfloat)surface->h / h;
00037
00038 image = SDL_CreateRGBSurface(
00039 SDL_SWSURFACE,
00040 w, h,
00041 32,
00042 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
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
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
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
00074 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00075 SDL_SetAlpha(surface, saved_flags, saved_alpha);
00076 }
00077
00078
00079
00080
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);
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
00107 image = SDL_LoadBMP(Filename);
00108 if ( image == NULL ) {
00109 return 0;
00110 }
00111 w = image->w;
00112 h = image->h;
00113
00114
00115 return SDL_GL_LoadTexture(image, texcoord);
00116
00117 }
00118 #else
00119 #include <GL/glaux.h>
00120 int BitmapLoad(const char* Filename)
00121 {
00122 GLuint texid;
00123 AUX_RGBImageRec *Bitmap = NULL;
00124 FILE *FileHandle = NULL;
00125 char *pod;
00126 int i, j;
00127
00128 printf("Ucitavanje grafike iz datoteke %s...\n", Filename);
00129 FileHandle = fopen(Filename,"rb");
00130
00131 if(FileHandle != NULL)
00132 {
00133 Bitmap = auxDIBImageLoad(Filename);
00134 } else
00135 {
00136
00137 return(1);
00138 }
00139
00140 fclose(FileHandle);
00141
00142 glGenTextures(1,
00143 &texid);
00144
00145 glBindTexture(GL_TEXTURE_2D,
00146 texid);
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,
00170 4,
00171 Bitmap->sizeX,
00172 Bitmap->sizeY,
00173 GL_RGBA,
00174 GL_UNSIGNED_BYTE,
00175 pod);
00176
00177
00178 glTexParameteri(GL_TEXTURE_2D,
00179 GL_TEXTURE_MIN_FILTER,
00180 GL_NEAREST);
00181
00182 glTexParameteri(GL_TEXTURE_2D,
00183 GL_TEXTURE_MAG_FILTER,
00184 GL_NEAREST);
00185
00186 if (Bitmap)
00187 {
00188 if (Bitmap->data)
00189 {
00190 free(Bitmap->data);
00191 }
00192 free(Bitmap);
00193 }
00194 free(pod);
00195
00196 return(texid);
00197 }
00198
00199 #endif