-
Notifications
You must be signed in to change notification settings - Fork 13
/
OffscreenMeshVisualizer.h
139 lines (119 loc) · 3.37 KB
/
OffscreenMeshVisualizer.h
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
#ifndef FACESHAPEFROMSHADING_OFFSCREENMESHVISUALIZER_H
#define FACESHAPEFROMSHADING_OFFSCREENMESHVISUALIZER_H
//#include "Geometry/geometryutils.hpp"
//#include "Utils/utility.hpp"
#include "basicmesh.h"
#include "parameters.h"
#include <QDir>
#include <QImage>
#include <QOpenGLContext>
#include <QOpenGLFramebufferObject>
#include <QOffscreenSurface>
#include <boost/timer/timer.hpp>
#include "nlohmann/json.hpp"
using json = nlohmann::json;
namespace ColorEncoding {
inline void encode_index(int idx, unsigned char& r, unsigned char& g, unsigned char& b) {
r = static_cast<unsigned char>(idx & 0xff); idx >>= 8;
g = static_cast<unsigned char>(idx & 0xff); idx >>= 8;
b = static_cast<unsigned char>(idx & 0xff);
}
inline int decode_index(unsigned char r, unsigned char g, unsigned char b, int& idx) {
idx = b; idx <<= 8; idx |= g; idx <<= 8; idx |= r;
return idx;
}
}
class OffscreenMeshVisualizer {
public:
enum MVPMode {
OrthoNormal,
OrthoNormalExtended,
CamPerspective,
BackgroundImage
};
enum RenderMode {
Texture,
BarycentricCoordinates,
Normal,
Mesh,
MeshAndImage,
TexturedMesh
};
OffscreenMeshVisualizer(int width, int height)
: width(width), height(height), index_encoded(true), lighting_enabled(false) {
// Load rendering settings
{
const string home_directory = QDir::homePath().toStdString();
cout << "Home dir: " << home_directory << endl;
ifstream fin(home_directory + "/Data/Settings/blendshape_vis_ao.json");
fin >> rendering_settings;
}
}
void LoadRenderingSettings(const string& filename) {
ifstream fin(filename);
fin >> rendering_settings;
}
void BindMesh(const BasicMesh& in_mesh) {
mesh = in_mesh;
}
void BindTexture(const QImage& in_texture) {
texture = in_texture;
}
void BindImage(const QImage& img) {
image = img;
}
void SetMeshRotationTranslation(const Vector3d& R, const Vector3d& T) {
mesh_rotation = R;
mesh_translation = T;
}
void SetCameraParameters(const CameraParameters& cam_params) {
camera_params = cam_params;
}
void SetFacesToRender(const vector<int>& indices) {
faces_to_render = indices;
}
void SetNormals(const vector<float>& ns) {
normals = ns;
}
void SetAmbientOcclusion(const vector<float>& ao_in) {
ao = ao_in;
}
void SetRenderMode(RenderMode mode_in) {
render_mode = mode_in;
}
void SetMVPMode(MVPMode mode_in) {
mode = mode_in;
}
void SetIndexEncoded(bool val) {
index_encoded = val;
}
void SetEnableLighting(bool val) {
lighting_enabled = val;
}
QImage Render(bool multi_sampled=false) const;
pair<QImage, vector<float>> RenderWithDepth(bool multi_sampled=false) const;
protected:
void SetupViewing(const MVPMode&) const;
void CreateTexture() const;
void EnableLighting() const;
void DisableLighting() const;
private:
int width, height;
MVPMode mode;
RenderMode render_mode;
Vector3d mesh_rotation, mesh_translation;
CameraParameters camera_params;
mutable vector<int> faces_to_render;
mutable vector<float> normals;
mutable vector<float> ao;
bool index_encoded;
bool lighting_enabled;
BasicMesh mesh;
QImage image;
mutable GLuint image_tex;
QImage texture;
mutable glm::dmat4 Mview;
json rendering_settings;
mutable vector<GLuint> enabled_lights;
};
#endif //FACESHAPEFROMSHADING_OFFSCREENMESHVISUALIZER_H