int VTKMeshExporter::RequestData( vtkInformation* vtkNotUsed(request), vtkInformationVector** inputVector, vtkInformationVector* outputVector) { update(); // Run FAST pipeline Mesh::pointer input = getStaticInputData<Mesh>(); MeshAccess::pointer access = input->getMeshAccess(ACCESS_READ); vtkInformation *outInfo = outputVector->GetInformationObject(0); vtkPolyData *output = this->GetOutput(); vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); points->SetNumberOfPoints(input->getNrOfVertices()); for(int i = 0; i < input->getNrOfVertices(); i++) { MeshVertex v = access->getVertex(i); VectorXf position = v.getPosition(); if(input->getDimensions() == 2) { points->SetPoint(i, position.x(), position.y(), 0); } else { points->SetPoint(i, position.x(), position.y(), position.z()); } } output->SetPoints(points); vtkSmartPointer<vtkCellArray> polys = vtkSmartPointer<vtkCellArray>::New(); if(input->getDimensions() == 2) { for(int i = 0; i < input->getNrOfLines(); i++) { VectorXui line = access->getLine(i); polys->InsertNextCell(2); polys->InsertCellPoint(line.x()); polys->InsertCellPoint(line.y()); } output->SetLines(polys); } else { for(int i = 0; i < input->getNrOfTriangles(); i++) { VectorXui triangle = access->getTriangle(i); polys->InsertNextCell(3); polys->InsertCellPoint(triangle.x()); polys->InsertCellPoint(triangle.y()); polys->InsertCellPoint(triangle.z()); } output->SetPolys(polys); } // TODO if 3D, also export normals return 1; }
void MeshRenderer::draw() { boost::lock_guard<boost::mutex> lock(mMutex); glEnable(GL_NORMALIZE); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); boost::unordered_map<uint, Mesh::pointer>::iterator it; for(it = mMeshToRender.begin(); it != mMeshToRender.end(); it++) { Mesh::pointer surfaceToRender = it->second; if(surfaceToRender->getDimensions() != 3) continue; // Draw the triangles in the VBO AffineTransformation::pointer transform = SceneGraph::getAffineTransformationFromData(surfaceToRender); glPushMatrix(); glMultMatrixf(transform->data()); float opacity = mDefaultOpacity; Color color = mDefaultColor; ProcessObjectPort port = getInputPort(it->first); if(mInputOpacities.count(port) > 0) { opacity = mInputOpacities[port]; } if(mInputColors.count(port) > 0) { color = mInputColors[port]; } // Set material properties if(opacity < 1) { // Enable transparency glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } GLfloat GLcolor[] = { color.getRedValue(), color.getGreenValue(), color.getBlueValue(), opacity }; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GLcolor); GLfloat specReflection[] = { mDefaultSpecularReflection, mDefaultSpecularReflection, mDefaultSpecularReflection, 1.0f }; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specReflection); GLfloat shininess[] = { 16.0f }; glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); VertexBufferObjectAccess::pointer access = surfaceToRender->getVertexBufferObjectAccess(ACCESS_READ, getMainDevice()); GLuint* VBO_ID = access->get(); // Normal Buffer glBindBuffer(GL_ARRAY_BUFFER, *VBO_ID); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, 24, 0); glNormalPointer(GL_FLOAT, 24, (float*)(sizeof(GLfloat)*3)); glDrawArrays(GL_TRIANGLES, 0, surfaceToRender->getNrOfTriangles()*3); // Release buffer glBindBuffer(GL_ARRAY_BUFFER, 0); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); if(opacity < 1) { // Disable transparency glDisable(GL_BLEND); } glPopMatrix(); } glDisable(GL_LIGHTING); glDisable(GL_NORMALIZE); glColor3f(1.0f, 1.0f, 1.0f); // Reset color }
void MeshRenderer::draw2D( cl::BufferGL PBO, uint width, uint height, Eigen::Transform<float, 3, Eigen::Affine> pixelToViewportTransform, float PBOspacing, Vector2f translation ) { boost::lock_guard<boost::mutex> lock(mMutex); OpenCLDevice::pointer device = getMainDevice(); cl::CommandQueue queue = device->getCommandQueue(); std::vector<cl::Memory> v; v.push_back(PBO); queue.enqueueAcquireGLObjects(&v); // Map would probably be better here, but doesn't work on NVIDIA, segfault surprise! //float* pixels = (float*)queue.enqueueMapBuffer(PBO, CL_TRUE, CL_MAP_WRITE, 0, width*height*sizeof(float)*4); boost::shared_array<float> pixels(new float[width*height*sizeof(float)*4]); queue.enqueueReadBuffer(PBO, CL_TRUE, 0, width*height*4*sizeof(float), pixels.get()); boost::unordered_map<uint, Mesh::pointer>::iterator it; for(it = mMeshToRender.begin(); it != mMeshToRender.end(); it++) { Mesh::pointer mesh = it->second; if(mesh->getDimensions() != 2) // Mesh must be 2D continue; Color color = mDefaultColor; ProcessObjectPort port = getInputPort(it->first); if(mInputColors.count(port) > 0) { color = mInputColors[port]; } MeshAccess::pointer access = mesh->getMeshAccess(ACCESS_READ); std::vector<VectorXui> lines = access->getLines(); std::vector<MeshVertex> vertices = access->getVertices(); // Draw each line for(int i = 0; i < lines.size(); ++i) { Vector2ui line = lines[i]; Vector2f a = vertices[line.x()].getPosition(); Vector2f b = vertices[line.y()].getPosition(); Vector2f direction = b - a; float lengthInPixels = ceil(direction.norm() / PBOspacing); // Draw the line for(int j = 0; j <= lengthInPixels; ++j) { Vector2f positionInMM = a + direction*((float)j/lengthInPixels); Vector2f positionInPixels = positionInMM / PBOspacing; int x = round(positionInPixels.x()); int y = round(positionInPixels.y()); y = height - 1 - y; if(x < 0 || y < 0 || x >= width || y >= height) continue; pixels[4*(x + y*width)] = color.getRedValue(); pixels[4*(x + y*width) + 1] = color.getGreenValue(); pixels[4*(x + y*width) + 2] = color.getBlueValue(); } } } //queue.enqueueUnmapMemObject(PBO, pixels); queue.enqueueWriteBuffer(PBO, CL_TRUE, 0, width*height*4*sizeof(float), pixels.get()); queue.enqueueReleaseGLObjects(&v); }