forked from Borf/NetworkEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_scene_road.cpp
138 lines (102 loc) · 4.09 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
#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>
Api scene_road_add("scene/road/add", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, json &data)
{
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>();
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/NetworkEngine/textures/tarmac_diffuse.png");
mesh->material.normalmap = vrlib::Texture::loadCached("data/NetworkEngine/textures/tarmac_normal.png");
mesh->material.specularmap = vrlib::Texture::loadCached("data/NetworkEngine/textures/tarmac_specular.png");
mesh->material.color.shinyness = 1;
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 (int 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);
}
}
vrlib::tien::Node* n = new vrlib::tien::Node("Road", &engine->tien.scene);
n->addComponent(new vrlib::tien::components::Transform(glm::vec3(0, 0, 0)));
n->addComponent(new vrlib::tien::components::MeshRenderer(mesh));
}
json packet;
packet["id"] = "scene/road/add";
packet["data"]["status"] = "ok";
tunnel->send(packet);
return;
}
}
json packet;
packet["id"] = "route/delete";
packet["data"]["status"] = "error";
packet["data"]["error"] = "Route not found";
tunnel->send(packet);
});
Api scene_road_delete("scene/road/delete", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, json &data)
{
sendError(tunnel, "scene/road/delete", "Not implemented");
});