Jump to content

  • Log In with Google      Sign In   
  • Create Account


Distorted Texture on Sphere


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 andyS91   Members   -  Reputation: 105

Like
0Likes
Like

Posted 19 March 2012 - 05:49 AM

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#)

Sponsor:

#2 RobinsonUK   Members   -  Reputation: 108

Like
0Likes
Like

Posted 19 March 2012 - 07:39 AM

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.

#3 Tsus   Members   -  Reputation: 987

Like
0Likes
Like

Posted 19 March 2012 - 04:25 PM

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!

Acagamics e.V. – IGDA Student Game Development Club (University of Magdeburg, Germany)


#4 andyS91   Members   -  Reputation: 105

Like
0Likes
Like

Posted 20 March 2012 - 08:59 AM

Thank you very much - now I've got it! Posted Image
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[i].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 ;)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS