-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
456 lines (387 loc) · 13.9 KB
/
main.cpp
File metadata and controls
456 lines (387 loc) · 13.9 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#ifdef __APPLE__
# include <OPENGL/OpenGL.h>
# include <GLUT/Glut.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/glut.h>
#endif
#include <iostream>
#include <cstdlib>
#include "fbo.h"
#include "Texture.h"
#include "Shader.h"
#include "canny.h"
#include "trimesh2/Trimesh.h"
//GLubyte image[32][512][512][4];
GLubyte tempImage[512][512][4];
GLfloat image[512*512*3], eImage[512*512*3]; // edge/contour image
GLubyte tex_image[512][512][4], finalImage[512][512][4]; // texture in which the edge image will be stored
#define WIND_WIDTH 512
#define WIND_HEIGHT 512
#define GL_RGBA16F 0x881A
using namespace std;
using namespace trimesh;
static int g_window;
Shader nprShader, curvShader, zBufShader, testShader;
Texture zBufTexture, pencilTexture, curvTexture, tempTexture, edgeTexture;
TriMesh * mesh;
FBO *curvatureFBO, *zBufFBO;
int xMin, yMin, xMax, yMax, zMin, zMax;
//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Method to initialize the lighting values in the scene.
**/
void initializeLightValues()
{
float ambLight[4] = {0.2f, 0.2f, 0.2f, 1.0f};
float diffLight[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float specLight[4] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specLight);
float ambMat[4] = {0.3f, 0.3f, 0.3f, 1.0f};
float diffMat[4] = {0.9f, 0.5f, 0.5f, 1.0f};
float specMat[4] = {0.6f, 0.6f, 0.6f, 1.0f};
glLightfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambMat);
glLightfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffMat);
glLightfv(GL_FRONT_AND_BACK, GL_SPECULAR, specMat);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100.0 );
//TODO: add shininess factor
GLfloat light_pos[4] = {0.0f, 0.0f, 1.0f, 0.0f};
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
/**
* This is a hack kind of a situation where we want to render all kinds of obj file meshes,
* and to ensure that the different objects fit inside the screen space,
* considering that the vertices are all different inside the object space,
* we normalize the screen coordinates accordingly to accomodate most of the objects.
**/
void computeBoundaryPoints()
{
xMin = mesh->vertices[0][0]; xMax = xMin;
yMin = mesh->vertices[0][1]; yMax = yMin;
zMin = mesh->vertices[0][2]; zMax = zMin;
for(TriMesh::Face face : mesh->faces)
{
int index1 = face.v[0]; int index2 = face.v[1]; int index3 = face.v[2];
int index;
for(int i=0; i<3; i++)
{
index = face.v[i];
int val = (int)mesh->vertices[index][0];
if(xMin>val)xMin = val-5;
else if(xMax<val)xMax = val+5;
val = (int)mesh->vertices[index][1];
if(yMin>val)yMin = val-5;
else if(yMax<val)yMax = val+5;
val = (int)mesh->vertices[index][2];
if(zMin>val)zMin = val-3;
else if(zMax<val)zMax = val+3;
}
}
}
/**
* Reading the obj file through the trimesh code to retrieve the mesh information.
* Also, enabling it to find the curvatures at each vertex.
**/
void readFromMesh(char * filename)
{
mesh = TriMesh::read(filename);
if(!mesh)
exit(1);
mesh->need_curvatures();
mesh->need_normals();
}
/**
* Method to initialize the shader objects and the texture objects
**/
void initShaderObjects()
{
// Initializing all the shader objects
//zBufShader.setShader("zBuffer", "zBuffer");
zBufShader.setShader("sample", "sample");
nprShader.setShader("npr", "npr");
curvShader.setShader("curv", "curv");
//testShader.setShader("zBuffer", "zBuffer");
// Initializing all the texture objects
zBufTexture.init(GL_RGBA16F, WIND_WIDTH, WIND_HEIGHT);
curvTexture.init(GL_RGBA16F, WIND_WIDTH, WIND_HEIGHT);
edgeTexture.init(GL_RGBA16F, WIND_WIDTH, WIND_HEIGHT);
// Initializing all the framebuffer objects
curvatureFBO = new FBO(GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, curvTexture.getID());
curvatureFBO->updateRBO(WIND_WIDTH, WIND_HEIGHT);
zBufFBO = new FBO(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, zBufTexture.getID());
zBufFBO->updateRBO(WIND_WIDTH, WIND_HEIGHT);
//edgeFBO = new FBO(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, edgeTexture.getID());
//edgeFBO->updateRBO(WIND_WIDTH, WIND_HEIGHT);
}
/**
* Method to read the pencil strokes from the bmp file, and store it as a texture object.
**/
void readFromPicture()
{
char filename[100];
//sprintf(filename, "texture/texture24.bmp");
sprintf(filename, "06.bmp");
FILE *bmpInput = fopen(filename, "rb");
fseek(bmpInput, 0x436L, SEEK_SET);
for(int i=0; i<512; i++)
{
for(int j=0; j<512; j++)
{
fread(&tempImage[i][j][0], 1, 1, bmpInput);
tempImage[i][j][1] = tempImage[i][j][0];
tempImage[i][j][2] = tempImage[i][j][0];
tempImage[i][j][3] = 0;
}
}
tempTexture.init(GL_RGB8, WIND_WIDTH, WIND_HEIGHT, &tempImage[0][0][0]);
}
/**
* Method to populate the z buffer texture through the z buffer frame buffer object.
* In this pass, we compute the object material shading values for each vertex of the object.
**/
void populateZBufferFBO()
{
zBufShader.enable();
zBufFBO->bindFBO();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
for(TriMesh::Face face : mesh->faces)
{
int index1 = face.v[0]; int index2 = face.v[1]; int index3 = face.v[2];
int index;
glBegin(GL_TRIANGLES);
for(int i=0; i<3; i++)
{
index = face.v[i];
glVertex3f(mesh->vertices[index][0], mesh->vertices[index][1], mesh->vertices[index][2]);
glNormal3f(mesh->normals[index][0], mesh->normals[index][1], mesh->normals[index][2]);
}
glEnd();
}
zBufFBO->unbindFBO();
zBufShader.disable();
}
/**
* Method to populate the curvature texture through the curvature frame buffer object
* In this pass, at each vertex, we get the curvature directions, which is to be used for rotation of texture later in the pipeline.
**/
void populateCurvatureFBO()
{
curvShader.enable();
curvatureFBO->bindFBO();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glColor3ub(255, 255, 255);
for(TriMesh::Face face : mesh->faces)
{
int index;
glBegin(GL_TRIANGLES);
for(int i=0; i<3; i++)
{
index = face.v[i];
glVertex3f(mesh->vertices[index][0], mesh->vertices[index][1], mesh->vertices[index][2]);
glNormal3f(mesh->normals[index][0], mesh->normals[index][1], mesh->normals[index][2]);
curvShader.setAttribute("curvatureDirection", mesh->pdir1[index][0], mesh->pdir1[index][1], mesh->pdir1[index][2]);
curvShader.setAttribute("minCurvatureDirection", mesh->pdir2[index][0], mesh->pdir2[index][1], mesh->pdir2[index][2]);
}
glEnd();
}
curvatureFBO->unbindFBO();
curvShader.disable();
}
/**
* The initialize function
* First, we initialize all the shader objects, along with the frame buffer objects mapped with their corresponding texture objects.
* Then, we initialize the lighting and material color values.
* Then, we initialize the mesh object from the obj file with all the vertices and normal values from the trimesh structure
* Lastly, we compute the boundary points of the object on the screen, thereby comptuing the view matrix, glOrtho, etc to ensure that the object coordinates fit onto the screen.
**/
void initialize()
{
initShaderObjects();
initializeLightValues();
readFromMesh("granade.obj");
computeBoundaryPoints();
}
/**
* The final step of rendering where the pencil texture is mapped onto the screen.
* From the other two textures, we compute the exact points on the screen which needs to be colored.
**/
void drawTexture()
{
// Binding all the texture object values
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, curvTexture.getID());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, zBufTexture.getID());
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, tempTexture.getID());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, edgeTexture.getID());
// Enabling the npr shader with all the uniform texture attributes
nprShader.enable();
nprShader.setUniform("texSrc", 0);
nprShader.setUniform("texCur", 2);
nprShader.setUniform("tempTex", 3);
nprShader.setUniform("edgeTex", 1);
// Mapping the texture onto the whole of screen
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(xMin, yMin);
glTexCoord2f(1.0f, 0.0f); glVertex2f(xMax, yMin);
glTexCoord2f(1.0f, 1.0f); glVertex2f(xMax, yMax);
glTexCoord2f(0.0f, 1.0f); glVertex2f(xMin, yMax);
glEnd();
// Disabling the texture at the end
nprShader.disable();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void init(){
for(int i = 0;i < 512*512*3;i++){
image[i] = 0;
}
for(int x = 0; x < 512;x++){
for(int y = 0;y < 512;y++){
finalImage[x][y][0] = 0;
finalImage[x][y][1] = 0;
finalImage[x][y][2] = 0;
finalImage[x][y][3] = 0;
}
}
}
void detectContour(){
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
//glOrtho(-1000, 1000, -1000, 1000, -1000, 1000);
//testShader.enable();
//edgeFBO->bindFBO();
//init();
glColor3f(0.0, 0.0, 0.0);
// render the mesh
int nf = mesh->faces.size();
int vIndex1, vIndex2, vIndex3;
for(TriMesh::Face face : mesh->faces){
vIndex1 = face.v[0];
vIndex2 = face.v[1];
vIndex3 = face.v[2];
glBegin(GL_TRIANGLES);
glVertex3f(mesh->vertices[vIndex1][0], mesh->vertices[vIndex1][1], mesh->vertices[vIndex1][2]);
glVertex3f(mesh->vertices[vIndex2][0], mesh->vertices[vIndex2][1], mesh->vertices[vIndex2][2]);
glVertex3f(mesh->vertices[vIndex3][0], mesh->vertices[vIndex3][1], mesh->vertices[vIndex3][2]);
glEnd();
}
//read the pixel data from the rendered mesh scene
glReadPixels(0,0,512,512,GL_RGB, GL_FLOAT, &eImage);
glutSwapBuffers();
for(int i = 0;i < 512*512*3;i++){
if(eImage[i] == 0.0){
image[i] = 0.5;
} else {
image[i] = 0.0;
}
}
//perform canny edge detection to get the contour image
canny(eImage, 512, 512);
char filename[100];
sprintf(filename, "texture/texture01.bmp");
FILE *bmpInput = fopen(filename, "rb");
fseek(bmpInput, 0x436L, SEEK_SET);
for(int j=0; j<512; j++)
{
for(int k=0; k<512; k++)
{
fread(&tex_image[j][k][0], 1, 1, bmpInput);
tex_image[j][k][1] = tex_image[j][k][0];
tex_image[j][k][2] = tex_image[j][k][0];
tex_image[j][k][3] = 0;
}
}
for(int y=0;y<512;y++){
for(int x=0;x<512;x++){
if(eImage[(y * 512 + x) * 3 + 0]==0.0)
{
finalImage[y][x][0] = tex_image[y][x][0];
finalImage[y][x][1] = tex_image[y][x][1];
finalImage[y][x][2] = tex_image[y][x][2];
finalImage[y][x][3] = tex_image[y][x][3];
} else if(image[(y * 512 + x) * 3 + 0]==0.5){
finalImage[y][x][0] = 255;
finalImage[y][x][1] = 255;
finalImage[y][x][2] = 255;
finalImage[y][x][3] = 255;
}
}
}
//glDrawPixels(512, 512, GL_RGB, GL_FLOAT, eImage);
//glDrawPixels(512,512, GL_RGBA, GL_UNSIGNED_BYTE, &finalImage[0][0][0]);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, edgeTexture.getID());
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, finalImage);
glDisable(GL_TEXTURE_2D);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* The method to draw the scene objects.
* First, we take in the pencil strokes from the bmp file and then store in a texture object
* Then, we render the object into a frame buffer object. Its in here that we apply the shading on the object. This object is mapped to a zBufTexture
* Then, we render the object into a curvature frame buffer object. Its in here that we store the curvature directions required for rotation of texture imagges. This image is mapped to a curvTexture object
* We take all the three textures and map them onto the screen, thereby rendering the pencil shading.
**/
void drawScene()
{
glOrtho(xMin, xMax, yMin, yMax, zMin, zMax);
readFromPicture();
populateZBufferFBO();
populateCurvatureFBO();
detectContour();
drawTexture();
}
/**
* Display function of the code
**/
void renderScene()
{
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
initialize();
drawScene();
glutSwapBuffers();
}
/**
* Method which deals with all the keyboard press actions
**/
static void handleKeyboard(unsigned char key, int x, int y)
{
switch(key)
{
default:
break;
case 27:
glutDestroyWindow(g_window);
exit(0);
break;
}
}
/**
* The main function of the code..
* The window sizes are set here.
**/
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WIND_WIDTH, WIND_HEIGHT);
g_window = glutCreateWindow("Pencil Shading");
glutDisplayFunc(renderScene);
glutKeyboardFunc(handleKeyboard);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glutMainLoop();
return 0;
}