Sometimes we need to use texture mapping, or texture mapping, to make our models look more realistic.
Texture mapping is a powerful tool for creating visual lifelike effects. The basic idea of texture mapping is that during rendering, images can be attached to the surface of the rendered object, thus creating a more detailed rendering effect.
The texture map needs to provide three pieces of information: the surface of the texture map to be attached, the texture map, and the texture coordinates.
Here is an example of texturing a cylinder by “pasting” a 2D image onto the surface of the cylinder.
Texture mapping is a powerful tool for creating visual lifelike effects. The basic idea of texture mapping is that during rendering, images can be attached to the surface of the rendered object, thus creating a more detailed rendering effect.
The texture map needs to provide three pieces of information: the surface of the texture map to be attached, the texture map, and the texture coordinates.
Here is an example of texturing a cylinder by “pasting” a 2D image onto the surface of the cylinder.
#include <vtkCylinderSource.h>
#include <vtkLineSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkTubeFilter.h>
#include <vtkBMPReader.h>
#include <vtkJPEGReader.h>
#include <vtkTexture.h>
#include <vtkTextureMapToCylinder.h>
#include <vtkTransformTextureCoords.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " texture(.jpg)" << std::endl;
return EXIT_FAILURE;
}
// Create a Cylinder
vtkSmartPointer<vtkCylinderSource> cylinderSource =
vtkSmartPointer<vtkCylinderSource>::New();
cylinderSource->SetHeight(10.0);
cylinderSource->SetCenter(0.0, 0.0, 0.0);
cylinderSource->SetRadius(2.0);
cylinderSource->SetResolution(50);
// Load a jpeg file
vtkSmartPointer<vtkJPEGReader> jpegReader =
vtkSmartPointer<vtkJPEGReader>::New();
jpegReader->SetFileName(argv[1]);
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(jpegReader->GetOutputPort());
texture->InterpolateOn();
// map two-dimensional to three-dimensional, use cylinder
vtkSmartPointer<vtkTextureMapToCylinder> textureMap =
vtkSmartPointer<vtkTextureMapToCylinder>::New();
textureMap->SetInputConnection(cylinderSource->GetOutputPort());
vtkSmartPointer<vtkTransformTextureCoords> transformTextureCoords =
vtkSmartPointer<vtkTransformTextureCoords>::New();
transformTextureCoords->SetInputConnection(textureMap->GetOutputPort());
//transformTextureCoords->SetScale(1, 1, 0);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(/*textureMap*/transformTextureCoords->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor =
vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->SetTexture(texture);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(600, 600);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(cylinderActor);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
How to create a cylinder
The key code
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(/*bmpReader*/jpegReader->GetOutputPort());
texture->InterpolateOn();
// map two-dimensional to three-dimensional, use cylinder
vtkSmartPointer<vtkTextureMapToCylinder> textureMap =
vtkSmartPointer<vtkTextureMapToCylinder>::New();
textureMap->SetInputConnection(cylinderSource->GetOutputPort());
vtkSmartPointer<vtkTransformTextureCoords> transformTextureCoords =
vtkSmartPointer<vtkTransformTextureCoords>::New();
transformTextureCoords->SetInputConnection(textureMap->GetOutputPort());
//transformTextureCoords->SetScale(1, 1, 0);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(/*textureMap*/transformTextureCoords->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor =
vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->SetTexture(texture);
rendering
The complete code
Reference
VTK texture mapping principle introduction
VTK TransformTextRoots for texture mapping
VTK/Examples/Cxx/Texture/TexturedSphere
Read More:
- VTK learning notes: visual model
- Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:
- VTK: VTK error problem set
- Examples of VTK image reading and display
- Using vtk8.1 in qcreator
- UE4 texture streaming pool over xxmb error
- OpenGL cube texture map
- Solutions to several VTK compilation errors (vtk5.8 + VS2010)
- Implement Ogre video texture with VMR9
- Browser prompt: Source mapping error: request failed with status 404 Source URL: http://xxx.js Source mapping URL: jquery.min.map
- VTK series: installation and introduction of VTK
- IsADirectoryError: [Errno 21] Is a directory: ‘/home/user8/Desktop/low_light/Learning-Texture-Invari
- VTK cultivation 26: basic operation of image_ Three dimensional image slice extraction
- [VTK] header file and lib file name often used in VTK
- Learning notes of OpenGL — blending
- Learning notes — opengl01
- Using qvtk renderwindoninteractor to load VTK widget
- [debug notes] pointer error when STD:: vector is used in VTK “access conflict when reading location XXX”
- VTK + QT + vs compilation and installation configuration
- OpenGL learning notes: Problems and Solutions