GPU::Texture2D GPU::Texture2D::Clone() const { glPushAttrib( GL_TEXTURE_BIT ); GPU::Texture2D newTex; newTex.Create(); newTex.m_Width = Width(); newTex.m_Height = Height(); if( m_Id ) { GPU::FrameBuffer fbuffer( Width(), Height() ); fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this ); fbuffer.Bind(); newTex.Bind(); glCopyTexImage2D( GL_TEXTURE_2D, 0, InternalFormat(), 0, 0, Width(), Height(), 0 ); newTex.Unbind(); fbuffer.Unbind(); } glPopAttrib(); return newTex; }
bool FBug96Test() { Bug96Test::simpleStruct_6 si; si.en(Bug96Test::GREEN); // Create the serializer object. char *buffer = (char*)calloc(1, si.getMaxSerializedSize()); eprosima::FastBuffer fbuffer(buffer, si.getMaxSerializedSize()); EnumerationsSer serializer(fbuffer); // Serialize structure. try { serializer << si; } catch(eprosima::Exception &ex) { std::cout << "TEST FAILED<FBug96Test>: Serialization. " << ex.what() << std::endl; free(buffer); return false; } // Reset serializer object. serializer.reset(); // Object used to deserialize the data. Bug96Test::simpleStruct_6 sires; // Deserialize structure. try { serializer >> sires; } catch(eprosima::Exception &ex) { std::cout << "TEST FAILED<FBug95Test>: Deserialization. " << ex.what() << std::endl; free(buffer); return false; } // Compare data. bool compareOK = true; compareOK &= si.en() == sires.en(); if(!compareOK) { std::cout << "TEST FAILED<FBug96Test>: Wrong values" << std::endl; free(buffer); return false; } return true; }
bool GPU::Texture2D::DumpTo( void *buffer, const GLenum format, const GLenum type, const GLint level ) const { if( m_Id ) { #if glGetTextureImageEXT // Extension GL_EXT_direct_state_access is no supported by every common computer... glGetTextureImageEXT( m_Id, GL_TEXTURE_2D, level, format, type, buffer ); #else GPU::FrameBuffer fbuffer( m_Width, m_Height ); fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this ); fbuffer.DumpTo( GL_COLOR_ATTACHMENT0_EXT, buffer, format, type ); #endif } return m_Id != 0; }
void snapshot(const std::string filename) { GLenum buffer(GL_BACK); const int offset = 8; int w = glutGet(GLUT_WINDOW_WIDTH) - offset; int h = glutGet(GLUT_WINDOW_HEIGHT) - offset; std::vector<GLubyte> fbuffer(3 * w * h); glReadBuffer(buffer); glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); display(); glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, &fbuffer[0]); std::vector<GLubyte> image(3 * w*h); unsigned int x, y, fbuffer_offset, image_offset; for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { fbuffer_offset = 3 * (y * w + x); image_offset = 3 * ((h - y - 1)* w + x); for (int i = 0; i < 3; ++i) image[image_offset + i] = fbuffer[fbuffer_offset + i]; } } std::string name = filename; name.append(".ppm"); FILE *ppmFile = fopen(name.c_str(), "wb"); fprintf(ppmFile, "P6\n"); fprintf(ppmFile, "%d %d\n", w, h); fprintf(ppmFile, "255\n"); fwrite(&image[0], 1, w * h * 3, ppmFile); fclose(ppmFile); }
void FilterOutputOpticalFlowPlugin::paintShadowTexture( GPU::Texture2D &shadowMap, vcg::Matrix44f &proj, vcg::Matrix44f &pose ) { glPushAttrib( GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_VIEWPORT_BIT | GL_POLYGON_BIT | GL_TRANSFORM_BIT ); glEnable( GL_DEPTH_TEST ); glEnable( GL_POLYGON_OFFSET_FILL ); glPolygonOffset( 2.0f, 2.0f ); glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadMatrixf( proj.V() ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadMatrixf( pose.V() ); GPU::FrameBuffer fbuffer( shadowMap.Width(), shadowMap.Height() ); fbuffer.Attach( GL_DEPTH_ATTACHMENT, shadowMap ); fbuffer.Bind(); glClear( GL_DEPTH_BUFFER_BIT ); m_MeshVBO.Bind(); m_MeshVBO.DrawElements( GL_TRIANGLES, 0, 3*m_Mesh->fn ); m_MeshVBO.Unbind(); fbuffer.Unbind(); glPopMatrix(); glMatrixMode( GL_PROJECTION ); glPopMatrix(); glPopAttrib(); }
v8::Handle<v8::Value> BufferWrap::filter_point_in_time(const v8::Arguments& args) { INSTANCE_CHECK(BufferWrap, "Buffer", "filter_point_in_time"); v8::HandleScope scope; if (args.Length() != 1) { return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a point in time as first and only argument"))); } osmium::Timestamp point_in_time; if (args[0]->IsInt32()) { point_in_time = args[0]->Int32Value(); } else if (args[0]->IsString()) { v8::String::Utf8Value time_string { args[0] }; point_in_time = osmium::Timestamp(*time_string); } else if (args[0]->IsDate()) { point_in_time = osmium::Timestamp(static_cast<int32_t>(v8::Date::Cast(*args[0])->NumberValue() / 1000)); } typedef osmium::DiffIterator<osmium::memory::Buffer::t_iterator<osmium::OSMObject>> diff_iterator; osmium::memory::Buffer& buffer = unwrap<BufferWrap>(args.This()); osmium::memory::Buffer fbuffer(buffer.committed(), osmium::memory::Buffer::auto_grow::yes); { auto dbegin = diff_iterator(buffer.begin<osmium::OSMObject>(), buffer.end<osmium::OSMObject>()); auto dend = diff_iterator(buffer.end<osmium::OSMObject>(), buffer.end<osmium::OSMObject>()); std::for_each(dbegin, dend, [point_in_time, &fbuffer](const osmium::DiffObject& d) { if (((d.end_time() == 0 || d.end_time() > point_in_time) && d.start_time() <= point_in_time) && d.curr().visible()) { fbuffer.add_item(d.curr()); fbuffer.commit(); } }); } return scope.Close(new_external<BufferWrap>(std::move(fbuffer))); }
void FilterOutputOpticalFlowPlugin::retroProjection( RasterFaceMap &rpatches, float coverageThreshold, QMap<int,QVector<int>> &validPairs ) { glPushAttrib( GL_ALL_ATTRIB_BITS ); glEnable( GL_CULL_FACE ); glCullFace( GL_BACK ); GPU::Texture2D shadowMap; GPU::Texture2D colorMap; GPU::Shader reprojShader; setupMeshVBO(); setupShader( reprojShader ); validPairs.clear(); for( RasterFaceMap::iterator rproj=rpatches.begin(); rproj!=rpatches.end(); ++rproj ) { vcg::Matrix44f proj, pose, shadowProj; shadowTextureMatrices( rproj.key(), proj, pose, shadowProj ); setupShadowAndColorTextures( shadowMap, colorMap, rproj.key() ); paintShadowTexture( shadowMap, proj, pose ); for( RasterFaceMap::iterator rref=rpatches.begin(); rref!=rpatches.end(); ++rref ) if( rref != rproj ) { const vcg::Point2i vp = rref.key()->shot.Intrinsics.ViewportPx; // Create the framebuffer for the offscreen rendering. GPU::FrameBuffer fbuffer( vp.X(), vp.Y() ); fbuffer.Attach( GL_COLOR_ATTACHMENT0, GL_RGBA ); fbuffer.Attach( GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT ); fbuffer.Bind(); // Set up the OpenGL state and viewpoint matrices. glEnable( GL_DEPTH_TEST ); glDisable( GL_POLYGON_OFFSET_FILL ); float zNear, zFar; GlShot< vcg::Shot<float> >::GetNearFarPlanes( rref.key()->shot, m_Mesh->bbox, zNear, zFar ); if( zNear < 0.0001f ) zNear = 0.1f; if( zFar < zNear ) zFar = zNear + 1000.0f; GlShot< vcg::Shot<float> >::SetView( rref.key()->shot, zNear, zFar ); // Perform the rendering of the surface patch associated to the current reference view. // The other pictures are then reprojected onto it. glClearColor( 1.0f, 0.0f, 1.0f, 0.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); shadowMap.Bind( 0 ); colorMap.Bind( 1 ); reprojShader.Bind(); reprojShader.SetSampler( "u_ShadowMap" , 0 ); reprojShader.SetSampler( "u_ColorMap" , 1 ); reprojShader.SetUniform( "u_ShadowProj", shadowProj.V() ); m_MeshVBO.Bind(); for( FaceVec::iterator f=rref->begin(); f!=rref->end(); ++f ) m_MeshVBO.DrawElements( GL_TRIANGLES, 3*(*f - &m_Mesh->face[0]), 3 ); m_MeshVBO.Unbind(); reprojShader.Unbind(); colorMap.Unbind(); shadowMap.Unbind(); GlShot< vcg::Shot<float> >::UnsetView(); fbuffer.Unbind(); // Read back the content of the framebuffer. GLubyte *buffer = new GLubyte [ 4*vp.X()*vp.Y() ]; glPixelStorei( GL_PACK_ALIGNMENT, 1 ); fbuffer.DumpTo( GL_COLOR_ATTACHMENT0, buffer, GL_RGBA, GL_UNSIGNED_BYTE ); // Counts the the number of pixels covered by the whole surface patch, and the number of pixels // covered by the reprojection of the other image onto this patch. unsigned int coverage = 0, reprojected = 0; for( int n=0; n<4*vp.X()*vp.Y(); n+=4 ) if( buffer[n+3] == 255 ) { coverage ++; if( buffer[n]!=255 && buffer[n+1]!=0 && buffer[n+2]!=255 ) reprojected ++; } // If the ratio of reprojected pixels is below a given threshold, the current picture pair is ignored. if( (float)reprojected/coverage > coverageThreshold ) { QString filename = QString().sprintf("rectified_%03i_%03i.png", rproj.key()->id+1, rref.key()->id+1 ); QImage img( vp.X(), vp.Y(), QImage::Format_RGB888 ); for( int y=vp.Y()-1, n=0; y>=0; --y ) for( int x=0; x<vp.X(); ++x, n+=4 ) img.setPixel( x, y, qRgb(buffer[n],buffer[n+1],buffer[n+2]) ); img.save( filename ); validPairs[rref.key()->id+1].push_back( rproj.key()->id+1 ); } delete [] buffer; } } m_MeshVBO.Release(); glPopAttrib(); }
// This function performs visibility check and gathers weights for all mesh vertices with respect to the given raster, // and update their dominant images accordingly. // The computation is done by a shader that stores weight values in the frame buffer, with a negative weight for // vertices that are not visible from the current raster's viewpoint. For each mesh vertex, a pixel is written, // and pixel ordering in the framebuffer corresponds to vertex ordering in the mesh. void DominancyClassifier::checkDominancy( OOCRaster &rr ) { // Save the previous OpenGL state. glPushAttrib( GL_VIEWPORT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT ); // Create a frambuffer with a viewport big enough to define one pixel per mesh vertex. vcg::Point2i vp; vp[0] = 2048; vp[1] = (int) std::ceil( (float)m_Mesh.vn / vp[0] ); GPU::FrameBuffer fbuffer( vp[0], vp[1] ); fbuffer.Attach( GL_COLOR_ATTACHMENT0, GL_LUMINANCE32F_ARB ); fbuffer.Bind(); // Set up the shader that will compute visibility check and weight gathering. m_VisCheckShader.Bind(); m_ShadowMap.Bind( 0 ); m_VisCheckShader.SetSampler( "u_ShadowMap" , 0 ); m_WeightMap.Bind( 1 ); m_VisCheckShader.SetSampler( "u_WeightMap" , 1 ); if( m_WeightMask & W_SILHOUETTE ) { m_WeightMapSilh.Bind( 2 ); m_VisCheckShader.SetSampler( "u_WeightMapSilh", 2 ); } m_VisCheckShader.SetUniform( "u_Viewport", vp.V() ); m_VisCheckShader.SetUniform( "u_Viewpoint", rr.shot.GetViewPoint().V() ); m_VisCheckShader.SetUniform( "u_ShadowProj", m_TexProj.V() ); // Perform the rendering pass. glDisable( GL_DEPTH_TEST ); m_MeshVBO.Bind(); m_MeshVBO.DrawArrays( GL_POINTS, 0, m_Mesh.vn ); m_MeshVBO.Unbind(); // Restore the previous OpenGL state. m_ShadowMap.Unbind(); m_WeightMap.Unbind(); if( m_WeightMask & W_SILHOUETTE ) m_WeightMapSilh.Unbind(); m_VisCheckShader.Unbind(); fbuffer.Unbind(); glPopAttrib(); // Read the content of the framebuffer and update the vertex dominancy accordingly. float *weightBuffer = new float [ vp[0]*vp[1] ]; fbuffer.DumpTo( GL_COLOR_ATTACHMENT0, weightBuffer, GL_LUMINANCE, GL_FLOAT ); for( int v=0; v<m_Mesh.vn; ++v ) if( weightBuffer[v] > m_VertexDom[v].weight1 ) { m_VertexDom[v].weight2 = m_VertexDom[v].weight1; m_VertexDom[v].dominant2 = m_VertexDom[v].dominant1; m_VertexDom[v].weight1 = weightBuffer[v]; m_VertexDom[v].dominant1 = &rr; } else if( weightBuffer[v] > m_VertexDom[v].weight2 ) { m_VertexDom[v].weight2 = weightBuffer[v]; m_VertexDom[v].dominant2 = &rr; } delete [] weightBuffer; }
void DominancyClassifier::generateWeightsAndShadowMap( OOCRaster &rr ) { // Backup the previous OpenGL states. glPushAttrib( GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT ); // Set the OpenGL matrices so as to place the viewpoint on the given raster. glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadMatrixf( m_Proj.V() ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadMatrixf( m_Pose.V() ); // Create the off-screen rendering context, using the shadow map texture as depth buffer. //m_WeightMap.Create( GL_LUMINANCE32F_ARB, m_ShadowMap.Width(), m_ShadowMap.Height() ); m_WeightMap.Create( GL_LUMINANCE, m_ShadowMap.Width(), m_ShadowMap.Height() ); m_WeightMap.SetFiltering( GL_NEAREST ); GPU::FrameBuffer fbuffer( m_ShadowMap.Width(), m_ShadowMap.Height() ); fbuffer.Attach( GL_COLOR_ATTACHMENT0, m_WeightMap ); fbuffer.Attach( GL_DEPTH_ATTACHMENT, m_ShadowMap ); fbuffer.Bind(); // Perform the rendering pass that computes the the shadow map used afterward for visibility checks // as well as the first weight map containing the orientation mask, the image border mask and // the distance masks (the silhouette mask is computed hereafter, using the depth gather in the shadow map). glEnable( GL_DEPTH_TEST ); glEnable( GL_POLYGON_OFFSET_FILL ); glPolygonOffset( 2.0f, 2.0f ); glClearColor( 0.0f, 1.0f, 0.0f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); m_WeightShader.Bind(); m_MeshVBO.TexCoord.Disable(); m_MeshVBO.Bind(); m_MeshVBO.DrawElements( GL_TRIANGLES, 0, 3*m_Mesh.fn ); m_MeshVBO.Unbind(); m_MeshVBO.TexCoord.Enable(); m_WeightShader.Unbind(); fbuffer.Unbind(); // Restore the previous OpenGL states. glMatrixMode( GL_PROJECTION ); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); glPopMatrix(); glPopAttrib(); #if 0 unsigned char *b = new unsigned char [ fbuffer.Width()*fbuffer.Height() ]; fbuffer.DumpTo( GL_COLOR_ATTACHMENT0, b, GL_LUMINANCE, GL_UNSIGNED_BYTE ); QImage img( b, fbuffer.Width(), fbuffer.Height(), QImage::Format_Indexed8 ); img = img.convertToFormat( QImage::Format_RGB888 ); img.save( "test1.png" ); delete [] b; #endif // Recover the depth of the scene and create the texture containing the step discontinuity mask, if required. if( m_WeightMask & W_SILHOUETTE ) { glPushAttrib( GL_TEXTURE_BIT ); // Read the depth normalized between 0 and 1 from the framebuffer. floatbuffer buffer1, buffer2; buffer1.init( fbuffer.Width(), fbuffer.Height() ); buffer2.init( fbuffer.Width(), fbuffer.Height() ); fbuffer.DumpTo( GL_DEPTH_ATTACHMENT, buffer1.data, GL_DEPTH_COMPONENT, GL_FLOAT ); // WARNING: leads to a GL error. float zNear, zFar; GlShot< vcg::Shot<float> >::GetNearFarPlanes( rr.shot, m_Mesh.bbox, zNear, zFar ); if( zNear < 0.0001f ) zNear = 0.1f; if( zFar < zNear ) zFar = zNear + 1000.0f; float range = zFar - zNear; for( unsigned int i=0; i<fbuffer.Width()*fbuffer.Height(); ++i ) if( buffer1.data[i] < 1.0f ) buffer1.data[i] = zNear*zFar / ((zFar - buffer1.data[i]*range)*range); // Detect step discontinuities and compute the distance of each pixel to the closest one. buffer2.applysobel( &buffer1 ); buffer2.initborder( &buffer1 ); buffer2.distancefield(); // Create the OpenGL texture containing the weight values. //m_WeightMapSilh.Create( GL_LUMINANCE32F_ARB, fbuffer.Width(), fbuffer.Height(), GL_LUMINANCE, GL_FLOAT, buffer2.data ); m_WeightMapSilh.Create( GL_LUMINANCE, fbuffer.Width(), fbuffer.Height(), GL_LUMINANCE, GL_FLOAT, buffer2.data ); m_WeightMapSilh.SetFiltering( GL_NEAREST ); glPopAttrib(); #if 0 unsigned char *b = new unsigned char [ fbuffer.Width()*fbuffer.Height() ]; for( unsigned int i=0; i<fbuffer.Width()*fbuffer.Height(); ++i ) b[i] = (unsigned char)(255.0f*buffer2.data[i]); QImage img( b, fbuffer.Width(), fbuffer.Height(), QImage::Format_Indexed8 ); img = img.convertToFormat( QImage::Format_RGB888 ); img.save( "test2.png" ); delete [] b; #endif } }