-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_scene_road.cpp
150 lines (125 loc) · 5.29 KB
/
api_scene_road.cpp
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
#include "api.h"
#include "Route.h"
#include "NetworkEngine.h"
#include <VrLib/tien/components/TerrainRenderer.h>
#include <VrLib/tien/components/MeshRenderer.h>
#include <VrLib/tien/components/Transform.h>
#include <VrLib/tien/Terrain.h>
#include <VrLib/Texture.h>
void buildMesh(vrlib::tien::components::MeshRenderer::Mesh* mesh, const Route& route, const std::function<glm::vec3(const glm::vec2 &)>& getPos)
{
float inc = 0.1f;
glm::vec2 lastPos = route.getPositionXY(0);
vrlib::gl::VertexP3N2B2T2T2 v;
vrlib::gl::setN3(v, glm::vec3(0, 1, 0));
vrlib::gl::setTan3(v, glm::vec3(1, 0, 0));
vrlib::gl::setBiTan3(v, glm::vec3(0, 0, 1));
float t = 0;
int count = 0;
for (float f = inc; f < route.length; f += inc)
{
int i = mesh->vertices.size();
glm::vec2 pos = route.getPositionXY(f);
glm::vec2 diff = lastPos - pos;
glm::vec2 angled = glm::normalize(glm::vec2(diff.y, -diff.x));
for (float ff = -2.5f; ff <= 2.5f; ff += 0.1f)
{
vrlib::gl::setP3(v, getPos(pos + angled * ff)); vrlib::gl::setT2(v, glm::vec2(t, ff)); mesh->vertices.push_back(v);
if (count <= 0)
count--;
}
if (count < 0)
count = -count;
lastPos = pos;
t += inc * 1.0f;
}
for (size_t i = 0; i < mesh->vertices.size() / count; i++)
{
for (int ii = 0; ii < count - 1; ii++)
{
mesh->indices.push_back((count * i + ii + 0) % mesh->vertices.size());
mesh->indices.push_back((count * i + ii + 1) % mesh->vertices.size());
mesh->indices.push_back((count * i + ii + count) % mesh->vertices.size());
mesh->indices.push_back((count * i + ii + 1) % mesh->vertices.size());
mesh->indices.push_back((count * i + ii + count + 1) % mesh->vertices.size());
mesh->indices.push_back((count * i + ii + count) % mesh->vertices.size());
}
}
for (size_t i = 0; i < mesh->indices.size(); i += 3)
{
glm::vec3 normal(0, 1, 0), tan(1, 0, 0), bitan(0, 0, 1);
for (int ii = 0; ii < 3; ii++)
{
vrlib::gl::setN3(mesh->vertices[mesh->indices[i + ii]], normal);
vrlib::gl::setTan3(mesh->vertices[mesh->indices[i + ii]], tan);
vrlib::gl::setBiTan3(mesh->vertices[mesh->indices[i + ii]], bitan);
}
}
}
Api scene_road_add("scene/road/add", [](NetworkEngine* engine, nlohmann::json &data, nlohmann::json &packet)
{
float heightOffset = 0.01f;
if (data.find("heightoffset") != data.end())
heightOffset = data["heightoffset"];
for (size_t i = 0; i < engine->routes.size(); i++)
{
if (engine->routes[i]->id == data["route"])
{
const vrlib::tien::Node* terrainRenderingNode = engine->tien.scene.findNodeWithComponent<vrlib::tien::components::TerrainRenderer>();
//TODO: use terrainRenderingNode->transform->position.y (with parents) in the code down here
const Route& route = *engine->routes[i];
auto getPos = [&engine, &terrainRenderingNode](const glm::vec2 &p)
{
if (!engine->terrain)
return glm::vec3(p.x, 0.01, p.y);
glm::vec2 offset(0, 0);
if (terrainRenderingNode)
offset = glm::vec2(terrainRenderingNode->transform->position.x, terrainRenderingNode->transform->position.z);
return engine->terrain->getPosition(p - offset) + glm::vec3(offset.x, 0.01f, offset.y);
};
{
auto mesh = new vrlib::tien::components::MeshRenderer::Mesh();
mesh->material.texture = vrlib::Texture::loadCached(data.value("diffuse", "data/NetworkEngine/textures/tarmac_diffuse.png"));
mesh->material.normalmap = vrlib::Texture::loadCached(data.value("normal", "data/NetworkEngine/textures/tarmac_normal.png"));
mesh->material.specularmap = vrlib::Texture::loadCached(data.value("specular", "data/NetworkEngine/textures/tarmac_specular.png"));
mesh->material.color.shinyness = 1;
buildMesh(mesh, route, getPos);
vrlib::tien::Node* n = new vrlib::tien::Node("Road", &engine->tien.scene);
packet["status"] = "ok";
packet["data"]["uuid"] = n->guid;
// if(terrainRenderingNode)
// n->addComponent(new vrlib::tien::components::Transform(terrainRenderingNode->transform->position, terrainRenderingNode->transform->rotation, terrainRenderingNode->transform->scale));
// else
n->addComponent(new vrlib::tien::components::Transform(glm::vec3(0, 0, 0)));
n->addComponent(new vrlib::tien::components::MeshRenderer(mesh));
}
return;
}
}
packet["error"] = "Route not found";
});
Api scene_road_update("scene/road/update", [](NetworkEngine* engine, nlohmann::json &data, nlohmann::json &packet)
{
for (size_t i = 0; i < engine->routes.size(); i++)
{
if (engine->routes[i]->id == data["route"])
{
auto renderingNode = engine->tien.scene.findNodeWithName(data["id"]);
const vrlib::tien::Node* terrainRenderingNode = engine->tien.scene.findNodeWithComponent<vrlib::tien::components::TerrainRenderer>();
const Route& route = *engine->routes[i];
auto getPos = [&engine, &terrainRenderingNode](const glm::vec2 &p)
{
if (!engine->terrain)
return glm::vec3(p.x, 0.01, p.y);
glm::vec2 offset(0, 0);
if (terrainRenderingNode)
offset = glm::vec2(terrainRenderingNode->transform->position.x, terrainRenderingNode->transform->position.z);
return engine->terrain->getPosition(p - offset) + glm::vec3(offset.x, 0.01f, offset.y);
};
auto meshRenderer = renderingNode->getComponent<vrlib::tien::components::MeshRenderer>();
buildMesh(meshRenderer->mesh, route, getPos);
meshRenderer->updateMesh();
}
}
packet["error"] = "Route not found";
});