File: materialSettings.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (280 lines) | stat: -rw-r--r-- 9,825 bytes parent folder | download | duplicates (4)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/DIALOGS/materialSettings.h>
#include <BALL/VIEW/WIDGETS/scene.h>
#include <BALL/VIEW/KERNEL/representation.h>
#include <BALL/VIEW/DIALOGS/modifyRepresentationDialog.h>
#include <BALL/CONCEPT/property.h>

#include <QtWidgets/QLabel>
#include <QtWidgets/QSlider>

namespace BALL
{
	namespace VIEW
	{

		MaterialSettings::MaterialSettings(QWidget* parent, const char* name, Qt::WindowFlags fl)
			: QWidget(parent, fl),
				Ui_MaterialSettingsData(),
				PreferencesEntry(),
				current_representation_(0)
		{
			setupUi(this);
			setObjectName(name);
			setINIFileSectionName("MATERIAL_SETTINGS");
			setWidgetStackName((String)tr("Materials"));
			
			setColor(ambient_color_label, ColorRGBA(1.0, 1.0, 1.0));
			setColor(reflectiveness_color_label, ColorRGBA(1.0, 1.0, 1.0));
			setColor(specularity_color_label, ColorRGBA(1.0, 1.0, 1.0));

			// signals and slots connections
			connect( ambient_factor_slider, SIGNAL( valueChanged(int) ), this, SLOT( ambientFactorChanged() ) );
			connect( specularity_factor_slider, SIGNAL( valueChanged(int) ), this, SLOT( specularityFactorChanged() ) );
			connect( reflectiveness_factor_slider, SIGNAL( valueChanged(int) ), this, SLOT( reflectivenessFactorChanged() ) );
			connect( shininess_factor_slider, SIGNAL( valueChanged(int) ), this, SLOT( shininessFactorChanged() ) );
			connect( transparency_factor_slider, SIGNAL( valueChanged(int) ), this, SLOT( transparencyFactorChanged() ) );

			connect (update_directly_checkBox, SIGNAL(stateChanged(int)), this, SLOT (updateDirectlyBoxChanged()));
		
			connect( ambient_color_button, SIGNAL( clicked() ), this, SLOT( editAmbientColor() ) );
			connect( specularity_color_button, SIGNAL(clicked()), this, SLOT( editSpecularityColor() ) );
			connect( reflectiveness_color_button, SIGNAL(clicked()), this, SLOT( editReflectivenessColor() ) );

			registerWidgets_();
		}


		void MaterialSettings::apply()
		{
//TODO still missing: use the colors (ambient_color_button, ..) in POVRay and OpenGL 
//TODO what should happen, when this dialog is called for a specific representation in POVRay and OpenGL?
//TODO So far OpenGL and POVRay do not make any use of transparency!
			if (Scene::getInstance(0) && Scene::getInstance(0)->getStage())
			{
				Stage& stage = *Scene::getInstance(0)->getStage();

					// first, decide whether we have been called from the preferences or from the geometric control
					Stage::Material material;

					material.ambient_color    = VIEW::getColor(ambient_color_label);
					material.specular_color   = VIEW::getColor(specularity_color_label);
					material.reflective_color = VIEW::getColor(reflectiveness_color_label);

					material.ambient_intensity    = ambient_factor_label->text().toFloat();
					material.specular_intensity   = specularity_factor_label->text().toFloat();
					material.reflective_intensity = reflectiveness_factor_label->text().toFloat();

					material.shininess    = std::max(shininess_factor_label->text().toFloat(), 0.1f);
					material.transparency = transparency_factor_label->text().toFloat();

					if (objectName() == "MaterialSettingsForRepresentation")
					{
						// we have been called from the right mouse button menu of a representation and should *only*
						// apply the values to this one
						if (current_representation_)
						{
							Scene::getInstance(0)->updateMaterialForRepresentation(current_representation_, material);
						}
						else
							Log.error() << "MaterialSettings::apply(): Invalid representation selected!" << std::endl;
					}
					else
					{
						// we have been called from the preferences and should set new default values for *all* representations
						stage.getMaterial() = material;
						Scene::getInstance(0)->updateAllMaterials();
					}
				}
		}

		void MaterialSettings::setCurrentRepresentation(Representation* representation)
		{
			current_representation_ = representation;

			// update the sliders
			Stage::Material material;

			if (representation->hasProperty("Rendering::Material"))
			{
				NamedProperty mat_property = representation->getProperty("Rendering::Material");
				boost::shared_ptr<PersistentObject> mat_ptr = mat_property.getSmartObject();
				material = *(dynamic_cast<Stage::Material*>(mat_ptr.get()));
			}
			else
			{
				Stage* stage = Scene::getInstance(0)->getStage();
				material = stage->getMaterial();
			}

			// remember the update-directly value
			bool do_update = update_directly_checkBox->isChecked();
			// and disable it for now to prevent unnecessary raytracing calls
			update_directly_checkBox->setChecked(false);

			// now set the values
			setColor(ambient_color_label, material.ambient_color);
			setColor(reflectiveness_color_label, material.reflective_color);
			setColor(specularity_color_label, material.specular_color);

			setLabel_(*ambient_factor_label, material.ambient_intensity);
			ambient_factor_slider->setValue(material.ambient_intensity*(float)ambient_factor_slider->maximum());

			setLabel_(*specularity_factor_label, material.specular_intensity);
			specularity_factor_slider->setValue(material.specular_intensity*(float)specularity_factor_slider->maximum());

			setLabel_(*reflectiveness_factor_label, material.reflective_intensity);
			reflectiveness_factor_slider->setValue(material.reflective_intensity*(float)reflectiveness_factor_slider->maximum());

			setLabel_(*shininess_factor_label, material.shininess);
			shininess_factor_slider->setValue(sqrt(material.shininess*(float)shininess_factor_slider->maximum()));

			setLabel_(*transparency_factor_label, material.transparency);
			transparency_factor_slider->setValue(material.transparency);

			update_directly_checkBox->setChecked(do_update);
		}

		void MaterialSettings::updateDefaultMaterialsFromStage()
		{
			Stage* stage = Scene::getInstance(0)->getStage();
			const Stage::Material& material = stage->getMaterial();

			// now set the values
			setColor(ambient_color_label, material.ambient_color);
			setColor(reflectiveness_color_label, material.reflective_color);
			setColor(specularity_color_label, material.specular_color);

			ambient_factor_slider->setValue(material.ambient_intensity);
			setValues_(*ambient_factor_slider, *ambient_factor_label, 1);

			specularity_factor_slider->setValue(material.specular_intensity);
			setValues_(*specularity_factor_slider, *specularity_factor_label, 1);

			reflectiveness_factor_slider->setValue(material.reflective_intensity);
			setValues_(*reflectiveness_factor_slider, *reflectiveness_factor_label, 1);

			shininess_factor_slider->setValue(material.shininess);
			setValues_(*shininess_factor_slider, *shininess_factor_label, 1);	

			transparency_factor_slider->setValue(material.transparency*100);
			setValues_(*transparency_factor_slider, *transparency_factor_label, 1);

			if (update_directly_checkBox->isChecked())
				apply();
		}

		void MaterialSettings::ambientFactorChanged()
		{ 
			setValues_(*ambient_factor_slider, *ambient_factor_label, 100);
			if (update_directly_checkBox->isChecked())
				apply();
		}

		void MaterialSettings::specularityFactorChanged()
		{
			setValues_(*specularity_factor_slider, *specularity_factor_label, 100);	
			if (update_directly_checkBox->isChecked())
				apply();
		}

		void MaterialSettings::reflectivenessFactorChanged()
		{                                            
			setValues_(*reflectiveness_factor_slider, *reflectiveness_factor_label, 100);	
			if (update_directly_checkBox->isChecked())
				apply();
		}

		void MaterialSettings::shininessFactorChanged()
		{
			//setValues_(*shininess_factor_slider, *shininess_factor_label, 1);	
			setQuadraticValues_(*shininess_factor_slider, *shininess_factor_label, shininess_factor_slider->maximum());	
			if (update_directly_checkBox->isChecked())
				apply();
		}
		
		void MaterialSettings::transparencyFactorChanged()
		{
			String text = String((int)transparency_factor_slider->value());
			transparency_factor_label->setText(text.c_str());
			if (update_directly_checkBox->isChecked())
				apply();
		}
	
		void MaterialSettings::updateDirectlyBoxChanged()
		{
			if (update_directly_checkBox->isChecked())
			{
				apply();
			}
		}
	
		
		void MaterialSettings::setValues_(const QSlider& slider, QLabel& label, int divisor)
		{
			setLabel_(label, ((float)slider.value()) / divisor);
		}

		void MaterialSettings::setLabel_(QLabel& label, float value)
		{
			String text(10, "%2f", value);

			while (text.has('.') && text.hasSuffix("0"))
			{
				text = text.trimRight("0");
			}

			if (text.hasSuffix(".")) text += "0";
				
			label.setText(text.c_str());
		}
		
		void MaterialSettings::setQuadraticValues_(const QSlider& slider, QLabel& label, int divisor)
		{
			float quadratic_value =((float)slider.value()) / divisor; 
			quadratic_value *= quadratic_value;
			quadratic_value *= divisor;
			QString truncated_text;
			truncated_text.setNum(quadratic_value,'f', 3);
			//String text = String(quadratic_value);
			//
			//while (text.has('.') && text.hasSuffix("0"))
			//{
			//	text = text.trimRight("0");
			//}

			//if (text.hasSuffix(".")) text += "0";
			//	
			//label.setText(text.c_str());
			label.setText(truncated_text);
		}
		
		void MaterialSettings::editAmbientColor()
		{
			QColor color = VIEW::chooseColor(ambient_color_label);

			if (update_directly_checkBox->isChecked())
				apply();
		}	
		
		void MaterialSettings::editSpecularityColor()
		{
			QColor color = VIEW::chooseColor(specularity_color_label);

			if (update_directly_checkBox->isChecked())
				apply();
		}
		
		void MaterialSettings::editReflectivenessColor()
		{
			QColor color = VIEW::chooseColor(reflectiveness_color_label);

			if (update_directly_checkBox->isChecked())
				apply();
		}

	} // namespace VIEW
} // namespace BALL