File: floorface.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (209 lines) | stat: -rw-r--r-- 6,467 bytes parent folder | download | duplicates (2)
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
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the AUTHORS
 * 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 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "engines/stark/resources/floorface.h"

#include "engines/stark/debug.h"
#include "engines/stark/formats/xrc.h"
#include "engines/stark/resources/floor.h"

namespace Stark {
namespace Resources {

FloorFace::FloorFace(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Object(parent, subType, index, name),
		_distanceFromCamera(0),
		_unk2(0) {
	_type = TYPE;

	for (uint i = 0; i < ARRAYSIZE(_indices); i++) {
		_indices[i] = 0;
	}
}

FloorFace::~FloorFace() {
}

bool FloorFace::isPointInside(const Math::Vector3d &point) const {
	// Compute the barycentric coordinates of the point in the triangle
	float area = 1.0f / 2.0f
			* (-_vertices[1].y() * _vertices[2].x()
					+ _vertices[0].y() * (-_vertices[1].x() + _vertices[2].x())
					+ _vertices[0].x() * (_vertices[1].y() - _vertices[2].y())
					+ _vertices[1].x() * _vertices[2].y());

	float s = (_vertices[0].y() * _vertices[2].x() - _vertices[0].x() * _vertices[2].y()
			+ (_vertices[2].y() - _vertices[0].y()) * point.x()
			+ (_vertices[0].x() - _vertices[2].x()) * point.y())
					/ (2.0f * area);

	float t = (_vertices[0].x() * _vertices[1].y() - _vertices[0].y() * _vertices[1].x()
			+ (_vertices[0].y() - _vertices[1].y()) * point.x()
			+ (_vertices[1].x() - _vertices[0].x()) * point.y())
					/ (2.0f * area);

	// Check the coordinates are in the triangle
	return s > 0.0f && t > 0.0f && (1.0f - s - t) > 0.0f;
}

void FloorFace::computePointHeight(Math::Vector3d &point) const {
	// Compute the barycentric coordinates of the point in the triangle
	float area = 1.0f / 2.0f
			* (-_vertices[1].y() * _vertices[2].x()
					+ _vertices[0].y() * (-_vertices[1].x() + _vertices[2].x())
					+ _vertices[0].x() * (_vertices[1].y() - _vertices[2].y())
					+ _vertices[1].x() * _vertices[2].y());

	float s = (_vertices[0].y() * _vertices[2].x() - _vertices[0].x() * _vertices[2].y()
			+ (_vertices[2].y() - _vertices[0].y()) * point.x()
			+ (_vertices[0].x() - _vertices[2].x()) * point.y())
					/ (2.0f * area);

	float t = (_vertices[0].x() * _vertices[1].y() - _vertices[0].y() * _vertices[1].x()
			+ (_vertices[0].y() - _vertices[1].y()) * point.x()
			+ (_vertices[1].x() - _vertices[0].x()) * point.y())
					/ (2.0f * area);

	// Compute the Z coordinate of the point
	float pointZ = (1.0f - s - t) * _vertices[0].z() + s * _vertices[1].z() + t * _vertices[2].z();

	point.setValue(2, pointZ);
}

bool FloorFace::intersectRay(const Math::Ray &ray, Math::Vector3d &intersection) const {
	// Compute the triangle plane normal
	Math::Vector3d n = Math::Vector3d::crossProduct(_vertices[1] - _vertices[0],  _vertices[2] - _vertices[0]);
	if (n == Math::Vector3d()) {
		return false; // We don't handle degenerate triangles
	}

	// Point on triangle plane: dot(P - _vertices[0], n) = 0
	// Point on ray: P = origin + r * direction
	// Point on both => r = - dot(n, origin - _vertices[0]) / dot(n, direction)

	float num = -Math::Vector3d::dotProduct(n, ray.getOrigin() - _vertices[0]);
	float denom = Math::Vector3d::dotProduct(n, ray.getDirection());

	if (fabs(denom) < 0.00001) {
		// The ray is parallel to the plane
		return false;
	}

	float r = num / denom;
	if (r < 0.0) {
		// The ray goes away from the triangle
		return false;
	}

	// Compute the intersection point between the triangle plane and the ray
	intersection = ray.getOrigin() + r * ray.getDirection();

	// Check the intersection point is inside the triangle
	return isPointInside(intersection);
}

float FloorFace::distanceToRay(const Math::Ray &ray) const {
	Math::Vector3d center = getCenter();
	return Math::Vector3d::crossProduct(ray.getDirection(), center - ray.getOrigin()).getMagnitude();
}

float FloorFace::getDistanceFromCamera() const {
	return _distanceFromCamera;
}

int16 FloorFace::getVertexIndex(int32 index) const {
	assert(index < 3);
	return _indices[index];
}

void FloorFace::addEdge(FloorEdge *edge) {
	_edges.push_back(edge);
}

Common::Array<FloorEdge *> FloorFace::getEdges() const {
	return _edges;
}

FloorEdge *FloorFace::findNearestEdge(const Math::Vector3d &point) const {
	float minDistance = -1;
	FloorEdge *edge = nullptr;

	for (uint i = 0; i < _edges.size(); i++) {
		if (!_edges[i]->isEnabled()) {
			continue;
		}

		float distance = (point - _edges[i]->getPosition()).getSquareMagnitude();


		if (!edge || distance < minDistance) {
			minDistance = distance;
			edge = _edges[i];
		}
	}

	return edge;
}

Math::Vector3d FloorFace::getCenter() const {
	return (_vertices[0] + _vertices[1] + _vertices[2]) / 3.0;
}

bool FloorFace::hasVertices() const {
	return _indices[0] != 0 || _indices[1] != 0 || _indices[2] != 0;
}

void FloorFace::enable(bool e) {
	for (uint i = 0; i < _edges.size(); i++) {
		_edges[i]->enable(e);
	}
}

void FloorFace::readData(Formats::XRCReadStream *stream) {
	for (uint i = 0; i < ARRAYSIZE(_indices); i++) {
		_indices[i] = stream->readSint16LE();
	}

	_distanceFromCamera = stream->readFloat();

	for (uint i = 0; i < ARRAYSIZE(_indices); i++) {
		stream->readSint16LE(); // Skipped in the original
	}

	_unk2 = stream->readFloat();
}

void FloorFace::onAllLoaded() {
	Object::onAllLoaded();
	Floor *floor = Object::cast<Floor>(_parent);

	for (uint i = 0; i < ARRAYSIZE(_indices); i++) {
		_vertices[i] = floor->getVertex(_indices[i]);
	}
}

void FloorFace::printData() {
	debug("indices: %d %d %d, distanceFromCamera %f, unk2 %f", _indices[0], _indices[1], _indices[2], _distanceFromCamera, _unk2);
}

} // End of namespace Resources
} // End of namespace Stark