File: render_rfx.cpp

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (217 lines) | stat: -rwxr-xr-x 6,160 bytes parent folder | download | duplicates (3)
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
/****************************************************************************
* MeshLab                                                           o o     *
* A versatile mesh processing toolbox                             o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005-2008                                           \/)\/    *
* 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 "render_rfx.h"
#include <common/pluginmanager.h>

RenderRFX::RenderRFX()
{
	shadersSupported = false;
	shaderPass = -1;
	totPass = -1;
	dialog = NULL;
	activeShader = NULL;
}

RenderRFX::~RenderRFX()
{
	if (dialog) {
		dialog->close();
		delete dialog;
	}

	foreach (QAction *a, actionList)
		delete a;
	actionList.clear();
}

QList<QAction*> RenderRFX::actions()
{
	if (actionList.isEmpty())
		initActionList();

	return actionList;
}

void RenderRFX::initActionList()
{
	QDir shadersDir = QDir("/usr/share/meshlab");

#if defined(Q_OS_WIN)
	if (shadersDir.dirName() == "debug"   ||
	    shadersDir.dirName() == "release" ||
	    shadersDir.dirName() == "plugins")
		shadersDir.cdUp();
#elif defined(Q_OS_MAC)
	if (shadersDir.dirName() == "MacOS") {
		for (int i = 0; i < 6; ++i) {
			shadersDir.cdUp();
			if (shadersDir.exists("shadersrm"))
				break;
		}
	}
#endif

	if (!shadersDir.cd("shaders/shadersrm")) {
		QMessageBox::information(0, "MeshLab",
		                            "Unable to find the render monkey "
		                            "shaders directory.\n"
		                            "No shaders will be loaded.");
		return;
	}
	shaderDir = shadersDir.path();
	qDebug("Shader directory found '%s', and it contains %i rfx files",
	       qPrintable(shadersDir.path()),
	       shadersDir.entryList(QStringList("*.rfx")).size());

	// validate each .rfx file found and add to actionList
	// (this is actually just a sanity check, shaders created are deleted immediately)
	foreach (QString fileName, shadersDir.entryList(QDir::Files)) {
		if (fileName.endsWith(".rfx")) {
			RfxParser theParser(shadersDir.absoluteFilePath(fileName));
			if (theParser.isValidDoc()) {
				QAction *action = new QAction(fileName, this);
				action->setCheckable(false);
				actionList.append(action);
				delete theParser.GetShader();
			}
		}
	}
}

void RenderRFX::Init(QAction *action, MeshDocument &md, RenderMode &rmode, QGLWidget *parent)
{

	if (!actionList.contains(action))
	{
		shadersSupported = false;
		return;
	}
	if (activeShader) {
		delete activeShader;
		activeShader = NULL;
	}

	// parse shader file we're going to use
	RfxParser theParser(QDir(shaderDir).absoluteFilePath(action->text()));

	// Small hack that allow to use the current mesh textures for the shaders.
	foreach(MeshModel * mp, md.meshList)
			{
					if(mp->cm.textures.size()>0)
					{
                        QFileInfo meshBaseDir(mp->fullName());
						theParser.setMeshTexture(meshBaseDir.absolutePath()+"/"+QString(mp->cm.textures[0].c_str()));
					}			
			}

	theParser.Parse(md);
  RfxShader *tmp = theParser.GetShader();
  //activeShader = theParser.GetShader();
  //assert(activeShader);

  if (dialog) {
    dialog->close();
    delete dialog;
  }

	//verifies if there's some special attributes in the shader.
  if(!tmp->checkSpecialAttributeDataMask(&md))
  {
    if (activeShader) {
      delete activeShader;
      activeShader = NULL;
    }
		shadersSupported = false;
		return;
  }
  activeShader = tmp;

	parent->makeCurrent();
	GLenum err = glewInit();
	if (GLEW_OK == err) {
		if (GLEW_ARB_vertex_program && GLEW_ARB_fragment_program) {
			shadersSupported = true;

			activeShader->CompileAndLink();
			totPass = activeShader->GetTotalPasses();
			shaderPass = 0;

			dialog = new RfxDialog(activeShader, action, parent);
			dialog->move(0, 100);
			dialog->show();
		}
	}

	glGetError();
}

void RenderRFX::Render(QAction *action, MeshDocument &md,  RenderMode &rm, QGLWidget *parent)
{

  if(!activeShader) return;
	rm.textureMode = vcg::GLW::TMPerWedge;
	
	
	for(shaderPass=0;shaderPass<totPass;shaderPass++)
	{
		activeShader->Start(shaderPass);
			glGetError();
			foreach(MeshModel * mp, md.meshList)
			{
				
				if(mp->visible && activeShader->GetPass(shaderPass)->hasSpecialAttribute()){
					GLuint *program = activeShader->GetPass(shaderPass)->getProgram();
								
					Draw(&md, program, activeShader->GetPass(shaderPass)->AttributesList());
				

				}
				else{
					if(mp->visible) 
						mp->render(rm.drawMode,rm.colorMode,rm.textureMode);
				}
				
			}
	}
	glUseProgramObjectARB(0);

}

void RenderRFX::Finalize(QAction * /*act*/, MeshDocument */*md*/, GLArea */*gla*/)
{
	// close any open dialog
	if (dialog) {
		dialog->close();
		delete dialog;
		dialog = NULL;
	}

	// ... and delete any active shader
	if (activeShader) {
		delete activeShader;
		activeShader = NULL;
	}
}

Q_EXPORT_PLUGIN(RenderRFX)