-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalkMesh.hpp
92 lines (77 loc) · 3.1 KB
/
WalkMesh.hpp
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
#pragma once
#include <glm/glm.hpp>
#include <vector>
#include <unordered_map>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp> //allows the use of 'uvec2' as an unordered_map key
struct WalkMesh {
//Walk mesh will keep track of triangles, vertices:
std::vector< glm::vec3 > vertices;
std::vector< glm::vec3 > normals;
std::vector< glm::uvec3 > triangles; //CCW-oriented
//TODO: consider also loading vertex normals for interpolated "up" direction:
//std::vector< glm::vec3 > vertex_normals;
//This "next vertex" map includes [a,b]->c, [b,c]->a, and [c,a]->b for each triangle, and is useful for checking what's over an edge from a given point:
std::unordered_map< glm::uvec2, uint32_t > next_vertex;
struct WalkPoint {
glm::uvec3 triangle = glm::uvec3(-1U); //indices of current triangle
glm::vec3 weights = glm::vec3(std::numeric_limits< float >::quiet_NaN()); //barycentric coordinates for current point
};
void closestpt2triangle(std::vector<glm::vec3> &trianglePoints, glm::vec3 const &position, glm::vec3 &closestPoint) const;
void barycentric(glm::vec3 p, glm::vec3 a, glm::vec3 b, glm::vec3 c, float &u, float &v, float &w) const;
//used to initialize walking -- finds the closest point on the walk mesh:
// (should only need to call this at the start of a level)
WalkPoint start(glm::vec3 const &world_point) const;
//used to update walk point:
void walk(WalkPoint &wp, glm::vec3 const &step) const;
//used to read back results of walking:
glm::vec3 world_point(WalkPoint const &wp) const {
return wp.weights.x * vertices[wp.triangle.x]
+ wp.weights.y * vertices[wp.triangle.y]
+ wp.weights.z * vertices[wp.triangle.z];
}
glm::vec3 world_normal(WalkPoint const &wp) const {
//TODO: could interpolate vertex_normals instead of computing the triangle normal:
return glm::normalize(glm::cross(
vertices[wp.triangle.y] - vertices[wp.triangle.x],
vertices[wp.triangle.z] - vertices[wp.triangle.x]
));
}
WalkMesh(std::string const &filename);
};
// /*
// // The intent is that game code will work something like this:
//
// Load< WalkMesh > walk_mesh;
//
// Game {
// WalkPoint walk_point;
// }
// Game::Game() {
// //...
// walk_point = walk_mesh->start(level_start_position);
// }
//
// Game::update(float elapsed) {
// //update position on walk mesh:
// glm::vec3 step = player_forward * speed * elapsed;
// walk_mesh->walk(walk_point, step);
//
// //update player position:
// player_at = walk_mesh->world_point(walk_point);
//
// //update player orientation:
// glm::vec3 old_player_up = player_up;
// player_up = walk_mesh->world_normal(walk_point);
//
// glm::quat orientation_change = (compute rotation that takes old_player_up to player_up)
// player_forward = orientation_change * player_forward;
//
// //make sure player_forward is perpendicular to player_up (the earlier rotation should ensure that, but it might drift over time):
// player_forward = glm::normalize(player_forward - player_up * glm::dot(player_up, player_forward));
//
// //compute rightward direction from forward and up:
// player_right = glm::cross(player_forward, player_up);
//
// }
// */