-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.cpp
171 lines (142 loc) · 5.35 KB
/
scene.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "scene.hpp"
#include <cassert>
#include <cstdio>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/mat4x4.hpp>
#include <vector>
static Status LoadSplines(const char *track_filepath,
std::vector<std::vector<glm::vec3>> *splines) {
assert(track_filepath);
assert(splines);
auto &splines_ = *splines;
std::FILE *track_file = std::fopen(track_filepath, "r");
if (!track_file) {
std::fprintf(stderr, "Failed to open track file %s.\n", track_filepath);
return kStatus_IoError;
}
uint spline_count;
int rc = std::fscanf(track_file, "%u", &spline_count);
if (rc < 1) {
std::fprintf(stderr, "Failed to read spline count from track file %s.\n",
track_filepath);
std::fclose(track_file);
return kStatus_IoError;
}
splines->resize(spline_count);
char filepath[4096];
for (uint i = 0; i < spline_count; ++i) {
rc = std::fscanf(track_file, "%s", filepath);
if (rc < 1) {
std::fprintf(stderr,
"Failed to read path of spline file from track file %s.\n",
track_filepath);
std::fclose(track_file);
return kStatus_IoError;
}
FILE *file = std::fopen(filepath, "r");
if (!file) {
std::fprintf(stderr, "Failed to open spline file %s.\n", filepath);
std::fclose(track_file);
return kStatus_IoError;
}
uint ctrl_point_count;
uint type;
rc = std::fscanf(file, "%u %u", &ctrl_point_count, &type);
if (rc < 1) {
std::fprintf(stderr,
"Failed to read control point count and spline type from "
"spline file %s.\n",
filepath);
std::fclose(file);
std::fclose(track_file);
return kStatus_IoError;
}
splines_[i].resize(ctrl_point_count);
uint j = 0;
while ((rc = std::fscanf(file, "%f %f %f", &splines_[i][j].x,
&splines_[i][j].y, &splines_[i][j].z)) > 0) {
++j;
}
if (rc == 0) {
std::fprintf(stderr,
"Failed to read control point from spline file %s.\n",
filepath);
std::fclose(file);
std::fclose(track_file);
return kStatus_IoError;
}
std::fclose(file);
}
std::fclose(track_file);
return kStatus_Ok;
}
Status MakeScene(const SceneConfig *cfg, Scene *scene) {
assert(cfg);
assert(scene);
std::vector<std::vector<glm::vec3>> splines;
Status status = LoadSplines(cfg->track_filepath, &splines);
if (status != kStatus_Ok) {
std::fprintf(stderr, "Could not load splines.\n");
return status;
}
if (cfg->is_verbose) {
std::printf("Loaded spline count: %lu\n", splines.size());
for (uint i = 0; i < splines.size(); ++i) {
std::printf("Control point count in spline %u: %lu\n", i,
splines[i].size());
}
}
// TODO: Support for multiple splines.
assert(splines.size() == 1);
scene->camspl.mesh = new Mesh;
scene->camspl.mesh->vertex_list_type = kVertexListType_1P1T1N1B;
MakeCameraPath(splines[0].data(), splines[0].size(),
cfg->max_spline_segment_len, &scene->camspl.mesh->vl1p1t1n1b);
scene->ground.mesh = new Mesh;
MakeAxisAlignedXzSquarePlane(cfg->aabb_side_len, cfg->ground_tex_repeat_count,
scene->ground.mesh);
scene->ground.world_transform =
glm::translate(glm::mat4(1), cfg->ground_position);
scene->sky.mesh = new Mesh;
MakeAxisAlignedCube(cfg->aabb_side_len, cfg->sky_tex_repeat_count,
scene->sky.mesh);
scene->sky.world_transform = glm::translate(glm::mat4(1), cfg->sky_position);
scene->left_rail.mesh = new Mesh;
scene->left_rail.mesh->vertex_list_type = kVertexListType_1P1C;
scene->right_rail.mesh = new Mesh;
scene->right_rail.mesh->vertex_list_type = kVertexListType_1P1C;
MakeRails(&scene->camspl.mesh->vl1p1t1n1b, &cfg->rails_color,
cfg->rails_head_w, cfg->rails_head_h, cfg->rails_web_w,
cfg->rails_web_h, cfg->rails_gauge,
cfg->rails_pos_offset_in_camspl_norm_dir, scene->left_rail.mesh,
scene->right_rail.mesh);
scene->left_rail.world_transform =
glm::translate(glm::mat4(1), cfg->rails_position);
scene->right_rail.world_transform =
glm::translate(glm::mat4(1), cfg->rails_position);
scene->crossties.mesh = new Mesh;
scene->crossties.mesh->vertex_list_type = kVertexListType_1P1UV;
MakeCrossties(&scene->camspl.mesh->vl1p1t1n1b, cfg->crossties_separation_dist,
cfg->crossties_pos_offset_in_camspl_norm_dir,
&scene->crossties.mesh->vl1p1uv);
scene->crossties.world_transform =
glm::translate(glm::mat4(1), cfg->crossties_position);
return kStatus_Ok;
}
void FreeModelVertices(Scene *scene) {
assert(scene);
delete[] scene->ground.mesh->vl1p1uv.positions;
delete[] scene->ground.mesh->vl1p1uv.uv;
delete[] scene->ground.mesh->indices;
delete[] scene->sky.mesh->vl1p1uv.positions;
delete[] scene->sky.mesh->vl1p1uv.uv;
delete[] scene->sky.mesh->indices;
delete[] scene->crossties.mesh->vl1p1uv.positions;
delete[] scene->crossties.mesh->vl1p1uv.uv;
delete[] scene->left_rail.mesh->vl1p1c.positions;
delete[] scene->left_rail.mesh->vl1p1c.colors;
delete[] scene->left_rail.mesh->indices;
delete[] scene->right_rail.mesh->vl1p1c.positions;
delete[] scene->right_rail.mesh->vl1p1c.colors;
delete[] scene->right_rail.mesh->indices;
}