• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
andyS91

Distorted Texture on Sphere

3 posts in this topic

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 [url="http://www.gamedev.net/topic/548474-slimdx-mesh-utility-functions-source-included/"]http://www.gamedev.n...ource-included/[/url]. 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: [url="http://celestia.h-schmidt.net/earth-vt/unshaded1024.jpg"]http://celestia.h-sc...nshaded1024.jpg[/url]. 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#)
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 [url="http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates"]here[/url] (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!
0

Share this post


Link to post
Share on other sites
Thank you very much - now I've got it! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
If someone is having the same problems, here's the code for adding texture coordinates to a standard sphere mesh:

[code] 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();
}[/code]

[code] 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;
}[/code]

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: [url="http://www.gamedev.net/topic/548474-slimdx-mesh-utility-functions-source-included/"]http://www.gamedev.n...ource-included/[/url] with changes regarding the calculation of the UV-coordinates (which I've taken from here: [url="http://channel9.msdn.com/coding4fun/articles/Ask-the-ZMan-Applying-Textures-Part-3"]http://channel9.msdn...Textures-Part-3[/url]). 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 ;)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0