8 #include <OpenGLFramework/SpecializedBatches/MultipleDepthWarp.hh>
9 #include <Utils/IOUtils.hh>
14 MultipleDepthWarp::MultipleDepthWarp() {
18 numberOfSampForDepthCorr_ = 1000;
20 depthCorrTexLUTP_ = NULL;
22 useProjectiveTexturing_ =
false;
27 depthScaleBeforeWarping_ = 1.0;
29 createDepthMaps_ =
true;
30 multiStereoModus_ =
false;
33 warpType_ = DISPARITY;
36 clearColorBuffer_ =
true;
38 focallengthContained_ =
true;
41 MultipleDepthWarp::~MultipleDepthWarp() {
42 for (
unsigned int i = 0; i < depthMaps_.size(); i++) {
43 if (createDepthMaps_) {
46 delete resultDispTextures_[i];
51 std::vector<Projection>& sourceCameras,
bool createDepthMaps) {
53 createDepthMaps_ = createDepthMaps;
54 if (sourceCameras.empty()) {
55 BIASERR(
"sourceCameras.size() == 0 ");
59 targetParams_ = targetCam;
60 sourceParams_ = sourceCameras;
62 unsigned int width, height;
63 targetParams_.GetImageSize(width, height);
64 viewport_.SetSize(width, height);
65 viewport_.SetOrigin(0, 0);
67 modelViewMatrix_.MakeViewMatrixNew(targetParams_);
68 projectionMatrix_.MakeProjectionMatrixNew(targetParams_, zNear_, zFar_);
70 depthMaps_.resize(sourceParams_.size());
71 resultDispTextures_.resize(sourceParams_.size());
72 resultColorTextures_.resize(sourceParams_.size());
74 for (
unsigned int i = 0; i < depthMaps_.size(); i++) {
75 if (createDepthMaps_) {
78 depthMaps_[i]->Create();
79 depthMaps_[i]->SetMinFilter(GL_NEAREST);
80 depthMaps_[i]->SetMagFilter(GL_NEAREST);
81 depthMaps_[i]->SetWrapS(GL_CLAMP);
82 depthMaps_[i]->SetWrapT(GL_CLAMP);
83 batch_.SetTexture(depthMaps_[i], i);
87 resultDispTextures_[i]->
Create();
88 resultDispTextures_[i]->SetMagFilter(GL_NEAREST);
89 resultDispTextures_[i]->SetMinFilter(GL_NEAREST);
90 resultDispTextures_[i]->SetWrapS(GL_CLAMP);
91 resultDispTextures_[i]->SetWrapT(GL_CLAMP);
92 resultDispTextures_[i]->Allocate(width, height, GL_LUMINANCE32F_ARB);
95 resultColorTextures_[i]->
Create();
96 resultColorTextures_[i]->SetMagFilter(GL_LINEAR);
97 resultColorTextures_[i]->SetMinFilter(GL_LINEAR);
98 resultColorTextures_[i]->SetWrapS(GL_CLAMP);
99 resultColorTextures_[i]->SetWrapT(GL_CLAMP);
100 resultColorTextures_[i]->Allocate(width, height, GL_RGB);
103 projectiveTexture_.Create();
104 projectiveTexture_.SetMinFilter(GL_LINEAR);
105 projectiveTexture_.SetMagFilter(GL_LINEAR);
106 projectiveTexture_.SetWrapS(GL_CLAMP);
107 projectiveTexture_.SetWrapT(GL_CLAMP);
109 batch_.SetTexture(&projectiveTexture_, depthMaps_.size());
114 if (paramsDepth != NULL) {
115 CreateDepthCorrectionLUT_(*paramsDepth);
118 depthBufferMode_.SetDepthTest(
true);
119 batch_.SetDepthBufferMode(&depthBufferMode_);
120 batch_.SetProjectionMatrix(&projectionMatrix_);
121 batch_.SetModelViewMatrix(&modelViewMatrix_);
122 batch_.SetTextureMatrix(&textureMatrix_, 0);
123 batch_.SetViewport(&viewport_);
125 resultZBufferTexture_.Create();
126 resultZBufferTexture_.SetMagFilter(GL_NEAREST);
127 resultZBufferTexture_.SetMinFilter(GL_NEAREST);
128 resultZBufferTexture_.SetWrapS(GL_CLAMP);
129 resultZBufferTexture_.SetWrapT(GL_CLAMP);
130 resultZBufferTexture_.Allocate(width, height, GL_DEPTH_COMPONENT);
132 renderTarget_.Create();
133 renderTarget_.AttachTexture(*resultDispTextures_[0],
134 GL_COLOR_ATTACHMENT0_EXT);
135 renderTarget_.AttachTexture(*resultColorTextures_[0],
136 GL_COLOR_ATTACHMENT1_EXT);
137 renderTarget_.AttachTexture(resultZBufferTexture_,
138 GL_DEPTH_ATTACHMENT_EXT);
139 renderTarget_.CheckComplete();
141 vector<GLenum> drawbuffers(2);
142 drawbuffers[0] = GL_COLOR_ATTACHMENT0_EXT;
143 drawbuffers[1] = GL_COLOR_ATTACHMENT1_EXT;
144 renderTarget_.SetDrawBuffers(drawbuffers);
146 batch_.SetRenderTarget(&renderTarget_);
147 batch_.SetShaderProgram(&shaderProgram_);
148 if (multiStereoModus_) {
153 SetupPrimitiveRendering_();
161 targetParams_ = targetCam;
163 unsigned int width, height;
165 viewport_.SetSize(width, height);
166 viewport_.SetOrigin(0, 0);
168 modelViewMatrix_.MakeViewMatrixNew(targetParams_);
169 projectionMatrix_.MakeProjectionMatrixNew(targetParams_, zNear_, zFar_);
178 textureMatrix_.MakeTextureMatrixNew(textureCam,
false);
187 projectiveTexture_.UploadImage(projectiveTexture);
191 unsigned int index) {
192 if (index >= depthMaps_.size()) {
193 BIASERR(
"could not upload depth map, index " << index <<
" out of bound " << depthMaps_.size());
197 depthMaps_[index]->UploadImage(depthMap);
202 int MultipleDepthWarp::SetDepthMap(
glfTexture2D* depthMap,
unsigned int index) {
203 if (createDepthMaps_) {
204 BIASERR(
"to set depth map, Init must be called with createDepthMaps = false" << endl);
207 if (index >= depthMaps_.size()) {
208 BIASERR(
"could not set depth map, index " << index <<
" out of bound " << depthMaps_.size());
212 depthMaps_[index] = depthMap;
217 int MultipleDepthWarp::Warp(
unsigned int index) {
219 if (index < sourceParams_.size()) {
220 current_index_ = index;
221 renderTarget_.AttachTexture(*resultDispTextures_[index],
222 GL_COLOR_ATTACHMENT0_EXT);
223 renderTarget_.AttachTexture(*resultColorTextures_[index],
224 GL_COLOR_ATTACHMENT1_EXT);
225 renderTarget_.ClearDepthBuffer(1.0);
226 if (clearColorBuffer_) {
227 renderTarget_.ClearColorBuffer(0, 0, 0, 0);
229 unsigned int width, height;
230 sourceParams_[index].GetParameters()->GetImageSize(width, height);
231 unsigned int numIndicesPerStrip = width * 2;
232 unsigned int first = index * numIndicesPerStrip * (height - 1);
233 unsigned int targetWidth, targetHeight;
234 targetParams_.GetImageSize(targetWidth, targetHeight);
236 shaderProgram_.SetUniform(
"imageWidth", (
float) targetWidth);
237 shaderProgram_.SetUniform(
"depthScaleBefore",
238 depthScaleBeforeWarping_);
239 shaderProgram_.SetUniform(
"depthImage", (
int) index);
240 shaderProgram_.SetUniform(
"C", cameraCenters_[index]);
241 shaderProgram_.SetUniform(
"thr", thr_);
244 if(focallengthContained_) {
245 shaderProgram_.SetUniform(
"baselineFocallength", baseline_);
247 shaderProgram_.SetUniform(
"baselineFocallength", baseline_*(
float)targetParams_.GetFocalLength());
249 shaderProgram_.SetUniform(
"warpType", (
int) warpType_);
251 shaderProgram_.SetUniform(
"flop", (
int) 1);
253 shaderProgram_.SetUniform(
"flop", (
int) 0);
256 for (
unsigned int y = 0; y < height - 1; y++) {
257 batch_.SetRange(first, numIndicesPerStrip);
259 first += numIndicesPerStrip;
262 BIASERR(
"index exceeds number of source parameters");
267 BIASERR(
"call SetProjectios(...) first");
274 int MultipleDepthWarp::Warp() {
276 renderTarget_.ClearDepthBuffer(1.0);
277 if (clearColorBuffer_) {
278 renderTarget_.ClearColorBuffer(0, 0, 0, 0);
280 unsigned int first = 0;
281 for (
unsigned int i = 0; i < sourceParams_.size(); i++) {
283 unsigned int targetWidth, targetHeight;
284 targetParams_.GetImageSize(targetWidth, targetHeight);
286 shaderProgram_.SetUniform(
"depthImage", (
int) i);
287 shaderProgram_.SetUniform(
"projTexture", (
int) depthMaps_.size());
289 shaderProgram_.SetUniform(
"C", cameraCenters_[i]);
291 shaderProgram_.SetUniform(
"flop", (
int) 1);
293 shaderProgram_.SetUniform(
"flop", (
int) 0);
296 shaderProgram_.SetUniform(
"flip", (
int) 1);
298 shaderProgram_.SetUniform(
"flip", (
int) 0);
301 unsigned int width, height;
302 sourceParams_[i].GetParameters()->GetImageSize(width, height);
303 if (!multiStereoModus_) {
304 shaderProgram_.SetUniform(
"imageWidth", (
float) targetWidth);
305 shaderProgram_.SetUniform(
"depthScaleBefore",
306 depthScaleBeforeWarping_);
307 shaderProgram_.SetUniform(
"thr", thr_);
308 shaderProgram_.SetUniform(
"corrTexture", (
int) (depthMaps_.size() + 1));
309 if (useProjectiveTexturing_) {
310 shaderProgram_.SetUniform(
"useProjTex", (
int) 1);
312 shaderProgram_.SetUniform(
"useProjTex", (
int) 0);
314 shaderProgram_.SetUniform(
"minDepth", minDepth_);
315 shaderProgram_.SetUniform(
"maxDepth", maxDepth_);
316 shaderProgram_.SetUniform(
"depthRange", depthRange_);
319 if(focallengthContained_) {
320 shaderProgram_.SetUniform(
"baselineFocallength", baseline_);
322 shaderProgram_.SetUniform(
"baselineFocallength", baseline_*(
float)targetParams_.GetFocalLength());
325 shaderProgram_.SetUniform(
"warpType", (
int) warpType_);
326 shaderProgram_.SetUniform(
"sourceImageWidth", (
float) width);
327 shaderProgram_.SetUniform(
"sourceImageHeight", (
float) height);
329 if (depthCorType_ == LINEAR) {
333 std::vector<double> params;
335 shaderProgram_.SetUniform(
"d0", (
float) params[0]);
336 shaderProgram_.SetUniform(
"d1", (
float) params[1]);
340 shaderProgram_.SetUniform(
"depthCorType", (
int) depthCorType_);
345 batch_.SetTexture(&projectiveTexture_,0);
346 batch_.SetTexture(depthMaps_[i], i);
348 unsigned int numIndicesPerStrip = width * 2;
349 for (
unsigned int y = 0; y < height - 1; y++) {
350 batch_.SetRange(first, numIndicesPerStrip);
352 first += numIndicesPerStrip;
356 BIASERR(
"call SetProjectios(...) first");
363 int MultipleDepthWarp::SetupNViewsWarp(
const double& distToScreen,
364 const double& zoom,
const double& shift,
365 unsigned int numberOfViews,
unsigned int centralViewIndex,
bool separate)
369 double dScreen = distToScreen;
375 numberOfViews_ = numberOfViews;
376 separate_ = separate;
378 if (resultDepthMapsForNViews_.size() != numberOfViews) {
380 for (
unsigned int i = 0; i < resultDepthMapsForNViews_.size(); i++) {
381 delete resultDepthMapsForNViews_[i];
382 delete resultTexturesForNViews_[i];
385 resultDepthMapsForNViews_.resize(numberOfViews);
386 resultTexturesForNViews_.resize(numberOfViews);
387 modelViewMatricesForNViewWarp_.resize(numberOfViews);
388 projectionMatricesForNViewWarp_.resize(numberOfViews);
390 unsigned int width, height;
391 targetParams_.GetImageSize(width,height);
393 for (
unsigned int i = 0; i < numberOfViews; i++) {
396 int index = (int) i - (
int) centralViewIndex;
399 HomgPoint2D rightBottom((
double)width-0.5, (
double) height - 0.5,1);
404 p = (p / p[2]) * dScreen;
409 p[0] = p[0]- (index* shift);
414 q = (q / q[2]) * dScreen;
419 q[0] = q[0] - (index*shift);
425 modelViewMatricesForNViewWarp_[i].MakeViewMatrixNew(ppp);
426 projectionMatricesForNViewWarp_[i].MakeProjectionMatrixNew(ppp, zNear_, zFar_);
431 for (
unsigned int i = 0; i < numberOfViews; i++) {
433 resultDepthMapsForNViews_[i]->Create();
434 resultDepthMapsForNViews_[i]->SetMagFilter(GL_NEAREST);
435 resultDepthMapsForNViews_[i]->SetMinFilter(GL_NEAREST);
436 resultDepthMapsForNViews_[i]->SetWrapS(GL_CLAMP);
437 resultDepthMapsForNViews_[i]->SetWrapT(GL_CLAMP);
438 resultDepthMapsForNViews_[i]->Allocate(width, height, GL_LUMINANCE32F_ARB);
441 resultTexturesForNViews_[i]->
Create();
442 resultTexturesForNViews_[i]->SetMagFilter(GL_LINEAR);
443 resultTexturesForNViews_[i]->SetMinFilter(GL_LINEAR);
444 resultTexturesForNViews_[i]->SetWrapS(GL_CLAMP);
445 resultTexturesForNViews_[i]->SetWrapT(GL_CLAMP);
446 resultTexturesForNViews_[i]->Allocate(width, height, GL_RGB);
449 resultDepthMapsForNViews_.resize(1);
450 resultTexturesForNViews_.resize(1);
452 resultDepthMapsForNViews_[0]->Create();
453 resultDepthMapsForNViews_[0]->SetMagFilter(GL_NEAREST);
454 resultDepthMapsForNViews_[0]->SetMinFilter(GL_NEAREST);
455 resultDepthMapsForNViews_[0]->SetWrapS(GL_CLAMP);
456 resultDepthMapsForNViews_[0]->SetWrapT(GL_CLAMP);
457 resultDepthMapsForNViews_[0]->Allocate(width, height*numberOfViews, GL_LUMINANCE32F_ARB);
460 resultTexturesForNViews_[0]->
Create();
461 resultTexturesForNViews_[0]->SetMagFilter(GL_LINEAR);
462 resultTexturesForNViews_[0]->SetMinFilter(GL_LINEAR);
463 resultTexturesForNViews_[0]->SetWrapS(GL_CLAMP);
464 resultTexturesForNViews_[0]->SetWrapT(GL_CLAMP);
465 resultTexturesForNViews_[0]->Allocate(width, height*numberOfViews, GL_RGB);
467 renderTarget_.AttachTexture(*resultDepthMapsForNViews_[0],
468 GL_COLOR_ATTACHMENT0_EXT);
469 renderTarget_.AttachTexture(*resultTexturesForNViews_[0],
470 GL_COLOR_ATTACHMENT1_EXT);
471 resultZBufferTexture_.Allocate(width, height*numberOfViews_, GL_DEPTH_COMPONENT);
477 BIASERR(
"call SetProjectios(...) first");
484 int MultipleDepthWarp::WarpNViews()
489 for (
unsigned int i = 0; i < numberOfViews_; i++) {
490 batch_.SetModelViewMatrix(&modelViewMatricesForNViewWarp_[i]);
491 batch_.SetProjectionMatrix(&projectionMatricesForNViewWarp_[i]);
493 renderTarget_.AttachTexture(*resultDepthMapsForNViews_[i],
494 GL_COLOR_ATTACHMENT0_EXT);
495 renderTarget_.AttachTexture(*resultTexturesForNViews_[i],
496 GL_COLOR_ATTACHMENT1_EXT);
502 unsigned int width, height;
504 targetParams_.GetImageSize(width,height);
507 for (
unsigned int i = 0; i < numberOfViews_ ; i++) {
509 batch_.SetModelViewMatrix(&modelViewMatricesForNViewWarp_[i]);
510 batch_.SetProjectionMatrix(&projectionMatricesForNViewWarp_[i]);
512 viewport_.SetOrigin(0, (numberOfViews_ - 1 - i) *height);
514 clearColorBuffer_ =
true;
516 clearColorBuffer_ =
false;
522 BIASERR(
"call SetProjectios(...) first");
531 unsigned int width, height;
532 targetParams_.GetImageSize(width, height);
536 dispMap.
Init(width, height, 1);
538 resultDispTextures_[current_index_]->CopyToImage(dispMap);
545 unsigned int width, height;
546 targetParams_.GetImageSize(width, height);
550 texture.
Init(width, height, 3);
552 resultColorTextures_[current_index_]->CopyToImage(texture);
558 unsigned int width, height;
559 targetParams_.GetImageSize(width, height);
563 zbuffer.
Init(width, height, 1);
565 resultZBufferTexture_.CopyToImage(zbuffer);
570 void MultipleDepthWarp::SetDispForDistance(
const float& dist_from_cam,
574 baseline_ = disp*dist_from_cam;
575 focallengthContained_=
true;
578 void MultipleDepthWarp::
579 SetBaseline(
const float &baseline)
581 baseline_ = baseline;
582 focallengthContained_=
false;
585 void MultipleDepthWarp::SetZNearAndZFar(
const float& zNear,
const float& zFar) {
590 projectionMatrix_.MakeProjectionMatrixNew(targetParams_, zNear_, zFar_);
594 void MultipleDepthWarp::SetDiscardAngle(
float degree) {
595 thr_ = cos(M_PI * degree / 180.0);
598 void MultipleDepthWarp::SetDepthScaleBeforeWarping(
float scale) {
599 depthScaleBeforeWarping_ = scale;
605 void MultipleDepthWarp::SetupPrimitiveRendering_() {
607 vertexFormat.
AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, 4);
608 vertexFormat.
AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 0, GL_FLOAT, 2);
610 const int numComponenents = 6;
613 unsigned int sizeOfVertexBuffer = 0;
614 unsigned int sizeOfElementBuffer = 0;
615 for (
unsigned int i = 0; i < sourceParams_.size(); i++) {
616 unsigned int width, height;
617 sourceParams_[i].GetParameters()->GetImageSize(width, height);
618 sizeOfElementBuffer += width * (height - 1) * 2;
619 sizeOfVertexBuffer += width * height * numComponenents;
622 float* vertexData =
new float[sizeOfVertexBuffer];
623 GLuint* triangleIndices =
new GLuint[sizeOfElementBuffer];
625 unsigned int numTriangleIndex = 0;
626 unsigned int numVertexIndex = 0;
627 unsigned int bufferOffset = 0;
628 for (
unsigned int i = 0; i < sourceParams_.size(); i++) {
629 unsigned int width, height;
630 sourceParams_[i].GetParameters()->GetImageSize(width, height);
631 cameraCenters_.push_back(sourceParams_[i].GetC());
633 for (
unsigned int y = 0; y < height - 1; y++) {
634 for (
unsigned int x = 0; x < width; x++) {
641 triangleIndices[numTriangleIndex++] = y * width + x
643 triangleIndices[numTriangleIndex++] = (y + 1) * width + x
648 bufferOffset += width * height;
651 for (
unsigned int y = 0; y < height; y++) {
652 for (
unsigned int x = 0; x < width; x++) {
655 sourceParams_[i].UnProjectToRay(imgPoint, p, ray, 0,
false);
657 if (ray.
NormL2() > 1e-12) {
663 vertexData[numVertexIndex++] =
static_cast<float> (ray[0]);
664 vertexData[numVertexIndex++] =
static_cast<float> (ray[1]);
665 vertexData[numVertexIndex++] =
static_cast<float> (ray[2]);
666 vertexData[numVertexIndex++] = 1.0;
668 vertexData[numVertexIndex++] = (
static_cast<float> (x) + 0.5)
669 /
static_cast<float> (width);
670 vertexData[numVertexIndex++] = (
static_cast<float> (y) + 0.5)
671 /
static_cast<float> (height);
676 vertices_.Create(sizeOfVertexBuffer / numComponenents, vertexFormat,
678 elements_.Create(sizeOfElementBuffer, GL_UNSIGNED_INT, GL_TRIANGLE_STRIP,
681 batch_.SetElementBuffer(&elements_);
682 batch_.SetVertexBuffer(&vertices_);
683 delete[] triangleIndices;
688 void MultipleDepthWarp::SetupShaders_(
int shaderType) {
689 if (shaderType == 0) {
693 "uniform sampler2D depthImage;\n"
694 "uniform sampler2D corrTexture;"
696 "uniform float depthScaleBefore;\n"
697 "uniform float imageWidth;\n"
698 "uniform float sourceImageHeight;"
699 "uniform float sourceImageWidth;"
700 "//uniform float dist_from_cam;\n"
701 "//uniform float disp_for_dist_from_cam;\n"
702 "uniform float baselineFocallength;\n"
705 "uniform int warpType;"
708 "uniform float minDepth;"
709 "uniform float maxDepth;"
710 "uniform float depthRange;"
711 "uniform int depthCorType;"
713 "varying float disp;\n"
717 "vec2 coord = gl_MultiTexCoord0.st;\n"
718 "vec2 tmp_coord = coord;\n"
720 "coord[0] = 1.0-coord[0];\n"
722 "coord[1] = 1.0 - coord[1];\n"
723 "float depth = texture2D(depthImage, coord).r * depthScaleBefore;\n"
724 "if (depthCorType == 2) {\n"
725 "depth = (d1 + 1)* depth + d0;\n"
729 "vec4 depthAdjustedVertex = gl_Vertex * depth;\n"
731 " if (depthCorType == 1) {\n"
732 " if (depthAdjustedVertex[2] < maxDepth && depthAdjustedVertex[2] > minDepth) {\n"
733 " float texCoordForZ = (depthAdjustedVertex[2] - minDepth) / depthRange;\n"
734 " float newZValue = texture2D(corrTexture, vec2(texCoordForZ,0.5)).r;\n"
735 " depthAdjustedVertex = (depthAdjustedVertex / depthAdjustedVertex[2])*newZValue;\n"
738 " depthAdjustedVertex.w = 1.0;\n"
739 " depthAdjustedVertex[0]+=C[0];\n"
740 " depthAdjustedVertex[1]+=C[1];\n"
741 " depthAdjustedVertex[2]+=C[2];\n"
743 " if (warpType == 0) {\n"
744 " //vec4 tmp = gl_ModelViewProjectionMatrix * vec4(imageWidth,0.0,0.0,0.0);\n"
745 " //vec4 t = vec4(abs(((dist_from_cam*disp_for_dist_from_cam)/tmp[0])), 0.0,0.0,0.0);\n"
746 " //vec4 proj_t = gl_ModelViewProjectionMatrix * t;\n"
748 " //disp = proj_t[0]*imageWidth;\n"
749 " // if(baseline>0)\n"
750 " disp = baselineFocallength;\n"
751 " } else {//else depth > 0 \n"
752 " disp = length(depthAdjustedVertex.xyz);\n"
758 "gl_Position = depthAdjustedVertex;\n"
765 string fragmentShaderCode =
""
766 "uniform sampler2D projTexture;"
767 "varying float resDisp;\n"
770 "gl_FragData[0].r = resDisp;\n"
774 "gl_FragData[1].rgb = texture2DProj(projTexture, gl_TexCoord[0]);"
779 string geometryShaderCode =
""
780 "#extension GL_EXT_geometry_shader4 : enable\n"
782 "varying in float disp[3];\n"
783 "varying out float resDisp;\n"
785 "uniform float thr;\n"
786 "uniform int warpType;"
787 "uniform int useProjTex;"
792 "vec4 v0 = gl_PositionIn[0];"
793 "vec4 v1 = gl_PositionIn[1];"
794 "vec4 v2 = gl_PositionIn[2];"
796 "vec3 mean_vec = (v0.xyz+v1.xyz+v2.xyz)/3.0;"
798 "vec3 cam_dir = C-mean_vec;"
799 "vec3 dir1 = v0.xyz - v1.xyz;"
800 "vec3 dir2 = v2.xyz - v1.xyz;"
802 "vec3 n = cross(dir1,dir2);"
803 "vec3 norm_n = normalize(n);"
804 "vec3 norm_cam = normalize(cam_dir);"
806 "float res = dot(norm_n,norm_cam);"
808 "if (res < thr && disp[0] >= 0 && disp[1] >= 0 && disp[2] >= 0) {"
810 "gl_Position = gl_ModelViewProjectionMatrix*v0;"
811 "if (useProjTex == 1)"
812 "gl_TexCoord[0] = gl_TextureMatrix[0] * v0;"
813 "if (warpType == 0) {"
814 "resDisp = disp[0]/gl_Position[3];\n"
817 "resDisp = disp[0];\n"
821 "gl_Position = gl_ModelViewProjectionMatrix*v1;"
822 "if (useProjTex == 1)"
823 "gl_TexCoord[0] = gl_TextureMatrix[0] * v1;"
824 "if (warpType == 0) {"
825 "resDisp = disp[1]/gl_Position[3];\n"
828 "resDisp = disp[1];\n"
832 "gl_Position = gl_ModelViewProjectionMatrix*v2;"
833 "if (useProjTex == 1)"
834 "gl_TexCoord[0] = gl_TextureMatrix[0] * v2;"
835 "if (warpType == 0) {"
836 "resDisp = disp[2]/gl_Position[3];\n"
839 "resDisp = disp[2];\n"
848 vertexShader_.Create(GL_VERTEX_SHADER, vertexShaderCode);
849 fragmentShader_.Create(GL_FRAGMENT_SHADER, fragmentShaderCode);
850 geometryShader_.Create(GL_GEOMETRY_SHADER_EXT, geometryShaderCode);
851 shaderProgram_.Create();
852 shaderProgram_.AttachShader(vertexShader_);
853 shaderProgram_.AttachShader(fragmentShader_);
854 shaderProgram_.AttachShader(geometryShader_);
856 glProgramParameteriEXT(shaderProgram_.GetProgramId(),
857 GL_GEOMETRY_VERTICES_OUT_EXT, 3);
858 glProgramParameteriEXT(shaderProgram_.GetProgramId(),
859 GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
860 glProgramParameteriEXT(shaderProgram_.GetProgramId(),
861 GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);
862 shaderProgram_.Link();
866 string fragmentShaderCode=
"";
870 "uniform sampler2D depthImage;\n"
877 "vec2 coord = gl_MultiTexCoord0.st;"
880 "coord[0] = 1.0-coord[0];"
882 "coord[1] = 1.0 - coord[1];"
884 "float depth = texture2D(depthImage, coord).r;\n"
886 "if (depth == 0) depth = 10000;\n"
887 "vec4 depthAdjustedVertex = gl_Vertex * depth;\n"
888 "depthAdjustedVertex.w = 1.0;\n"
889 "depthAdjustedVertex[0]+=C[0];\n"
890 "depthAdjustedVertex[1]+=C[1];\n"
891 "depthAdjustedVertex[2]+=C[2];\n"
893 "gl_Position = gl_ModelViewProjectionMatrix*depthAdjustedVertex;"
894 "gl_TexCoord[0] = gl_TextureMatrix[0] * depthAdjustedVertex;"
897 if (shaderType == 1) {
898 fragmentShaderCode =
""
899 "uniform sampler2D projTexture;"
902 "gl_FragData[1].rgb = texture2DProj(projTexture, gl_TexCoord[0]);"
906 vertexShader_.Create(GL_VERTEX_SHADER, vertexShaderCode);
907 fragmentShader_.Create(GL_FRAGMENT_SHADER, fragmentShaderCode);
909 shaderProgram_.Create();
910 shaderProgram_.AttachShader(vertexShader_);
911 shaderProgram_.AttachShader(fragmentShader_);
913 shaderProgram_.Link();
920 cout <<
"---------------------" << endl;
921 depthCorrImageLUT_.Init(numberOfSampForDepthCorr_, 2, 1);
922 depthCorrImageLUT_.SetColorModel(ImageBase::CM_Grey);
925 depthRange_ = maxDepth_ - minDepth_;
927 cout << minDepth_ <<
" max depth " << maxDepth_ << endl;
929 float step = depthRange_ / (float) numberOfSampForDepthCorr_;
932 for (
int i = 0; i < (int)numberOfSampForDepthCorr_; i++) {
933 float depth = i*step + minDepth_;
938 depthCorrImageLUT_.SetPixel(depth, i, 0);
939 depthCorrImageLUT_.SetPixel(depth, i, 1);
945 if (depthCorrTexLUTP_ != NULL) {
946 delete depthCorrTexLUTP_;
947 depthCorrTexLUTP_ = NULL;
950 depthCorrTexLUTP_->Create();
951 depthCorrTexLUTP_->SetMagFilter(GL_LINEAR);
952 depthCorrTexLUTP_->SetMinFilter(GL_LINEAR);
953 depthCorrTexLUTP_->SetWrapS(GL_CLAMP);
954 depthCorrTexLUTP_->SetWrapT(GL_CLAMP);
955 depthCorrTexLUTP_->UploadImage(depthCorrImageLUT_);
957 batch_.SetTexture(depthCorrTexLUTP_, depthMaps_.size() + 1);
virtual BIAS::Vector3< double > GetC() const
Get projection center.
void Release()
reimplemented from ImageBase
int SetIntrinsics(double minPhi, double maxPhi, double minTheta, double maxTheta, double angleStep, double aspectratio=1.0)
Method calculates K-Matrix and image size from specified angles.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
additional depth calibration parameters for a perspective depth camera to be used for ToF-(PMD) camer...
void Create()
Creates the texture but does not upload any data yet.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void UnDistortDepth(HomgPoint2D const &pos, float &d, bool bIsInCartesianCoords=false)
UnDistort depth value d at image position pos.
unsigned int GetWidth() const
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
unsigned int GetHeight() const
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
void GetMinMaxDepth(float &minDepth, float &maxDepth)
get the minimum and maximum depth values with which the calibration has been done ...
void GetDepthCalibrationParameters(std::vector< double > ¶ms)
Get the depth calibration parameters.
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the viewing ray from the camera center (in the camera coordinate system) which belongs to ...
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
Vector3< T > & Normalize()
normalize this vector to length 1
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)