File: te_mesh.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (242 lines) | stat: -rw-r--r-- 6,772 bytes parent folder | download
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "tetraedge/tetraedge.h"
#include "tetraedge/te/te_renderer.h"
#include "tetraedge/te/te_light.h"
#include "tetraedge/te/te_mesh.h"
#include "tetraedge/te/te_mesh_opengl.h"
#include "tetraedge/te/te_mesh_tinygl.h"
#include "tetraedge/te/te_material.h"

namespace Tetraedge {

TeMesh::TeMesh() : _matrixForced(false), _hasAlpha(false), _initialMaterialIndexCount(0),
_drawWires(false), _shouldDraw(true) {
}


void TeMesh::defaultMaterial(const TeIntrusivePtr<Te3DTexture> &texture) {
	TeMaterial::Mode mode = TeMaterial::MaterialMode1;
	if (texture && !texture->hasAlpha())
		mode = TeMaterial::MaterialMode0;
	_materials.resize(1);
	_materials[0] = TeMaterial(texture, mode);
}

const TeMaterial *TeMesh::material(uint index) const {
	assert(!_materials.empty());
	if (index < _materials.size()) {
		return &_materials[index];
	} else {
		return &_materials[0];
	}
}

void TeMesh::destroy() {
	_hasAlpha = false;
	_updatedVerticies.clear();
	_updatedNormals.clear();
	_verticies.clear();
	_normals.clear();
	_uvs.clear();
	_colors.clear();
	_indexes.clear();
	_materialIndexes.clear();
	_faceCounts.clear();
	_matricies.clear();
}

bool TeMesh::hasAlpha(uint idx) {
	// Note: I don't understand this logic, but it's what the game does..
	bool hasGlobalAlpha = _hasAlpha && !_colors.empty();

	bool retval = hasGlobalAlpha;
	if (idx < _materials.size()) {
		const TeMaterial &material = _materials[idx];
		if (material._enableSomethingDefault0) {
			retval = false;
		} else {
			retval = true;
			if (!hasGlobalAlpha && material._mode != TeMaterial::MaterialMode1 && material._ambientColor.a() == 255)
				retval = (material._diffuseColor.a() != 255);
		}
	}
	return retval;
}

TeVector3f32 TeMesh::normal(uint idx) const {
	if (!_updatedNormals.empty())
		return _updatedNormals[idx];
	else
		return _normals[idx];
}

void TeMesh::resizeUpdatedTables(unsigned long newSize) {
	_updatedVerticies.resize(newSize);
	_updatedNormals.resize(newSize);
}

void TeMesh::setColor(const TeColor &col) {
	Te3DObject2::setColor(col);

	if (!_verticies.empty()) {
		const TeColor colnow = Te3DObject2::color();
		_colors.resize(_verticies.size());
		if (colnow.a() != 255)
			_hasAlpha = true;

		for (uint i = 0; i < _verticies.size(); i++) {
			_colors[i] = colnow;
		}
	}
}

void TeMesh::setColor(uint idx, const TeColor &col) {
	if (col.a() != 255) {
		_hasAlpha = true;
	}
	_colors.resize(_verticies.size());
	_colors[idx] = col;
}

void TeMesh::setConf(unsigned long vertexCount, unsigned long indexCount, enum Mode mode, uint materialCount, uint materialIndexCount) {
	destroy();
	_initialMaterialIndexCount = materialIndexCount;
	_verticies.resize(vertexCount);
	_indexes.resize(indexCount);
	_materials.resize(materialCount);
	_matricies.resize(vertexCount);
	setMode(mode);
}

void TeMesh::setIndex(uint idx, uint val) {
	_indexes[idx] = val;
}

void TeMesh::setNormal(uint idx, const TeVector3f32 &val) {
	_normals.resize(_verticies.size());
	_normals[idx] = val;
}

void TeMesh::setTextureUV(uint idx, const TeVector2f32 &val) {
	_uvs.resize(_verticies.size());
	_uvs[idx] = val;
}

void TeMesh::setVertex(uint idx, const TeVector3f32 &val) {
	_verticies[idx] = val;
}

TeVector3f32 TeMesh::vertex(uint idx) const {
	if (!_updatedVerticies.empty())
		return _updatedVerticies[idx];
	else
		return _verticies[idx];
}

void TeMesh::attachMaterial(uint idx, const TeMaterial &src) {
	TeMaterial &dest = _materials[idx];
	dest._texture = src._texture;
	dest._enableLights = src._enableLights;
	dest._enableSomethingDefault0 = src._enableSomethingDefault0;
	dest._emissionColor = src._emissionColor;
	dest._shininess = src._shininess;
	dest._diffuseColor = src._diffuseColor;
	dest._specularColor = src._specularColor;
	dest._mode = src._mode;
	dest._ambientColor = src._ambientColor;
}

void TeMesh::facesPerMaterial(uint idx, unsigned short value) {
	_faceCounts.resize(_materials.size());
	_faceCounts[idx] = value;
}

void TeMesh::matrixIndex(uint num, unsigned short val) {
	_matricies[num] = val;
}

void TeMesh::update(const Common::Array<TeMatrix4x4> *matricies1, const Common::Array<TeMatrix4x4> *matricies2) {
	if (visible()) {
		if (matricies1) {
			_updatedVerticies.resize(_verticies.size());
			_updatedNormals.resize(_normals.size());
			updateTo(matricies1, matricies2, _updatedVerticies, _updatedNormals);
		} else {
			_updatedVerticies.clear();
			_updatedNormals.clear();
		}
	}
}

void TeMesh::update(TeIntrusivePtr<TeModelVertexAnimation> vertexanim) {
	_updatedVerticies.resize(_verticies.size());
	_updatedNormals.resize(_normals.size());

	const Common::Array<TeVector3f32> animverts = vertexanim->getVertices();
	assert(animverts.size() >= _verticies.size());
	for (uint i = 0; i < _verticies.size(); i++) {
		_updatedVerticies[i] = animverts[i];
	}
	for (uint i = 0; i < _normals.size(); i++) {
		_updatedNormals[i] = _normals[i];
	}
}

void TeMesh::updateTo(const Common::Array<TeMatrix4x4> *matricies1, const Common::Array<TeMatrix4x4> *matricies2,
				Common::Array<TeVector3f32> &verts, Common::Array<TeVector3f32> &normals) {
	static const TeMatrix4x4 emptyMatrix;
	for (uint i = 0; i < _verticies.size(); i++) {
		uint m = _matricies[i];
		const TeMatrix4x4 *mat;
		if (m < matricies1->size()) {
			mat = &((*matricies1)[m]);
		} else {
			m -= matricies1->size();
			if (m < matricies2->size())
				mat = &((*matricies2)[m]);
			else
				mat = &emptyMatrix;
		}
		verts[i] = mat->mult4x3(_verticies[i]);
		normals[i] = mat->mult3x3(_normals[i]);
	}
}

/*static*/
TeMesh *TeMesh::makeInstance() {
	Graphics::RendererType r = g_engine->preferredRendererType();

#if defined(USE_OPENGL_GAME)
	if (r == Graphics::kRendererTypeOpenGL)
		return new TeMeshOpenGL();
#endif

#if defined(USE_TINYGL)
	if (r == Graphics::kRendererTypeTinyGL)
		return new TeMeshTinyGL();
#endif
	error("Couldn't create TeMesh for selected renderer");

}

} // end namespace Tetraedge