direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
RGBWindow.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This file is part of the OpenKinect Project. http://www.openkinect.org *
3  * *
4  * Copyright (c) 2010 individual OpenKinect contributors. *
5  * See the CONTRIB file for details. *
6  * *
7  * This file is part of direcs. *
8  * www.direcs.de *
9  * *
10  * direcs is free software: you can redistribute it and/or modify it *
11  * under the terms of the GNU General Public License as published *
12  * by the Free Software Foundation, version 3 of the License. *
13  * *
14  * direcs is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
21  * *
22  ***************************************************************************/
23 
24 #include "RGBWindow.h"
25 #include <iostream>
26 #include <QDebug>
27 #include <QKinect.h>
28 
29 
30 RGBWindow::RGBWindow(QWidget *_parent) : QGLWidget(_parent)
31 {
32 
33  // re-size the widget to that of the parent (in this case the GLFrame passed in on construction)
34  this->resize(_parent->size());
35 
36  // start the timer to re-draw as quick as possible
37  startTimer(50); // we slow that down cause of heavy CPU usage when this is 0 ms
38 
39  // create space for our image data
40  m_rgb.resize(640*480*3);
41 
42  // default to RGB mode
43  m_mode=0;
44 }
45 
46 
48 {
49  // This virtual function is called once before the first call to paintGL() or resizeGL(),
50  // and then once whenever the widget has been assigned a new QGLContext.
51  // This function should set up any required OpenGL context rendering flags, defining display lists, etc.
52 
53  glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background
54  // enable depth testing for drawing
55 
56  glEnable(GL_DEPTH_TEST);
57  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
58 
59  glClearDepth(1.0);
60 
61  // set the texture data
62  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
63  glEnable(GL_TEXTURE_2D);
64  glGenTextures(1, & m_rgbTexture);
65  glBindTexture(GL_TEXTURE_2D, m_rgbTexture);
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
68 }
69 
70 
71 void RGBWindow::resizeGL(int _w, int _h)
72 {
73  // This virtual function is called whenever the widget has been resized.
74  // The new size is passed in width and height.
75 
76  glViewport(0,0,_w,_h);
77  glMatrixMode(GL_PROJECTION);
78  glLoadIdentity();
79  glOrtho (0, 640, 480, 0, -1.0f, 1.0f);
80  glMatrixMode(GL_MODELVIEW);
81 }
82 
83 
85 {
86  // This virtual function is called whenever the widget needs to be painted.
87  // this is our main drawing routine
88 
89  // ok this is using immediate mode GL but will do for a sample
90  // see my other NGL versions which are much faster
91  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92 
93  QKinect *kinect=QKinect::instance();
94 
95  if (m_mode ==0)
96  {
97  // get RGB image
98  kinect->getRGB(m_rgb);
99  }
100  else if(m_mode == 1)
101  {
102  kinect->getDepth(m_rgb);
103  }
104 
105  glBindTexture(GL_TEXTURE_2D, m_rgbTexture);
106  glTexImage2D(GL_TEXTURE_2D, 0, 3, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, &m_rgb[0]);
107  glLoadIdentity();
108 
109  glEnable(GL_TEXTURE_2D);
110 
111  glBegin(GL_TRIANGLE_FAN);
112  glColor4f(255.0f, 255.0f, 255.0f, 255.0f);
113  glTexCoord2f(0, 0); glVertex3f(0,0,0);
114  glTexCoord2f(1, 0); glVertex3f(640,0,0);
115  glTexCoord2f(1, 1); glVertex3f(640,480,0);
116  glTexCoord2f(0, 1); glVertex3f(0,480,0);
117  glEnd();
118 }
119 
120 
121 void RGBWindow::mouseMoveEvent (QMouseEvent * _event)
122 {
123  updateGL();
124 }
125 
126 
127 void RGBWindow::mousePressEvent (QMouseEvent * _event)
128 {
129 }
130 
131 
132 void RGBWindow::mouseReleaseEvent (QMouseEvent * _event)
133 {
134  // this event is called when the mouse button is released
135  // we then set Rotate to false
136 }
137 
138 
139 void RGBWindow::timerEvent(QTimerEvent *_event)
140 {
141  // re-draw GL
142  updateGL();
143 }
144 
145 
147 {
148 }