-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (143 loc) · 5.72 KB
/
main.cpp
File metadata and controls
175 lines (143 loc) · 5.72 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
#include "Globals.hpp"
#include "DataParser.hpp"
#include "GeometryUtils.hpp"
#include "CameraControl.hpp"
#include "Shader.hpp"
#include <iostream>
#include <memory>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <chrono>
#include "Window.hpp"
#include "renderers/Renderer.hpp"
// Global current camera
InputCamera* currCamera;
void error_callback( int error, const char *msg ) {
std::string s;
s = " [" + std::to_string(error) + "] " + msg + '\n';
std::cerr << s << std::endl;
}
int main(int argc, char** argv){
if (argc == 1){
fprintf( stderr, "Need extra CLI argument\n" );
return -1;
}
bool useproxy = true;
std::string datasetDir(argv[1]);
if (datasetDir.back() == '/')
datasetDir = datasetDir.substr(0, datasetDir.size()-1);
// argv[2] is either to use a simple proxy (true) or SfM reconstruction (false)
if (argc == 3)
useproxy = bool(atoi(argv[2]));
const vector<InputCamera> cameras = readCameras(datasetDir);
vector<glm::vec3> sfmVertices;
vector<glm::ivec3> faces;
// Read SfM anyway to reason about proxy
tie(sfmVertices, faces) = readPLY(datasetDir);
vector<glm::vec3> viewdirs, centers;
for(auto It = cameras.begin(); It != cameras.end(); ++It)
{
viewdirs.push_back(-glm::row(It->R, 2));
centers.push_back(It->C);
}
// Define proxy
std::unique_ptr<Mesh> proxy;
if (pangolin::FileExists(datasetDir + "/meshes/recon.ply") && !useproxy)
proxy = make_unique<Plane>(sfmVertices, faces);
else
proxy = shapeFromPoses(sfmVertices, centers, viewdirs);
// Add camera centres to proxy
for(auto It = cameras.begin(); It != cameras.end(); ++It)
{
proxy->vertices.push_back(It->C);
}
// Initialize triangulated image grid from SfM.
int imGridNumVertices, imGridNumTriangles;
vector<glm::ivec3> imGridTriangles(proxy->indices.begin(), proxy->indices.end());
vector<glm::vec3> imGridVertices(proxy->vertices.begin(), proxy->vertices.end());
std::unique_ptr<Mesh> imGrid = std::make_unique<Plane>(imGridVertices, imGridTriangles);
imGridNumVertices = imGrid->vertices.size();
imGridNumTriangles = imGrid->indices.size();
glfwSetErrorCallback( error_callback );
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
// glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
MyWindow* window = new MyWindow(currCamera, &cameras);
std::cout << glGetError() << std::endl;
if( !window )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window->mWindow);
// Init GLEW (after creating context!) otherwise we cant use its functionality
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
// Get OpenGL version string
std::cout << "" << "GLFW Version: " << GLFW_VERSION_MAJOR << "." << GLFW_VERSION_MINOR << std::endl;
std::cout << "" << "OpenGL Vendor: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "" << "OpenGL Renderer: " << glGetString(GL_RENDERER) << std::endl;
std::cout << "" << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
// initialize current camera
window->totalCameras = cameras.size();
window->currIndex = cameras.size() / 2; // start with the middle camera
currCamera = new InputCamera(cameras[window->currIndex]);
// create renderers
RendererDepth rendererDepth(*currCamera, *proxy);
RendererULR rendererULR(*currCamera, *imGrid, rendererDepth.getDepthTexture(), !useproxy);
// Assing camera texture IDs and initialize input RGBAs after openGL context creation
std::vector<float> depth(currCamera->img.w*currCamera->img.h);
for (auto& cam : cameras)
{
*currCamera = cam;
rendererDepth.render();
glBindTexture(GL_TEXTURE_2D, rendererDepth.getDepthTexture());
glGetTexImage(GL_TEXTURE_2D, 0, GL_RED, GL_FLOAT, depth.data());
const_cast<InputCamera&>(cam).assignTextureID(depth);
}
// back to original initialization
*currCamera = cameras[window->currIndex];
std::map<float, int> dists; // distance, index
do{
// Legacy: blending candidates
glm::mat4 kviews[window->k], kprojections[window->k];
// find closest cameras to the current one
dists = closestKCams(*currCamera, cameras, window->k, kviews, kprojections);
window->dists = dists;
if (useproxy)
{
// get new triangulated image grid
imGrid->indices.clear();
imGrid->vertices.clear();
tie(imGridNumVertices, imGridNumTriangles) = triangulation(*proxy, *currCamera, &imGrid->indices, &imGrid->vertices);
}
window->imGridVertices = &imGrid->vertices;
window->imGridTriangles = &imGrid->indices;
// render
rendererDepth.render();
rendererULR.render(cameras, dists, *window);
glfwSwapBuffers(window->mWindow);
glfwPollEvents();
}while(glfwGetKey(window->mWindow, GLFW_KEY_ESCAPE) != GLFW_PRESS);
rendererDepth.deleteRenderer();
rendererULR.deleteRenderer();
delete currCamera;
delete window;
glfwTerminate();
return 0;
}