• 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
nullsquared

Storing normals as spherical coordinates

11 posts in this topic

We've all come across the problem of storing XYZ normals efficiently and precisely without using too much space... And one solution I've heard is to convert the 3-component normal into 2-component spherical coordinates. What is the math behind converting a normal into spherical coordinates and back? Will 16 bits be enough to store these spherical coordinates relatively accurately (by relatively accurately, I mean as accurate as storing the 3 components of the normal as 16 bits each directly)? What is the numerical range of the converted spherical coordinates?
0

Share this post


Link to post
Share on other sites
A spherical co-ordinate still uses 3 components: a distance from the origin (often named radius), an angle for the latitude, and another angle for the longitude. With the special interest in normals (i.e. direction vectors of length 1) obviously the radius value is constrained to 1, and hence need not be stored explicitely. The ranges for the both angles are usually a full circle (e.g. 360°) for the latitude and a half circle (e.g. 180°) for the longitude. Special use cases may limit the angles further, e.g. if only the upper half sphere is needed.

General conversion formulas can be found on the inet, e.g. here.
0

Share this post


Link to post
Share on other sites
Spherical normal storage works great. I recommend storing (atan2(y,x), z). You don't need to encode z with acos(), and you can use a sincos() or atan2() lookup texture on older cards to turn ALU ops into tex ops.
0

Share this post


Link to post
Share on other sites
If those normals are vertex normals (which I assume since you are probably not using a 32 bit normal map, dah!), you also might want to try to encode them as 8 bit normals, which is usually enough. An 8 bit normal would be encoded like a color in DX, so you'll have to map the range [-1,1] to [0,255] and convert that to a RGB color:
Encode: enc = MakeDWORDColor((n * 0.5 + 0.5) * 255)
Decode: n = enc * 2 - 1 (maps range [0,1] back to [-1,1] in HLSL)
For spherical it's like patw describes:
Encode: enc = (atan2(n.y,n.x),n.z)
Decode: sincos(enc.x, ss, cc); n = (cc, ss, enc.z)
If you also have to store the tangent, you could try storing the full TBN system as a quaternion.
0

Share this post


Link to post
Share on other sites
All sounds very nice, but I think there might be a problem linearly interpolating such coordinates. So, although it's probably a very good solution for storing normal buffers for deferred renderers, I'm afraid trying to linearly interpolate such normals maps might result in artifacts. Imagine interpolating between a pixel with a coordinate of 179(u8 = 255) to one of it's neighbors with a coordinate of -180(u8 = 0).
0

Share this post


Link to post
Share on other sites
Ah Pixelot a very good point. I am using it specifically for a G-buffer that it is the ideal application for it.

This is a Flickr set of normal-encoding comparison shots. The thumbnails look like just black images, but if you go to full size you will see grey/white pixels. These values represent the difference between the encoded normal, read from the G-buffer, and the forward-render calculated normal (IE sample bump map, transform tangent->gbuffer_space)

Normal Storage Comparison
0

Share this post


Link to post
Share on other sites
This is indeed for a deferred renderer, therefore interpolation does not matter.

Basically, I have a GBuffer with 4 RGBA8 textures. Currently, I encode 32-bit depth in the first one, 16-bit XY normal components in the second one, and 16-bit Z component into the third one along with other miscellaneous data. What I'm trying to do is get the normal down to 2 components, such that I can encode the full 16-bits-per-component XYZ normal in a single RGBA8 texture [wink].
0

Share this post


Link to post
Share on other sites
Here in 9th grade math we've barely touched trig, so please bear with me: what is the range of atan2? -pi to pi, or 0 to 2pi, or...?
0

Share this post


Link to post
Share on other sites
Checked documentation, it is from -pi to pi.

Anyways, I implemented this, and I'm getting very odd and inconsistent results when I retrieve the "original" normals. Here is my code, spot any errors?

decode

float2 spherical = TEX2DLOD(normalMap, IN.uv0).xy;
float2 viewNormXY = float2(0, 0);
sincos(spherical.x * 2 * 3.14159265 - 3.14159265, viewNormXY.y, viewNormXY.x);
float3 viewNorm = float3(viewNormXY, spherical.y * 2 - 1);


encode

float2 spherical = float2((atan2(normal.y, normal.x) + 3.14159265) / (2.0 * 3.14159265), normal.z * 0.5 + 0.5);


Edit:
patw, I'd be interested to see how you've done it.
0

Share this post


Link to post
Share on other sites
Encode:
return (float2(atan2(nrmNorm.y, nrmNorm.x) / 3.14159265358979323846f, nrmNorm.z) + 1.0) * 0.5;

Decode:
float2 spGPUAngles = spherical.xy * 2.0 - 1.0;
float2 sincosTheta;
sincos(spGPUAngles.x * 3.14159265358979323846f, sincosTheta.x, sincosTheta.y);
float2 sincosPhi = float2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);

return float3(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y);

Storing z instead of acos(z) just saves some ops the decoding, you still need sin(phi) and cos(phi) to reconstruct XY. You just happen to already have cos(acos(z)), and the trig identity: '1 - sin(x)^2 = cos(x)^2' does the rest.

Edit:
Didn't answer the question I guess. You are seeing odd results because the conversion back from spherical is missing a step. You are computing sincos(theta) but not sincos(phi). The reason why I say store just normal.z and not acos(normal.z) is because the length of the vector is 1.0 (did I mention this method of encode/decode only works on normalized vectors) and so doing acos(normal.z/1) and then recovering cos(acos(normal.z/1)) is a very silly thing to do. Instead I store normal.z, and then compute sin(phi) by using the law of sines.
0

Share this post


Link to post
Share on other sites
@PatW

I tried using your suggestion on encoding normals to spherical.

But I seem to get some weird lines when I decode it in the light pass.

http://s54.photobucket.com/albums/g111/vidextreme/Light%20PrePass/?action=view¤t=WeirdLines.jpg


I tested to encode and decode in the same pass just to verify Encode-Decode seem to be correct. But weird lines appear when I decode them in different pass. Could this be caused by buffer format problem?
I'm using A16B16G16R16F in both normal+depth and light pass.

Thanks in advance.

EDIT:
OK, scratch that. I solve the problem. It was the filtering, I'm scaling this when I'm rendering this, and I'm using LINEAR. It's a Du'h! momment. Anyway, my solution was to use POINT filter instead.

[Edited by - vidextreme on May 27, 2009 3:29:55 AM]
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