source: trunk/FACT++/gui/Q3DCameraWidget.cc@ 14559

Last change on this file since 14559 was 14304, checked in by tbretz, 12 years ago
Added missing include of glu-.h
File size: 7.3 KB
Line 
1/*
2 * Q3DCameraWidget.cc
3 *
4 * Created on: Aug 26, 2011
5 * Author: lyard
6 */
7#include "Q3DCameraWidget.h"
8#include <math.h>
9#include <sstream>
10
11#include <GL/glu.h>
12
13 Q3DCameraWidget::Q3DCameraWidget(QWidget* pparent) : BasicGlCamera(pparent),
14 currentLoc()
15 {
16 _data.resize(432000);
17 _colorR.resize(432000);
18 _colorG.resize(432000);
19 _colorB.resize(432000);
20 _x.resize(432000);
21 _y.resize(432000);
22 _z.resize(432000);
23 for (int i=0;i<432000;i++)
24 {
25 _data[i] = 0;
26 _colorR[i] = 0;
27 _colorG[i] = 0;
28 _colorB[i] = 0;
29 _x[i] = 0;
30 _y[i] = 0;
31 _z[i] = 0;
32 }
33 }
34 Q3DCameraWidget::~Q3DCameraWidget()
35 {
36
37 }
38 void Q3DCameraWidget::timedUpdate()
39 {
40 updateGL();
41 }
42
43 int rotation =130;
44 int rotationy = 30;
45 float transZ = 0;
46 void Q3DCameraWidget::mousePressEvent(QMouseEvent* cEvent)
47 {
48
49 if (cEvent->buttons() & Qt::LeftButton)
50 {
51 rotationy = -60 + (cEvent->pos().y()/(float)height())*120.f;
52 rotation = 130 + (cEvent->pos().x()/(float)width())*180.f;
53 }
54 else if (cEvent->buttons() & Qt::RightButton)
55 {
56 if (cEvent->pos().y() > height()/2)
57 transZ -= 0.5;
58 else
59 transZ += 0.5;
60 }
61 updateGL();
62 }
63 void Q3DCameraWidget::mouseDoubleClickEvent(QMouseEvent *cEvent)
64 {
65
66 }
67 void Q3DCameraWidget::mouseMoveEvent(QMouseEvent *cEvent)
68 {
69 if (cEvent->buttons() & Qt::LeftButton) {
70 mousePressEvent(cEvent);
71 }
72
73 }
74 void Q3DCameraWidget::paintGL()
75 {
76 makeCurrent();
77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78 glLoadIdentity();
79
80 glTranslatef(-0.0,-0.0, -5);
81 glTranslatef(0,0,(float)(transZ));
82 glRotatef((float)rotationy,1.0,0.0,0.0);
83 glRotatef((float)rotation, 0.0, 1.0, 0.0);
84
85 glColor3f(1.0,0.0,0.0);
86
87 glBegin(GL_TRIANGLES);
88 for (int i=0;i<1439;i++)
89 {
90 for (int j=6;j<250;j++)
91 {
92 //get the 4 vertices that we need for drawing this patch
93 glColor3f(_colorR[i*300+j],_colorG[i*300+j],_colorB[i*300+j]);
94 glVertex3f(_x[i*300+j], _y[i*300+j], _z[i*300+j]);
95 glColor3f(_colorR[i*300+j+1],_colorG[i*300+j+1],_colorB[i*300+j+1]);
96 glVertex3f(_x[i*300+j+1], _y[i*300+j+1], _z[i*300+j+1]);
97 glColor3f(_colorR[(i+1)*300+j],_colorG[(i+1)*300+j],_colorB[(i+1)*300+j]);
98 glVertex3f(_x[(i+1)*300+j], _y[(i+1)*300+j], _z[(i+1)*300+j]);
99
100 glColor3f(_colorR[i*300+j+1],_colorG[i*300+j+1],_colorB[i*300+j+1]);
101 glVertex3f(_x[i*300+j+1], _y[i*300+j+1], _z[i*300+j+1]);
102 glColor3f(_colorR[(i+1)*300+j+1],_colorG[(i+1)*300+j+1],_colorB[(i+1)*300+j+1]);
103 glVertex3f(_x[(i+1)*300+j+1], _y[(i+1)*300+j+1], _z[(i+1)*300+j+1]);
104 glColor3f(_colorR[(i+1)*300+j],_colorG[(i+1)*300+j],_colorB[(i+1)*300+j]);
105 glVertex3f(_x[(i+1)*300+j], _y[(i+1)*300+j], _z[(i+1)*300+j]);
106
107 }
108 }
109 glEnd();
110
111 }
112 void Q3DCameraWidget::calculateColorsAndPositions()
113 {
114 short min = 10000;
115 short max = -10000;
116 for (int k=0;k<1440;k++)
117 for (int j=6;j<251;j++)
118 {
119 int i = k*300+j;
120 if (_data[i] < min)
121 min = _data[i];
122 if (_data[i] > max)
123 max = _data[i];
124 }
125 float span = max - min;
126
127
128 //max should be at one, min at -1
129
130 for (int i=0;i<1440;i++)
131 {
132 for (int j=6;j<251;j++)
133 {
134 _x[i*300+j] = -1 + (2.f*i)/1440.f;
135 _y[i*300+j] = -0.5 + 1.0f*(_data[i*300+j] - min)/span;
136 _z[i*300+j] = -1+(2.f*j)/300.f;
137 float value = (_data[i*300 + j] - min)/span;
138 if (value < 0.33)
139 {
140 _colorR[i*300+j] = 0;
141 _colorG[i*300+j] = 0;
142 _colorB[i*300+j] = value/0.33;
143 }
144 if (value >= 0.33 && value <= 0.66)
145 {
146 _colorR[i*300+j] = 0;
147 _colorG[i*300+j] = (value-0.33)/0.33;
148 _colorB[i*300+j] = 1 - ((value-0.33)/0.33);
149 }
150 if (value > 0.66)
151 {
152 _colorR[i*300+j] = (value-0.66)/0.33;
153 _colorG[i*300+j] = 1 - ((value-0.66)/0.33);
154 _colorB[i*300+j] = 0;
155
156 }
157 }
158 }
159
160
161
162 }
163 void Q3DCameraWidget::setData(float* ddata)
164 {
165 for (int i=0;i<1440;i++)
166 for (int j=0;j<300;j++)
167 _data[i*300+j] = (short)(ddata[i*300 + j]);
168 calculateColorsAndPositions();
169 if (isVisible())
170 updateGL();
171
172 }
173 void Q3DCameraWidget::setData(short* ddata)
174 {
175 for (int i=0;i<1440;i++)
176 for (int j=0;j<300;j++)
177 _data[i*300+j] = ddata[i* 300 + j];
178 calculateColorsAndPositions();
179 if (isVisible())
180 updateGL();
181
182 }
183 void Q3DCameraWidget::drawCameraBody()
184 {
185 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
186
187 GLfloat color[4] = {0.8f, 1.f, 1.f, 1.f};
188 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
189 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0f);
190 gluCylinder( gluNewQuadric(),
191 0.62,
192 0.62,
193 1.83,
194 30,
195 2 );
196 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
197
198
199
200 }
201
202 void Q3DCameraWidget::initializeGL()
203 {
204 qglClearColor(QColor(25,25,38));
205
206 glShadeModel(GL_SMOOTH);
207 glEnable(GL_DEPTH_TEST);
208 glDepthFunc(GL_LESS);
209 glDisable(GL_LIGHTING);
210// glEnable(GL_LIGHTING);
211// glEnable(GL_LIGHT0);
212// glEnable(GL_AUTO_NORMAL);
213 glDisable(GL_CULL_FACE);
214// glCullFace(GL_FRONT);
215
216 glEnable(GL_POLYGON_SMOOTH);
217 glEnable(GL_BLEND);
218 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
219 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
220 }
221 void Q3DCameraWidget::resizeGL(int cWidth, int cHeight)
222 {
223 glViewport(0,0,cWidth, cHeight);
224 glMatrixMode(GL_PROJECTION);
225 glLoadIdentity();
226 GLfloat windowRatio = (float)cWidth/(float)cHeight;
227 if (windowRatio < 1)
228 {
229// windowRatio = 1.0f/windowRatio;
230 gluPerspective(40.f, windowRatio, 1, 100);
231// gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
232 pixelSize = 2*viewSize/(float)cWidth;
233 shownSizex = 2*viewSize;
234 shownSizey = 2*viewSize*windowRatio;
235 }
236 else
237 {
238 gluPerspective(40.f, windowRatio,1, 8);
239// gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
240 pixelSize = 2*viewSize/(float)cHeight;
241 shownSizex = 2*viewSize*windowRatio;
242 shownSizey = 2*viewSize;
243 }
244 glMatrixMode(GL_MODELVIEW);
245 }
Note: See TracBrowser for help on using the repository browser.