6 if (downsampled) cvReleaseImage(&downsampled);
14 if (!textureGenerated) {
15 glGenTextures(1,(GLuint*) &texture);
16 textureGenerated =
true;
18 glBindTexture(GL_TEXTURE_2D, texture);
20 for (texWidth=1; texWidth < im->width; texWidth <<=1);
21 for (texHeight=1; texHeight < im->height; texHeight <<=1);
23 if (texWidth > 1024) texWidth = 1024;
24 if (texHeight > 1024) texHeight = 1024;
28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
30 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (smooth ? GL_LINEAR : GL_NEAREST));
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (smooth ? GL_LINEAR : GL_NEAREST));
33 int sz = texWidth*texHeight*4;
34 char *buffer = (
char *) malloc(sz);
35 memset(buffer, 0, sz);
37 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth,texHeight, 0, GL_RGBA,
38 GL_UNSIGNED_BYTE, buffer);
49 IplImage *im = (downsampled ? downsampled : this->im);
51 if ((im->width > texWidth) || (im->height > texHeight)) {
52 if (downsampled) cvReleaseImage(&downsampled);
53 downsampled = cvCreateImage(cvSize(texWidth, texHeight), this->im->depth, this->im->nChannels);
57 glEnable(GL_TEXTURE_2D);
58 glBindTexture(GL_TEXTURE_2D, texture);
60 if (allowCache && !reload)
return;
64 cvResize(this->im, downsampled, CV_INTER_AREA);
69 case IPL_DEPTH_8U: type = GL_UNSIGNED_BYTE;
break;
70 case IPL_DEPTH_8S: type = GL_BYTE;
break;
71 case IPL_DEPTH_16S: type = GL_SHORT;
break;
72 case IPL_DEPTH_32F: type = GL_FLOAT;
break;
74 std::cerr <<
"IplTexture::loadTexture(): unsupported pixel type.\n";
77 switch (im->nChannels) {
78 case 1: format = GL_LUMINANCE;
break;
79 case 3: format = (im->channelSeq[0] ==
'B') ? GL_BGR_EXT : GL_RGB;
break;
80 case 4: format = GL_RGBA;
break;
82 std::cerr <<
"IplTexture::loadTexture(): unsupported number of channels.\n";
87 glPixelStorei(GL_UNPACK_ALIGNMENT, im->align);
89 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im->width, im->height,
90 format, type, im->imageData);
92 uScale = (double(im->width)/double(this->im->width))/
double(texWidth);
93 vScale = (double(im->height)/double(this->im->height))/
double(texHeight);
98 vOrigin = double(im->height)/double(texHeight);
106 glDisable(GL_TEXTURE_2D);
126 textureGenerated =
false;