QtGLContext
::QtGLContext(QWidget* parent
/*= 0*/,
const QGLWidget* sharedWidget
/*= 0*/, Qt
::WFlags f
/*= 0*/) : QGLWidget(parent, sharedWidget, f
), m_imgP
(0), m_mousePressed
(false) {
//////qDebug() << "### total cams = " << ncams;
capture = cvCaptureFromCAM( CV_CAP_ANY );
if (capture) {
SelfUpdate();
}
}
void QtGLContext::SelfUpdate()
{
IplImage* imgPtr = cvQueryFrame( capture );
setImage((unsigned char*)imgPtr->imageData, imgPtr->width, imgPtr->height, imgPtr->nChannels * imgPtr->depth , true);
QTimer::singleShot(80,
this,
SLOT(SelfUpdate
()));
}
bool QtGLContext::setImage(
unsigned char* imgP,
const int width,
const int height,
const int pixeldepth,
const bool flipped /*=false*/)
{
m_imgP = imgP; m_width = width; m_height = height; m_pixeldepth = pixeldepth;
m_flipped = flipped;
this->updateGL();
// only 8, 24 and 32 bit images are supported
if (!imgP || (pixeldepth!=8 && pixeldepth!=24 && pixeldepth!=32))
return false;
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
// calculate texture size
m_texWidth = NextLargerPowerOfTwo(m_width);
m_texHeight = NextLargerPowerOfTwo(m_height);
// create texture memory
unsigned char* textureGL = new GLubyte[m_texHeight*m_texWidth* (pixeldepth>>3)];
// calculate texture coordinates for image
m_texUpperLeftX = float (m_texWidth-m_width) / (float) (m_texWidth);
m_texUpperLeftY = float (m_texHeight-m_height) / (float) (m_texHeight);
m_texLowerRightX = 1.0; // (float) (_texWidth) / (float) _height;
m_texLowerRightY = 1.0; // (float) (_texHeight) / (float) _width;
// tell OpenGL which texture "id" we will be working with.:
glBindTexture(GL_TEXTURE_2D, m_texNameGL);
// tell OpenGL that the pixel data which is going to be passed to it is aligned in byte order:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// set the various parameters for the current OpenGL texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// tell OpenGL how the texture will act when it is rendered into a scene:
// The GL_MODULATE attribute allows you to apply effects such as lighting
// and coloring to your texture. If you do not want lighting and coloring to effect
// your texture and you would like to display the texture unchanged when coloring
// is applied replace GL_MODULATE with GL_DECAL
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, _texWidth, _texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data());
switch(m_pixeldepth) {
case 8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_texWidth, m_texHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,textureGL);
break;
case 24:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_texWidth,m_texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, textureGL);
break;
case 32:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_texWidth,m_texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureGL);
break;
default:
glDisable(GL_TEXTURE_2D);
delete[] textureGL;
return false;
}
glDisable(GL_TEXTURE_2D);
//glFlush();
delete[] textureGL;
resetBox();
return true;
}
QtGLContext::QtGLContext(QWidget* parent /*= 0*/,
const QGLWidget* sharedWidget /*= 0*/, Qt::WFlags f /*= 0*/) : QGLWidget(parent, sharedWidget, f), m_imgP(0), m_mousePressed(false)
{
//////qDebug() << "### total cams = " << ncams;
capture = cvCaptureFromCAM( CV_CAP_ANY );
if (capture) {
SelfUpdate();
}
}
void QtGLContext::SelfUpdate()
{
IplImage* imgPtr = cvQueryFrame( capture );
setImage((unsigned char*)imgPtr->imageData, imgPtr->width, imgPtr->height, imgPtr->nChannels * imgPtr->depth , true);
QTimer::singleShot(80, this, SLOT(SelfUpdate()));
}
bool QtGLContext::setImage(
unsigned char* imgP,
const int width,
const int height,
const int pixeldepth,
const bool flipped /*=false*/)
{
m_imgP = imgP; m_width = width; m_height = height; m_pixeldepth = pixeldepth;
m_flipped = flipped;
this->updateGL();
// only 8, 24 and 32 bit images are supported
if (!imgP || (pixeldepth!=8 && pixeldepth!=24 && pixeldepth!=32))
return false;
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
// calculate texture size
m_texWidth = NextLargerPowerOfTwo(m_width);
m_texHeight = NextLargerPowerOfTwo(m_height);
// create texture memory
unsigned char* textureGL = new GLubyte[m_texHeight*m_texWidth* (pixeldepth>>3)];
// calculate texture coordinates for image
m_texUpperLeftX = float (m_texWidth-m_width) / (float) (m_texWidth);
m_texUpperLeftY = float (m_texHeight-m_height) / (float) (m_texHeight);
m_texLowerRightX = 1.0; // (float) (_texWidth) / (float) _height;
m_texLowerRightY = 1.0; // (float) (_texHeight) / (float) _width;
// tell OpenGL which texture "id" we will be working with.:
glBindTexture(GL_TEXTURE_2D, m_texNameGL);
// tell OpenGL that the pixel data which is going to be passed to it is aligned in byte order:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// set the various parameters for the current OpenGL texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// tell OpenGL how the texture will act when it is rendered into a scene:
// The GL_MODULATE attribute allows you to apply effects such as lighting
// and coloring to your texture. If you do not want lighting and coloring to effect
// your texture and you would like to display the texture unchanged when coloring
// is applied replace GL_MODULATE with GL_DECAL
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, _texWidth, _texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data());
switch(m_pixeldepth) {
case 8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_texWidth, m_texHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,textureGL);
break;
case 24:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_texWidth,m_texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, textureGL);
break;
case 32:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_texWidth,m_texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureGL);
break;
default:
glDisable(GL_TEXTURE_2D);
delete[] textureGL;
return false;
}
glDisable(GL_TEXTURE_2D);
//glFlush();
delete[] textureGL;
resetBox();
return true;
}
To copy to clipboard, switch view to plain text mode
Bookmarks