Sphere Mapping Textures

For questions about using Classic.

Post » Tue Apr 10, 2012 5:16 pm

Hi folks!

Currently I'm coding a GUI-frontend for the Noise Plugin, which i really like to share with you once it's finished. One cool feature I had in mind was to prepare (polar distort) a texture to be correctly mapped on a 3D sphere. My ingredients are a Image Manipulator to read the original pixels from the generated noise and an Array in 2:1 dimension to process the fancy math. Sadly, I can't figure out how to port the following code into construct. Maybe you can help me.

This is what I've done so far:


This is the code I'm trying to translate:
[CODE]for (j=0;j<image.height;j++) {
     theta = PI * (j - (image.height-1)/2.0) / (double)(image.height-1);
     for (i=0;i<image.width;i++) {
          phi = TWOPI * (i - image.width/2.0) / (double)image.width;
          phi2 = phi * cos(theta);
          i2 = phi2 * image.width / TWOPI + image.width/2;
          if (i2 < 0 || i2 > image.width-1) {
              newpixel = red;                         /* Should not happen */
          } else {
              newpixel = imagein[j*image.width+i2];
          }
          imageout[j*image.width+i] = image.newpixel;
     }
}[/CODE]

I got it from here:
http://local.wasp.uwa.edu.au/~pbourke/texture_colour/texturemap
(Texture map correction for spherical mapping)

Edit:
To show you what I'm aiming for:

The texture on the sphere looks distorted on its poles.
Maybe I don't necessarily have to go the Get/Set-pixel way and there are other solutions like mesh-distortion a sprite which i copy into a canvas or something like that. I just want to produce neat looking planets oppenheimer2012-04-10 18:29:01
B
59
S
7
G
2
Posts: 93
Reputation: 4,228

Post » Tue Apr 10, 2012 7:44 pm

I thought this interesting enough to try out.
http://dl.dropbox.com/u/5426011/examples10/sphereTex.cap

The results look pretty bad...
Either his method is flawed or another method is needed for texture mapping a sphere.
B
79
S
24
G
54
Posts: 4,747
Reputation: 40,757

Post » Tue Apr 10, 2012 8:24 pm

Nice job there Rojo.
I think, in theory, you could cut out both the sprite, and image manipulator. Canvas now has the ability to do distort maps, and Noise2 can generate without having to make an image.

Then again you could also use color filters on the distort map.
newt2012-04-10 20:30:12
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Tue Apr 10, 2012 8:48 pm

Thanks alot for your replies!
I agree, Rojo, the formula i googled all day long does not look promising enough. Maybe i was confused by all the mapping techniques that are availabe. Another formula i stumbled upon was the Mercator Projection
Perhaps this is the place to be.. dunno i'm confused..

In general you're right newt, but i wanted to physically distort the sprite (texture) because it should be mapped onto a real 3d sphere (via textureSetter). And the whole point of this tool i'm developing is to output an INI which is then loaded into my space-game which should work procedurally. The containg planets/suns consist of 3d-meshes, because i find them easier to handle (e.g. roll, pitch, yaw right away).

So any ideas on how to push those damn pixels in an appropriate way are more than welcome.

EDIT:
I like to illustrate that thing a bit further by pointing to the follow article which shows the progress in photoshop (sigh..)
spherical mapping
Basically it's just:
1) Rectangular to Polar
2) Polar to Rectangular
3) Flip Image / Rotate 180 degr.
4) Rectangular to Polar
5) Polar to Rectangularoppenheimer2012-04-10 20:56:07
B
59
S
7
G
2
Posts: 93
Reputation: 4,228

Post » Tue Apr 10, 2012 8:54 pm

The library that Noise uses already has support for polar noise generation. I just didn't add it into the plugin because I never thought it'd be utilized by a Construct user, as it's more of a 3D thing. I can look into adding it to the plugin at some point in the future, but I've got kind of a full plate right now.

libnoise is actually *much* more powerful than Noise, with all kinds of options and post processing stuff. I only implemented things I thought might be relevant to a Construct user.Arsonide2012-04-10 20:56:25
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Tue Apr 10, 2012 9:14 pm

Hey Arsonide

I think that Noise V2 does it's job pretty well, thx for that! Of course it might be easier to further post process an image than to recode that plugin once more for C1. I can imagine this sort of update for a potential C2 plugin if you're into that one day.

Edit:
[quote=newt]Then again you could also use color filters on the distort map.[/quote]
I didn't get that. Do You think that this is a benefit over meshes?

Edit2:
This whole thing is driving me mad. It seems that there isn't a 'perfect' mapping solution, because the poles consist of 1 point each; which is impossible to map.
Buuut i think your canvas/sprite distortion solution is more elegant. With a little effort i might change your example to have pitch/yaw/roll.

One more question though:
Is it possible to increase the distortion mesh quality? I tried to double the X/Y values but it gave me weird effects like little white dots and a whole different "tiling" of the texture. Multisampling is a showstopper also.

Edit3:
It's me again... Sadly, distorting a sprite/canvas does not solve the problem of mangled poles. Seems like i have to dive in a bit further and search for some other solutions.oppenheimer2012-04-11 11:08:26
B
59
S
7
G
2
Posts: 93
Reputation: 4,228

Post » Wed Apr 11, 2012 1:57 pm

OK, i almost got it.. long story short:
"Lambert cylindrical equal-area projection"
The technique seems to do the job, hopefully.

The X-pixels are untouched and Y-pixels get compressed towards the poles via Sin... But as you can see there is still something wrong with the formula. My guess is that there is some PI missing somewhere:



Please have a look at the cap: sphereTex2.cap
B
59
S
7
G
2
Posts: 93
Reputation: 4,228

Post » Wed Apr 11, 2012 4:30 pm

"I didn't get that. Do You think that this is a benefit over meshes?"

Meshes can have color filters applied at the row/column.
It would be faster than perpixel, but its per vertex instead.
It really wouldn't work here, unless the texture is really small.
It would be useful if say you wanted to add lighting.

As to increasing the quality by basically subdividing it, yes that will add some, but keep in mind there is the hardware limitation, around 64x64.newt2012-04-11 16:35:27
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Thu Apr 12, 2012 10:09 pm

i won't forget your solution and ideas newt, it might come in handy in the future.

with lucids help in the chat it turned out out that some sort of cosp plays a role in this matter:

which is this:
cosp(0,IM.GetImageHeight,LoopIndex("j")/IM.GetImageHeight)

but it exaggerates the distortion i tried to work against.
i can imagine that a sine-interpolation would be needed here.

something like this:
Lerp(0,IM.GetImageHeight, Sin(LoopIndex("j")/IM.GetImageHeight))

But it's just a pixel mess.
B
59
S
7
G
2
Posts: 93
Reputation: 4,228

Post » Thu Apr 12, 2012 10:53 pm

For sine interpolation:
Lerp(0,IM.GetImageHeight, (1+Sin(LoopIndex("j")*180/IM.GetImageHeight))/2)

But that still won't eliminate the distortion at the poles.
Paul Brouke's method eliminates the pole distortion but causes seams because parts of the image are discarded.
The area in that shape is the only part of the image that is preserved.

The libnoise website it has some sphere textures that are distorted in a way similar to pbrouke's method but I think that it was generated special for spheres.
http://libnoise.sourceforge.net/examples/textures/index.html

Also the Photoshop method just blurs the texture near the poles, it would still have the pole distortion.
B
79
S
24
G
54
Posts: 4,747
Reputation: 40,757

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests