File: te_pick_mesh2.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 (178 lines) | stat: -rw-r--r-- 5,430 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
/* 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 "common/util.h"
#include "common/math.h"

#include "tetraedge/tetraedge.h"

#include "tetraedge/te/te_mesh.h"
#include "tetraedge/te/te_pick_mesh2.h"
#include "tetraedge/te/te_renderer.h"
#include "tetraedge/te/te_ray_intersection.h"

namespace Tetraedge {

TePickMesh2::TePickMesh2() : _lastTriangleHit(0) {
}

void TePickMesh2::draw() {
	if (!worldVisible())
		return;

	const uint nverticies = _verticies.size();
	Common::SharedPtr<TeMesh> mesh(TeMesh::makeInstance());
	mesh->setConf(nverticies, nverticies, TeMesh::MeshMode_Triangles, 0, 0);
	for (uint i = 0; i < nverticies; i++) {
		mesh->setIndex(i, i);
		mesh->setVertex(i, _verticies[i]);
	}

	TeRenderer *renderer = g_engine->getRenderer();

	const TeColor prevCol = renderer->currentColor();

	renderer->enableWireFrame(); // NOTE: added this so we can draw _clickMeshes in scene.
	renderer->setCurrentColor(TeColor(0xff, 0, 0, 0xff));
	renderer->pushMatrix();
	renderer->multiplyMatrix(transformationMatrix());
	mesh->draw();

	renderer->popMatrix();
	renderer->setCurrentColor(prevCol);
	renderer->disableWireFrame();
}

bool TePickMesh2::intersect(const TeVector3f32 &origin, const TeVector3f32 &dir, TeVector3f32 &hitPtOut, float &hitDistOut, bool lastHitFirst, unsigned long *triangleHitOut) {
	if (_verticies.size() / 3 == 0)
		return false;

	TeVector3f32 hitPt;
	float hitDist;
	const TeMatrix4x4 worldTrans = worldTransformationMatrix();
	const Math::Ray ray(origin, dir);
	if (lastHitFirst) {
		const TeVector3f32 triv1 = worldTrans * _verticies[_lastTriangleHit * 3 + 0];
		const TeVector3f32 triv2 = worldTrans * _verticies[_lastTriangleHit * 3 + 1];
		const TeVector3f32 triv3 = worldTrans * _verticies[_lastTriangleHit * 3 + 2];
		bool result = ray.intersectTriangle(triv1, triv2, triv3, hitPt, hitDist);
		if (result && hitDist >= 0.0 && hitDist < FLT_MAX) {
			hitPtOut = origin + dir * hitDist;
			hitDistOut = hitDist;
			if (triangleHitOut)
				*triangleHitOut = _lastTriangleHit;
			return true;
		}
	}

	float lastHitDist = FLT_MAX;
	for (uint i = 0; i < _verticies.size() / 3; i++) {
		const TeVector3f32 triv1 = worldTrans * _verticies[i * 3 + 0];
		const TeVector3f32 triv2 = worldTrans * _verticies[i * 3 + 1];
		const TeVector3f32 triv3 = worldTrans * _verticies[i * 3 + 2];
		bool result = ray.intersectTriangle(triv1, triv2, triv3, hitPt, hitDist);
		if (result && hitDist >= 0.0 && hitDist < FLT_MAX) {
			_lastTriangleHit = i;
			lastHitDist = hitDist;
			if (lastHitFirst)
				break;
		}
	}
	if (lastHitDist != FLT_MAX) {
		hitPtOut = origin + dir * lastHitDist;
		hitDistOut = lastHitDist;
		if (triangleHitOut)
			*triangleHitOut = _lastTriangleHit;
		return true;
	}
	return false;
}

bool TePickMesh2::intersect2D(const TeVector2f32 &pt) {
	error("TODO: Implement TePickMesh2::intersect2D");
}

unsigned long TePickMesh2::lastTriangleHit() const {
	if (_lastTriangleHit < _verticies.size() / 3)
		return _lastTriangleHit;
	return 0;
}

static float TeSgn(float f) {
	if (f < 0.0)
		return -1.0;
	else if (f > 0.0)
		return 1.0;
	return 0.0;
}

bool TePickMesh2::pointInTriangle(const TeVector2f32 &p1, const TeVector2f32 &p2, const TeVector2f32 &p3, const TeVector2f32 &p4) const {
	float f1 = TeSgn(TeVector2f32(p3 - p2).crossProduct(p4 - p2));
	float f2 = TeSgn(TeVector2f32(p3 - p2).crossProduct(p1 - p2));

	f1 = -f1;
	if (f1 == f2)
		return false;

	f2 = TeSgn(TeVector2f32(p4 - p3).crossProduct(p1 - p3));
	if (f1 == f2)
		return false;

	f2 = TeSgn(TeVector2f32(p2 - p4).crossProduct(p1 - p4));
	return f1 != f2;
}

void TePickMesh2::setNbTriangles(uint num) {
	_verticies.resize(num * 3);
	_lastTriangleHit = 0;
}

void TePickMesh2::setTriangle(uint num, const TeVector3f32 &v1, const TeVector3f32 &v2, const TeVector3f32 &v3) {
	assert(num <= _verticies.size() / 3);
	_verticies[num * 3 + 0] = v1;
	_verticies[num * 3 + 1] = v2;
	_verticies[num * 3 + 2] = v3;
}

/*static*/
void TePickMesh2::serialize(Common::WriteStream &stream, const TePickMesh2 &mesh) {
	error("TODO: Implement TePickMesh2::serialize");
}

/*static*/
void TePickMesh2::deserialize(Common::ReadStream &stream, TePickMesh2 &mesh) {
	Te3DObject2::deserialize(stream, mesh);
	uint32 ntriangles = stream.readUint32LE();
	if (ntriangles > 100000)
		error("TePickMesh2::deserialize: Improbable number of triangles %d", ntriangles);

	mesh._verticies.resize(ntriangles * 3);
	mesh._lastTriangleHit = 0;

	for (uint i = 0; i < ntriangles * 3; i++) {
		TeVector3f32 vec;
		TeVector3f32::deserialize(stream, vec);
		mesh._verticies[i] = vec;
	}
}


} // end namespace Tetraedge