1 #include <GLviewer/TriangleMeshSplatRendering.hh>
2 #include <Image/Camera.hh>
3 #include <Base/Image/ImageIO.hh>
4 #include <GLviewer/GLProjectionParametersBase.hh>
9 const char* DEPTH_PASS_VS =
10 "uniform float depthTolerance;"
11 "uniform vec3 cameraCenter;"
14 " vec3 point = gl_Vertex.xyz;"
15 " vec3 diff = normalize(point - cameraCenter);"
16 " point += diff * depthTolerance;"
17 " gl_Position = gl_ModelViewProjectionMatrix * vec4(point, 1.0);"
20 const char* DEPTH_PASS_FS =
23 " gl_FragColor = vec4(0, 0, 0, 0);"
26 const char* NORMALIZATION_PASS_VS =
29 " gl_TexCoord[0] = gl_MultiTexCoord0;"
30 " gl_Position = gl_Vertex;"
33 const char* NORMALIZATION_PASS_FS =
34 "uniform sampler2D colorTexture;"
35 "uniform sampler2D depthTexture;"
38 " vec4 color = texture2D(colorTexture, gl_TexCoord[0].st);"
39 " float invAlpha = 1.0 / color.a;"
40 " color.r *= invAlpha;"
41 " color.g *= invAlpha;"
42 " color.b *= invAlpha;"
43 " gl_FragColor = color;"
44 " gl_FragDepth = texture2D(depthTexture, gl_TexCoord[0].st).r;"
49 : internalWidth_(1280)
50 , internalHeight_(960)
51 , depthTolerance_(0.1f)
52 , enableBlending_(true)
53 , mustCreateShaders_(true)
54 , mustCreateFrameBuffer_(true)
72 internalWidth_ = width;
73 internalHeight_ = height;
74 mustCreateFrameBuffer_ =
true;
80 depthTolerance_ = depthTolerance;
94 double width = max[0] - min[0];
95 double height = max[1] - min[1];
96 double depth = max[2] - min[2];
98 double size = std::max(width, std::max(height, depth));
106 tdos_.push_back(tdo);
117 if (!enableBlending_)
119 for (
unsigned int i = 0; i < tdos_.size(); i++)
121 tdos_[i]->OpenGLOutIndexedFaceSets();
126 if (mustCreateShaders_)
129 mustCreateShaders_ =
false;
132 if (mustCreateFrameBuffer_)
134 CreateFrameBuffer(internalWidth_, internalHeight_);
135 mustCreateFrameBuffer_ =
false;
144 GLint oldViewport[4];
145 glGetIntegerv(GL_VIEWPORT, oldViewport);
148 Vector3<double> cameraCenter = camera_->GetMyselfAsProjectionParameterBase()->GetC();
161 glEnable(GL_DEPTH_TEST);
162 glDepthMask(GL_TRUE);
163 glDepthFunc(GL_LESS);
166 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
169 depthPassProgram_.
SetUniform(
"cameraCenter", cameraCenter);
170 depthPassProgram_.
SetUniform(
"depthTolerance", depthTolerance_);
171 depthPassProgram_.
Bind();
174 for (
unsigned int i = 0; i < tdos_.size(); i++) {
175 tdos_[i]->OpenGLOutIndexedFaceSets();
183 glEnable(GL_DEPTH_TEST);
184 glDepthMask(GL_FALSE);
185 glDepthFunc(GL_LEQUAL);
188 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
192 glBlendEquation(GL_FUNC_ADD);
193 glBlendFunc(GL_ONE, GL_ONE);
199 for (
unsigned int i = 0; i < tdos_.size(); i++) {
200 tdos_[i]->OpenGLOutIndexedFaceSets();
209 glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
212 glEnable(GL_DEPTH_TEST);
213 glDepthMask(GL_TRUE);
214 glDepthFunc(GL_LEQUAL);
220 normalizationPassProgram_.
Bind();
223 glActiveTexture(GL_TEXTURE0);
225 glActiveTexture(GL_TEXTURE1);
230 glTexCoord2f(0.0f, 0.0f);
231 glVertex2f(-1.0f, -1.0f);
232 glTexCoord2f(1.0f, 0.0f);
233 glVertex2f(1.0f, -1.0f);
234 glTexCoord2f(1.0f, 1.0f);
235 glVertex2f(1.0f, 1.0f);
236 glTexCoord2f(0.0f, 1.0f);
237 glVertex2f(-1.0f, 1.0f);
241 glEnable(GL_DEPTH_TEST);
242 glDepthMask(GL_TRUE);
243 glDepthFunc(GL_LESS);
245 glActiveTexture(GL_TEXTURE0);
248 void TriangleMeshSplatRendering::
252 depthPassProgram_.
Create();
255 depthPassProgram_.
Link();
258 normalizationPassProgram_.
Create();
261 normalizationPassProgram_.
Link();
262 normalizationPassProgram_.
SetUniform(
"colorTexture", 0);
263 normalizationPassProgram_.
SetUniform(
"depthTexture", 1);
266 void TriangleMeshSplatRendering::
267 CreateFrameBuffer(
int width,
int height)
275 colorBuffer_.
Allocate(width, height, GL_RGBA16F_ARB);
284 depthBuffer_.
Allocate(width, height, GL_DEPTH_COMPONENT);
304 for (
unsigned int i = 0; i < tdos_.size(); i++)
307 tdos_[i]->GetBoundingBox(localMin, localMax);
309 min[0] = std::min(min[0], localMin[0]);
310 min[1] = std::min(min[1], localMin[1]);
311 min[2] = std::min(min[2], localMin[2]);
313 max[0] = std::max(max[0], localMax[0]);
314 max[1] = std::max(max[1], localMax[1]);
315 max[2] = std::max(max[2], localMax[2]);
void GetBoundingBox(BIAS::Vector3< double > &min, BIAS::Vector3< double > &max)
void AttachShaderFromSource(GLenum type, const std::string &sourceCode)
Attaches a shader to the program, without the need to create an instance of the glfShader class...
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a texture with undefined content.
void AttachTexture(const glfTexture &texture, GLenum attachmentPoint, int attachedMipMapLevel=0, int zSlice=0, GLenum cubeMapSide=0)
Attaches a texture to an attachment point which can be.
void Create()
Creates the shader program.
void SetUniform(const std::string &varName, float value)
int GetWidth(int mipmap=0) const
Returns the width of a mipmap of the texture.
void Link()
Links the attached shaders.
void Create()
Creates the texture but does not upload any data yet.
void Bind() const
Binds the texture.
Unified output of 3D entities via OpenGL or VRML.
void SetWrapS(GLenum wrapS)
Sets the wrapping mode for the 1st texture coordinate.
void SetMinFilter(GLenum minFilter)
Sets the minifying function.
void Bind() const
Binds the shader program.
Abstract interface class to handle changes in rendering parameters by controllers and in rendering co...
int GetHeight(int mipmap=0) const
Returns the height of a mipmap of the texture.
void CheckComplete() const
Checks whether the framebuffer object is supported in its current state.
static glfScreen & GetInstance()
Returns the global screen instance.
virtual void Bind() const =0
Makes this render target the currently used render target.
void AddThreeDOut(BIAS::ThreeDOut *tdo)
Adds an arbitrary triangle ThreeDOut object to the scene.
TriangleMeshSplatRendering()
void SetWrapT(GLenum wrapT)
Sets the wrapping mode for the 2nd texture coordinate.
void SetInternalResolution(int width, int height)
Sets the resolution of the internal framebuffer object.
void ClearDepthBuffer(float depth=1.0f)
Clears the depth buffer of the render target with the given value.
void GenerateMipMap()
Explicitly generates the mipmaps of the texture in hardware.
virtual void Bind() const
Makes this render target the currently used render target.
void SetDepthTolerance(float depthTolerance)
Sets the maximum depth difference at which geometry is considered to represent the same surface and i...
void SetCamera(BIAS::GLProjectionParametersInterface *camera)
void ClearColorBuffer(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
Clears the color buffer of the render target with the given color.
~TriangleMeshSplatRendering()
void ComputeDepthTolerance(float fraction=0.01f)
Set depth tolerance based on fraction of bounding box of tdo objects.
void Splat()
main splatting routine
Interface for render targets.
void Create()
Creates the framebuffer object.
void SetMagFilter(GLenum magFilter)
Sets the magnification function.