You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

838 lines
27 KiB

#include "FSGLVideoWidget.h"
#include "VideoWidget.h"
#define LOG( X ) m_pLog->Log( "FSGLVideoWidget", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "FSGLVideoWidget", this, _LOG_ERROR, X )
// вершины полноэкранного прямоугольника
const fsqVertex fsqVertices[6] = {
{ { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } },
{ { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } },
{ { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } },
{ { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } },
{ { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } },
{ { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } }
};
FSGLVideoWidget::FSGLVideoWidget(QWidget *parent, int sizeX, int sizeY, const QColor &background) :
QOpenGLWidget(parent),
//m_mainWindow(mw),
//m_showBubbles(true),
//m_qtLogo(true),
//m_frames(0),
m_pShaderProgram(0),
//m_program2(0),
m_pTexture(0),
//m_transparent(false),
//m_btn(0),
//m_hasButton(button),
m_background(background),
basicMediaDestination(){
m_nSourceWidth = sizeX;
m_nSourceHeight = sizeY;
m_bDirectDrawing = true;
m_bUpdateIsPending = false;
setFixedSize( sizeX, sizeY );
setMouseTracking( true );
//grabKeyboard();
//releaseKeyboard();
m_nKeyCode = 0;
m_nOldKeyCode = 0;
m_nManualControlMode = CD_AUTO;
m_pCamera = NULL;
m_eStreamAspectRatio = FAR_UNKNOWN;
//m_eWidgetAspectRatio = detectAspectRatio(width(), height());
//setGeometry(m_nPosX, m_nPosY, m_nWidth+1, m_nHeight+1);
//setMinimumSize(QSize(320, 240));
//setSizeIncrement(QSize(20, 20));
//m_backingStore = new QBackingStore( );
//setBackgroundRole(QPalette::Window);
setAutoFillBackground(false);
setAttribute(Qt::WA_OpaquePaintEvent);
setCursor(QCursor(Qt::PointingHandCursor));
//create();
m_pParentViewer = NULL;
m_pPixmap = NULL;
m_pGraber = NULL;
m_mFrame = NULL;
m_mOutput = NULL;
m_pTimer = NULL;
//m_bMouseActive = false;
m_nMouseX = 0;
m_nMouseY = 0;
fsqVAO = 0;
fsqVBO = 0;
m_pPropertiesProtectorMutex = new basicMutex();
m_pWidgetProtectorMutex = new basicMutex();
};
void FSGLVideoWidget::setBindingSize(basicMediaDestination *P, int x, int y){
m_pWidgetProtectorMutex->lock();
m_pParentViewer = P;
setFixedSize(x, y);
m_eWidgetAspectRatio = detectAspectRatio(width(), height());
if(m_pPixmap != NULL) delete m_pPixmap;
m_pPixmap = new QPixmap(width(), height());
m_pPixmap->fill(m_background);
m_pWidgetProtectorMutex->unlock();
};
void FSGLVideoWidget::setCameraControl(basicCamera *C, objectCaps CC){
if (m_pCamera != NULL){
if (m_nCameraCaps & OC_PAN_CONTROLL){
if (m_nManualControlMode&CD_LEFT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
if (m_nManualControlMode&CD_RIGHT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
};
if (m_nCameraCaps & OC_TILT_CONTROLL){
if (m_nManualControlMode&CD_UP)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
if (m_nManualControlMode&CD_DOWN)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
};
if (m_nCameraCaps & OC_ZOOM_CONTROLL){
if (m_nManualControlMode&CD_TELE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
if (m_nManualControlMode&CD_WIDE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
};
m_nManualControlMode = CD_AUTO;
};
m_nCameraCaps = CC;
m_pCamera = C;
};
bool FSGLVideoWidget::event(QEvent *event){
if( event->type() == QEvent::UpdateRequest ){
m_bUpdateIsPending = false;
//renderNow();
return true;
}
if (event->type() == QEvent::MouseButtonRelease){
if (((QMouseEvent*)event)->button() == Qt::LeftButton){
if (m_nManualControlMode & CD_MANUAL){
if (m_nManualControlMode&(CD_TELE | CD_WIDE)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
if (m_nManualControlMode&(CD_UP | CD_DOWN)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
if (m_nManualControlMode&(CD_LEFT | CD_RIGHT)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
m_nManualControlMode = CD_AUTO;
}
else{
m_nManualControlMode = CD_MANUAL;
grabKeyboard();
m_nMenuActivityCounter = 75;
};
return true;
};
};
if (event->type() == QEvent::MouseButtonDblClick){
((VideoWidget *)m_pParentViewer)->hideFullScreen();
return true;
}
if (m_nManualControlMode & CD_MANUAL){
if (event->type() == QEvent::KeyPress){
m_nKeyCode = ((QKeyEvent *)event)->key();
switch (m_nKeyCode){
case 32:
((VideoWidget *)m_pParentViewer)->hideFullScreen();
break;
case 87://w
if (m_nCameraCaps & OC_ZOOM_CONTROLL){
if ((m_nManualControlMode & CD_TELE) == 0){
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_TELE);
((basicCameraPTZ *)m_pCamera)->cmdBeginZoomTele();
}
else {
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
};
m_nMenuActivityCounter = 75;
};
break;
case 65://A
if (m_nCameraCaps & OC_ZOOM_CONTROLL){
if ((m_nManualControlMode & CD_WIDE) == 0){
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_WIDE);
((basicCameraPTZ *)m_pCamera)->cmdBeginZoomWide();
}
else {
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
};
m_nMenuActivityCounter = 75;
};
break;
case 0x01000012://Left
if (m_nCameraCaps & OC_PAN_CONTROLL){
if ((m_nManualControlMode & CD_LEFT) == 0){
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_LEFT);
((basicCameraPTZ *)m_pCamera)->cmdBeginPanLeft();
}
else {
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
};
m_nMenuActivityCounter = 75;
};
break;
case 0x01000013://Up
if (m_nCameraCaps & OC_TILT_CONTROLL){
if ((m_nManualControlMode & CD_UP) == 0){
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_UP);
((basicCameraPTZ *)m_pCamera)->cmdBeginTiltUp();
}
else {
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
};
m_nMenuActivityCounter = 75;
};
break;
case 0x01000014://Right
if (m_nCameraCaps & OC_PAN_CONTROLL){
if ((m_nManualControlMode & CD_RIGHT) == 0){
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_RIGHT);
((basicCameraPTZ *)m_pCamera)->cmdBeginPanRight();
}
else {
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
};
m_nMenuActivityCounter = 75;
};
break;
case 0x01000015://Down
if (m_nCameraCaps & OC_TILT_CONTROLL){
if ((m_nManualControlMode & CD_DOWN) == 0){
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_DOWN);
((basicCameraPTZ *)m_pCamera)->cmdBeginTiltDown();
}
else {
m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
};
m_nMenuActivityCounter = 75;
};
break;
};
m_nOldKeyCode = m_nKeyCode;
return true;
};
};
return QWidget::event(event);
}
void FSGLVideoWidget::InitGLAttributes(void){
// We do not use the depth buffer at all (for speed)
glDisable(GL_DEPTH_TEST);
// use culling for rendering speed
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE); // this should give us some optimization for free
// disable blending (for speed)
glDisable(GL_BLEND);
// default blending options to uncomment if you want to use blending in the future
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// clear to black
glClearColor(0, 0, 0, 1);
};
void FSGLVideoWidget::InitTextures(void){
//m_pTexture = new QOpenGLTexture(QImage("qt.png"));
m_iFrame = QGLWidget::convertToGLFormat(QImage("qt.png"));
glGenTextures(1, &m_nTextureID);
GL_ASSERT("InitTextures");
//bind the texture ID
glBindTexture(GL_TEXTURE_2D, m_nTextureID);
//texture parameters
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//generate the texture
//GLint format, internalFormat;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_iFrame.width(), m_iFrame.height(), 0, m_iFrame.format(), GL_UNSIGNED_BYTE, m_iFrame.bits());
//Unbind texture
glBindTexture(GL_TEXTURE_2D, NULL);
};
void FSGLVideoWidget::InitShaders(void){
m_pVertexShader = new QOpenGLShader(QOpenGLShader::Vertex);
m_pVertexShader->compileSourceFile("vertex_shader_duplo_1.glsl");
m_pFragmentShader = new QOpenGLShader(QOpenGLShader::Fragment);
m_pFragmentShader->compileSourceFile("fragment_shader_duplo_1.glsl");
m_pShaderProgram = new QOpenGLShaderProgram;
m_pShaderProgram->addShader(m_pVertexShader);
m_pShaderProgram->addShader(m_pFragmentShader);
m_pShaderProgram->bindAttributeLocation("myVertex", PROGRAM_VERTEX_ATTRIBUTE);
m_pShaderProgram->bindAttributeLocation("inTexCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
m_pShaderProgram->link();
};
void FSGLVideoWidget::InitSamplers(void){
//int i = m_pShaderProgram->uniformLocation("StandardTex");
//GL_ASSERT("glGetUniformLocation StandardTex");
//glUniform1i(i, 0); // texture unit 0 to correspond with "StandardTex" inside shader
m_pShaderProgram->setUniformValue("StandardTex", 0);
//GL_ASSERT("glUniform1i");
};
void FSGLVideoWidget::InitBuffers(void){
// texture coordinates using opengl convention (bottom to top)
GLfloat texCoords[] ={
0, 0, // lower left
1, 0, // lower right
1, 1, // upper right
1, 1, // upper right
0, 1, // upper left
0, 0, // lower left
};
//InitBuffersHelper(texCoords, sizeof(texCoords), &m_uTexCoordBuffer);
// store vertices in a buffer for fast access
glGenBuffers(1, &m_uTexCoordBuffer);
//GL_ASSERT("glGenBuffersOverlay");
glBindBuffer(GL_ARRAY_BUFFER, m_uTexCoordBuffer);
//GL_ASSERT("glBindBufferOverlay");
glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);
//GL_ASSERT("glBufferDataOverlay");
glBindBuffer(GL_ARRAY_BUFFER, NULL);
////////////////////////////////////////
// the vertical coordinate is flipped so that it matches my mental model of how a texture is laid in memory
// (top to bottom)
GLfloat texCoordsFlipped[] =
{
0, 1, // lower left
1, 1, // lower right
1, 0, // upper right
1, 0, // upper right
0, 0, // upper left
0, 1, // lower left
};
//InitBuffersHelper(texCoordsFlipped, sizeof(texCoordsFlipped), &m_uTexCoordFlippedBuffer);
// store vertices in a buffer for fast access
glGenBuffers(1, &m_uTexCoordFlippedBuffer);
//GL_ASSERT("glGenBuffersOverlay");
glBindBuffer(GL_ARRAY_BUFFER, m_uTexCoordFlippedBuffer);
//GL_ASSERT("glBindBufferOverlay");
glBufferData(GL_ARRAY_BUFFER, sizeof(texCoordsFlipped), texCoordsFlipped, GL_STATIC_DRAW);
//GL_ASSERT("glBufferDataOverlay");
glBindBuffer(GL_ARRAY_BUFFER, NULL);
/////////////////////////////////////////
GLfloat verticesFullScreen[] =
{
-1, -1, // lower left
1, -1,
1, 1,
1, 1,
-1, 1,
-1, -1,
};
//InitBuffersHelper(verticesFullScreen, sizeof(verticesFullScreen), &m_uVertexBufferFullScreen);
// store vertices in a buffer for fast access
glGenBuffers(1, &m_uVertexBufferFullScreen);
//GL_ASSERT("glGenBuffersOverlay");
glBindBuffer(GL_ARRAY_BUFFER, m_uVertexBufferFullScreen);
//GL_ASSERT("glBindBufferOverlay");
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesFullScreen), verticesFullScreen, GL_STATIC_DRAW);
//GL_ASSERT("glBufferDataOverlay");
glBindBuffer(GL_ARRAY_BUFFER, NULL);
};
void FSGLVideoWidget::InitProjectionMatrices(void){
// NOW setup projection matrices!
// this is a 2D projection that changes nothing from the normalized device coordinates.
// I used to have a near/far plane but decided to disable depth testing for speed improvement so there is no need for a projection matrix at all.
// I am just leaving the code here so that others have a starting point if they want to use a projection matrix.
// column major order
GLfloat mProjection[16] =
{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
//InitProjectionMatricesHelper(m_ProgramRGBA, mProjection);
//int i = glGetUniformLocation(uProgram, "matProjection");
int i = m_pShaderProgram->uniformLocation("matProjection");
//GL_ASSERT("glGetUniformLocation matProjection");
glUniformMatrix4fv(i,
1, // 1 matrix
GL_FALSE, // column major order
mProjection);
//GL_ASSERT("glUniformMatrix4fv (matProjection)");
};
void FSGLVideoWidget::InitViewMatrices(void){
// NOW setup view matrices
// "column major order" which means that the rows and columns are swapped.
// fZ is actually in the 4th columm, and 2nd row (not the 3rd column , 4th row as it may appear)
GLfloat mView[16] =
{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
//InitViewMatricesHelper(m_ProgramRGBA, mView);
int i = m_pShaderProgram->uniformLocation("matView");
//GL_ASSERT("glGetUniformLocation matView");
glUniformMatrix4fv(i,
1, // 1 matrix
GL_FALSE, // column major order
mView);
//GL_ASSERT("glUniformMatrix4fv");
};
void FSGLVideoWidget::initializeGL(){
initializeOpenGLFunctions();
InitTextures();
// создадим VAO и VBO для рендера полноэкранного прямоугольника
// Use a vertex buffer object. Client-side pointers are old-school and should be avoided.
m_vbo.create();
m_vbo.bind();
// For the cube all the data belonging to the texture coordinates and
// normals is placed separately, after the vertices. Here, for the Qt logo,
// let's do something different and potentially more efficient: create a
// properly interleaved data set.
//fsqVertex fsqVertices[6];
InitShaders();
m_vbo.allocate(fsqVertices, 6 * sizeof(fsqVertex));
m_vbo.release();
glGenBuffers(1, &fsqVBO);
// заполняем VBO вершинными данными
glBindBuffer(GL_ARRAY_BUFFER, fsqVBO);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(fsqVertex), fsqVertices, GL_STATIC_DRAW);
// задаем параметры расположения вершинных атрибутов в VBO
glVertexAttribPointer(GL_VERTEX_ARRAY_BUFFER_BINDING, 3, GL_FLOAT, GL_FALSE, sizeof(fsqVertex), GL_OFFSET_(0));
glEnableVertexAttribArray(GL_VERTEX_ARRAY_BUFFER_BINDING);
glVertexAttribPointer(GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING, 2, GL_FLOAT, GL_FALSE, sizeof(fsqVertex), GL_OFFSET_(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING);
};
//void FSGLVideoWidget::renderLater(){
// if (!m_bUpdateIsPending) {
// m_bUpdateIsPending = true;
// QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
// LOG("Delayed Rendering Request");
// }
//}
void FSGLVideoWidget::paintGL(){
QPainter painter;
m_pWidgetProtectorMutex->lock();
painter.begin(this);
painter.beginNativePainting();
// clear to a black frame
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// clear to black
glClearColor(1, 0.5, 0.1, 1);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
//glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
//Remove any previous transformations
//glLoadIdentity();
//Move to rendering point
//glTranslatef(-m_nSourceWidth / 4.f, -m_nSourceHeight / 4.f, 0.f);
// устанавливаем в качестве активного дефолтный FBO
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_pShaderProgram->bind();
m_pShaderProgram->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
m_pShaderProgram->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
// устанавливаем текстуру сцены в 0-вой текстурный юнит
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_nTextureID);
m_pShaderProgram->setUniformValue("colorTexture", 0);
// выводим полноэкранный прямоугольник на экран
glViewport(0, 0, width(), height());
m_vbo.bind();
glDrawArrays(GL_TRIANGLES, 0, 6);
m_pShaderProgram->release();
/*
glBindTexture(GL_TEXTURE_2D, m_nTextureID );
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f); glVertex2f(0.f, 0.f);
glTexCoord2f(1.f, 0.f); glVertex2f(m_nSourceWidth/2, 0.f);
glTexCoord2f(1.f, 1.f); glVertex2f(m_nSourceWidth/2, m_nSourceHeight/2);
glTexCoord2f(0.f, 1.f); glVertex2f(0.f, m_nSourceHeight/2);
glEnd();
/*
glBindTexture(GL_TEXTURE_2D, m_nTextureID );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_nTextureID);
//Render textured quad
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f); glVertex2f(0.f, 0.f);
glTexCoord2f(1.f, 0.f); glVertex2f(m_nSourceWidth, 0.f);
glTexCoord2f(1.f, 1.f); glVertex2f(m_nSourceWidth, m_nSourceHeight);
glTexCoord2f(0.f, 1.f); glVertex2f(0.f, m_nSourceHeight);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
*/
glDisable(GL_TEXTURE_2D);
//glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
painter.endNativePainting();
painter.setPen(Qt::black);
painter.drawText(20, 40, " Hobo Hobo paintGL calls / s");
painter.end();
m_fAngle += 1.0f;
update();
m_pWidgetProtectorMutex->unlock();
}
void FSGLVideoWidget::resizeGL(int, int){
}
void FSGLVideoWidget::exposeEvent(QExposeEvent *){
LOG_ERROR("Expose EVT!");
}
//! [2]
//void FSGLVideoWidget::paintEvent(QPaintEvent *evt){
// renderNow();
//}
//! [3]
/*
void FSGLVideoWidget::renderNow(){
return;
m_pWidgetProtectorMutex->lock();
try{
if (m_nMenuActivityCounter)m_nMenuActivityCounter--;
else if (m_nManualControlMode & CD_MANUAL){
m_nManualControlMode = CD_AUTO;
releaseKeyboard();
};
QPainter painter(this);
//painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint((QPainter::RenderHint)7);// QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing
//if( !painter.isActive() ) painter.begin(this);
render(&painter);
//if (pa inter.isActive()) painter.end();
}catch (std::exception& e){
LOG_ERROR("RenderNow exception!");
};
m_pWidgetProtectorMutex->unlock();
}
*/
//! [3]
//! [4]
void FSGLVideoWidget::render(QPainter *painter){
if( painter != NULL ){
try{
char buffer[64];
sprintf(buffer, "Coords: %d %d Key: %d", m_nMouseX, m_nMouseY, m_nKeyCode);
painter->save();
painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap);
QPen BgPen(Qt::SolidPattern, 2);
BgPen.setColor(Qt::black);
QPen FgPen(Qt::SolidPattern, 0);
FgPen.setColor(Qt::yellow);
painter->setPen(BgPen);
painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
painter->setPen(FgPen);
painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
if (m_pCamera != NULL){
if (m_nManualControlMode & CD_MANUAL){
QRect ManualRect(10, height() - m_pManualMode->height() - 10, m_pManualMode->width(), m_pManualMode->height());
if (m_nCameraCaps & OC_TILT_CONTROLL){
QRect UpRect(
(width() - m_pPixUp->width()) / 2,
(height() / 2) - m_pPixUp->height() - 10,
m_pPixUp->width(),
m_pPixUp->height());
QRect DownRect(
(width() - m_pPixDown->width()) / 2,
(height() / 2) + 10,
m_pPixDown->width(),
m_pPixDown->height());
if (m_nManualControlMode & CD_UP) {
painter->fillRect(UpRect, QColor(255, 255, 255, 63));
}
else {
painter->drawRect(UpRect);
};
if (m_nManualControlMode & CD_DOWN){
painter->fillRect(DownRect, QColor(255, 255, 255, 63));
}
else {
painter->drawRect(DownRect);
};
painter->drawPixmap(UpRect, *m_pPixUp);
painter->drawPixmap(DownRect, *m_pPixDown);
};
if (m_nCameraCaps & OC_PAN_CONTROLL){
QRect LeftRect(
(width() / 2) - m_pPixLeft->width() - 10 - (m_pPixLeft->width() / 2),
(height() / 2) - (m_pPixLeft->height() / 2),
m_pPixLeft->width(),
m_pPixLeft->height());
QRect RightRect(
(width() / 2) + 10 + (m_pPixRight->width() / 2),
(height() / 2) - (m_pPixRight->height() / 2),
m_pPixRight->width(),
m_pPixRight->height());
if (m_nManualControlMode & CD_RIGHT){
painter->fillRect(RightRect, QColor(255, 255, 255, 63));
}
else {
painter->drawRect(RightRect);
};
if (m_nManualControlMode & CD_LEFT){
painter->fillRect(LeftRect, QColor(255, 255, 255, 63));
}
else {
painter->drawRect(LeftRect);
};
painter->drawPixmap(LeftRect, *m_pPixLeft);
painter->drawPixmap(RightRect, *m_pPixRight);
};
if (m_nCameraCaps & OC_ZOOM_CONTROLL){
QRect WideRect(
25,
25,
m_pManualMode->width(),
m_pManualMode->height());
QRect TeleRect(
width() - m_pPixTele->height() - 25,
25,
m_pPixTele->width(),
m_pPixTele->height());
if (m_nManualControlMode & CD_WIDE){
painter->fillRect(WideRect, QColor(255, 255, 255, 63));
}
else {
painter->drawRect(WideRect);
};
if (m_nManualControlMode & CD_TELE){
painter->fillRect(TeleRect, QColor(255, 255, 255, 63));
}
else {
painter->drawRect(TeleRect);
};
painter->drawPixmap(WideRect, *m_pPixWide);
painter->drawPixmap(TeleRect, *m_pPixTele);
};
painter->fillRect(ManualRect, QColor(255, 255, 255, 63));
painter->drawPixmap(ManualRect, *m_pManualMode);
};
};
//char buffer[64];
//sprintf(buffer, "Coords: %dx%d %d %d ? %d %d", pos().x(), pos().y(), width(), height(), m_nMouseX, m_nMouseY);
//painter->save();
//painter->fillRect(QRect(0, 0, width(), height()), Qt::green);
//int x, y;
//x = width();
//y = height();
//painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap);
//QPen BgPen(Qt::SolidPattern,2);
//BgPen.setColor(Qt::black);
//QPen FgPen(Qt::SolidPattern,0);
//FgPen.setColor(Qt::yellow);
//painter->setPen(BgPen);
//painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
//painter->setPen(FgPen);
//painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline , buffer);
painter->restore();
}catch (std::exception& e){
LOG_ERROR("Render exception!");
};
};
}
bool FSGLVideoWidget::init(void){
LOG("init.");
m_pPixmap = new QPixmap(m_nSourceWidth, m_nSourceHeight);// , QImage::Format_RGBA8888);
if( m_pPixmap==NULL ) return false;
m_pManualMode = new QPixmap(":pictures/appbar.controller.snes.png", "PNG");
m_pPixUp = new QPixmap(":pictures/appbar.chevron.up.png", "PNG");
m_pPixDown = new QPixmap(":pictures/appbar.chevron.down.png", "PNG");
m_pPixLeft = new QPixmap(":pictures/appbar.chevron.left.png", "PNG");
m_pPixRight = new QPixmap(":pictures/appbar.chevron.right.png", "PNG");
m_pPixWide = new QPixmap(":pictures/appbar.arrow.expand.png", "PNG");
m_pPixTele = new QPixmap(":pictures/appbar.arrow.collapsed.png", "PNG");
m_pTimer = new QTimer();
if (m_pTimer != NULL){
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(update()));
m_pTimer->start(40);
};
m_eWidgetAspectRatio = detectAspectRatio(width(), width());
m_pPixmap->fill(Qt::black);
m_pPropertiesProtectorMutex->lock();
m_pPropertiesProtectorMutex->unlock();
return true;
};
FSGLVideoWidget::~FSGLVideoWidget(){
if (m_pCamera)m_pCamera->setViewer(NULL);
if (m_pTimer)m_pTimer->stop();
delete( m_pPropertiesProtectorMutex );
delete( m_pWidgetProtectorMutex );
};
void FSGLVideoWidget::setSourceSize(int sizeX, int sizeY){
m_pWidgetProtectorMutex->lock();
m_nSourceWidth = sizeX;
m_nSourceHeight = sizeY;
m_eStreamAspectRatio = detectAspectRatio(m_nSourceWidth, m_nSourceHeight);
if (m_pPixmap != NULL)delete (m_pPixmap);
m_pPixmap = new QPixmap(m_nSourceWidth, m_nSourceHeight);
m_pWidgetProtectorMutex->unlock();
};
void FSGLVideoWidget::pushFrame(cv::Mat *Frame){
if( Frame != NULL ){
try{
assert(Frame->isContinuous());
if (Frame->cols != m_nSourceWidth || Frame->rows != m_nSourceHeight) setSourceSize(Frame->cols, Frame->rows);
m_pWidgetProtectorMutex->lock();
m_mOutput = Frame;
// QPainter dbPainter(m_pPixmap);
/*if (m_eStreamAspectRatio != m_eWidgetAspectRatio){
int tHeight;
int tOffset;
if (m_eWidgetAspectRatio == FAR_16X9){
tHeight = (m_mOutput->cols / 16) * 9;
tOffset = (m_mOutput->rows - tHeight) >> 1;
dbPainter.drawImage(QRect(0, 0, width(), height()),
QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
QRect(0, tOffset, m_mOutput->cols, tHeight));
}
else {
if (m_eWidgetAspectRatio == FAR_5X4){
tHeight = (m_mOutput->cols / 5) * 4;
tOffset = (m_mOutput->rows - tHeight) >> 1;
dbPainter.drawImage(QRect(0, 0, width(), height()),
QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
QRect(0, tOffset, m_mOutput->cols, tHeight));
}
else {
tHeight = (m_mOutput->cols / 4) * 3;
tOffset = (m_mOutput->rows - tHeight) >> 1;
dbPainter.drawImage(QRect(0, 0, width(), height()),
QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
QRect(0, tOffset, m_mOutput->cols, tHeight));
};
};
}
else {
*/
//dbPainter.drawImage(0, 0, QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888));
m_iFrame = QGLWidget::convertToGLFormat(QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888));
//};
m_pWidgetProtectorMutex->unlock();
}
catch (std::exception& e){
LOG_ERROR("Exception catched:");
LOG_ERROR(e.what());
};
m_pPropertiesProtectorMutex->unlock();
//delete(Frame);
};
};
#undef LOG
#undef LOG_ERROR