00001 #include "texture.h" 00002 00003 #include <iostream> 00004 #include <qimage.h> 00005 00007 util::Texture1D::Texture1D() : 00008 data(0), 00009 size(0), 00010 tex_name(0) 00011 {} 00012 00014 util::Texture1D::Texture1D(const Texture1D& texture) : 00015 data(0), 00016 size(0), 00017 tex_name(0) 00018 { 00019 if (!texture.data) return; 00020 00021 size = texture.size; 00022 data = new GLubyte[size * 4]; 00023 00024 for (unsigned int i = 0; i < size; i++) 00025 data[i] = texture.data[i]; 00026 00027 glEnable(GL_TEXTURE_1D); 00028 00029 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00030 00031 glGenTextures(1, &tex_name); 00032 glBindTexture(GL_TEXTURE_1D, tex_name); 00033 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 00034 00035 clamp(); 00036 filter(); 00037 modulate(); 00038 } 00039 00044 util::Texture1D::Texture1D(std::string filename) : 00045 data(0), 00046 size(0), 00047 tex_name(0) 00048 { 00049 QImage image(filename.c_str()); 00050 if (!image.width()) { 00051 std::cerr << "Error: Texture initialised by " 00052 << filename << " is empty." << std::endl; 00053 return; 00054 } 00055 00056 size = image.width(); 00057 00058 data = new GLubyte[size * 4]; 00059 for (unsigned int x = 0; x < size; x++) { 00060 data[x * 4 + 0] = (GLubyte)qRed(image.pixel(x, 0)); 00061 data[x * 4 + 1] = (GLubyte)qGreen(image.pixel(x, 0)); 00062 data[x * 4 + 2] = (GLubyte)qBlue(image.pixel(x, 0)); 00063 data[x * 4 + 3] = (GLubyte)qAlpha(image.pixel(x, 0)); 00064 } 00065 00066 glEnable(GL_TEXTURE_1D); 00067 00068 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00069 00070 glGenTextures(1, &tex_name); 00071 glBindTexture(GL_TEXTURE_1D, tex_name); 00072 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 00073 00074 clamp(); 00075 filter(); 00076 modulate(); 00077 } 00078 00080 const util::Texture1D& util::Texture1D::operator=(const util::Texture1D& texture) { 00081 glDeleteTextures(1, &tex_name); 00082 delete[] data; 00083 data = 0; 00084 size = 0; 00085 00086 if (!texture.data) return *this; 00087 00088 size = texture.size; 00089 data = new GLubyte[size * 4]; 00090 00091 for (unsigned int i = 0; i < size; i++) 00092 data[i] = texture.data[i]; 00093 00094 glEnable(GL_TEXTURE_1D); 00095 00096 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00097 00098 glGenTextures(1, &tex_name); 00099 glBindTexture(GL_TEXTURE_1D, tex_name); 00100 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 00101 00102 clamp(); 00103 filter(); 00104 modulate(); 00105 00106 return *this; 00107 } 00108 00110 util::Texture1D::~Texture1D() { 00111 glDeleteTextures(1, &tex_name); 00112 delete[] data; 00113 } 00114 00116 void util::Texture1D::bind() { 00117 glBindTexture(GL_TEXTURE_1D, tex_name); 00118 } 00119 00123 void util::Texture1D::clamp(bool enable) { 00124 if (enable) 00125 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP); 00126 else 00127 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); 00128 } 00129 00133 void util::Texture1D::filter(bool enable) { 00134 if (enable) { 00135 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00136 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00137 } 00138 else { 00139 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 00140 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 00141 } 00142 } 00143 00145 void util::Texture1D::modulate() { 00146 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 00147 } 00148 00150 void util::Texture1D::decal() { 00151 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 00152 } 00153 00155 void util::Texture1D::blend() { 00156 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); 00157 } 00158 00160 void util::Texture1D::replace() { 00161 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00162 } 00163 00165 util::Texture2D::Texture2D() : 00166 data(0), 00167 size(0), 00168 width(0), 00169 height(0), 00170 tex_name(0) 00171 {} 00172 00174 util::Texture2D::Texture2D(const Texture2D& texture) : 00175 data(0), 00176 size(0), 00177 tex_name(0) 00178 { 00179 if (!texture.data) return; 00180 00181 size = texture.size; 00182 width = texture.width; 00183 height = texture.height; 00184 data = new GLubyte[size * 4]; 00185 00186 for (unsigned int i = 0; i < size * 4; i++) 00187 data[i] = texture.data[i]; 00188 00189 glEnable(GL_TEXTURE_2D); 00190 00191 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00192 00193 glGenTextures(1, &tex_name); 00194 glBindTexture(GL_TEXTURE_2D, tex_name); 00195 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 00196 00197 clamp(); 00198 filter(); 00199 modulate(); 00200 } 00201 00206 util::Texture2D::Texture2D(std::string filename) : 00207 data(0), 00208 size(0), 00209 tex_name(0) 00210 { 00211 QImage image(filename.c_str()); 00212 if (!image.width()) { 00213 std::cerr << "Error: Texture initialised by " 00214 << filename << " is empty." << std::endl; 00215 return; 00216 } 00217 00218 width = image.width(); 00219 height = image.height(); 00220 size = width * height; 00221 00222 data = new GLubyte[size * 4]; 00223 for (unsigned int x = 0; x < width; x++) { 00224 for (unsigned int y = 0; y < height; y++) { 00225 data[(x + y * width) * 4 + 0] = (GLubyte)qRed(image.pixel(x, y)); 00226 data[(x + y * width) * 4 + 1] = (GLubyte)qGreen(image.pixel(x, y)); 00227 data[(x + y * width) * 4 + 2] = (GLubyte)qBlue(image.pixel(x, y)); 00228 data[(x + y * width) * 4 + 3] = (GLubyte)qAlpha(image.pixel(x, y)); 00229 } 00230 } 00231 00232 glEnable(GL_TEXTURE_2D); 00233 00234 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00235 00236 glGenTextures(1, &tex_name); 00237 glBindTexture(GL_TEXTURE_2D, tex_name); 00238 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 00239 00240 clamp(); 00241 filter(); 00242 modulate(); 00243 } 00244 00246 const util::Texture2D& util::Texture2D::operator=(const util::Texture2D& texture) { 00247 glDeleteTextures(1, &tex_name); 00248 delete[] data; 00249 data = 0; 00250 size = 0; 00251 width = 0; 00252 height = 0; 00253 00254 if (!texture.data) return *this; 00255 00256 size = texture.size; 00257 data = new GLubyte[size * 4]; 00258 00259 for (unsigned int i = 0; i < size * 4; i++) 00260 data[i] = texture.data[i]; 00261 00262 glEnable(GL_TEXTURE_2D); 00263 00264 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00265 00266 glGenTextures(1, &tex_name); 00267 glBindTexture(GL_TEXTURE_2D, tex_name); 00268 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 00269 00270 clamp(); 00271 filter(); 00272 modulate(); 00273 00274 return *this; 00275 } 00276 00278 util::Texture2D::~Texture2D() { 00279 glDeleteTextures(1, &tex_name); 00280 delete[] data; 00281 } 00282 00284 void util::Texture2D::bind() { 00285 glBindTexture(GL_TEXTURE_2D, tex_name); 00286 } 00287 00291 void util::Texture2D::clamp(bool enable) { 00292 if (enable) { 00293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 00294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 00295 } 00296 else { 00297 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 00298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 00299 } 00300 } 00301 00305 void util::Texture2D::filter(bool enable) { 00306 if (enable) { 00307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00308 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00309 } 00310 else { 00311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 00312 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 00313 } 00314 } 00315 00317 void util::Texture2D::modulate() { 00318 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 00319 } 00320 00322 void util::Texture2D::decal() { 00323 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 00324 } 00325 00327 void util::Texture2D::blend() { 00328 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); 00329 } 00330 00332 void util::Texture2D::replace() { 00333 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00334 }