-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSketchRenderer.cpp
280 lines (251 loc) · 8.43 KB
/
SketchRenderer.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
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
278
279
280
/*
* File: SketchRenderer.cpp
* Author: swl
*
* Created on November 29, 2015, 12:02 PM
*/
#include <GL/glew.h>
#include "SketchRenderer.h"
GLuint SketchRenderer::colorTexID = 0;
GLuint SketchRenderer::depthBufferID = 0;
GLuint SketchRenderer::frameBufferID = 0;
GLuint SketchRenderer::listID = 0;
GLuint SketchRenderer::renderSize = 256;
float SketchRenderer::shrinkage = 0.9;
float SketchRenderer::depthThres = 0.01;
float SketchRenderer::normalAngleThres = 60;
std::vector<glm::vec3> SketchRenderer::vertices;
void SketchRenderer::init()
{
int ac = 0;
char** av;
glutInit(&ac, av);
glutInitWindowSize(renderSize, renderSize);
glutCreateWindow("SketchRenderer");
glewInit();
listID = glGenLists(1);
glutDisplayFunc(render);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB);
glClearColor(0, 0, 0, 0);
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);
glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
glClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
glGenTextures(1, &colorTexID);
glBindTexture(GL_TEXTURE_2D, colorTexID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//NULL means reserve texture memory, but texels are undefined
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, renderSize, renderSize,
0, GL_RGB, GL_FLOAT, NULL);
//-------------------------
glGenFramebuffers(1, &frameBufferID);
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID);
//Attach 2D texture to this FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexID, 0);
//-------------------------
glGenRenderbuffers(1, &depthBufferID);
glBindRenderbuffer(GL_RENDERBUFFER, depthBufferID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32,
renderSize, renderSize);
//-------------------------
//Attach depth buffer to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferID);
glutHideWindow();
}
void SketchRenderer::load(const char* path)
{
loadOFF(path);
}
void SketchRenderer::renderView(const glm::vec3& front, const glm::vec3& up)
{
glm::vec3 right = glm::normalize(glm::cross(front, up));
glm::vec3 rectUp = glm::cross(right, front);
glm::vec2 xb, yb;
for (unsigned i=0; i<vertices.size(); i++)
{
float x = glm::dot(vertices[i], right);
float y = glm::dot(vertices[i], rectUp);
if (i == 0)
{
xb = glm::vec2(x, x);
yb = glm::vec2(y, y);
}
xb[0] = std::min(x, xb[0]);
xb[1] = std::max(x, xb[1]);
yb[0] = std::min(y, yb[0]);
yb[1] = std::max(y, yb[1]);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float dx = xb[1] - xb[0];
float dy = yb[1] - yb[0];
float len = std::max(dx, dy) / shrinkage;
glOrtho(-len*0.5, len*0.5, -len*0.5, len*0.5, -5, 5);
float xc = (xb[0] + xb[1])*0.5;
float yc = (yb[0] + yb[1])*0.5;
glm::vec3 eye = xc*right+yc*rectUp;
glm::vec3 center = eye+front;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye.x, eye.y, eye.z,
center.x, center.y, center.z,
rectUp.x, rectUp.y, rectUp.z);
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glCallList(listID);
glDisable(GL_DEPTH_TEST);
glFlush();
}
cv::Mat1f SketchRenderer::genShading(const glm::vec3& front, const glm::vec3& up)
{
cv::Mat1f image(renderSize, renderSize);
image.setTo(0.0);
renderView(front, up);
std::vector<glm::vec3> normalMap(renderSize*renderSize);
glReadPixels(0, 0, renderSize, renderSize, GL_RGB, GL_FLOAT, normalMap.data());
for(unsigned y=0; y<renderSize; y++)
{
for(unsigned x=0; x<renderSize; x++)
{
glm::vec3 n = normalMap[(renderSize-1-y)*renderSize+x];
if(glm::length(n) < 0 || std::isnan(n.x))
continue;
n = glm::normalize(n);
image.at<float>(y, x) = fabs(glm::dot(n, front));
}
}
return image;
}
cv::Mat1f SketchRenderer::genSketch(const glm::vec3& front, const glm::vec3& up)
{
cv::Mat1f sketch(renderSize, renderSize);
sketch.setTo(0.0);
renderView(front, up);
std::vector<glm::vec3> normalMap(renderSize*renderSize);
glReadPixels(0, 0, renderSize, renderSize, GL_RGB, GL_FLOAT, normalMap.data());
std::vector<float> depthMap(renderSize*renderSize);
glReadPixels(0, 0, renderSize, renderSize, GL_DEPTH_COMPONENT, GL_FLOAT, depthMap.data());
std::vector<glm::ivec2> offList;
offList.push_back(glm::ivec2(-1, -1));
offList.push_back(glm::ivec2(-1, 0));
offList.push_back(glm::ivec2(-1, 1));
offList.push_back(glm::ivec2(0, -1));
offList.push_back(glm::ivec2(0, 1));
offList.push_back(glm::ivec2(1, -1));
offList.push_back(glm::ivec2(1, 0));
offList.push_back(glm::ivec2(1, 1));
for(unsigned y=1; y+1<renderSize; y++)
{
for(unsigned x=1; x+1<renderSize; x++)
{
const glm::vec3 &n = normalMap[(renderSize-1-y)*renderSize+x];
const float &d = depthMap[(renderSize-1-y)*renderSize+x];
for(unsigned k=0; k<offList.size(); k++)
{
unsigned nx = x+offList[k].x;
unsigned ny = y+offList[k].y;
const glm::vec3 &nn = normalMap[(renderSize-1-ny)*renderSize+nx];
const float &nd = depthMap[(renderSize-1-ny)*renderSize+nx];
if(fabs(nd-d) > depthThres)
{
sketch.at<float>(y, x) = 1.0f;
}
else if(glm::length(n) > 0.5)
{
if(glm::length(nn) < 0.5 ||
acosf(glm::dot(n, nn))*180*M_1_PI > normalAngleThres)
sketch.at<float>(y, x) = 1.0f;
}
}
}
}
return sketch;
}
void SketchRenderer::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, colorTexID);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1);
glVertex2f(0, 1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(1, 0);
glVertex2f(1, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
}
void SketchRenderer::loadOFF(const char* path)
{
FILE* file = fopen(path, "r");
char line[1024];
fgets(line, 1024, file);
fgets(line, 1024, file);
unsigned nv, nf;
sscanf(line, "%u %u", &nv, &nf);
vertices.resize(nv);
glm::vec3 ub, lb;
for(unsigned i=0; i<nv; i++)
{
glm::vec3& v = vertices[i];
fgets(line, 1024, file);
sscanf(line, "%f %f %f", &v.x, &v.y, &v.z);
if(i == 0)
{
ub = lb = v;
}
else
{
ub = glm::max(ub, v);
lb = glm::min(lb, v);
}
}
glm::vec3 db = ub-lb;
glm::vec3 cb = (ub+lb)*0.5f;
float d = std::max(db.x, std::max(db.y, db.z));
for(unsigned i=0; i<nv; i++)
{
vertices[i] = (vertices[i] - cb) / d;
}
glNewList(listID, GL_COMPILE);
for(unsigned i=0; i<nf; i++)
{
glBegin(GL_TRIANGLES);
glm::uvec3 f;
unsigned num = 0;
fscanf(file, "%u", &num);
fscanf(file, "%u %u %u", &f.x, &f.y, &f.z);
for(unsigned j=0; j+3<=num; j++)
{
glm::vec3 normal = glm::cross(vertices[f[1]] - vertices[f[0]],
vertices[f[2]] - vertices[f[1]]);
normal = glm::normalize(normal);
if(std::isnan(normal.x))
normal = glm::vec3(0, 0, 0);
for(unsigned k=0; k<3; k++)
{
glColor3f(normal.x, normal.y, normal.z);
glVertex3fv((float*)&vertices[f[k]]);
}
f.y = f.z;
if(j+3 != num)
fscanf(file, "%u", &f.z);
}
//fgets(line, 1024, file);
glEnd();
}
glEndList();
fclose(file);
}