• Welcome to Dark Bit Factory & Gravity.
 

Texture Mapped Sphere

Started by rdc, January 13, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

rdc

I have been trying to wrap my head around how to texture map a sphere in 2D, but my peanut brain just doesn't get it. Some of the demos I have seen do this w/o OGL, but all the examples I have found are GL examples. If someone has a few idle minutes, could I see a little working example? It doesn't have to be anything fancy, just the basics. I'll trade for some Karma. :)

Thanks.

Jim

Many ways to skin that cat - do you mean wrapping a square texture onto a sphere, so the top/bottom edges become the poles of the sphere?  Or just to draw a 2d circle that looks like it's a textured 3d sphere?  Or some other plan?

Jim
Challenge Trophies Won:

rdc

Either one would be fine, what ever is easiest to explain. I don't want to waste a bunch of your time. :)

ninogenio

i can do both but not till tomorow so if anyone wants to jump in feel free.

the 3d one is by far the more complicated you will need either an affine or perspective correct triangle mapper routine then its simply a case of loading a model and mapping your square texture from pole to pole across the uv coords ill do an example like i said and and put plenty off comments in there.
Challenge Trophies Won:

rdc

Thanks man. There is no hurry, whenever you can get to it.

Jim


void GLOBJECT::sphereuvsN(void)
{
GLUV *uv;
GLNORMAL *norm;
unsigned int x;

const double oopi = 1.0/M_PI;
const double oo2pi = 1.0/(M_PI*2.0);

uvs = uv = curr_uv;
norm = normals;

for (x = 0; x < v_count; x++, norm++)
{
double s;

uv->v = acos(norm->z) * oopi;
s = sin(uv->v * M_PI);
if (s>0.0)
{
if (norm->y >= 0.0)
uv->u = acos(norm->x / s) * oo2pi;
else
uv->u = (M_PI + acos(norm->x / s)) * oo2pi;
}
else
{
uv->u = 0.5;
}
uv++;
}

curr_uv = uv;
}


Here's a snippet from the pumpkin engine.  It computes uvs for spheres, but in fact it'll work for any shape.  The norm array is the normals at the vertices.  For spheres this is simple as the normal is the same as the position.  It calculates 2d points between 0 and 1.

Post if there's anything you don't understand.  I'm just being lazy by c&ping the code in.

Jim
Challenge Trophies Won:

rdc

Thanks man, I'll have a go at it.

ninogenio

#7
heres the 2d texture mapped sphere example it works by interpolating the uvs along the scanlines.

i didnt have a great deal of time to add comments so i will do tomorow and also ill bash up a 3d example for you.

enjoy.

Challenge Trophies Won:

rdc

Thanks, I'll take a look at it.

rdc

Just wanted to let you know I got it working. Thanks for the help guys.