diff --git a/src/fgt/filter_sdfgpu/filter_sdfgpu.cpp b/src/fgt/filter_sdfgpu/filter_sdfgpu.cpp index e953e76b1..5d0de32c2 100644 --- a/src/fgt/filter_sdfgpu/filter_sdfgpu.cpp +++ b/src/fgt/filter_sdfgpu/filter_sdfgpu.cpp @@ -15,6 +15,7 @@ #include #include #include +#include using namespace std; using namespace vcg; @@ -71,8 +72,11 @@ bool SdfGpuPlugin::applyFilter(MeshDocument& md, RichParameterSet& pars, vcg::Ca assert( onPrimitive==ON_VERTICES && "Face mode not supported yet" ); //******* GL & MESH INIT **********/ - initGL(cm.vn); - setupMesh( md, onPrimitive ); + setupMesh( md, onPrimitive ); + initGL(cm.vn); + vertexDataToTexture(*mm); + + //******** CALCULATE SDF *************/ std::vector unifDirVec; @@ -136,7 +140,7 @@ bool SdfGpuPlugin::initGL(unsigned int numVertices) mFboResult->bind(); glPushAttrib(GL_COLOR_BUFFER_BIT); - glClearColor(0,0,1,0); + glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); glPopAttrib(); @@ -171,6 +175,10 @@ bool SdfGpuPlugin::initGL(unsigned int numVertices) mSDFProgram->addUniform("texSize"); mSDFProgram->disable(); + assert(mFboResult->isValid()); + assert(mFboA->isValid()); + assert(mFboB->isValid()); + checkGLError::qDebug("GL Init failed"); } @@ -317,6 +325,8 @@ void SdfGpuPlugin::useDepthPeelingShader(FramebufferObject* fbo) void SdfGpuPlugin::calculateSdfHW(FramebufferObject& fboFront, FramebufferObject& fboBack, const Point3f& cameraDir) { + mFboResult->bind(); + glViewport(0, 0, mResTextureDim, mResTextureDim); GLfloat mv_pr_Matrix_f[16]; // modelview-projection matrix glGetFloatv(GL_MODELVIEW_MATRIX, mv_pr_Matrix_f); @@ -342,6 +352,22 @@ void SdfGpuPlugin::calculateSdfHW(FramebufferObject& fboFront, FramebufferObject glUseProgram(mSDFProgram->id()); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, fboFront.getAttachedId(GL_DEPTH_ATTACHMENT)); + mSDFProgram->setUniform1i("depthTextureFront",0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, fboBack.getAttachedId(GL_DEPTH_ATTACHMENT)); + mSDFProgram->setUniform1i("depthTextureBack",1); + + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, mVertexCoordsTexture->id()); + mSDFProgram->setUniform1i("vTexture",2); + + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, mVertexNormalsTexture->id()); + mSDFProgram->setUniform1i("nTexture",3); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, mVertexCoordsTexture->id()); mSDFProgram->setUniform1i("vTexture",0); @@ -349,17 +375,9 @@ void SdfGpuPlugin::calculateSdfHW(FramebufferObject& fboFront, FramebufferObject glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, mVertexNormalsTexture->id()); mSDFProgram->setUniform1i("nTexture",1); + glEnable( GL_TEXTURE_2D ); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, fboFront.getAttachedId(GL_DEPTH_ATTACHMENT)); - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); - mSDFProgram->setUniform1i("depthTextureFront",2); - - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, fboBack.getAttachedId(GL_DEPTH_ATTACHMENT)); - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); - mSDFProgram->setUniform1i("depthTextureBack",3); - + // cameraDir.Normalize(); // Set view direction mSDFProgram->setUniform3f("viewDirection", cameraDir.X(), cameraDir.Y(), cameraDir.Z()); @@ -372,8 +390,7 @@ void SdfGpuPlugin::calculateSdfHW(FramebufferObject& fboFront, FramebufferObject // Set viewport Size mSDFProgram->setUniform1f("viewpSize", mResTextureDim ); - mFboResult->bind(); - glViewport(0, 0, mResTextureDim, mResTextureDim); + // Screen-aligned Quad glBegin(GL_QUADS); glVertex3f(-1.0f, -1.0f, 0.0f); //L-L @@ -382,6 +399,7 @@ void SdfGpuPlugin::calculateSdfHW(FramebufferObject& fboFront, FramebufferObject glVertex3f(-1.0f, 1.0f, 0.0f); //U-L glEnd(); + mFboResult->unbind(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); @@ -389,21 +407,23 @@ void SdfGpuPlugin::calculateSdfHW(FramebufferObject& fboFront, FramebufferObject void SdfGpuPlugin::applySdfHW(MeshModel &m) { - const unsigned int texelNum = mResTextureDim*mResTextureDim; + const unsigned int texelNum = mResTextureDim*mResTextureDim; - GLfloat *result = new GLfloat[texelNum*4]; + GLfloat *result = new GLfloat[texelNum*4]; - mFboResult->bind(); + mFboResult->bind(); - glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); - glReadPixels(0, 0, mResTextureDim, mResTextureDim, GL_RGBA, GL_FLOAT, result); + glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); + glReadPixels(0, 0, mResTextureDim, mResTextureDim, GL_RGBA, GL_FLOAT, result); - for (int i=0; i < m.cm.vn; ++i) - { - m.cm.vert[i].Q() = result[i*4]; - } + for (int i=0; i < m.cm.vn; ++i) + { + m.cm.vert[i].Q() = result[i*4]; + } - delete [] result; + mFboResult->unbind(); + + delete [] result; } @@ -424,7 +444,7 @@ void SdfGpuPlugin::TraceRays(int peelingIteration, float tolerance, const Point3 glPolygonOffset(1.0f, 1.0f); fillFrameBuffer(i%2==0, mm); - + mFboA->unbind(); glDisable(GL_POLYGON_OFFSET_FILL); if(i%2) { diff --git a/src/fgt/filter_sdfgpu/shaders/calculateSdf.frag b/src/fgt/filter_sdfgpu/shaders/calculateSdf.frag index 5659fe371..9d3e40b98 100644 --- a/src/fgt/filter_sdfgpu/shaders/calculateSdf.frag +++ b/src/fgt/filter_sdfgpu/shaders/calculateSdf.frag @@ -25,8 +25,8 @@ uniform sampler2D vTexture; uniform sampler2D nTexture; -uniform sampler2DShadow depthTextureFront; -uniform sampler2DShadow depthTextureBack; +uniform sampler2D depthTextureFront; +uniform sampler2D depthTextureBack; uniform vec3 viewDirection; uniform mat4 mvprMatrix; uniform float viewpSize; @@ -48,22 +48,19 @@ void main(void) vec4 V = texture2D(vTexture, coords); vec4 N = texture2D(nTexture, coords); - - vec4 P = project(V * (viewpSize/texSize)); + + vec4 P = project(V) * (viewpSize/texSize); P = P / P.w; - float zFront = shadow2D(depthTextureFront, P.xyz).r; + float zFront = texture2D(depthTextureFront, P.xy).r; - /* if ( zFront <= P.z ) + if ( zFront >= P.z ) { - float zBack = shadow2D(depthTextureBack, P.xyz).r; - sdf = (zBack-zFront)*max(0.0,dot(viewDirection,N.xyz)); - }*/ + float zBack = texture2D(depthTextureBack, P.xy).r; + sdf = (zBack-zFront)*max(dot(N.xyz, viewDirection), 0.0); + } - sdf = dot(viewDirection,N.xyz)); - - gl_FragColor = vec4(sdf,sdf,sdf,1.0); - + gl_FragColor = vec4(sdf, sdf, sdf,1.0); } \ No newline at end of file