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

Last change on this file since 14909 was 14793, checked in by lyard, 12 years ago
fixed warning multiple-display
File size: 8.0 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 _warningWritten = false;
34 }
35 Q3DCameraWidget::~Q3DCameraWidget()
36 {
37
38 }
39 void Q3DCameraWidget::timedUpdate()
40 {
41 updateGL();
42 }
43
44 int rotation =130;
45 int rotationy = 30;
46 float transZ = 0;
47 void Q3DCameraWidget::mousePressEvent(QMouseEvent* cEvent)
48 {
49
50 if (cEvent->buttons() & Qt::LeftButton)
51 {
52 rotationy = -60 + (cEvent->pos().y()/(float)height())*120.f;
53 rotation = 130 + (cEvent->pos().x()/(float)width())*180.f;
54 }
55 else if (cEvent->buttons() & Qt::RightButton)
56 {
57 if (cEvent->pos().y() > height()/2)
58 transZ -= 0.5;
59 else
60 transZ += 0.5;
61 }
62 updateGL();
63 }
64 void Q3DCameraWidget::mouseDoubleClickEvent(QMouseEvent *cEvent)
65 {
66
67 }
68 void Q3DCameraWidget::mouseMoveEvent(QMouseEvent *cEvent)
69 {
70 if (cEvent->buttons() & Qt::LeftButton) {
71 mousePressEvent(cEvent);
72 }
73
74 }
75 void Q3DCameraWidget::paintGL()
76 {
77 makeCurrent();
78 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79 glLoadIdentity();
80
81 glTranslatef(-0.0,-0.0, -5);
82 glTranslatef(0,0,(float)(transZ));
83 glRotatef((float)rotationy,1.0,0.0,0.0);
84 glRotatef((float)rotation, 0.0, 1.0, 0.0);
85
86 glColor3f(1.0,0.0,0.0);
87
88 glBegin(GL_TRIANGLES);
89 for (int i=0;i<1439;i++)
90 {
91 for (int j=6;j<250;j++)
92 {
93 //get the 4 vertices that we need for drawing this patch
94 glColor3f(_colorR[i*300+j],_colorG[i*300+j],_colorB[i*300+j]);
95 glVertex3f(_x[i*300+j], _y[i*300+j], _z[i*300+j]);
96 glColor3f(_colorR[i*300+j+1],_colorG[i*300+j+1],_colorB[i*300+j+1]);
97 glVertex3f(_x[i*300+j+1], _y[i*300+j+1], _z[i*300+j+1]);
98 glColor3f(_colorR[(i+1)*300+j],_colorG[(i+1)*300+j],_colorB[(i+1)*300+j]);
99 glVertex3f(_x[(i+1)*300+j], _y[(i+1)*300+j], _z[(i+1)*300+j]);
100
101 glColor3f(_colorR[i*300+j+1],_colorG[i*300+j+1],_colorB[i*300+j+1]);
102 glVertex3f(_x[i*300+j+1], _y[i*300+j+1], _z[i*300+j+1]);
103 glColor3f(_colorR[(i+1)*300+j+1],_colorG[(i+1)*300+j+1],_colorB[(i+1)*300+j+1]);
104 glVertex3f(_x[(i+1)*300+j+1], _y[(i+1)*300+j+1], _z[(i+1)*300+j+1]);
105 glColor3f(_colorR[(i+1)*300+j],_colorG[(i+1)*300+j],_colorB[(i+1)*300+j]);
106 glVertex3f(_x[(i+1)*300+j], _y[(i+1)*300+j], _z[(i+1)*300+j]);
107
108 }
109 }
110 glEnd();
111
112 }
113 void Q3DCameraWidget::calculateColorsAndPositions()
114 {
115 short min = 10000;
116 short max = -10000;
117 for (int k=0;k<1440;k++)
118 for (int j=6;j<251;j++)
119 {
120 int i = k*300+j;
121 if (_data[i] < min)
122 min = _data[i];
123 if (_data[i] > max)
124 max = _data[i];
125 }
126 float span = max - min;
127
128
129 //max should be at one, min at -1
130
131 for (int i=0;i<1440;i++)
132 {
133 for (int j=6;j<251;j++)
134 {
135 _x[i*300+j] = -1 + (2.f*i)/1440.f;
136 _y[i*300+j] = -0.5 + 1.0f*(_data[i*300+j] - min)/span;
137 _z[i*300+j] = -1+(2.f*j)/300.f;
138 float value = (_data[i*300 + j] - min)/span;
139 if (value < 0.33)
140 {
141 _colorR[i*300+j] = 0;
142 _colorG[i*300+j] = 0;
143 _colorB[i*300+j] = value/0.33;
144 }
145 if (value >= 0.33 && value <= 0.66)
146 {
147 _colorR[i*300+j] = 0;
148 _colorG[i*300+j] = (value-0.33)/0.33;
149 _colorB[i*300+j] = 1 - ((value-0.33)/0.33);
150 }
151 if (value > 0.66)
152 {
153 _colorR[i*300+j] = (value-0.66)/0.33;
154 _colorG[i*300+j] = 1 - ((value-0.66)/0.33);
155 _colorB[i*300+j] = 0;
156
157 }
158 }
159 }
160
161
162
163 }
164 void Q3DCameraWidget::setData(float* ddata)
165 {
166 if (!_warningWritten)
167 {
168 _warningWritten = true;
169 cout << "Info : 3D plotter disabled. requires more work so that less than 300 slices per pixel can be loaded" << endl;
170 cout << "Contact Etienne (etienne.lyard@unige.ch) for more information." << endl;
171 }
172 //disabled for now as I am working with 150 slices only
173/* for (int i=0;i<1440;i++)
174 for (int j=0;j<300;j++)
175 _data[i*300+j] = (short)(ddata[i*300 + j]);
176 calculateColorsAndPositions();
177 if (isVisible())
178 updateGL();
179*/
180 }
181 void Q3DCameraWidget::setData(short* ddata)
182 {
183 if (!_warningWritten)
184 {
185 _warningWritten = true;
186 cout << "Info : 3D plotter disabled. requires more work so that less than 300 slices per pixel can be loaded" << endl;
187 cout << "Contact Etienne (etienne.lyard@unige.ch) for more information." << endl;
188 }
189 /* for (int i=0;i<1440;i++)
190 for (int j=0;j<300;j++)
191 _data[i*300+j] = ddata[i* 300 + j];
192 calculateColorsAndPositions();
193 if (isVisible())
194 updateGL();
195*/
196 }
197 void Q3DCameraWidget::drawCameraBody()
198 {
199 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
200
201 GLfloat color[4] = {0.8f, 1.f, 1.f, 1.f};
202 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
203 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0f);
204 gluCylinder( gluNewQuadric(),
205 0.62,
206 0.62,
207 1.83,
208 30,
209 2 );
210 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
211
212
213
214 }
215
216 void Q3DCameraWidget::initializeGL()
217 {
218 qglClearColor(QColor(25,25,38));
219
220 glShadeModel(GL_SMOOTH);
221 glEnable(GL_DEPTH_TEST);
222 glDepthFunc(GL_LESS);
223 glDisable(GL_LIGHTING);
224// glEnable(GL_LIGHTING);
225// glEnable(GL_LIGHT0);
226// glEnable(GL_AUTO_NORMAL);
227 glDisable(GL_CULL_FACE);
228// glCullFace(GL_FRONT);
229
230 glEnable(GL_POLYGON_SMOOTH);
231 glEnable(GL_BLEND);
232 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
233 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
234 }
235 void Q3DCameraWidget::resizeGL(int cWidth, int cHeight)
236 {
237 glViewport(0,0,cWidth, cHeight);
238 glMatrixMode(GL_PROJECTION);
239 glLoadIdentity();
240 GLfloat windowRatio = (float)cWidth/(float)cHeight;
241 if (windowRatio < 1)
242 {
243// windowRatio = 1.0f/windowRatio;
244 gluPerspective(40.f, windowRatio, 1, 100);
245// gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
246 pixelSize = 2*viewSize/(float)cWidth;
247 shownSizex = 2*viewSize;
248 shownSizey = 2*viewSize*windowRatio;
249 }
250 else
251 {
252 gluPerspective(40.f, windowRatio,1, 8);
253// gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
254 pixelSize = 2*viewSize/(float)cHeight;
255 shownSizex = 2*viewSize*windowRatio;
256 shownSizey = 2*viewSize;
257 }
258 glMatrixMode(GL_MODELVIEW);
259 }
Note: See TracBrowser for help on using the repository browser.