File: rfx_glpass.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 (178 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download | duplicates (5)
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
/****************************************************************************
* 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 "rfx_glpass.h"

RfxGLPass::~RfxGLPass()
{
	if (shaderLinked)
		glDeleteObjectARB(shaderProgram);

	foreach (RfxState *state, rfxStates)
		delete state;
	rfxStates.clear();

	foreach (RfxUniform *uniform, shaderUniforms)
		delete uniform;
	shaderUniforms.clear();

	foreach (RfxSpecialAttribute *attribute, shaderSpecialAttributes)
		delete attribute;
	shaderSpecialAttributes.clear();
}

void RfxGLPass::SetShaderSource(const QString &source, bool isFragment)
{
	if (isFragment)
		frag = source;
	else
		vert = source;
}

void RfxGLPass::CompileAndLink()
{
	if (frag.isEmpty() || vert.isEmpty()) {
		compileLog = "OK (No sources)";
		return;
	}

	GLubyte *ShaderSource;
	GLint ShaderLen;

	// compile vertex shader
	ShaderSource = (GLubyte *)new char[vert.length() + 1];
	memcpy(ShaderSource, vert.toLocal8Bit().data(), vert.length());
	ShaderLen = (GLint) vert.length();

	GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertShader, 1, (const GLchar **)&ShaderSource, &ShaderLen);
	glCompileShader(vertShader);
	
	delete[] ShaderSource;
	ShaderSource = (GLubyte *)new char[frag.length() + 1];
	memcpy(ShaderSource, frag.toLocal8Bit().data(), frag.length());
	ShaderLen = (GLint) frag.length();

	// compile fragment shader
	GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragShader, 1, (const GLchar **)&ShaderSource, &ShaderLen);
	glCompileShader(fragShader);

	delete[] ShaderSource;

	// attach shaders to program and link
	shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertShader);
	glAttachShader(shaderProgram, fragShader);
	glLinkProgram(shaderProgram);

	GLint res;
	glGetObjectParameterivARB(shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &res);
	if (!res) {
		FillInfoLog(shaderProgram);
	} else {
		compileLog = "OK";

		// shader link successful. update uniform locations
		// and load textures
		shaderLinked = true;
		foreach (RfxUniform *uni, shaderUniforms) {
			uni->LoadTexture();
			uni->UpdateUniformLocation(shaderProgram);
		}
	}
}

void RfxGLPass::FillInfoLog(GLhandleARB obj)
{
	GLint infologLength = 0;
	GLsizei charsWritten = 0;
	char *infoLog;

	glGetObjectParameterivARB(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength);

	if (infologLength > 0) {
		infoLog = new char[infologLength];
		glGetInfoLogARB(obj, infologLength, &charsWritten, infoLog);

		compileLog = "FAILED\n";
		compileLog.append(infoLog);

		delete[] infoLog;
	}
}

void RfxGLPass::Start()
{
	// set gl states
	foreach (RfxState *state, rfxStates)
		state->SetEnvironment();

	// if this pass has no glsl sources,
	// keep using glsl program of the previous pass
	if (!frag.isEmpty() && !vert.isEmpty()) {

		glUseProgram(shaderProgram);

		// pass uniforms
		foreach (RfxUniform *uni, shaderUniforms) {
			uni->PassToShader();
		}
	}
}

RfxUniform* RfxGLPass::getUniform(const QString& uniIdx)
{
	foreach (RfxUniform *u, shaderUniforms) {
		if (u->GetName() == uniIdx)
			return u;
	}

	return NULL;
}

/*
	Checks whether the actual mesh document contains the value needed to set up the value of the special attributes.
	If not an alert message is shown and it returns false.
	@param md the mesh document to test.
	@return true if the mesh document contains all the value per vertex needed to set up the special attribute values, false otherwise.
*/
bool RfxGLPass::checkSpecialAttributeDataMask(MeshDocument* md){
	QListIterator<RfxSpecialAttribute*> iterator = QListIterator<RfxSpecialAttribute*>(this->shaderSpecialAttributes);

	RfxSpecialAttribute* spa;
	
	while(iterator.hasNext()){
		spa = iterator.next();
		if(!md->mm()->hasDataMask(spa->getDataMask()))
		{
			 QMessageBox msgBox;
			 msgBox.setIcon(QMessageBox::Warning);
			 msgBox.setWindowTitle("Attribute missed");
			 msgBox.setText(QString("The requested shader needs the model contains per %1 value").arg(spa->getDescription()));
			 int ret = msgBox.exec();
			return false;
		}
	}
	return true;
}