26 #include <OpenGLFramework/Base/glfException.hh>
27 #include <Base/Debug/TimeMeasure.hh>
37 bool targetParametersHaveDistortion)
40 pmdParams_.
GetImageSize(depthImageWidth_, depthImageHeight_);
42 pmdDepthImage_.Create();
43 pmdDepthImage_.SetMinFilter(GL_NEAREST);
44 pmdDepthImage_.SetMagFilter(GL_NEAREST);
45 pmdDepthImage_.SetWrapS(GL_CLAMP);
46 pmdDepthImage_.SetWrapT(GL_CLAMP);
47 batch_.SetTexture(&pmdDepthImage_, 0);
49 depthBufferMode_.SetDepthTest(
true);
50 batch_.SetDepthBufferMode(&depthBufferMode_);
52 batch_.SetProjectionMatrix(&Projection_);
53 batch_.SetModelViewMatrix(&modelView_);
55 shaderProgram_.Create();
56 batch_.SetShaderProgram(&shaderProgram_);
58 batch_.SetViewport(&viewport_);
60 targetParametersHaveDistortion_ = targetParametersHaveDistortion;
62 SetupShadersDefault_();
64 SetupDefaultPrimitiveRendering_();
77 return depthBufferMode_;
85 pmdDepthImage_.UploadImage(pmdDepthImage);
91 pmdDepthImage_.UploadImage(pmdDepthImage);
97 projectiveTexture_.Create();
98 projectiveTexture_.SetMinFilter(GL_LINEAR);
99 projectiveTexture_.SetMagFilter(GL_LINEAR);
100 projectiveTexture_.SetWrapS(GL_CLAMP);
101 projectiveTexture_.SetWrapT(GL_CLAMP);
102 batch_.SetTexture(&projectiveTexture_, 1);
108 relativePPP.
SetPose(relativePose);
113 textureMatrix_.MakeTextureMatrixNew(relativePPP,
false);
114 batch_.SetTextureMatrix(&textureMatrix_, 1);
117 SetupShadersProjectiveTexturing_();
119 shaderProgram_.SetUniform(
"C", relativePPP.
GetC());
125 projectiveTexture_.UploadImage(projectiveTexture);
131 batch_.SetRenderTarget(renderTarget);
137 viewport_.SetSize(width, height);
138 viewport_.SetOrigin(0, 0);
145 double zNear,
double zFar)
147 targetParams_ = relativePpp;
148 modelView_.MakeViewMatrixNew(targetParams_);
149 Projection_.MakeProjectionMatrixNew(targetParams_, zNear, zFar);
151 if(targetParametersHaveDistortion_) {
154 shaderProgram_.SetUniform(
"k0", k0);
155 shaderProgram_.SetUniform(
"k1", k1);
158 unsigned int width, height;
159 targetParams_.GetImageSize(width, height);
160 viewport_.SetSize(width, height);
161 viewport_.SetOrigin(0, 0);
170 modelView_.Make(modelViewMatrix);
171 Projection_.Make(projectionMatrix);
172 viewport_.SetSize(width, height);
173 viewport_.SetOrigin(0, 0);
181 batch_.SetProjectionMatrix(&globalModelViewNProjection_);
182 batch_.SetModelViewMatrix(&globalModelViewNProjection_);
186 batch_.SetProjectionMatrix(&Projection_);
187 batch_.SetModelViewMatrix(&modelView_);
188 batch_.SetDepthBufferMode(&depthBufferMode_);
197 batch_.SetViewport(&globalViewport_);
201 batch_.SetViewport(&viewport_);
208 Projection_.MakeProjectionMatrix(targetParams_, zNear, zFar);
219 if (defaultRendering_)
221 unsigned int numIndicesPerStrip = depthImageWidth_ * 2;
222 unsigned int first = 0;
223 for (
unsigned int y = 0; y < depthImageHeight_ - 1; y++)
225 batch_.SetRange(first, numIndicesPerStrip);
227 first += numIndicesPerStrip;
232 BIASERR(
"no rendering implemented!\n");
244 defaultRendering_(true)
262 const int numComponenents = 6;
266 unsigned int count = 0;
278 vertexData[count++] =
static_cast<float> (ray[0]);
279 vertexData[count++] =
static_cast<float> (ray[1]);
280 vertexData[count++] =
static_cast<float> (ray[2]);
281 vertexData[count++] = 1.0;
283 vertexData[count++] = (
static_cast<float> (x) + 0.5)
285 vertexData[count++] = (
static_cast<float> (y) + 0.5)
309 unsigned int numTriangleIndex = 0;
311 for (
unsigned int y = 0; y < depthImageHeight_ - 1; y++)
321 triangleIndices[numTriangleIndex++] = y * depthImageWidth_ + x;
322 triangleIndices[numTriangleIndex++] = (y + 1) * depthImageWidth_ + x;
326 GL_UNSIGNED_INT, GL_TRIANGLE_STRIP, triangleIndices);
328 delete[] triangleIndices;
338 stringstream vertexProgramCode;
340 "uniform sampler2D pmdDepthImage;\n"
341 "uniform float scale = 1.0;"
342 "uniform float imageWidth = 900;"
343 "uniform float dist_from_cam = 2000;"
344 "uniform float disp_for_dist_from_cam = 255.0;"
345 "varying float disp;\n"
350 "uniform float k0;\n"
351 "uniform float k1;\n"
357 " float depth = texture2D(pmdDepthImage, gl_MultiTexCoord0.st).r;\n"
358 " if(depth == 0) depth = 0.9;\n"
359 " vec4 depthAdjustedVertex = gl_Vertex * depth;\n"
360 " depthAdjustedVertex.w = 1.0; \n"
361 " depthAdjustedVertex = gl_ModelViewMatrix * depthAdjustedVertex;\n";
366 "float normalizedRad = (depthAdjustedVertex[0]*depthAdjustedVertex[0]);\n "
367 "normalizedRad += (depthAdjustedVertex[1]*depthAdjustedVertex[1]);\n"
368 "normalizedRad /= (depthAdjustedVertex[2]*depthAdjustedVertex[2]);\n"
369 "float scale = 1.0f + (k0 + k1*normalizedRad)*normalizedRad;\n"
370 "depthAdjustedVertex[0] = scale*depthAdjustedVertex[0];\n"
371 "depthAdjustedVertex[1] = scale*depthAdjustedVertex[1];\n";
375 "vec4 tmp = gl_ModelViewProjectionMatrix * vec4(imageWidth,0.0,0.0,0.0);"
376 "vec4 t = vec4(abs(((dist_from_cam*disp_for_dist_from_cam)/tmp[0])), 0.0,0.0,0.0);"
377 "vec4 proj_t = gl_ModelViewProjectionMatrix * t;"
380 "vec4 pos = gl_ProjectionMatrix * depthAdjustedVertex;\n"
381 "disp = scale*abs(proj_t[0]/pos[3])*imageWidth;"
383 "gl_Position = pos;";
385 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
388 string fragmentProgramCode =
389 "varying float disp;"
392 "gl_FragData[0] = gl_TexCoord[0];"
393 "gl_FragData[1] = vec4(disp);"
398 string geometryProgramCode =
""
399 "#extension GL_EXT_geometry_shader4 : enable\n"
405 "vec4 v0 = gl_PositionIn[0];"
406 "vec4 v1 = gl_PositionIn[1];"
407 "vec4 v2 = gl_PositionIn[2];"
409 "float dmax = max ( max ( distance(v0,v1), distance(v1,v2) ), distance(v0,v2) );"
414 "gl_TexCoord[0] = gl_TexCoordIn[0][0];"
417 "gl_TexCoord[0] = gl_TexCoordIn[1][0];"
420 "gl_TexCoord[0] = gl_TexCoordIn[2][0];"
439 GL_GEOMETRY_VERTICES_OUT_EXT, 3);
441 GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
443 GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);
450 const float& disp,
const float& scale)
460 double zFar,
float N,
int output)
466 float FN = zNear * zFar;
467 float FMinusN = zFar - zNear;
468 float relativeHypoSpacing = FMinusN / N;
470 stringstream fragmentProgramStream;
471 fragmentProgramStream <<
"uniform float zNear;" <<
"uniform float zFar;"
472 <<
"uniform float FMinusN;" <<
"uniform float FN;" <<
"uniform float N;"
473 <<
"uniform float D;";
476 fragmentProgramStream <<
"void main() {"
477 <<
" float z = FN/(zFar - gl_FragCoord.z*FMinusN);"
478 <<
" float r = (z-zNear)/D;" <<
" float l = floor(r);"
479 <<
" float u = ceil(r);"
480 <<
" float w = r - l;"
481 <<
" gl_FragData[" << output <<
"].r = l;" <<
" gl_FragData[" << output
482 <<
"].g = u;" <<
" gl_FragData[" << output <<
"].b = w;" <<
"}";
516 cout<<
"projective texturing...\n";
517 stringstream vertexProgramCode;
519 "uniform sampler2D pmdDepthImage;"
520 "uniform float scale = 1.0;"
521 "uniform float imageWidth = 900;"
522 "uniform float dist_from_cam = 2000;"
523 "uniform float disp_for_dist_from_cam = 255.0;"
524 "varying float disp;\n"
529 "uniform float k0;\n"
530 "uniform float k1;\n"
535 " float depth = texture2D(pmdDepthImage, gl_MultiTexCoord0.st).r;\n"
536 " if(depth == 0) depth = 0.9;\n"
537 " vec4 depthAdjustedVertex = gl_Vertex * depth;\n"
538 " depthAdjustedVertex.w = 1.0;\n"
539 " vec4 origdepthAdjustedVertex = depthAdjustedVertex;"
540 " depthAdjustedVertex = gl_ModelViewMatrix * depthAdjustedVertex;\n";
545 "float normalizedRad = (depthAdjustedVertex[0]*depthAdjustedVertex[0]);\n "
546 "normalizedRad += (depthAdjustedVertex[1]*depthAdjustedVertex[1]);\n"
547 "normalizedRad /= (depthAdjustedVertex[2]*depthAdjustedVertex[2]);\n"
548 "float scale = 1.0f + (k0 + k1*normalizedRad)*normalizedRad;\n"
549 "depthAdjustedVertex[0] = scale*depthAdjustedVertex[0];\n"
550 "depthAdjustedVertex[1] = scale*depthAdjustedVertex[1];\n";
554 "vec4 tmp = gl_ModelViewProjectionMatrix * vec4(imageWidth,0.0,0.0,0.0);"
555 "vec4 t = vec4(abs(((dist_from_cam*disp_for_dist_from_cam)/tmp[0])), 0.0,0.0,0.0);"
556 "vec4 proj_t = gl_ModelViewProjectionMatrix * t;"
559 "vec4 pos = gl_ProjectionMatrix * depthAdjustedVertex;\n"
560 "disp = scale*abs(proj_t[0]/pos[3])*imageWidth;"
564 "gl_TexCoord[0] = gl_TextureMatrix[1] * origdepthAdjustedVertex;"
567 string fragmentProgramCode =
568 "uniform sampler2D projectiveTexture;"
570 "varying float disp;"
573 "gl_FragData[0] = texture2DProj(projectiveTexture, gl_TexCoord[0]);"
574 "gl_FragData[1] = vec4(disp);"
virtual BIAS::Vector3< double > GetC() const
Get projection center.
void ExecuteLinearHypothesesAssignment(const BIAS::ProjectionParametersPerspective &relativePpp, double zNear, double zFar, float N, int output=0)
Function is used to assign a hypotheses to the warped depth map.
void SetVertexBuffer(const glfVertexBuffer *vertexBuffer)
Sets the vertex buffer.
Defines the usage of the depth buffer.
BIAS::glfShader fragmentProgram_
void SetUniform(const std::string &varName, float value)
std::string GetInfoLog() const
Returns the info log containing information about the linking of the shader program.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
void SetViewportSize(unsigned int width, unsigned int height)
void SetRenderTarget(const BIAS::glfRenderTarget *renderTarget)
void Link()
Links the attached shaders.
void AttachShader(const glfShader &shader)
Attaches a shader to the program.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
bool targetParametersHaveDistortion_
void Create(GLenum type)
Creates the shader without the GLSL source code, use other Create() methods to upload source afterwar...
void SetDispForDistanceAndScale(const float &dist_from_cam, const float &disp, const float &scale)
if disparity output is required, then points with the distance (dist_from_cam) will get disparity (di...
BIAS::glfDepthBufferMode & GetDepthBufferMode()
unsigned int depthImageHeight_
BIAS::glfVertexBuffer vertices_
BIAS::glfBatch & GetBatch()
void SetupDefaultPrimitiveRendering_()
void UploadProjectiveTexture(const BIAS::Image< unsigned char > &projectiveTexture)
Upload the color image to the graphics card.
Exception class used for run-time errors in the OpenGLFramework.
A batch represents a single Draw call including all parameters (render states).
void GetUndistortion(double &kc1, double &kc2, double &kc3, double &kc4) const
void Create(int numVertices, const glfVertexFormat &format, const void *data=NULL, bool useBufferIfPossible=true)
Creates the vertex buffer for the given number of vertices and vertex format.
virtual void SetPose(const BIAS::Pose pose)
Set pose from pose object.
void SetViewportToGlobal(bool global)
BIAS::ProjectionParametersPerspective pmdParams_
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
BIAS::glfShader geometryProgram_
void Init(const BIAS::ProjectionParametersPerspective &pppd, bool targetParametersHaveDistortion=false)
Initializes all neccessary data in order to render using a batch.
void SetupShadersDefault_()
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
void SetupShadersProjectiveTexturing_()
void Create(int numIndices, GLenum type, GLenum mode, const void *indices)
Creates the element buffer for the given number of indices.
void PrintRealTime(std::ostream &os=std::cout, const bool &verbose=true) const
print the real time (=wall time clok)
void ChangeZClipping(double zNear, double zFar)
void SetTargetCameraParametersToGlobal(bool global)
void SetTargetCameraParameters(const BIAS::ProjectionParametersPerspective &relativePpp, double zNear, double zFar)
is also used to determine viewport
void UseProjectiveTexturing(const BIAS::ProjectionParametersPerspective &ppp)
ppp extrinsicis have to be in global coordinates of pppd of Init call! projective texturing cannot d...
void SetElementBuffer(const glfElementBuffer *elementBuffer)
Sets the element buffer.
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 ...
BIAS::glfShader vertexProgram_
Interface for render targets.
virtual const BIAS::Pose & GetPose() const
Return complete pose object.
This is the base class for images in BIAS.
unsigned int depthImageWidth_
void UploadDepthMap(const BIAS::Image< float > &pmdDepthImage)
Upload the pmd depth image to the graphics card.
std::string GetInfoLog() const
Returns the info log containing information about the compilation of the shader.
Vector3< T > & Normalize()
normalize this vector to length 1
class TimeMeasure contains functions for timing real time and cpu time.
BIAS::glfElementBuffer elements_
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
BIAS::glfShaderProgram shaderProgram_