Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Stonemonkey on July 09, 2007

Title: Cubemapping
Post by: Stonemonkey on July 09, 2007
I've made a start on this, only got the rendering to the maps done so far which is the easy bit but i have a couple of questions:

What space should it be done in and is it ok or not to have the map aligned to an object or should the cubemap be aligned with the world axis?

I am planning on chopping up my tris so that they are split where the vectors cross the planes x=y, x=-y, x=z, x=-z, y=z, y=-z . I think anyway, but wondering if that can be done linearly or not?
Title: Re: Cubemapping
Post by: Jim on July 09, 2007
Not totally sure how cube mapping works.

It's your choice as to how the cube is aligned.  This is why there's a texture matrix in d3d/opengl.

I think for the polygons you use the normal.  Whichever is the largest dimension picks which of the 6 maps to use (as you say, +-x, +-y, +-z) then use the normal to index the 2d texture in some way - I guess this is where the tricky bit comes in!  Far easier with sphere maps, the normal just indexes the sphere directly!

Jim
Title: Re: Cubemapping
Post by: Stonemonkey on July 09, 2007
My idea atm is to reflect the camera-vertex vector off the vertex using the normal and use that reflected vector for the mapping but since the reflected vector of each vertex can point to a different face of the cube I reckon I'll have to chop up the triangle so that each sub triangle only has part of a single cube face covering it. I see some problems ahead and I'm not quite sure about which way to start just yet.
I don't really see the problem with the indexing though,if i'm using the texture at +z then I'll use the vector to calculate what x and y are when z=1.0 then it's a case of u=x*.5+.5,v=y*.5+.5 sort of thing although it'll be slightly different for each side depending on the orientation of the camera for each render.
Title: Re: Cubemapping
Post by: Stonemonkey on July 10, 2007
A little test of it so far, I think I had some reports of my engine not working for some people before so it might not work. Anyway, it's only the cubemap mapped directly onto a cube so far.

It takes a little while for it to start up and it's the 2 mouse buttons to turn left/right, both to move forward.

EDIT: attatchment removed, updated test attatched further down.
Title: Re: Cubemapping
Post by: Jim on July 10, 2007
That looks like it's working just fine!  Nice demo!
Title: Re: Cubemapping
Post by: Stonemonkey on July 10, 2007
Cool, and thanks. Still got the hard stuff to do though.
Title: Re: Cubemapping
Post by: ninogenio on July 10, 2007
that works just fine here fryer very cool stuff,

im curios what more has to be added to it cause to the untrained eye like mine it looks spot on.
Title: Re: Cubemapping
Post by: Stonemonkey on July 10, 2007
At the moment, it renders to the cubemap which is rotated with the cube and the cubemap is applied directly onto the cube which isn't right for reflections and won't work for any shape other than a cube. What I have to do now is reflect each camera-vertex vector off each vertex and use the reflected vector to get the coordinates for the cubemap.
Title: Re: Cubemapping
Post by: ninogenio on July 10, 2007
ah yes i see that now.

hope you get it all figured out which im sure you will.
Title: Re: Cubemapping
Post by: Stonemonkey on July 10, 2007
I think you're right Jim, I really can't see my idea of splitting it up into smaller tris working properly so I think that I'm gonna have to test each pixel to find which texture to read from.
Title: Re: Cubemapping
Post by: taj on July 11, 2007
Uhoh doesnt work here on atix1800 winxp sp2. I gte a white screen and an egg timer cursor :0(
Title: Re: Cubemapping
Post by: Stonemonkey on July 11, 2007
Ah yep, it takes a little while to start up. The white screen with egg timer is what it does.
Title: Re: Cubemapping
Post by: Stonemonkey on July 11, 2007
I think I'm getting there but not sure if this is quite right yet:

EDIT: that maybe wasn't the best shape to test it with, made it a sphere now and it's looking much better.

It won't run from the zip, you need to unzip the files to a folder. It takes a little while to start up so you get a white window for a bit and then use the mouse buttons to turn, both together move forward.
Title: Re: Cubemapping
Post by: ninogenio on July 11, 2007
stunning stunning work mate htf do you work that fast k+
Title: Re: Cubemapping
Post by: Stonemonkey on July 11, 2007
Caffeine, lots of caffeine.

Also I was able to do most of it using my existing triangle code but instead of sending u,v,shade to it i sent the reflected 3d vector and then made the changes for the texture lookup.
Title: Re: Cubemapping
Post by: ninogenio on July 11, 2007
again it really is great and i cant believe you managed it in software at these fps  :kewl:
Title: Re: Cubemapping
Post by: Stonemonkey on July 11, 2007
Thanks nino, the way I'm doing it now should allow for normal mapping too. But that's for some other time.
Title: Re: Cubemapping
Post by: Jim on July 11, 2007
Looks tremendous!
Title: Re: Cubemapping
Post by: Stonemonkey on July 11, 2007
Cheers Jim,  I'm quite pleased with it so far.
Title: Re: Cubemapping
Post by: Paul on July 12, 2007
Thats really cool, good work
Title: Re: Cubemapping
Post by: Stonemonkey 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) );
}
}
Title: Re: Cubemapping
Post by: Jim 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
Title: Re: Cubemapping
Post by: Stonemonkey 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?
Title: Re: Cubemapping
Post by: Jim 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
Title: Re: Cubemapping
Post by: taj 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
Title: Re: Cubemapping
Post by: Stonemonkey 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
Title: Re: Cubemapping
Post by: taj 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
Title: Re: Cubemapping
Post by: Stonemonkey 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..
Title: Re: Cubemapping
Post by: Clyde on August 10, 2007
Very impressive StoneMonkey dude.
Btw, is CubeMapping the same as LenseMapping ?
Title: Re: Cubemapping
Post by: Stonemonkey 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.