00001
00002
00003 #include "TextureFactory.h"
00004
00005 #include "Configuration.h"
00006 #include "Renderer.h"
00007 #include "Texture.h"
00008
00009
00010 namespace pge {
00011
00012
00013
00014
00015
00016
00017 Texture* TextureFactory::createTexture2D(const std::string &filename, const std::string &name, bool mipmap) {
00018
00019
00020
00021 Image *image;
00022 Texture *texture;
00023
00024
00025
00026 image = new Image(filename, false);
00027 if(!image->isLoaded()) {
00028
00029 printf("Error loading image %s.\n", filename.c_str());
00030 return NULL;
00031 }
00032
00033
00034 texture = new Texture(image, name);
00035
00036
00037 if(mipmap) {
00038 texture->createTexture2D(mipmap, TEXTURE_MINFILTER_MIPMAP, TEXTURE_MAGFILTER,
00039 TEXTURE_WRAPS, TEXTURE_WRAPT);
00040 } else {
00041 texture->createTexture2D(mipmap, TEXTURE_MINFILTER, TEXTURE_MAGFILTER,
00042 TEXTURE_WRAPS, TEXTURE_WRAPT);
00043 }
00044
00045 texture->setAlphaBlending(false);
00046
00047
00048 return texture;
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 Texture* TextureFactory::createTexture2D(const std::string &filename, const std::string &name,
00058 bool mipmap, bool alpha, int ar, int ag, int ab, unsigned int minFilter,
00059 unsigned int magFilter, unsigned int wrapS, unsigned int wrapT){
00060
00061
00062
00063 Image *image;
00064 Texture *texture;
00065
00066
00067
00068 image = new Image(filename, alpha, ar, ag, ab);
00069 if(!image->isLoaded()) {
00070 printf("ERROR: [%s] Error loading image %s.\n", __FILE__, filename.c_str());
00071 return NULL;
00072 }
00073
00074
00075 texture = new Texture(image, name);
00076
00077
00078 texture->createTexture2D(mipmap, minFilter, magFilter, wrapS, wrapT);
00079
00080 texture->setAlphaBlending(alpha);
00081
00082
00083 return texture;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 Texture* TextureFactory::createTexture2D(unsigned char *image,
00093 const std::string &name, int width, int height, int bits,
00094 Image::ImageType type, bool mipmap, bool alpha, unsigned int minFilter,
00095 unsigned int magFilter, unsigned int wrapS, unsigned int wrapT) {
00096
00097
00098
00099 Image *img;
00100 Texture *texture;
00101
00102
00103
00104 img = new Image(image, width, height, bits, type, alpha);
00105 if(!img->isLoaded()) {
00106
00107 printf("Error\n");
00108 return NULL;
00109 }
00110
00111
00112 texture = new Texture(img, name);
00113
00114
00115 texture->createTexture2D(mipmap, minFilter, magFilter, wrapS, wrapT);
00116
00117 texture->setAlphaBlending(alpha);
00118
00119
00120 delete img;
00121 img = NULL;
00122
00123
00124 return texture;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 unsigned int TextureFactory::createCubemapTexture(const std::string &base) {
00134
00135
00136
00137 int i;
00138 std::string files[6];
00139 Image *images[6];
00140 unsigned int cubeTargets[6];
00141
00142 unsigned int textureDescriptor;
00143 int width = 0;
00144 int height = 0;
00145
00146
00147
00148
00149 files[0] = base + "_back.jpg";
00150 files[1] = base + "_front.jpg";
00151 files[2] = base + "_left.jpg";
00152 files[3] = base + "_right.jpg";
00153 files[4] = base + "_bottom.jpg";
00154 files[5] = base + "_top.jpg";
00155
00156 cubeTargets[0] = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
00157 cubeTargets[1] = GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
00158 cubeTargets[2] = GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
00159 cubeTargets[3] = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
00160 cubeTargets[4] = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
00161 cubeTargets[5] = GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
00162
00163
00164 for(i = 0; i < 6; i++) {
00165 images[i] = new Image(files[i]);
00166 if(!images[i]->isLoaded()) {
00167
00168 return 0;
00169 }
00170 }
00171
00172
00173 glEnable(GL_TEXTURE_CUBE_MAP);
00174 glGenTextures(1, &textureDescriptor);
00175 glBindTexture(GL_TEXTURE_CUBE_MAP, textureDescriptor);
00176
00177
00178 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00179 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00180
00181
00182 for(i = 0; i < 6; i++) {
00183 width = images[i]->getWidth();
00184 height = images[i]->getHeight();
00185
00186 if(images[i]->getType() == Image::RGBA) {
00187 glTexImage2D(cubeTargets[i], 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, images[i]->getImage());
00188 } else if(images[i]->getType() == Image::RGB) {
00189 glTexImage2D(cubeTargets[i], 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, images[i]->getImage());
00190 }
00191 }
00192 glDisable(GL_TEXTURE_CUBE_MAP);
00193
00194 return textureDescriptor;
00195 }
00196 }