Author Topic: Texture Mapped Sphere  (Read 3921 times)

0 Members and 1 Guest are viewing this topic.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Texture Mapped Sphere
« on: January 13, 2007 »
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.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Texture Mapped Sphere
« Reply #1 on: January 13, 2007 »
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:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Texture Mapped Sphere
« Reply #2 on: January 13, 2007 »
Either one would be fine, what ever is easiest to explain. I don't want to waste a bunch of your time. :)

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Texture Mapped Sphere
« Reply #3 on: January 13, 2007 »
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:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Texture Mapped Sphere
« Reply #4 on: January 13, 2007 »
Thanks man. There is no hurry, whenever you can get to it.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Texture Mapped Sphere
« Reply #5 on: January 13, 2007 »
Code: [Select]
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:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Texture Mapped Sphere
« Reply #6 on: January 13, 2007 »
Thanks man, I'll have a go at it.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Texture Mapped Sphere
« Reply #7 on: January 18, 2007 »
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.

« Last Edit: January 19, 2007 by ninogenio »
Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Texture Mapped Sphere
« Reply #8 on: January 18, 2007 »
Thanks, I'll take a look at it.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Texture Mapped Sphere
« Reply #9 on: January 20, 2007 »
Just wanted to let you know I got it working. Thanks for the help guys.