-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRenderDepthMeshImpl.cpp
executable file
·120 lines (93 loc) · 4.04 KB
/
RenderDepthMeshImpl.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
#include <mex.h>
#include <math.h>
#include "OffscreenGL.h"
#include "OffscreenCommon.h"
void drawPatchToDepthBuffer(GLuint listName, float *imageBuffer, unsigned int imgHeight, unsigned int imgWidth, unsigned int zoomFactor = 1)
{
glCallList (listName);
glFlush ();
// This is a temporary bug fix for Nvidia's open program
// seems the width of the pixel has to be a multiple of 4
// for other width, we have to pad the width and remove it later
unsigned int paddedWidth = imgWidth * zoomFactor % 4;
if (paddedWidth != 0) {
paddedWidth = 4 - paddedWidth + imgWidth * zoomFactor;
} else {
paddedWidth = imgWidth * zoomFactor;
}
#ifndef NDEBUG
mexPrintf("paddedWidth = %d, imgHeight * zoomFactor=%d\n", paddedWidth, imgHeight * zoomFactor);
#endif
// Read off of the depth buffer
float *dataBuffer = (float *)mxMalloc(paddedWidth * imgHeight * zoomFactor * sizeof(GL_FLOAT));
glReadPixels(0,0,paddedWidth, imgHeight * zoomFactor, GL_DEPTH_COMPONENT,GL_FLOAT, dataBuffer);
// reorder the pixel data for the opengl to matlab conversion
unsigned int imgSize = imgHeight * imgWidth * zoomFactor * zoomFactor;
unsigned int imgSize2 = imgSize * 2;
unsigned int matlabImgIndex = 0;
unsigned int oglImageIndex = 0;
for (int j = 0; j < imgWidth * zoomFactor; j++) {
for (int i = 0; i < imgHeight * zoomFactor; i++, matlabImgIndex++) {
oglImageIndex = (j + (imgHeight*zoomFactor -1-i) * paddedWidth);
imageBuffer[matlabImgIndex] = dataBuffer[oglImageIndex];
}
}
mxFree(dataBuffer);
}
static void renderDepthMesh(double *FM, int fNum, double *VM, int vNum,
const mxArray *CamParamS, double *imgSizeV, double *zNearFarV, unsigned int zoomFactor,
// output
float *imgBuffer)
{
cameraSetup(CamParamS, zNearFarV[0], zNearFarV[1], (unsigned int) imgSizeV[0], (unsigned int) imgSizeV[1], zoomFactor);
#ifndef NDEBUG
mexPrintf("Start to create the display list: fNum=%d, vNum=%d\n", fNum, vNum);
#endif
GLuint list = createDisplayList(FM, fNum, VM, vNum, 1);
#ifndef NDEBUG
mexPrintf("Start to draw the patch\n");
#endif
drawPatchToDepthBuffer(list, imgBuffer, (int) imgSizeV[0], (int) imgSizeV[1], zoomFactor);
// Release the display list.
if (list) {
glDeleteLists(list, 1);
list = 0;
}
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// get the vertex array, face array, and color array
double *FM = mxGetPr(prhs[0]);
int fNum = mxGetM(prhs[0]);
double *VM = mxGetPr(prhs[1]);
int vNum = mxGetM(prhs[1]);
// get the camera parameters
const mxArray *CamParamS = prhs[2];
double *imgSizeV = mxGetPr(prhs[3]);
double *zNearFarV = mxGetPr(prhs[4]);
double zoomFactor = mxGetScalar(prhs[5]);
//printf("in RCMI: imgSizeV[0] = %f, imgSizeV[1] = %f\n", imgSizeV[0], imgSizeV[1]);
int output2Size[2];
output2Size[0] = (int)0;
output2Size[1] = (int)1;
/*
* printf("In RCMI: imgSizeV[0]*zoomFactor = %d, imgSizeV[1]*zoomFactor = %d\n",
* (int)(imgSizeV[0] * zoomFactor), (int)(imgSizeV[1] * zoomFactor));
*/
int maxHeight = (int)(imgSizeV[0] * zoomFactor);
int maxWidth = (int)(imgSizeV[1] * zoomFactor);
OffscreenGL offscreenGL(maxHeight, maxWidth);
float *imgBuffer;
if (offscreenGL.RGB8Setup()){
mexPrintf("OpenGLCanvas setup Successful\n");
output2Size[0] = (int) (imgSizeV[0] * zoomFactor);
output2Size[1] = (int) (imgSizeV[1] * zoomFactor);
plhs[0] = mxCreateNumericArray(2, output2Size, mxSINGLE_CLASS, mxREAL);
imgBuffer = (float *) mxGetData(plhs[0]);
renderDepthMesh(FM, fNum, VM, vNum, CamParamS, imgSizeV,
zNearFarV, (unsigned int) zoomFactor, imgBuffer);
} else {
mexPrintf("OpenGLCanvas setup failed\n");
}
}