-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLighted3DViewerWidget.cpp
202 lines (183 loc) · 6.72 KB
/
Lighted3DViewerWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* $Id$ */
/*
*
* Copyright (C) 2001-2004 EPFL (Swiss Federal Institute of Technology,
* Lausanne) This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* In addition, as a special exception, EPFL gives permission to link
* the code of this program with the Qt non-commercial edition library
* (or with modified versions of Qt non-commercial edition that use the
* same license as Qt non-commercial edition), and distribute linked
* combinations including the two. You must obey the GNU General
* Public License in all respects for all of the code used other than
* Qt non-commercial edition. If you modify this file, you may extend
* this exception to your version of the file, but you are not
* obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Authors : Nicolas Aspert, Diego Santa-Cruz and Davy Jacquet
*
* Web site : http://mesh.epfl.ch
*
* Reference :
* "MESH : Measuring Errors between Surfaces using the Hausdorff distance"
* in Proceedings of IEEE Intl. Conf. on Multimedia and Expo (ICME) 2002,
* vol. I, pp. 705-708, available on http://mesh.epfl.ch
*
*/
#include <Lighted3DViewerWidget.h>
#include <qmessagebox.h>
#include <qapplication.h>
#include <geomutils.h>
Lighted3DViewerWidget::Lighted3DViewerWidget(struct model_error *model_err,
QWidget *parent, const char *name)
: Basic3DViewerWidget(model_err->mesh, parent, name)
{
model = model_err;
two_sided_material = 1;
not_orientable_warned = 0;
}
// slot to toggle light
void Lighted3DViewerWidget::setLight(bool state)
{
GLboolean light_state;
struct model *r_model = model->mesh;
if (!getGLInitialized()) {
fprintf(stderr,
"Lighted3DViewerWidget::setLight() called"
" before GL context is initialized!\n");
return;
}
// Get state from renderer
makeCurrent();
light_state = glIsEnabled(GL_LIGHTING);
if (light_state != !state) // harmless
printf("Mismatched state between qcbLight/light_state\n");
if (light_state==GL_FALSE){ // We are now switching to lighted mode
if (r_model->normals !=NULL){// Are these ones computed ?
glEnable(GL_LIGHTING);
} else { // Normals should have been computed
fprintf(stderr,"ERROR: normals were not computed!\n");
}
}
else if (light_state==GL_TRUE){// We are now switching to
// non-lighted mode
glDisable(GL_LIGHTING);
}
checkGLErrors("setLight()");
updateGL();
}
// Slot to switch between 1-sided and 2-sided material (for open meshes)
void Lighted3DViewerWidget::setTwoSidedMaterial(bool state)
{
if (state != (bool)two_sided_material) // harmless ...
printf("Mismatched state qcbTwoSide/two_sided_material\n");
two_sided_material = !two_sided_material;
if (getGLInitialized()) { // only update GL if already initialized
makeCurrent();
QApplication::setOverrideCursor(Qt::waitCursor);
rebuildList();
updateGL();
QApplication::restoreOverrideCursor();
}
}
// Slot to invert normals (directly in the model)
void Lighted3DViewerWidget::invertNormals(bool state)
{
GLboolean lightState=state;
struct model *r_model = model->mesh;
int i;
if (!getGLInitialized()) {
fprintf(stderr,"invertNormals() called before GL context is "
"initialized!\n");
return;
}
makeCurrent();
QApplication::setOverrideCursor(Qt::waitCursor);
lightState = glIsEnabled(GL_LIGHTING);
if (lightState == GL_TRUE && r_model->normals != NULL) {
for (i=0; i<r_model->num_vert; i++)
neg_v(&(r_model->normals[i]), &(r_model->normals[i]));
rebuildList();
updateGL();
}
QApplication::restoreOverrideCursor();
}
// Herited from Basic3DViewerWidget
void Lighted3DViewerWidget::initializeGL()
{
Basic3DViewerWidget::initializeGL();
if (!this->model->info->closed)
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
if (model->mesh->normals !=NULL){// Are these ones computed ?
if (!this->model->info->orientable && !not_orientable_warned) {
not_orientable_warned = 1;
QMessageBox::warning(this,"Not orientable model",
"Model is not orientable.\n"
"Surface shading is probably incorrect.");
}
glEnable(GL_LIGHTING);
} else {// Normals should have been computed
fprintf(stderr,"ERROR: normals were not computed!\n");
}
}
// Herited from Basic3DViewerWidget
void Lighted3DViewerWidget::rebuildList()
{
// Surface material characteristics for lighted mode
static const float front_amb_mat[4] = {0.11f, 0.06f, 0.11f, 1.0f};
static const float front_diff_mat[4] = {0.43f, 0.47f, 0.54f, 1.0f};
static const float front_spec_mat[4] = {0.33f, 0.33f, 0.52f, 1.0f};
static const float front_mat_shin = 10.0f;
static const float back_amb_mat[4] = {0.3f, 0.3f, 0.3f, 1.0f};
static const float back_diff_mat[4] = {0.5f, 0.5f, 0.5f, 1.0f};
static const float back_spec_mat[4] = {0.33f, 0.33f, 0.52f, 1.0f};
static const float back_mat_shin = 10.0f;
if (two_sided_material) {
glMaterialfv(GL_FRONT,GL_AMBIENT,front_amb_mat);
glMaterialfv(GL_FRONT,GL_DIFFUSE,front_diff_mat);
glMaterialfv(GL_FRONT,GL_SPECULAR,front_spec_mat);
glMaterialf(GL_FRONT,GL_SHININESS,front_mat_shin);
glMaterialfv(GL_BACK,GL_AMBIENT,back_amb_mat);
glMaterialfv(GL_BACK,GL_DIFFUSE,back_diff_mat);
glMaterialfv(GL_BACK,GL_SPECULAR,back_spec_mat);
glMaterialf(GL_BACK,GL_SHININESS,back_mat_shin);
} else {
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,front_amb_mat);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,front_diff_mat);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,front_spec_mat);
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,front_mat_shin);
}
Basic3DViewerWidget::rebuildList();
}
// Herited from Basic3DViewerWidget
void Lighted3DViewerWidget::keyPressEvent(QKeyEvent *k)
{
Basic3DViewerWidget::keyPressEvent(k);
switch (k->key()) {
case Key_F2:
emit toggleLight();
break;
case Key_F4:
emit toggleNormals();
break;
case Key_F5:
emit toggleTwoSidedMaterial();
break;
default:
break;
}
}