File: shaderDialog.cpp

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (375 lines) | stat: -rw-r--r-- 14,148 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/****************************************************************************
* MeshLab                                                           o o     *
* An extendible mesh processor                                    o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005, 2009                                          \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* 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 (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#include "shaderDialog.h"
#include <QTextStream>
#include <QFileDialog>
#include <QMessageBox>
#define DECFACTOR 100000.0f

using namespace vcg;

ShaderDialog::ShaderDialog(ShaderInfo *sInfo, QGLWidget* gla, RenderMode &rm, QWidget *parent)
: QDockWidget(parent)
{
	ui.setupUi(this);
  this->setWidget(ui.frame);
	this->setFeatures(QDockWidget::AllDockWidgetFeatures);
	this->setAllowedAreas(Qt::LeftDockWidgetArea);
	this->setFloating(true);

	shaderInfo = sInfo;
	glarea = gla;
	rendMode = &rm;
	colorSignalMapper = new QSignalMapper(this);
	valueSignalMapper = new QSignalMapper(this);

	QGridLayout * qgrid = new QGridLayout(ui.uvTab);
	qgrid->setColumnMinimumWidth(0, 45);
	qgrid->setColumnMinimumWidth(1, 40);
	qgrid->setColumnMinimumWidth(2, 40);
	qgrid->setColumnMinimumWidth(3, 40);

	QLabel *perVertexColorLabel = new QLabel(this);
	perVertexColorLabel->setText("Use PerVertex Color");
	QCheckBox *perVertexColorCBox = new QCheckBox(this);
	rendMode->colorMode =  GLW::CMNone;
	connect(perVertexColorCBox, SIGNAL(stateChanged(int)), this, SLOT(setColorMode(int)));

	qgrid->addWidget(perVertexColorLabel, 0, 0);
	qgrid->addWidget(perVertexColorCBox, 0, 1);

	int row=1;
	std::map<QString, UniformVariable>::iterator i;
	for (i = shaderInfo->uniformVars.begin(); i != shaderInfo->uniformVars.end(); ++i) {

		QLabel *varNameLabel = new QLabel(this);
		varNameLabel->setObjectName(i->first+"_name");
		varNameLabel->setText(i->first);

		qgrid->addWidget(varNameLabel, row, 0);

    int varNum = UniformVariable::getVarsNumber(i->second.type);

		switch (i->second.widget) {

			case WIDGET_NONE: {
				for (int j=0;j<varNum;++j) {
					QLineEdit *qline = new QLineEdit(this);
					qline->setAlignment(Qt::AlignRight);
					qline->setObjectName(tr("%1%2").arg(i->first).arg(j));
					if (i->second.type == SINGLE_INT) {
						qline->setText(tr("%1").arg(i->second.ival[j]));

					} else {
						qline->setText(tr("%1").arg(i->second.fval[j]));

					}

					connect(qline, SIGNAL(textChanged(QString)), valueSignalMapper, SLOT(map()));
					valueSignalMapper->setMapping(qline,tr("%1%2").arg(i->first).arg(j));
					lineEdits[tr("%1%2").arg(i->first).arg(j)]=qline;

					qgrid->addWidget(qline, row, j+1);
				}
												} break;
			case WIDGET_COLOR : {

				QPushButton * colorButton = new QPushButton(this);
				colorButton->setText("Change");
				connect(colorButton, SIGNAL(clicked()), colorSignalMapper, SLOT(map()));
				colorSignalMapper->setMapping(colorButton,i->first);					

				qgrid->addWidget(colorButton, row, 1);


													} break;
			case WIDGET_SLIDER : {
				for (int j=0;j<varNum;++j) {
					QSlider *qslider = new QSlider(this);
					qslider->setTickPosition(QSlider::NoTicks);
					qslider->setOrientation(Qt::Horizontal);
					qslider->setObjectName(tr("%1%2").arg(i->first).arg(j));
					if (i->second.type == SINGLE_INT) {
						qslider->setTickInterval(i->second.step);
						qslider->setRange(i->second.min, i->second.max);
						qslider->setValue(i->second.ival[j]);
					} else {
						qslider->setTickInterval(i->second.step*DECFACTOR);
						qslider->setRange(i->second.min*DECFACTOR, i->second.max*DECFACTOR);
						qslider->setValue(i->second.fval[j]*DECFACTOR);						
					}
					connect(qslider, SIGNAL(valueChanged(int)), valueSignalMapper, SLOT(map()));
					valueSignalMapper->setMapping(qslider,tr("%1%2").arg(i->first).arg(j));

					sliders[tr("%1%2").arg(i->first).arg(j)]=qslider;

					qgrid->addWidget(qslider, row, j+1);

				}			 
													 } break;
		}	
		++row;
	}


	connect(colorSignalMapper, SIGNAL(mapped(const QString &)), this, SLOT(setColorValue(const QString &)));
	connect(valueSignalMapper, SIGNAL(mapped(const QString &)), this, SLOT(valuesChanged(const QString &)));

	//Texture Tab Section

	if (shaderInfo->textureInfo.size() > 0) {
		textLineSignalMapper = new QSignalMapper(this);
		textButtonSignalMapper = new QSignalMapper(this);


		QGridLayout * qgridTexTab = new QGridLayout(ui.textureTab);
		qgridTexTab->setColumnMinimumWidth(0, 45);
		qgridTexTab->setColumnMinimumWidth(1, 40);

		row = 0;
		std::vector<TextureInfo>::iterator textIter;
		for (textIter = shaderInfo->textureInfo.begin(); textIter != shaderInfo->textureInfo.end(); ++textIter) {
			QLabel *textNameLabel = new QLabel(this);
			QLineEdit *textValueEdit = new QLineEdit(this);
			QPushButton * textButton = new QPushButton(this);
			textButton->setText("Browse");
			textNameLabel->setText(tr("Texture Unit %1:").arg(row));
			textValueEdit->setText(textIter->path);
			qgridTexTab->addWidget(textNameLabel, row, 0);
			qgridTexTab->addWidget(textValueEdit, row, 1);
			qgridTexTab->addWidget(textButton, row, 2);


			connect(textValueEdit, SIGNAL(editingFinished()), textLineSignalMapper, SLOT(map()));
			textLineSignalMapper->setMapping(textValueEdit,row);

			connect(textButton, SIGNAL(clicked()), textButtonSignalMapper, SLOT(map()));
			textButtonSignalMapper->setMapping(textButton,row);

			textLineEdits.push_back(textValueEdit);
			++row;
		}

		connect(textLineSignalMapper, SIGNAL(mapped(int)), this, SLOT(changeTexturePath(int)));
		connect(textButtonSignalMapper, SIGNAL(mapped(int)), this, SLOT(browseTexturePath(int)));
	}

	//OpenGL Status Tab Section

	QGridLayout * qgridGlStatus = new QGridLayout(ui.glTab);
	qgridGlStatus->setColumnMinimumWidth(0, 45);
	qgridGlStatus->setColumnMinimumWidth(1, 40);

	row = 0;
	std::map<int, QString>::iterator glIterator;
	for (glIterator = shaderInfo->glStatus.begin(); glIterator != shaderInfo->glStatus.end(); ++glIterator) {
		QLabel *glVarLabel = new QLabel(this);
		QLabel *glValueLabel = new QLabel(this);


		switch (glIterator->first) {
			case SHADE:  glVarLabel->setText("glShadeModel"); glValueLabel->setText(glIterator->second); ++row; break;
			case ALPHA_TEST:  glVarLabel->setText("GL_ALPHA_TEST"); glValueLabel->setText(glIterator->second); ++row; break;
			case ALPHA_FUNC:  glVarLabel->setText("glAlphaFunc"); glValueLabel->setText(glIterator->second + ", " + shaderInfo->glStatus[ALPHA_CLAMP]); ++row; break;
				//case ALPHA_CLAMP: used in ALPHA_FUNC
			case BLENDING:  glVarLabel->setText("GL_BLEND"); glValueLabel->setText(glIterator->second); ++row; break;
			case BLEND_FUNC_SRC:  glVarLabel->setText("glBlendFunc"); glValueLabel->setText(glIterator->second + ", " + shaderInfo->glStatus[BLEND_FUNC_SRC]); ++row; break;
				//case BLEND_FUNC_DST: used in BLEND_FUNC_SRC
			case BLEND_EQUATION: glVarLabel->setText("glBlendEquation"); glValueLabel->setText(glIterator->second); ++row; break;
			case DEPTH_TEST: glVarLabel->setText("GL_DEPTH_TEST"); glValueLabel->setText(glIterator->second); ++row; break;
			case DEPTH_FUNC: glVarLabel->setText("glDepthFunc"); glValueLabel->setText(glIterator->second); ++row; break;
				//case CLAMP_NEAR:
				//case CLAMP_FAR:
			case CLEAR_COLOR_R: glVarLabel->setText("glClearColor"); glValueLabel->setText(glIterator->second + ", " +
														shaderInfo->glStatus[CLEAR_COLOR_G] + ", " +
														shaderInfo->glStatus[CLEAR_COLOR_B] + ", " +
														shaderInfo->glStatus[CLEAR_COLOR_A]); ++row; break;
				//case CLEAR_COLOR_G: used in CLEAR_COLOR_R
				//case CLEAR_COLOR_B: used in CLEAR_COLOR_R
				//case CLEAR_COLOR_A: used in CLEAR_COLOR_R
		}

		qgridGlStatus->addWidget(glVarLabel, row, 0);
		qgridGlStatus->addWidget(glValueLabel, row, 1);

	}

	//Vertex and Fragment Program Tabs Section
	
	QFile qf;
	QTextStream ts(&qf);

	qf.setFileName(shaderInfo->vpFile);
	if (!qf.open(QIODevice::ReadOnly | QIODevice::Text) )
		QMessageBox::critical(this,"Opengl Shader" ,"unable to open file");
	ui.vpTextBrowser->insertPlainText(ts.readAll());
	qf.close();

	qf.setFileName(shaderInfo->fpFile);
	if (!qf.open(QIODevice::ReadOnly | QIODevice::Text) )
		QMessageBox::critical(this,"Opengl Shader" ,"unable to open file");
	ui.fpTextBrowser->insertPlainText(ts.readAll());
	qf.close();

	//End of Vertex and Fragment Program Tabs Section


//	this->setWindowFlags(Qt::WindowStaysOnTopHint);
	connect(ui.okButton, SIGNAL(clicked()), this, SLOT(accept()));




}

ShaderDialog::~ShaderDialog()
{

}

void ShaderDialog::setColorValue(const QString &varName)
{
	QColor old;
	if ( shaderInfo->uniformVars[varName].type == ARRAY_3_FLOAT) {
		old.setRgbF(shaderInfo->uniformVars[varName].fval[0], shaderInfo->uniformVars[varName].fval[1], shaderInfo->uniformVars[varName].fval[2]);
	} else if (shaderInfo->uniformVars[varName].type == ARRAY_4_FLOAT) {
		old.setRgbF(shaderInfo->uniformVars[varName].fval[0], shaderInfo->uniformVars[varName].fval[1], shaderInfo->uniformVars[varName].fval[2], shaderInfo->uniformVars[varName].fval[3]);
	}

	QColor newColor = QColorDialog::getColor(old, this);
	if (newColor.isValid()) {

		shaderInfo->uniformVars[varName].fval[0] = newColor.redF(); 
		shaderInfo->uniformVars[varName].fval[1] = newColor.greenF();
		shaderInfo->uniformVars[varName].fval[2] = newColor.blueF();

		if (shaderInfo->uniformVars[varName].type == ARRAY_4_FLOAT) {

			shaderInfo->uniformVars[varName].fval[3] = newColor.alphaF();

		}
	}
  glarea->update();
}


void ShaderDialog::valuesChanged(const QString &varNameAndIndex) {


	int varIndex = varNameAndIndex[varNameAndIndex.length()-1].digitValue();
	QString varName = varNameAndIndex;
	varName.chop(1);

	int varWidget = shaderInfo->uniformVars[varName].widget;
	int varType = shaderInfo->uniformVars[varName].type;
	switch (varWidget) {
		case WIDGET_NONE: {
			QLineEdit * qline = lineEdits[varNameAndIndex];
			if (varType == SINGLE_INT) {
				shaderInfo->uniformVars[varName].ival[varIndex] = qline->text().toInt();
			} else {
				shaderInfo->uniformVars[varName].fval[varIndex] = qline->text().toFloat();
			}
											} break;
		case WIDGET_SLIDER: {
			QSlider * qslider = sliders[varNameAndIndex];
			if (varType == SINGLE_INT) {
				shaderInfo->uniformVars[varName].ival[varIndex] = qslider->value();
			} else {
				shaderInfo->uniformVars[varName].fval[varIndex] = qslider->value()/DECFACTOR; 
			}
												} break;
	}
  glarea->update();

}


void ShaderDialog::setColorMode(int state) {
	if (state == Qt::Checked) {
		rendMode->colorMode = GLW::CMPerVert;
	} else {
		rendMode->colorMode = GLW::CMNone;
	}
  glarea->update();
}

void ShaderDialog::changeTexturePath(int i) {
	shaderInfo->textureInfo[i].path = textLineEdits[i]->text();
	reloadTexture(i);
}

void ShaderDialog::browseTexturePath(int i) {
	QFileDialog fd(0,"Choose new texture");

	QDir shadersDir = QDir("/usr/share/meshlab");
#if defined(Q_OS_WIN)
	if (shadersDir.dirName() == "debug" || shadersDir.dirName() == "release")
		shadersDir.cdUp();
#elif defined(Q_OS_MAC)
	if (shadersDir.dirName() == "MacOS") {
		shadersDir.cdUp();
		shadersDir.cdUp();
		shadersDir.cdUp();
	}
#endif
	shadersDir.cd("textures");
	
	fd.setDirectory(shadersDir);
	fd.move(500, 100);

	QStringList newPath;
	if (fd.exec())
	{
		newPath = fd.selectedFiles();
		textLineEdits[i]->setText(newPath.at(0));
		shaderInfo->textureInfo[i].path = newPath.at(0);
		reloadTexture(i);
	} 
	
}

void ShaderDialog::reloadTexture(int i) {
	glDeleteTextures( 1, &shaderInfo->textureInfo[i].tId);

	glEnable(shaderInfo->textureInfo[i].Target);
	QImage img, imgScaled, imgGL;
	img.load(shaderInfo->textureInfo[i].path);
	// image has to be scaled to a 2^n size. We choose the first 2^N <= picture size.
	int bestW=pow(2.0,floor(::log(double(img.width() ))/::log(2.0)));
	int bestH=pow(2.0,floor(::log(double(img.height()))/::log(2.0)));
	imgScaled=img.scaled(bestW,bestH,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
	imgGL=QGLWidget::convertToGLFormat(imgScaled);

	glGenTextures( 1, &(shaderInfo->textureInfo[i].tId) );
	glBindTexture( shaderInfo->textureInfo[i].Target, shaderInfo->textureInfo[i].tId );
	glTexImage2D( shaderInfo->textureInfo[i].Target, 0, 3, imgGL.width(), imgGL.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, imgGL.bits() );
	glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_MIN_FILTER, shaderInfo->textureInfo[i].MinFilter );
	glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_MAG_FILTER, shaderInfo->textureInfo[i].MagFilter ); 
	glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_WRAP_S, shaderInfo->textureInfo[i].WrapS ); 
	glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_WRAP_T, shaderInfo->textureInfo[i].WrapT ); 
	glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_WRAP_R, shaderInfo->textureInfo[i].WrapR ); 

  glarea->update();
}