• 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
Muzzy A

Texture Mapping to Sphere is wrong

7 posts in this topic

Hey, trying to wrap a 2D texture around a sphere, but I'm getting this line of jumbled texture coords somehow, I can't quite figure it out.

[img]http://s16.postimage.org/vdmhuz651/texture_wrap.png[/img]

[code]
//Here is my shader code in HLSL

// VERTEX SHADER
float p = 3.141592f;
float3 x = float3(1, 0, 0);
float3 z = float3(0, 0, 1);

float3 norm = normalize(input.normal);

// find theta using the normal and z
float theta = acos(dot(norm,z));

// find the x-y projection of the normal
float3 proj = normalize(float3(norm.x, norm.y, 0));

// find phi using the x-y projection and x
float phi = acos(dot(proj,x));

// if x-y projection is in quadrant 3 or 4, then phi = 2PI - phi
if (proj.y < 0)
phi = 2*p - phi;

float3 coords = float3(phi / (2*p), theta / p, 0);

output.texCoord = coords;


// PIXEL SHADER
float4 color = float4(tex2D(gSampler,float2(input.texCoord.x,input.texCoord.y)).rgb,1);
return color;
[/code]

Any help would be appreciated, I'd love it, if this looked good Edited by Muzzy A
0

Share this post


Link to post
Share on other sites
Somewhere on your sphere you will have a "start" line where the x texture coord is zero, and then you increase it going around the sphere until it should reach 1.0 on exactly the same line as it was zero before. My guess is that when you reach the vertices right before the other side you set your x texture coord to 0.99 or something like that (correctly), but then when you get back to the starting point you set it to 0.0 again instead of 1.0, so the last strip of triangles gets the whole sphere texture compressed into a thin line.
You could fix this by for example duplicating that line of vertices and use 1.0 instead of 0.0 for the last one. It's easier if you precompute the texcoords and store in the vertex buffer, as it can be difficult to distinguish if you want 0.0 or 1.0 in the shader, unless you add some other type of marker to the vertices.
0

Share this post


Link to post
Share on other sites
Calculate phi using atan2:

[CODE]
float3 norm = normalize(input.normal);

float theta = acos(norm.z);
float phi = atan2(norm.y, norm.x);

float2 texCoord = float2(phi / 2, theta) / 3.14159265;

float4 color = 1.0f;
color.rgb = tex2D(gSampler, texCoord).rgb;
[/CODE]

The problem lies in the fact, that you're calculating the texture coordinates in the vertex shader. The problem is that one vertex is on the far right of the texture, while the other 2 vertices of a triangle are on the far left of the texture, which results in almost the whole texture being visible on such a triangle. You need to fix your sphere if you want to do the texture coordinate calculation in the vertex shader. If you're going to do that, you should also probably bake the texture coordinates into the vertex buffer. Edited by CryZe
0

Share this post


Link to post
Share on other sites
I moved the calculation of the texture coords to when im creating the vertex buffer, i pass it through the vertex buffer. But still i get that same line through the texture. I'm not sure what i could change to make it work right though? Any ideas?
0

Share this post


Link to post
Share on other sites
Your sphere likely consists of "stripes" or "columns" going from the north pole to the south pole, consisting of triangles. Each stripe is textured by a rectangle in the texture. Think about this until you see it clearly. You wrap your rectangular texture around the sphere, and each column of triangles get texture coords somewhere in the texture.

Your problem is at the edge where the right side of your texture touches the left side of your texture, when it is completely wrapped around your sphere.

The stripe at exactly the right side of the texture should have texture X coordinates from 1.0 at the right side to 1.0 - x on the left side, where x is whatever width each stripe has in the texture.

In your case, when going completely around the sphere the "last stripe" will in fact not get 1.0 on its right side, but 0.0, as you are back at the beginning, so that stripe will be textured with the whole texture from 0.0 to 1.0 - x, instead of the small part from 1.0 - x to 1.0.

You must duplicate your vertices at that particular line, so when creating triangles, instead of using the last line of vertices together with the first to make the last part of the sphere, duplicate the first vertices and replace the texture-coord values in the copy with 1.0 instead of 0.0. Use those vertices instead when creating your last stripe of triangles.
1

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1346766324' post='4976421']
Your sphere likely consists of "stripes" or "columns" going from the north pole to the south pole, consisting of triangles. Each stripe is textured by a rectangle in the texture. Think about this until you see it clearly. You wrap your rectangular texture around the sphere, and each column of triangles get texture coords somewhere in the texture.

Your problem is at the edge where the right side of your texture touches the left side of your texture, when it is completely wrapped around your sphere.

The stripe at exactly the right side of the texture should have texture X coordinates from 1.0 at the right side to 1.0 - x on the left side, where x is whatever width each stripe has in the texture.

In your case, when going completely around the sphere the "last stripe" will in fact not get 1.0 on its right side, but 0.0, as you are back at the beginning, so that stripe will be textured with the whole texture from 0.0 to 1.0 - x, instead of the small part from 1.0 - x to 1.0.

You must duplicate your vertices at that particular line, so when creating triangles, instead of using the last line of vertices together with the first to make the last part of the sphere, duplicate the first vertices and replace the texture-coord values in the copy with 1.0 instead of 0.0. Use those vertices instead when creating your last stripe of triangles.
[/quote]

Ya i understood what you meant the first time you said it, but i'm using D3DXCreateSphere() to generate one for me, i don't know how they set up the verts in there so i don't quite know which tex coord would be the last. I tried setting the last few texcoords to 1, didn't change a thing.
0

Share this post


Link to post
Share on other sites
You must duplicate one meridian (the one corresponding to the jumbled texture) so that one has the x-component of the texcoords to 0, and the other has them to 1. Not sure how you can achieve this through the opaque D3DX sphere mesh creation, though.
0

Share this post


Link to post
Share on other sites
[quote name='Muzzy A' timestamp='1346809223' post='4976646']
Ya i understood what you meant the first time you said it, but i'm using D3DXCreateSphere() to generate one for me, i don't know how they set up the verts in there so i don't quite know which tex coord would be the last. I tried setting the last few texcoords to 1, didn't change a thing.
[/quote]

Don't use D3DXCreateSphere. Or, just send the normal from the vertex shader, and do your calculations in the pixel shader instead.
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