Author Topic: Cubemapping  (Read 9199 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Cubemapping
« Reply #20 on: July 13, 2007 »
Not particularly nice code and not really the way I was wanting to do it but this goes inside the triangle code. Atm it's stuck to 128*128 maps and the values of vx,vy,vz (which is the camera-vertex vector normalised then reflected using the vertex normal in object space) are interpolated with perspective correction using u,v,s. The cubemap's rendered to by doing 6 renders starting with the objects position and orientation and rotating through 90 each time.

Code: [Select]
zzz=1.0f/zz;
lu=u*zzz;
lv=v*zzz;
ls=s*zzz;
if ((abs(lu)>abs(lv))&&(abs(lu)>abs(ls)))
{
if (lu>0.0f)
{
zzz=1.0f/lu;
ud=ls*zzz;
vd=lv*zzz;
argb=*(cubemap->texture[1]->argb+(int)((1.0f-ud)*63.5f) + ((int)((1.0f-vd)*63.5f)<<7) );
}else{
zzz=-1.0f/lu;
ud=ls*zzz;
vd=lv*zzz;
argb=*(cubemap->texture[3]->argb+(int)((1.0f+ud)*63.5f) + ((int)((1.0f-vd)*63.5f)<<7) );
}
}

if ((abs(lv)>abs(lu))&&(abs(lv)>abs(ls)))
{
if (lv>0.0f)
{
zzz=1.0f/lv;
ud=ls*zzz;
vd=lu*zzz;
argb=*(cubemap->texture[4]->argb+(int)((1.0f+ud)*63.5f) + ((int)((1.0f-vd)*63.5f)<<7) );
}else{
zzz=-1.0f/lv;
ud=-ls*zzz;
vd=lu*zzz;
argb=*(cubemap->texture[5]->argb+(int)((1.0f-ud)*63.5f) + ((int)((1.0f+vd)*63.5f)<<7) );
}
}

if ((abs(ls)>abs(lu))&&(abs(ls)>abs(lv)))
{
if (ls>0.0f)
{
zzz=1.0f/ls;
ud=lu*zzz;
vd=lv*zzz;
argb=*(cubemap->texture[0]->argb+(int)((1.0f+ud)*63.5f) + ((int)((1.0f-vd)*63.5f)<<7) );
}else{
zzz=-1.0f/ls;
ud=lu*zzz;
vd=lv*zzz;
argb=*(cubemap->texture[2]->argb+(int)((1.0f-ud)*63.5f) + ((int)((1.0f-vd)*63.5f)<<7) );
}
}
« Last Edit: July 13, 2007 by Stonemonkey »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Cubemapping
« Reply #21 on: July 22, 2007 »
Thanks for the code, Karma!
If you make those if/elses instead of three if's you'll avoid computing about half of those abs calls.

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Cubemapping
« Reply #22 on: July 22, 2007 »
Yep Jim, there's a lot that can be done to it. Setting up some variables so it's not indexing the textures too and changing the float to int conversions. I'll maybe texture it and use the alpha channel in the textures to say how reflective each texel is.

Another thing about ABS? With floats(singles) I've been trying using AND to set bit 31 to 0 as it seems a bit faster than using ABS, any problems with that?
« Last Edit: July 22, 2007 by Stonemonkey »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Cubemapping
« Reply #23 on: July 22, 2007 »
I don't think there's any problem with doing an AND.  It does force the FPU to store the value in memory, do the AND, and reload it into the FPU though.  I would have thought FABS in the FPU would be faster, but perhaps there's some problem with that.

If you're using Visual Studio 2005, you could try fabsf() which is declared in math.h.  fabsf is the float version of fabs().

Jim
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Cubemapping
« Reply #24 on: July 22, 2007 »
Sorry to piggy back on your thread stonemonkey but a quick question:

Does anyone have code/link for DYNAMIC cube mapping without shaders in OpenGL? (nehes is static).
I'm looking for a sphere in a moving environment example.

Thanks,
Chris
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Cubemapping
« Reply #25 on: July 22, 2007 »
That's cool Chris.

There's a bit on dynamic cubemaps here although it doesn't have any code or examples but it shouldn't be much different from static cubemaps other than either rendering the scene (or the closest objects at least) to each of the 6 textures or setting the viewport and copying to the textures.

http://developer.nvidia.com/object/cube_map_ogl_tutorial.html

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Cubemapping
« Reply #26 on: July 22, 2007 »
Stonemonkey,

thanks thats very useful I can see only one OGl extension import is required so its not bad for 4k at all. Shame the link to the .c file is broken at NVidias page but theres enough details to write it yourself. Cool I'll look into this.

Chris
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Cubemapping
« Reply #27 on: July 23, 2007 »
No problem chris.

Forgot something about the way I was using AND for ABS, I've been using integer comparisons too. Another thing to get rid of in that is the multiplies since I'll only be making the cubemap textures square.

The edges of the cubemap in my test are sometimes slightly visible, it's not anything to do with the cubemapping though, I'm blurring them slightly and just using a routine I already had for blurring textures but it's set up to deal with textures that are tiled so it wraps around a little and it'd be a bit of work to deal with blurring along the edges shared by different textures of the cube..

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Cubemapping
« Reply #28 on: August 10, 2007 »
Very impressive StoneMonkey dude.
Btw, is CubeMapping the same as LenseMapping ?
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Cubemapping
« Reply #29 on: August 10, 2007 »
The best way I can think of to describe it would be to imagine a wireframe cube around an object, then remove the object and place a camera in the centre of the cube. the camera is pointed out towards the centre of each face of the wireframe cube in turn and each view is rendered to a texture.

If you texture a cube with those textures the result is similar to a skybox. But with cubemapping, you interpolate the reflected camera->vertex vector to point to some point on the cubemap.