[vtkusers] texturing a sphere

Jared Cohen Jared.Cohen at noaa.gov
Tue Aug 24 10:20:34 EDT 2004


  I had this exact problem. It turns out that the regular 
vtkSphereSource class has no normals, so it can't be given a texture. 
And no, you can't just add normals using the vtkPolyDataNormals class; I 
tried that. In the end, there were only 2 ways to make this work:

1) Use a vtkTexturedSphereSource class instead of a regular 
SphereSource. This is the simplest way, but the downside is that you can 
only create full spheres, not partial spheres.

2) A more flexible, but more complex way, is to create a PLANE (using 
vtkPlaneSource), and then warp it into a spherical shape. This is done 
by passing it through a vtkTransformPolyDataFilter that uses a 
vtkSphericalTransform.
The correct dimensions for the plane (if you're creating a full sphere) 
can be obtained like this:

//---------------------------------------------------------------------

deg2rad = PI / 180

Radius = 1.0 (or whatever you want for it)
Theta = Longitude angle
    min_theta    =   0.0 * deg2rad        =   0.0
    max_theta   =   360.0 * deg2rad   =   2.0 * PI
Phi = Latitude angle
    min_phi        =  0.0 * deg2rad         =   0.0
    max_phi       =  180.0 * deg2rad    =   PI

plane = vtkPlaneSource()
plane.SetOrigin(radius, max_phi, min_theta)
plane.SetPoint1(radius, max_phi, max_theta)
plane.SetPoint2(radius, min_phi, min_theta)
plane.SetXResolution(36) //or whatever you want, but don't make it too low
plane.SetYResolution(36) //ditto
plane.Update()

//---------------------------------------------------------------------


Now that you have your plane set to the correct size, you warp it into a 
sphere like this:


//---------------------------------------------------------------------

transform = vtkSphericalTransform()

tpoly = vtkTransformPolyDataFilter()
tpoly.SetInput(plane.GetOutput())
tpoly.SetTransform(transform)
tpoly.Update()

mapper = vtkPolyDataMapper()
mapper.SetInput(tpoly.GetOutput())
......

//---------------------------------------------------------------------

And then just go from there as usual.


Hope that helps :-)

>Hi all, I'm trying to texture a sphere with a PNG image. I do this with
>code similar to
>
>PNGReader->SetFileName("file.png");
>Texture->SetInput(PNGReader->GetOutput());
>Texture->InterpolateOn(); //also tried w/o setting this
>SphereSource->SetCenter(0,0,0);
>SphereSource->SetRadius(1);
>PolyDataMapper->SetInput(SphereSource->GetOutput());
>Actor->SetMapper(PolyDataMapper);
>Actor->SetTexture(Texture);
>
>My sphere changes a deep blue when I enable the texture code, but I do
>not see the actual texture on it. The actual image is small and of even
>dimensions,
>
>earth.png: PNG image data, 32 x 32, 16-bit/color RGB, non-interlaced
>
>so I do not believe this to be a power-of-2 issue or problem loading a
>texture that is beyond what my opengl implementation can do.
>
>Perhaps most importantly, I tried running the 'TPlane.py' example and
>got some disappointing output (in addition to no visible texture on the
>plane):
>
>ERROR: In /home/tfogal/tarballs/VTK/Rendering/vtkOpenGLTexture.cxx,
>line 111
>vtkOpenGLTexture (0x6640d0): No scalar values found for texture input!
>
>Any ideas?
>
>-tom
>  
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040824/49b14a93/attachment.htm>


More information about the vtkusers mailing list