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
00022 w = power_of_two(surface->w);
00023 h = power_of_two(surface->h);
00024 texcoord[0] = 0.0f;
00025 texcoord[1] = 0.0f;
00026 texcoord[2] = (GLfloat)surface->w / w;
00027 texcoord[3] = (GLfloat)surface->h / h;
00028
00029 image = SDL_CreateRGBSurface(
00030 SDL_SWSURFACE,
00031 w, h,
00032 32,
00033 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
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
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
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
00065 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00066 SDL_SetAlpha(surface, saved_flags, saved_alpha);
00067 }
00068
00069
00070
00071
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);
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
00098 image = SDL_LoadBMP(Filename);
00099 if ( image == NULL ) {
00100 return 0;
00101 }
00102 w = image->w;
00103 h = image->h;
00104
00105
00106 return SDL_GL_LoadTexture(image, texcoord);
00107
00108 }
00109 #else
00110 #include <GL/glaux.h>
00111 int BitmapLoad(const char* Filename)
00112 {
00113 GLuint texid;
00114 AUX_RGBImageRec *Bitmap = NULL;
00115 FILE *FileHandle = NULL;
00116 char *pod;
00117 int i, j;
00118
00119 printf("Ucitavanje grafike iz datoteke %s...\n", Filename);
00120 FileHandle = fopen(Filename,"rb");
00121
00122 if(FileHandle != NULL)
00123 {
00124 Bitmap = auxDIBImageLoad(Filename);
00125 } else
00126 {
00127
00128 return(1);
00129 }
00130
00131 fclose(FileHandle);
00132
00133 glGenTextures(1,
00134 &texid);
00135
00136 glBindTexture(GL_TEXTURE_2D,
00137 texid);
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,
00161 4,
00162 Bitmap->sizeX,
00163 Bitmap->sizeY,
00164 GL_RGBA,
00165 GL_UNSIGNED_BYTE,
00166 pod);
00167
00168
00169 glTexParameteri(GL_TEXTURE_2D,
00170 GL_TEXTURE_MIN_FILTER,
00171 GL_NEAREST);
00172
00173 glTexParameteri(GL_TEXTURE_2D,
00174 GL_TEXTURE_MAG_FILTER,
00175 GL_NEAREST);
00176
00177 if (Bitmap)
00178 {
00179 if (Bitmap->data)
00180 {
00181 free(Bitmap->data);
00182 }
00183 free(Bitmap);
00184 }
00185 free(pod);
00186
00187 return(texid);
00188 }
00189
00190 #endif