Zulkhairi

I'm still new in directx programming and quite dont understand with the volume texture. I've been learning directx programming with the example given by microsoft itself. it does quite a lot of things but lack of documentation.

Straight to the point, i dont understand this chunk of code:

{
D3DLOCKED_BOX LockedBox;
hr = m_pVolumeTexture->LockBox( 0, &LockedBox, 0, 0 );
if( FAILED(hr) )
return hr;

for( UINT w=0; w<16; w++ )
{
BYTE* pSliceStart = (BYTE*)LockedBox.pBits;

for( UINT v=0; v<16; v++ )
{
for( UINT u=0; u<16; u++ )
{
FLOAT du = (u-7.5f)/7.5f;
FLOAT dv = (v-7.5f)/7.5f;
FLOAT dw = (w-7.5f)/7.5f;
FLOAT fScale = sqrtf( du*du + dv*dv + dw*dw ) / sqrtf(1.0f);

if( fScale > 1.0f ) fScale = 0.0f;
else fScale = 1.0f - fScale;

DWORD r = (DWORD)((w<<4)*fScale);
DWORD g = (DWORD)((v<<4)*fScale);
DWORD b = (DWORD)((u<<4)*fScale);

((DWORD*)LockedBox.pBits)[ u ] = 0xff000000 + (r<<16) + (g<<8) + (b);
}
LockedBox.pBits = (BYTE*)LockedBox.pBits + LockedBox.RowPitch;
}
LockedBox.pBits = pSliceStart + LockedBox.SlicePitch;
}

m_pVolumeTexture->UnlockBox( 0 );
}

i know what it does is filling the box of the volume texture that we already define using LockBox(), but the rest is still blur.

Can somebody please help me in this.

Thanks in advance. I really appreciate your help.



Re: Game Technologies: General Need assistant in volume texture

Inaki Ayucar

Well, it obviously loops through the three dimensions of the box (w, v, u). Each dimension is divided in 16 parts (0..15), and it fills each part up with the color calculated in the variables r,g,b. To know what shows that color, letí»s examine how ití»s calculated:

1.- First, the du, dv, dw seem to be differential values (basing on their name). Ití»s courious the 7.5f value. If you look at it, ití»s easy to discover that ití»s the half of the biggest index that u,v,w will have (15).

2.- All of du,dv,dw are calculated in the same way. Letí»s see whatí»s the result for du with some possible values of "u":

u = 0 --> du = ( 0 - 7.5f) / 7.5f = -1

u = 7.5 --> du = 0

u = 15 --> du = 1

3.- Ití»s easy to see that the du,dv,dw show the distance from the current u, v, w to the boxí»s center (normalized to the range -1 .. 1).

4.- So, once we got the du, dv, dw values, knowing that they represent the distance to the center, fScale is calculated with the formulae Sqrt( du * du + dv * dv + dw * dw). Thatí»s exactly the Vector Length formulae. What I doní»t know is why it divides that by Sqrt (1f) (which will be always 1). I doní»t think thatí»s important. So, fScale is the length of the vector composed with (du, dv, dw). In other words, it express with a single number (instead of a vector ) how far from the center of the box is the point (u,v,w).

5.- Because of the way fScale is calculated, it must be always positive (the length ecuation). After that, we see that it inverts the fScale value, truncating it to the range 0..1 too. So now it shows the inverse of the distance to the center (ití»s bigger as ití»s closer to the center).

6.- At last, a r,g,b color is calculated. Firstly composing each colorí»s component (r,g,b) based on the inverted distance of point 5, and later composing the final color as an integer, with a typical composition of RGB colors, just like the one used in System.Drawing.Color.ToArgb().

Conclusion:

This algorithm fills up the Volume Texture with 16 samples in each dimension. Each one gets a higher color as it gets closer to the center of the box. In other words, ití»s a gradient that fades out as it getí»s closer to the edges of the box.

Regards !






Re: Game Technologies: General Need assistant in volume texture

Zulkhairi

Thank you for your answer. But anothing thing that i want to ask is that, where is actually "coloring process" take place is it the

((DWORD*)LockedBox.pBits)[ u ] = 0xff000000 + (r<<16) + (g<<8) + (b);

or

LockedBox.pBits = (BYTE*)LockedBox.pBits + LockedBox.RowPitch;

ot this one

LockedBox.pBits = pSliceStart + LockedBox.SlicePitch;

Could you help me me in this. Thank you in advance. :)





Re: Game Technologies: General Need assistant in volume texture

Inaki Ayucar

LockedBox.pBits = (BYTE*)LockedBox.pBits + LockedBox.RowPitch;
LockedBox.pBits = pSliceStart + LockedBox.SlicePitch;

These are calculations to know the size of a row inside the texture

((DWORD*)LockedBox.pBits)[ u ] = 0xff000000 + (r<<16) + (g<<8) + (b);

This sentence composes the rgb color and stores it into the texture, so this one is your coloring function.

Anyway, the colors are based in the distances to the edges, so the previous calculations of those distances also affect the color.






Re: Game Technologies: General Need assistant in volume texture

Zulkhairi

Let say that i want to color the whole volume texture with blue color, and i dont want to make it like the example. can i use

((DWORD*)LockedBox.pBits)[ u ] = 0xff000000 + (r<<16) + (g<<8) + (b);

but i modify it a little bit, and no [ u ], because i dont color it one by one, just color the whole volume using one line only.

Is it possible





Re: Game Technologies: General Need assistant in volume texture

Zulkhairi

Can somebody please teach me how to color volume texture. Is there any function to do it

I try to develop a program where you can see rectangle with color in it. but now i can only make it black only. i cant even change the color. what i'm affraid is that the black rectangle is actually garbage volume texture. FYI, i use volume texture to create the rectangle.

Can somebody please help me in this

Thank you.