Skip to main content
GameDev.net gamedev.net
🔒 Locked

Distorted Texture on Sphere

Started by andyS91 Mar 19, 2012 at 11:49 AM 2 replies 4k views
Original Post
andyS91
andyS91
I was trying to add some Textures to a sphere created with Mesh.CreateSphere. As far as i know those spheres don't have texture coordinates so i tried to add them using the sample code provided here http://www.gamedev.n...ource-included/. I called the ComputeTexCoords method from the first post with the previously created mesh as argument and then set the texture with SetTexture in the render method (Mike's code in the second post throws for some reason out of bounds exceptions and I don't know why). When launching the program everything seems to be ok but when i rotate the sphere by 90° (no matter in which direction) the texture is highly distorted (like a vortex around the x-axis). The texture I was trying to use is this one: http://celestia.h-sc...nshaded1024.jpg. If it's helpful i'll post a screenshot of the distorted texture on the sphere later (it's on another computer).
Can someone tell me what I'm doing wrong? Is it something with the ComputeTexCoords method or with the Texture? Do I have to change some RenderState/SamplerState settings?
Thank you in advance.

p.s.: I'm using Direct3D9 with SlimDX and Visual Studio 2008 (C#)
RobinsonUK
RobinsonUK
A picture would be useful. But the problem here is probably the fact that a sphere has two singularities (at either pole), so you need a smarter way of assigning your texture coordinate and mapping what is a rectangular texture onto a shape that doesn't have the same geometric invariants. Cube or Sphere maps would work better.
Tsus
Tsus
Hi!

The texture you’re trying to apply is given in theta-phi coordinates, thus the texture coordinates you are looking for are the spherical coordinates of the points on the sphere (you can throw away the radius). You can convert the object space positions of the vertices (Cartesian coordinates) directly to spherical coordinates, see here (make sure you use atan2). You can do this even in the vertex shader, so there is actually no need for modifying your vertex buffer.
To make texture coordinates out of that you have to scale-bias the spherical coordinates (phi and theta) from [-Pi, Pi] or [-Pi/2,Pi/2] respectively to [0,1].
Feel free to check back if you need help.

Cheers!
andyS91
andyS91
Thank you very much - now I've got it! smile.png
If someone is having the same problems, here's the code for adding texture coordinates to a standard sphere mesh:

public static void ComputeTexCoords(Device device, ref Mesh mesh)
{
mesh.ComputeNormals();

Mesh newMesh = mesh.Clone(device, mesh.CreationOptions, mesh.VertexFormat | VertexFormat.Texture1);
mesh.Dispose();
mesh = newMesh;
VertexElement[] elems = mesh.GetDeclaration();
int posElem = FindElementIndex(elems, DeclarationUsage.Position);
int normalElem = FindElementIndex(elems, DeclarationUsage.Normal);
int texCoordsElem = FindElementIndex(elems, DeclarationUsage.TextureCoordinate);
DataStream ds = mesh.LockVertexBuffer(LockFlags.None);
while (ds.Position < ds.Length)
{
long oldPos = ds.Position;
Vector3 vec;
ds.Position += elems[normalElem].Offset;
vec = ds.Read<Vector3>();
ds.Position = oldPos;
var phi = Math.Acos((double)vec.Y);
var v = (float)(phi / Math.PI);
var u = 0f;

if (vec.Y == 1.0f || vec.Y == -1.0f)
{
u = 0.5f;
}
else
{
u = (float)(Math.Acos(Math.Max(Math.Min(( double)-vec.Z /
Math.Sin(phi), 1.0 ), -1.0)) / (2.0 * Math.PI)) ;
u = (vec.X > 0f) ? u : 1 - u;
}
ds.Position += elems[texCoordsElem].Offset;
ds.Write<float>(u);
ds.Write<float>(v);
ds.Position = oldPos + mesh.BytesPerVertex;
}
mesh.UnlockVertexBuffer();
}


public static int FindElementIndex(VertexElement[] elems, DeclarationUsage usage)
{
for (int i = 0; i < elems.Length; ++i)
{
if (elems.Usage == usage)
return i;
}
return -1;
}


Just create a sphere with Mesh.CreateSphere and a Texture with Texture.FromFile, call the ComputeTexCoords method with this mesh and your D3D9 device as argument and set up drawing the texture with device.setTexture in your render method. I also recommend to set the renderstate to device.SetRenderState(RenderState.Wrap0, TextureWrapping.WrapCoordinate0) to avoid a ugly seam.

Most of the sourcecode isn't written by me (indeed I've written very less of it myself). The underlaying structure is from this post here: http://www.gamedev.n...ource-included/ with changes regarding the calculation of the UV-coordinates (which I've taken from here: http://channel9.msdn...Textures-Part-3). I hope the authors of those articles don't mind if I reuse their code.
Thanks again for your help and good luck for those who try to do the same ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.