File: rfx_uniform.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 (243 lines) | stat: -rw-r--r-- 5,862 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
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
/****************************************************************************
* 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_uniform.h"

RfxUniform::RfxUniform(const QString &_name, const QString &_type)
{
	value = NULL;
	identifier = _name;
	type = GetUniformType(_type);
	textureLoaded = false;
	textureNotFound = false;
	textureRendered = false;

	_isRmColorVariable = false;

	minVal = 0.0;
	maxVal = 0.0;
}

RfxUniform::~RfxUniform()
{
	if (value) {
		if (type == INT || type == BOOL || type == FLOAT)
			delete value;
		else
			delete[] value;
	}

	// render targets are deleted by rfx_shader
	if (textureLoaded && !textureRendered)
		glDeleteTextures(1, &textureId);
}

void RfxUniform::UpdateUniformLocation(GLuint programId)
{
	location = glGetUniformLocation(programId, identifier.toLocal8Bit().data());
}

// static member initialization
const char *RfxUniform::UniformTypeString[] = {
	"int", "float", "bool",
	"vec2", "vec3", "vec4",
	"ivec2", "ivec3", "ivec4",
	"bvec2", "bvec3", "bvec4",
	"mat2", "mat3", "mat4",
	"sampler1D", "sampler2D", "sampler3D", "samplerCube",
	"sampler1DShadow", "sampler2DShadow"
};

RfxUniform::UniformType RfxUniform::GetUniformType(const QString& stringType)
{
	int i;
	for (i = 0; i < TOTAL_TYPES; ++i) {
		if (stringType == UniformTypeString[i])
			break;
	}

	return (UniformType)i;
}

void RfxUniform::SetValue(float _value[16])
{
	switch (type) {
	case INT:
	case BOOL:
	case FLOAT:
		value = new float;
		*value = _value[0];
		break;

	case VEC2:
	case IVEC2:
	case BVEC2:
		value = new float[2];
		memcpy(value, _value, sizeof(float) * 2);
		break;

	case VEC3:
	case IVEC3:
	case BVEC3:
		value = new float[3];
		memcpy(value, _value, sizeof(float) * 3);
		break;

	case VEC4:
	case IVEC4:
	case BVEC4:
	case MAT2:
		value = new float[4];
		memcpy(value, _value, sizeof(float) * 4);
		break;

	case MAT3:
		value = new float[9];
		memcpy(value, _value, sizeof(float) * 9);
		break;

	case MAT4:
		value = new float[16];
		memcpy(value, _value, sizeof(float) * 16);
		break;

	default:
		break;

	}
}

void RfxUniform::SetValue(const QString &texFileName)
{
	textureFile = texFileName;
}

void RfxUniform::LoadTexture()
{
	if (textureRendered) {
		textureNotFound = false;
		textureLoaded = true;
		textureTarget = GL_TEXTURE_2D;

		return;
	}

	if (!QFileInfo(textureFile).exists()) {
		textureNotFound = true;
		return;
	} else {
		textureNotFound = false;
	}

	switch (type) {
	case SAMPLER2D:
		textureTarget = GL_TEXTURE_2D;
		break;
	case SAMPLER3D:
		textureTarget = GL_TEXTURE_3D;
		break;
	case SAMPLERCUBE:
		textureTarget = GL_TEXTURE_CUBE_MAP;
		break;
	default:
		return;
	}

	glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxTexUnits);

	textureLoaded = (RfxTextureLoader::LoadTexture(textureFile,
	                                               textureStates,
	                                               &textureId)
	                 && texUnit < maxTexUnits);
}

void RfxUniform::PassToShader()
{
	switch (type) {
	case INT:
	case BOOL:
		glUniform1i(location, (GLint)*value);
		break;

	case FLOAT:
		glUniform1f(location, *value);
		break;

	case IVEC2:
	case BVEC2:
		glUniform2i(location, (GLint)value[0], (GLint)value[1]);
		break;

	case VEC2:
		glUniform2f(location, value[0], value[1]);
		break;

	case IVEC3:
	case BVEC3:
		glUniform3i(location, (GLint)value[0], (GLint)value[1], (GLint)value[2]);
		break;

	case VEC3:
		glUniform3f(location, value[0], value[1], value[2]);
		break;

	case IVEC4:
	case BVEC4:
		glUniform4i(location, (GLint)value[0], (GLint)value[1], (GLint)value[2], (GLint)value[3]);
		break;

	case VEC4:
		glUniform4f(location, value[0], value[1], value[2], value[3]);
		break;

	case MAT2:
		glUniformMatrix2fv(location, 1, GL_FALSE, value);
		break;

	case MAT3:
		glUniformMatrix3fv(location, 1, GL_FALSE, value);
		break;

	case MAT4:
		glUniformMatrix4fv(location, 1, GL_FALSE, value);
		break;

	case SAMPLER2D:
	case SAMPLER3D:
	case SAMPLERCUBE:
		if (textureLoaded) {

			if (textureRendered)
				textureId = rTarget->GetTexture();

			glActiveTexture(GL_TEXTURE0 + texUnit);
			glBindTexture(textureTarget, textureId);

			glUniform1i(location, texUnit);
		}
		break;

	default:
		qDebug("don't know what to do with %s", UniformTypeString[type]);
	}
}