-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
277 lines (214 loc) · 8.75 KB
/
main.cpp
File metadata and controls
277 lines (214 loc) · 8.75 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <iostream>
#include <opencv2/opencv.hpp>
#include <utils.hpp>
#include <render.h>
#include <config.h>
/**
* Generate layered RGBA-D data for each mesh area, together with intrinsics
*/
int generateLayers(){
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "RGB render", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//glfwSetCursorPosCallback(window, mouse_callback);
//glfwSetScrollCallback(window, scroll_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
unsigned int textureColorbuffer;
configFramebuffer(textureColorbuffer);
glEnable(GL_DEPTH_TEST);
std::vector<MeshItem> mItem;
std::vector<Texture> mTextures;
cv::Mat instance, depth;
cv::Point p;
cv::Vec4b targetInstance;
std::map<int, std::vector<int> > hashSubmesh;
std::map<int, std::string> instance2room;
int meshId;
load3DModel(mItem, mTextures, instance2room);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
initTextures(mTextures);
std::ifstream pstream;
std::string posefile = ROOT_DIR + "data/poses.txt";
pstream.open(posefile.c_str(), std::ifstream::in);
if (!pstream.is_open())
{
std::cout << posefile.c_str() << std::endl;
std::cout << "poses.txt not found" << std::endl;
throw;
}
std::ifstream rgbstream;
std::string rgbfile = ROOT_DIR + "data/rgbs.txt";
rgbstream.open(rgbfile.c_str(), std::ifstream::in);
if (!rgbstream.is_open())
{
std::cout << rgbfile.c_str() << std::endl;
std::cout << "rgbs.txt not found" << std::endl;
throw;
}
// for every view in the area
for(int view = 0; view < NVIEWS; view++){
std::map<ushort, int> hashInstance;
std::vector<cv::Mat> depth;
std::vector<cv::Mat> color;
std::vector<cv::Mat> label;
std::cout << "--------- view " << view << " ----------" << std::endl;
std::string poseName;
pstream >> poseName;
std::string rgbName;
rgbstream >> rgbName;
Frame frame;
frame.posePath = ROOT_DIR + "data/pose/" + poseName;
readK(frame);
//initialize buffers
initBuffers(mItem);
// optinally render perturbed view ====================
if(RENDER_PERTURBED_VIEW){
std::ifstream ststream;
ststream.open(OUT_DIR + AREA + "/pose_st/" + std::to_string(view) + ".txt");
float trans[16] = {0};
trans[15] = 1;
ststream >> trans[0] >> trans[1] >> trans[2] >> trans[3] >>
trans[4] >> trans[5] >> trans[6] >> trans[7] >>
trans[8] >> trans[9] >> trans[10] >> trans[11];
frame.perturbPose = glm::make_mat4(trans);
frame.perturbPose = glm::transpose(frame.perturbPose);
frame.perturbPose = glm::inverse(frame.perturbPose);
ststream.close();
renderView(window, mItem, mTextures, "color", frame, true);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
getColor(true, AREA, std::to_string(view) + "_target");
renderView(window, mItem, mTextures, "depth", frame, true);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
getDepth(true, AREA, std::to_string(view) + "_target");
}
// save intrinsics and original rgb name for every view/frame ============
Json::Value metadata;
std::ifstream imetafile(OUT_DIR + AREA + "/metadata.json", std::ifstream::binary);
if(imetafile){
imetafile >> metadata;
}
imetafile.close();
Json::Value framedata;
framedata["source_frame"] = AREA + "/data/rgb/" + rgbName;
framedata["intrinsics"].append(frame.fx);
framedata["intrinsics"].append(frame.fy);
framedata["intrinsics"].append(frame.cx);
framedata["intrinsics"].append(frame.cy);
metadata[std::to_string(view)] = framedata;
std::ofstream ometafile(OUT_DIR + AREA + "/metadata.json", std::ifstream::binary);
ometafile << metadata;
ometafile.close();
// ==================================================
// render full color, depth and instance image ======
renderView(window, mItem, mTextures, "color", frame, false);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
getColor(true, AREA, std::to_string(view));
renderView(window, mItem, mTextures, "depth", frame, false);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
getDepth(true, AREA, std::to_string(view));
renderView(window, mItem, mTextures, "instance", frame, false);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
instance = getLabel(true, AREA, std::to_string(view));
// ==================================================
//cv::waitKey(500);
// get set of unique instances that appear in the view
for(int i=0; i<SCR_HEIGHT; i++){
for(int j=0; j<SCR_WIDTH; j++){
hashInstance.insert(std::pair<ushort, int> (instance.at<unsigned short>(i,j), 0));
}
}
int t = 0;
std::string currentRoom = getRoomLabel(poseName);
// save each unique instance in the current room as a separate layer
for (std::map<ushort,int>::iterator it=hashInstance.begin(); it!=hashInstance.end(); ++it){
if(it->first == 0 || currentRoom != instance2room[(int)it->first]){
continue;
}
targetInstance = instanceIDtoRGB(it->first);
for(int m=0; m<mItem.size(); m++){
extractLayer(targetInstance, mItem[m]);
}
initBuffers(mItem);
// render color, depth and instance layers ======
renderView(window, mItem, mTextures, "color", frame, false);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
color.push_back(getColor(false, AREA, std::to_string(view)));
renderView(window, mItem, mTextures, "depth", frame, false);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
depth.push_back(getDepth(false, AREA, std::to_string(view)));
renderView(window, mItem, mTextures, "instance", frame, false);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
label.push_back(getLabel(false, AREA, std::to_string(view)));
// ==============================================
restoreIndices(mItem);
t++;
}
//sortLayers(depth, color, label);
std::cout << "number of layers: " << depth.size() << std::endl;
// save all layers of a frame
saveDepthLayers(AREA, depth, view);
saveColorLayers(AREA, color, view);
saveLabelLayers(AREA, label, view);
}
pstream.close();
glDeleteVertexArrays(1, &VAO);
// clear memory leaks ======================
//glDeleteVertexArrays(1, &VAO);
for(int m=0; m < mItem.size(); m++){
glDeleteBuffers(1, &mItem[m].VBO);
glDeleteBuffers(1, &mItem[m].EBO);
mItem[m].vertices.clear();
mItem[m].vertices.shrink_to_fit();
mItem[m].indices.clear();
mItem[m].indices.shrink_to_fit();
mItem[m].tempIndices.clear();
mItem[m].tempIndices.shrink_to_fit();
}
for(int m=0; m<mTextures.size(); m++){
glDeleteTextures(1, &mTextures[m].id);
}
mTextures.clear();
mTextures.shrink_to_fit();
mItem.clear();
mItem.shrink_to_fit();
// ========================================
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
int main()
{
generateLayers();
return 0;
}