Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "texture.h"
00036
00037 #include "util/file.h"
00038
00039
00040 namespace glut {
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 static GLint
00051 getGlutInternalFormat
00052 (
00053 IN const media::image_t& image
00054 )
00055 {
00056 return image.bytes_per_pixel;
00057 }
00058
00059
00060
00061 static GLint
00062 getGlutFormatFromColorFormat
00063 (
00064 IN const media::image_t& image
00065 )
00066 {
00067 switch (image.color_format) {
00068 case media::eColorFormat_Grayscale: return GL_LUMINANCE;
00069 case media::eColorFormat_GrayAlpha: return GL_LUMINANCE_ALPHA;
00070 case media::eColorFormat_RGB: return GL_RGB;
00071 case media::eColorFormat_RGBA: return GL_RGBA;
00072 default:
00073 ASSERT_THROW(false, "unknown color format: "
00074 << image.color_format);
00075 }
00076
00077
00078 return 0;
00079 }
00080
00081
00082
00083 static int
00084 createOpenGLTexture
00085 (
00086 IN const media::image_t& image
00087 )
00088 {
00089 ASSERT_THROW(image.isValid(), "Invalid image?");
00090
00091 GLuint id = 0;
00092 glGenTextures(1, &id);
00093 DPRINTF("Generated texture %d...", id);
00094
00095 glBindTexture(GL_TEXTURE_2D, id);
00096
00097
00098 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00099 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00100
00101
00102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00104
00105 GLint level = 0;
00106 GLint internalFormat = getGlutInternalFormat(image);
00107 GLint format = getGlutFormatFromColorFormat(image);
00108 GLint border = 0;
00109 DPRINTF("internalFormat = %d", internalFormat);
00110 DPRINTF("format = %d", format);
00111
00112 GLsizei width = image.width;
00113 GLsizei height = image.height;
00114 DPRINTF(" texture width: %d", width);
00115 DPRINTF(" texture height: %d", height);
00116 glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width,
00117 height, border, format, GL_UNSIGNED_BYTE, image.data);
00118
00119 return id;
00120 }
00121
00122
00123
00124 class CreateTextureTask : public Task {
00125 public:
00126 CreateTextureTask(IN const media::image_t * image) {
00127 ASSERT(image, "null");
00128 m_image = image;
00129 }
00130 ~CreateTextureTask(void) throw() { }
00131
00132
00133 int getTextureId(void) const throw() { return m_textureId; }
00134
00135
00136 void doTask(void) {
00137 ASSERT(m_image, "null");
00138 m_textureId = createOpenGLTexture(*m_image);
00139 }
00140
00141 private:
00142 CreateTextureTask(void) throw() : m_image(NULL), m_textureId(0) { }
00143
00144
00145 const media::image_t * m_image;
00146 int m_textureId;
00147 };
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 int
00158 createTextureFromImage
00159 (
00160 IN const media::image_t& image
00161 )
00162 {
00163 ASSERT_THROW(image.isValid(), "Invalid image?");
00164 smart_ptr<CreateTextureTask> task = new CreateTextureTask(&image);
00165 ASSERT(task, "out of memory");
00166
00167 requestTask(task);
00168
00169 return task->getTextureId();
00170 }
00171
00172
00173
00174 };
00175