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

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "FSGLVideoWidget.h"
  2. #include "VideoWidget.h"
  3. #define LOG( X ) m_pLog->Log( "FSGLVideoWidget", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "FSGLVideoWidget", this, _LOG_ERROR, X )
  5. // вершины полноэкранного прямоугольника
  6. const fsqVertex fsqVertices[6] = {
  7. { { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } },
  8. { { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } },
  9. { { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } },
  10. { { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } },
  11. { { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } },
  12. { { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } }
  13. };
  14. FSGLVideoWidget::FSGLVideoWidget(QWidget *parent, int sizeX, int sizeY, const QColor &background) :
  15. QOpenGLWidget(parent),
  16. //m_mainWindow(mw),
  17. //m_showBubbles(true),
  18. //m_qtLogo(true),
  19. //m_frames(0),
  20. m_pShaderProgram(0),
  21. //m_program2(0),
  22. m_pTexture(0),
  23. //m_transparent(false),
  24. //m_btn(0),
  25. //m_hasButton(button),
  26. m_background(background),
  27. basicMediaDestination(){
  28. m_nSourceWidth = sizeX;
  29. m_nSourceHeight = sizeY;
  30. m_bDirectDrawing = true;
  31. m_bUpdateIsPending = false;
  32. setFixedSize( sizeX, sizeY );
  33. setMouseTracking( true );
  34. //grabKeyboard();
  35. //releaseKeyboard();
  36. m_nKeyCode = 0;
  37. m_nOldKeyCode = 0;
  38. m_nManualControlMode = CD_AUTO;
  39. m_pCamera = NULL;
  40. m_eStreamAspectRatio = FAR_UNKNOWN;
  41. //m_eWidgetAspectRatio = detectAspectRatio(width(), height());
  42. //setGeometry(m_nPosX, m_nPosY, m_nWidth+1, m_nHeight+1);
  43. //setMinimumSize(QSize(320, 240));
  44. //setSizeIncrement(QSize(20, 20));
  45. //m_backingStore = new QBackingStore( );
  46. //setBackgroundRole(QPalette::Window);
  47. setAutoFillBackground(false);
  48. setAttribute(Qt::WA_OpaquePaintEvent);
  49. setCursor(QCursor(Qt::PointingHandCursor));
  50. //create();
  51. m_pParentViewer = NULL;
  52. m_pPixmap = NULL;
  53. m_pGraber = NULL;
  54. m_mFrame = NULL;
  55. m_mOutput = NULL;
  56. m_pTimer = NULL;
  57. //m_bMouseActive = false;
  58. m_nMouseX = 0;
  59. m_nMouseY = 0;
  60. fsqVAO = 0;
  61. fsqVBO = 0;
  62. m_pPropertiesProtectorMutex = new basicMutex();
  63. m_pWidgetProtectorMutex = new basicMutex();
  64. };
  65. void FSGLVideoWidget::setBindingSize(basicMediaDestination *P, int x, int y){
  66. m_pWidgetProtectorMutex->lock();
  67. m_pParentViewer = P;
  68. setFixedSize(x, y);
  69. m_eWidgetAspectRatio = detectAspectRatio(width(), height());
  70. if(m_pPixmap != NULL) delete m_pPixmap;
  71. m_pPixmap = new QPixmap(width(), height());
  72. m_pPixmap->fill(m_background);
  73. m_pWidgetProtectorMutex->unlock();
  74. };
  75. void FSGLVideoWidget::setCameraControl(basicCamera *C, objectCaps CC){
  76. if (m_pCamera != NULL){
  77. if (m_nCameraCaps & OC_PAN_CONTROLL){
  78. if (m_nManualControlMode&CD_LEFT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  79. if (m_nManualControlMode&CD_RIGHT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  80. };
  81. if (m_nCameraCaps & OC_TILT_CONTROLL){
  82. if (m_nManualControlMode&CD_UP)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  83. if (m_nManualControlMode&CD_DOWN)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  84. };
  85. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  86. if (m_nManualControlMode&CD_TELE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  87. if (m_nManualControlMode&CD_WIDE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  88. };
  89. m_nManualControlMode = CD_AUTO;
  90. };
  91. m_nCameraCaps = CC;
  92. m_pCamera = C;
  93. };
  94. bool FSGLVideoWidget::event(QEvent *event){
  95. if( event->type() == QEvent::UpdateRequest ){
  96. m_bUpdateIsPending = false;
  97. //renderNow();
  98. return true;
  99. }
  100. if (event->type() == QEvent::MouseButtonRelease){
  101. if (((QMouseEvent*)event)->button() == Qt::LeftButton){
  102. if (m_nManualControlMode & CD_MANUAL){
  103. if (m_nManualControlMode&(CD_TELE | CD_WIDE)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  104. if (m_nManualControlMode&(CD_UP | CD_DOWN)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  105. if (m_nManualControlMode&(CD_LEFT | CD_RIGHT)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  106. m_nManualControlMode = CD_AUTO;
  107. }
  108. else{
  109. m_nManualControlMode = CD_MANUAL;
  110. grabKeyboard();
  111. m_nMenuActivityCounter = 75;
  112. };
  113. return true;
  114. };
  115. };
  116. if (event->type() == QEvent::MouseButtonDblClick){
  117. ((VideoWidget *)m_pParentViewer)->hideFullScreen();
  118. return true;
  119. }
  120. if (m_nManualControlMode & CD_MANUAL){
  121. if (event->type() == QEvent::KeyPress){
  122. m_nKeyCode = ((QKeyEvent *)event)->key();
  123. switch (m_nKeyCode){
  124. case 32:
  125. ((VideoWidget *)m_pParentViewer)->hideFullScreen();
  126. break;
  127. case 87://w
  128. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  129. if ((m_nManualControlMode & CD_TELE) == 0){
  130. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
  131. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_TELE);
  132. ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomTele();
  133. }
  134. else {
  135. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
  136. ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  137. };
  138. m_nMenuActivityCounter = 75;
  139. };
  140. break;
  141. case 65://A
  142. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  143. if ((m_nManualControlMode & CD_WIDE) == 0){
  144. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
  145. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_WIDE);
  146. ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomWide();
  147. }
  148. else {
  149. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
  150. ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  151. };
  152. m_nMenuActivityCounter = 75;
  153. };
  154. break;
  155. case 0x01000012://Left
  156. if (m_nCameraCaps & OC_PAN_CONTROLL){
  157. if ((m_nManualControlMode & CD_LEFT) == 0){
  158. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
  159. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_LEFT);
  160. ((basicCameraPTZ *)m_pCamera)->cmdBeginPanLeft();
  161. }
  162. else {
  163. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
  164. ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  165. };
  166. m_nMenuActivityCounter = 75;
  167. };
  168. break;
  169. case 0x01000013://Up
  170. if (m_nCameraCaps & OC_TILT_CONTROLL){
  171. if ((m_nManualControlMode & CD_UP) == 0){
  172. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
  173. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_UP);
  174. ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltUp();
  175. }
  176. else {
  177. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
  178. ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  179. };
  180. m_nMenuActivityCounter = 75;
  181. };
  182. break;
  183. case 0x01000014://Right
  184. if (m_nCameraCaps & OC_PAN_CONTROLL){
  185. if ((m_nManualControlMode & CD_RIGHT) == 0){
  186. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
  187. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_RIGHT);
  188. ((basicCameraPTZ *)m_pCamera)->cmdBeginPanRight();
  189. }
  190. else {
  191. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
  192. ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  193. };
  194. m_nMenuActivityCounter = 75;
  195. };
  196. break;
  197. case 0x01000015://Down
  198. if (m_nCameraCaps & OC_TILT_CONTROLL){
  199. if ((m_nManualControlMode & CD_DOWN) == 0){
  200. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
  201. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_DOWN);
  202. ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltDown();
  203. }
  204. else {
  205. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
  206. ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  207. };
  208. m_nMenuActivityCounter = 75;
  209. };
  210. break;
  211. };
  212. m_nOldKeyCode = m_nKeyCode;
  213. return true;
  214. };
  215. };
  216. return QWidget::event(event);
  217. }
  218. void FSGLVideoWidget::InitGLAttributes(void){
  219. // We do not use the depth buffer at all (for speed)
  220. glDisable(GL_DEPTH_TEST);
  221. // use culling for rendering speed
  222. glFrontFace(GL_CCW);
  223. glCullFace(GL_BACK);
  224. glEnable(GL_CULL_FACE); // this should give us some optimization for free
  225. // disable blending (for speed)
  226. glDisable(GL_BLEND);
  227. // default blending options to uncomment if you want to use blending in the future
  228. //glEnable(GL_BLEND);
  229. //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  230. // clear to black
  231. glClearColor(0, 0, 0, 1);
  232. };
  233. void FSGLVideoWidget::InitTextures(void){
  234. //m_pTexture = new QOpenGLTexture(QImage("qt.png"));
  235. m_iFrame = QGLWidget::convertToGLFormat(QImage("qt.png"));
  236. glGenTextures(1, &m_nTextureID);
  237. GL_ASSERT("InitTextures");
  238. //bind the texture ID
  239. glBindTexture(GL_TEXTURE_2D, m_nTextureID);
  240. //texture parameters
  241. //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  242. //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  243. //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  244. //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  245. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  246. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  247. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  248. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  249. //generate the texture
  250. //GLint format, internalFormat;
  251. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_iFrame.width(), m_iFrame.height(), 0, m_iFrame.format(), GL_UNSIGNED_BYTE, m_iFrame.bits());
  252. //Unbind texture
  253. glBindTexture(GL_TEXTURE_2D, NULL);
  254. };
  255. void FSGLVideoWidget::InitShaders(void){
  256. m_pVertexShader = new QOpenGLShader(QOpenGLShader::Vertex);
  257. m_pVertexShader->compileSourceFile("vertex_shader_duplo_1.glsl");
  258. m_pFragmentShader = new QOpenGLShader(QOpenGLShader::Fragment);
  259. m_pFragmentShader->compileSourceFile("fragment_shader_duplo_1.glsl");
  260. m_pShaderProgram = new QOpenGLShaderProgram;
  261. m_pShaderProgram->addShader(m_pVertexShader);
  262. m_pShaderProgram->addShader(m_pFragmentShader);
  263. m_pShaderProgram->bindAttributeLocation("myVertex", PROGRAM_VERTEX_ATTRIBUTE);
  264. m_pShaderProgram->bindAttributeLocation("inTexCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
  265. m_pShaderProgram->link();
  266. };
  267. void FSGLVideoWidget::InitSamplers(void){
  268. //int i = m_pShaderProgram->uniformLocation("StandardTex");
  269. //GL_ASSERT("glGetUniformLocation StandardTex");
  270. //glUniform1i(i, 0); // texture unit 0 to correspond with "StandardTex" inside shader
  271. m_pShaderProgram->setUniformValue("StandardTex", 0);
  272. //GL_ASSERT("glUniform1i");
  273. };
  274. void FSGLVideoWidget::InitBuffers(void){
  275. // texture coordinates using opengl convention (bottom to top)
  276. GLfloat texCoords[] ={
  277. 0, 0, // lower left
  278. 1, 0, // lower right
  279. 1, 1, // upper right
  280. 1, 1, // upper right
  281. 0, 1, // upper left
  282. 0, 0, // lower left
  283. };
  284. //InitBuffersHelper(texCoords, sizeof(texCoords), &m_uTexCoordBuffer);
  285. // store vertices in a buffer for fast access
  286. glGenBuffers(1, &m_uTexCoordBuffer);
  287. //GL_ASSERT("glGenBuffersOverlay");
  288. glBindBuffer(GL_ARRAY_BUFFER, m_uTexCoordBuffer);
  289. //GL_ASSERT("glBindBufferOverlay");
  290. glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);
  291. //GL_ASSERT("glBufferDataOverlay");
  292. glBindBuffer(GL_ARRAY_BUFFER, NULL);
  293. ////////////////////////////////////////
  294. // the vertical coordinate is flipped so that it matches my mental model of how a texture is laid in memory
  295. // (top to bottom)
  296. GLfloat texCoordsFlipped[] =
  297. {
  298. 0, 1, // lower left
  299. 1, 1, // lower right
  300. 1, 0, // upper right
  301. 1, 0, // upper right
  302. 0, 0, // upper left
  303. 0, 1, // lower left
  304. };
  305. //InitBuffersHelper(texCoordsFlipped, sizeof(texCoordsFlipped), &m_uTexCoordFlippedBuffer);
  306. // store vertices in a buffer for fast access
  307. glGenBuffers(1, &m_uTexCoordFlippedBuffer);
  308. //GL_ASSERT("glGenBuffersOverlay");
  309. glBindBuffer(GL_ARRAY_BUFFER, m_uTexCoordFlippedBuffer);
  310. //GL_ASSERT("glBindBufferOverlay");
  311. glBufferData(GL_ARRAY_BUFFER, sizeof(texCoordsFlipped), texCoordsFlipped, GL_STATIC_DRAW);
  312. //GL_ASSERT("glBufferDataOverlay");
  313. glBindBuffer(GL_ARRAY_BUFFER, NULL);
  314. /////////////////////////////////////////
  315. GLfloat verticesFullScreen[] =
  316. {
  317. -1, -1, // lower left
  318. 1, -1,
  319. 1, 1,
  320. 1, 1,
  321. -1, 1,
  322. -1, -1,
  323. };
  324. //InitBuffersHelper(verticesFullScreen, sizeof(verticesFullScreen), &m_uVertexBufferFullScreen);
  325. // store vertices in a buffer for fast access
  326. glGenBuffers(1, &m_uVertexBufferFullScreen);
  327. //GL_ASSERT("glGenBuffersOverlay");
  328. glBindBuffer(GL_ARRAY_BUFFER, m_uVertexBufferFullScreen);
  329. //GL_ASSERT("glBindBufferOverlay");
  330. glBufferData(GL_ARRAY_BUFFER, sizeof(verticesFullScreen), verticesFullScreen, GL_STATIC_DRAW);
  331. //GL_ASSERT("glBufferDataOverlay");
  332. glBindBuffer(GL_ARRAY_BUFFER, NULL);
  333. };
  334. void FSGLVideoWidget::InitProjectionMatrices(void){
  335. // NOW setup projection matrices!
  336. // this is a 2D projection that changes nothing from the normalized device coordinates.
  337. // 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.
  338. // I am just leaving the code here so that others have a starting point if they want to use a projection matrix.
  339. // column major order
  340. GLfloat mProjection[16] =
  341. {
  342. 1, 0, 0, 0,
  343. 0, 1, 0, 0,
  344. 0, 0, 1, 0,
  345. 0, 0, 0, 1
  346. };
  347. //InitProjectionMatricesHelper(m_ProgramRGBA, mProjection);
  348. //int i = glGetUniformLocation(uProgram, "matProjection");
  349. int i = m_pShaderProgram->uniformLocation("matProjection");
  350. //GL_ASSERT("glGetUniformLocation matProjection");
  351. glUniformMatrix4fv(i,
  352. 1, // 1 matrix
  353. GL_FALSE, // column major order
  354. mProjection);
  355. //GL_ASSERT("glUniformMatrix4fv (matProjection)");
  356. };
  357. void FSGLVideoWidget::InitViewMatrices(void){
  358. // NOW setup view matrices
  359. // "column major order" which means that the rows and columns are swapped.
  360. // fZ is actually in the 4th columm, and 2nd row (not the 3rd column , 4th row as it may appear)
  361. GLfloat mView[16] =
  362. {
  363. 1, 0, 0, 0,
  364. 0, 1, 0, 0,
  365. 0, 0, 1, 0,
  366. 0, 0, 0, 1
  367. };
  368. //InitViewMatricesHelper(m_ProgramRGBA, mView);
  369. int i = m_pShaderProgram->uniformLocation("matView");
  370. //GL_ASSERT("glGetUniformLocation matView");
  371. glUniformMatrix4fv(i,
  372. 1, // 1 matrix
  373. GL_FALSE, // column major order
  374. mView);
  375. //GL_ASSERT("glUniformMatrix4fv");
  376. };
  377. void FSGLVideoWidget::initializeGL(){
  378. initializeOpenGLFunctions();
  379. InitTextures();
  380. // создадим VAO и VBO для рендера полноэкранного прямоугольника
  381. // Use a vertex buffer object. Client-side pointers are old-school and should be avoided.
  382. m_vbo.create();
  383. m_vbo.bind();
  384. // For the cube all the data belonging to the texture coordinates and
  385. // normals is placed separately, after the vertices. Here, for the Qt logo,
  386. // let's do something different and potentially more efficient: create a
  387. // properly interleaved data set.
  388. //fsqVertex fsqVertices[6];
  389. InitShaders();
  390. m_vbo.allocate(fsqVertices, 6 * sizeof(fsqVertex));
  391. m_vbo.release();
  392. glGenBuffers(1, &fsqVBO);
  393. // заполняем VBO вершинными данными
  394. glBindBuffer(GL_ARRAY_BUFFER, fsqVBO);
  395. glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(fsqVertex), fsqVertices, GL_STATIC_DRAW);
  396. // задаем параметры расположения вершинных атрибутов в VBO
  397. glVertexAttribPointer(GL_VERTEX_ARRAY_BUFFER_BINDING, 3, GL_FLOAT, GL_FALSE, sizeof(fsqVertex), GL_OFFSET_(0));
  398. glEnableVertexAttribArray(GL_VERTEX_ARRAY_BUFFER_BINDING);
  399. glVertexAttribPointer(GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING, 2, GL_FLOAT, GL_FALSE, sizeof(fsqVertex), GL_OFFSET_(3 * sizeof(GLfloat)));
  400. glEnableVertexAttribArray(GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING);
  401. };
  402. //void FSGLVideoWidget::renderLater(){
  403. // if (!m_bUpdateIsPending) {
  404. // m_bUpdateIsPending = true;
  405. // QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
  406. // LOG("Delayed Rendering Request");
  407. // }
  408. //}
  409. void FSGLVideoWidget::paintGL(){
  410. QPainter painter;
  411. m_pWidgetProtectorMutex->lock();
  412. painter.begin(this);
  413. painter.beginNativePainting();
  414. // clear to a black frame
  415. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  416. // clear to black
  417. glClearColor(1, 0.5, 0.1, 1);
  418. glFrontFace(GL_CW);
  419. glCullFace(GL_FRONT);
  420. glEnable(GL_CULL_FACE);
  421. //glEnable(GL_DEPTH_TEST);
  422. glEnable(GL_TEXTURE_2D);
  423. //Remove any previous transformations
  424. //glLoadIdentity();
  425. //Move to rendering point
  426. //glTranslatef(-m_nSourceWidth / 4.f, -m_nSourceHeight / 4.f, 0.f);
  427. // устанавливаем в качестве активного дефолтный FBO
  428. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  429. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  430. m_pShaderProgram->bind();
  431. m_pShaderProgram->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
  432. m_pShaderProgram->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
  433. // устанавливаем текстуру сцены в 0-вой текстурный юнит
  434. glActiveTexture(GL_TEXTURE0);
  435. glBindTexture(GL_TEXTURE_2D, m_nTextureID);
  436. m_pShaderProgram->setUniformValue("colorTexture", 0);
  437. // выводим полноэкранный прямоугольник на экран
  438. glViewport(0, 0, width(), height());
  439. m_vbo.bind();
  440. glDrawArrays(GL_TRIANGLES, 0, 6);
  441. m_pShaderProgram->release();
  442. /*
  443. glBindTexture(GL_TEXTURE_2D, m_nTextureID );
  444. glBegin(GL_QUADS);
  445. glTexCoord2f(0.f, 0.f); glVertex2f(0.f, 0.f);
  446. glTexCoord2f(1.f, 0.f); glVertex2f(m_nSourceWidth/2, 0.f);
  447. glTexCoord2f(1.f, 1.f); glVertex2f(m_nSourceWidth/2, m_nSourceHeight/2);
  448. glTexCoord2f(0.f, 1.f); glVertex2f(0.f, m_nSourceHeight/2);
  449. glEnd();
  450. /*
  451. glBindTexture(GL_TEXTURE_2D, m_nTextureID );
  452. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  453. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  454. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  455. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  456. glActiveTexture(GL_TEXTURE0);
  457. glBindTexture(GL_TEXTURE_2D, m_nTextureID);
  458. //Render textured quad
  459. glBegin(GL_QUADS);
  460. glTexCoord2f(0.f, 0.f); glVertex2f(0.f, 0.f);
  461. glTexCoord2f(1.f, 0.f); glVertex2f(m_nSourceWidth, 0.f);
  462. glTexCoord2f(1.f, 1.f); glVertex2f(m_nSourceWidth, m_nSourceHeight);
  463. glTexCoord2f(0.f, 1.f); glVertex2f(0.f, m_nSourceHeight);
  464. glEnd();
  465. glDisable(GL_TEXTURE_2D);
  466. glDisable(GL_DEPTH_TEST);
  467. glDisable(GL_CULL_FACE);
  468. */
  469. glDisable(GL_TEXTURE_2D);
  470. //glDisable(GL_DEPTH_TEST);
  471. glDisable(GL_CULL_FACE);
  472. painter.endNativePainting();
  473. painter.setPen(Qt::black);
  474. painter.drawText(20, 40, " Hobo Hobo paintGL calls / s");
  475. painter.end();
  476. m_fAngle += 1.0f;
  477. update();
  478. m_pWidgetProtectorMutex->unlock();
  479. }
  480. void FSGLVideoWidget::resizeGL(int, int){
  481. }
  482. void FSGLVideoWidget::exposeEvent(QExposeEvent *){
  483. LOG_ERROR("Expose EVT!");
  484. }
  485. //! [2]
  486. //void FSGLVideoWidget::paintEvent(QPaintEvent *evt){
  487. // renderNow();
  488. //}
  489. //! [3]
  490. /*
  491. void FSGLVideoWidget::renderNow(){
  492. return;
  493. m_pWidgetProtectorMutex->lock();
  494. try{
  495. if (m_nMenuActivityCounter)m_nMenuActivityCounter--;
  496. else if (m_nManualControlMode & CD_MANUAL){
  497. m_nManualControlMode = CD_AUTO;
  498. releaseKeyboard();
  499. };
  500. QPainter painter(this);
  501. //painter.setRenderHint(QPainter::Antialiasing);
  502. painter.setRenderHint((QPainter::RenderHint)7);// QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing
  503. //if( !painter.isActive() ) painter.begin(this);
  504. render(&painter);
  505. //if (pa inter.isActive()) painter.end();
  506. }catch (std::exception& e){
  507. LOG_ERROR("RenderNow exception!");
  508. };
  509. m_pWidgetProtectorMutex->unlock();
  510. }
  511. */
  512. //! [3]
  513. //! [4]
  514. void FSGLVideoWidget::render(QPainter *painter){
  515. if( painter != NULL ){
  516. try{
  517. char buffer[64];
  518. sprintf(buffer, "Coords: %d %d Key: %d", m_nMouseX, m_nMouseY, m_nKeyCode);
  519. painter->save();
  520. painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap);
  521. QPen BgPen(Qt::SolidPattern, 2);
  522. BgPen.setColor(Qt::black);
  523. QPen FgPen(Qt::SolidPattern, 0);
  524. FgPen.setColor(Qt::yellow);
  525. painter->setPen(BgPen);
  526. painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
  527. painter->setPen(FgPen);
  528. painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
  529. if (m_pCamera != NULL){
  530. if (m_nManualControlMode & CD_MANUAL){
  531. QRect ManualRect(10, height() - m_pManualMode->height() - 10, m_pManualMode->width(), m_pManualMode->height());
  532. if (m_nCameraCaps & OC_TILT_CONTROLL){
  533. QRect UpRect(
  534. (width() - m_pPixUp->width()) / 2,
  535. (height() / 2) - m_pPixUp->height() - 10,
  536. m_pPixUp->width(),
  537. m_pPixUp->height());
  538. QRect DownRect(
  539. (width() - m_pPixDown->width()) / 2,
  540. (height() / 2) + 10,
  541. m_pPixDown->width(),
  542. m_pPixDown->height());
  543. if (m_nManualControlMode & CD_UP) {
  544. painter->fillRect(UpRect, QColor(255, 255, 255, 63));
  545. }
  546. else {
  547. painter->drawRect(UpRect);
  548. };
  549. if (m_nManualControlMode & CD_DOWN){
  550. painter->fillRect(DownRect, QColor(255, 255, 255, 63));
  551. }
  552. else {
  553. painter->drawRect(DownRect);
  554. };
  555. painter->drawPixmap(UpRect, *m_pPixUp);
  556. painter->drawPixmap(DownRect, *m_pPixDown);
  557. };
  558. if (m_nCameraCaps & OC_PAN_CONTROLL){
  559. QRect LeftRect(
  560. (width() / 2) - m_pPixLeft->width() - 10 - (m_pPixLeft->width() / 2),
  561. (height() / 2) - (m_pPixLeft->height() / 2),
  562. m_pPixLeft->width(),
  563. m_pPixLeft->height());
  564. QRect RightRect(
  565. (width() / 2) + 10 + (m_pPixRight->width() / 2),
  566. (height() / 2) - (m_pPixRight->height() / 2),
  567. m_pPixRight->width(),
  568. m_pPixRight->height());
  569. if (m_nManualControlMode & CD_RIGHT){
  570. painter->fillRect(RightRect, QColor(255, 255, 255, 63));
  571. }
  572. else {
  573. painter->drawRect(RightRect);
  574. };
  575. if (m_nManualControlMode & CD_LEFT){
  576. painter->fillRect(LeftRect, QColor(255, 255, 255, 63));
  577. }
  578. else {
  579. painter->drawRect(LeftRect);
  580. };
  581. painter->drawPixmap(LeftRect, *m_pPixLeft);
  582. painter->drawPixmap(RightRect, *m_pPixRight);
  583. };
  584. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  585. QRect WideRect(
  586. 25,
  587. 25,
  588. m_pManualMode->width(),
  589. m_pManualMode->height());
  590. QRect TeleRect(
  591. width() - m_pPixTele->height() - 25,
  592. 25,
  593. m_pPixTele->width(),
  594. m_pPixTele->height());
  595. if (m_nManualControlMode & CD_WIDE){
  596. painter->fillRect(WideRect, QColor(255, 255, 255, 63));
  597. }
  598. else {
  599. painter->drawRect(WideRect);
  600. };
  601. if (m_nManualControlMode & CD_TELE){
  602. painter->fillRect(TeleRect, QColor(255, 255, 255, 63));
  603. }
  604. else {
  605. painter->drawRect(TeleRect);
  606. };
  607. painter->drawPixmap(WideRect, *m_pPixWide);
  608. painter->drawPixmap(TeleRect, *m_pPixTele);
  609. };
  610. painter->fillRect(ManualRect, QColor(255, 255, 255, 63));
  611. painter->drawPixmap(ManualRect, *m_pManualMode);
  612. };
  613. };
  614. //char buffer[64];
  615. //sprintf(buffer, "Coords: %dx%d %d %d ? %d %d", pos().x(), pos().y(), width(), height(), m_nMouseX, m_nMouseY);
  616. //painter->save();
  617. //painter->fillRect(QRect(0, 0, width(), height()), Qt::green);
  618. //int x, y;
  619. //x = width();
  620. //y = height();
  621. //painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap);
  622. //QPen BgPen(Qt::SolidPattern,2);
  623. //BgPen.setColor(Qt::black);
  624. //QPen FgPen(Qt::SolidPattern,0);
  625. //FgPen.setColor(Qt::yellow);
  626. //painter->setPen(BgPen);
  627. //painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
  628. //painter->setPen(FgPen);
  629. //painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline , buffer);
  630. painter->restore();
  631. }catch (std::exception& e){
  632. LOG_ERROR("Render exception!");
  633. };
  634. };
  635. }
  636. bool FSGLVideoWidget::init(void){
  637. LOG("init.");
  638. m_pPixmap = new QPixmap(m_nSourceWidth, m_nSourceHeight);// , QImage::Format_RGBA8888);
  639. if( m_pPixmap==NULL ) return false;
  640. m_pManualMode = new QPixmap(":pictures/appbar.controller.snes.png", "PNG");
  641. m_pPixUp = new QPixmap(":pictures/appbar.chevron.up.png", "PNG");
  642. m_pPixDown = new QPixmap(":pictures/appbar.chevron.down.png", "PNG");
  643. m_pPixLeft = new QPixmap(":pictures/appbar.chevron.left.png", "PNG");
  644. m_pPixRight = new QPixmap(":pictures/appbar.chevron.right.png", "PNG");
  645. m_pPixWide = new QPixmap(":pictures/appbar.arrow.expand.png", "PNG");
  646. m_pPixTele = new QPixmap(":pictures/appbar.arrow.collapsed.png", "PNG");
  647. m_pTimer = new QTimer();
  648. if (m_pTimer != NULL){
  649. connect(m_pTimer, SIGNAL(timeout()), this, SLOT(update()));
  650. m_pTimer->start(40);
  651. };
  652. m_eWidgetAspectRatio = detectAspectRatio(width(), width());
  653. m_pPixmap->fill(Qt::black);
  654. m_pPropertiesProtectorMutex->lock();
  655. m_pPropertiesProtectorMutex->unlock();
  656. return true;
  657. };
  658. FSGLVideoWidget::~FSGLVideoWidget(){
  659. if (m_pCamera)m_pCamera->setViewer(NULL);
  660. if (m_pTimer)m_pTimer->stop();
  661. delete( m_pPropertiesProtectorMutex );
  662. delete( m_pWidgetProtectorMutex );
  663. };
  664. void FSGLVideoWidget::setSourceSize(int sizeX, int sizeY){
  665. m_pWidgetProtectorMutex->lock();
  666. m_nSourceWidth = sizeX;
  667. m_nSourceHeight = sizeY;
  668. m_eStreamAspectRatio = detectAspectRatio(m_nSourceWidth, m_nSourceHeight);
  669. if (m_pPixmap != NULL)delete (m_pPixmap);
  670. m_pPixmap = new QPixmap(m_nSourceWidth, m_nSourceHeight);
  671. m_pWidgetProtectorMutex->unlock();
  672. };
  673. void FSGLVideoWidget::pushFrame(cv::Mat *Frame){
  674. if( Frame != NULL ){
  675. try{
  676. assert(Frame->isContinuous());
  677. if (Frame->cols != m_nSourceWidth || Frame->rows != m_nSourceHeight) setSourceSize(Frame->cols, Frame->rows);
  678. m_pWidgetProtectorMutex->lock();
  679. m_mOutput = Frame;
  680. // QPainter dbPainter(m_pPixmap);
  681. /*if (m_eStreamAspectRatio != m_eWidgetAspectRatio){
  682. int tHeight;
  683. int tOffset;
  684. if (m_eWidgetAspectRatio == FAR_16X9){
  685. tHeight = (m_mOutput->cols / 16) * 9;
  686. tOffset = (m_mOutput->rows - tHeight) >> 1;
  687. dbPainter.drawImage(QRect(0, 0, width(), height()),
  688. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  689. QRect(0, tOffset, m_mOutput->cols, tHeight));
  690. }
  691. else {
  692. if (m_eWidgetAspectRatio == FAR_5X4){
  693. tHeight = (m_mOutput->cols / 5) * 4;
  694. tOffset = (m_mOutput->rows - tHeight) >> 1;
  695. dbPainter.drawImage(QRect(0, 0, width(), height()),
  696. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  697. QRect(0, tOffset, m_mOutput->cols, tHeight));
  698. }
  699. else {
  700. tHeight = (m_mOutput->cols / 4) * 3;
  701. tOffset = (m_mOutput->rows - tHeight) >> 1;
  702. dbPainter.drawImage(QRect(0, 0, width(), height()),
  703. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  704. QRect(0, tOffset, m_mOutput->cols, tHeight));
  705. };
  706. };
  707. }
  708. else {
  709. */
  710. //dbPainter.drawImage(0, 0, QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888));
  711. m_iFrame = QGLWidget::convertToGLFormat(QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888));
  712. //};
  713. m_pWidgetProtectorMutex->unlock();
  714. }
  715. catch (std::exception& e){
  716. LOG_ERROR("Exception catched:");
  717. LOG_ERROR(e.what());
  718. };
  719. m_pPropertiesProtectorMutex->unlock();
  720. //delete(Frame);
  721. };
  722. };
  723. #undef LOG
  724. #undef LOG_ERROR