If you are doing AR, and for texture mapping
use glTexImage2D and glTexSubImage2D
For the glTexImage2D to work, the SIZE OF IMAGE MUST be POWER of 2, e,g, 2, 4 , 8, 16
any size like 640 x 480 is unaccepatable since it is not power of 2.
void initTexture()
{
//Alloc Texture
IplImage* frameImg = cvQueryFrame(capture);
IplImage *textImg = cvCreateImage(cvSize(512,256), frameImg->depth, frameImg->nChannels);
cvResize(frameImg, textImg);
cvFlip(textImg, NULL, 0);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_2D, 0, 3,textImg->width, textImg->height,0,
GL_BGR_EXT,GL_UNSIGNED_BYTE, textImg->imageData);
cvReleaseImage(&textImg);
}
After the initialization of the textureMap, one can proceed to draw it using glTexSubImage2D
void drawCameraImage(IplImage* tempImage)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
//////////////////////////////////////////////////////////////////////////////////
// Do texture mapping for drawing image in OpenGL space
glViewport(0,0, GL_WINDOW_WIDTH, GL_WINDOW_HEIGHT) ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
glOrtho(0, GL_WINDOW_WIDTH, 0, GL_WINDOW_HEIGHT, 0, 2000) ;
glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;
glEnable(GL_TEXTURE_2D) ;
glBindTexture(GL_TEXTURE_2D, textureID) ;
LoadCameraImage(tempImage);
glBegin(GL_QUADS) ;
glTexCoord2f(0,0) ;
glVertex3f(0,0, -1999) ;
glTexCoord2f(1,0) ;
glVertex3f(GL_WINDOW_WIDTH,0, -1999) ;
glTexCoord2f(1,1) ;
glVertex3f(GL_WINDOW_WIDTH,GL_WINDOW_HEIGHT, -1999) ;
glTexCoord2f(0,1) ;
glVertex3f(0,GL_WINDOW_HEIGHT, -1999) ;
glEnd() ;
glDisable(GL_TEXTURE_2D) ;
}
void LoadCameraImage(IplImage* tempImage)
{
cvFlip(tempImage, NULL, 0);
IplImage *scaledImage = cvCreateImage(cvSize(512, 256), tempImage->depth, tempImage->nChannels);
cvResize(tempImage, scaledImage, CV_INTER_AREA);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, scaledImage->width, scaledImage->height, GL_BGR_EXT,
GL_UNSIGNED_BYTE, scaledImage->imageData);
cvReleaseImage(&scaledImage) ;
}
glTexSubImage2D is only effective after glTexImage2D is called, so remember to use glTexImage2D
SubImage function is faster than calling glTexImage2D again and again.
To learn more about openGL programming, you might want to read the following list of books:
No comments:
Post a Comment