Directx11 book exercises, Texture sampler address modes and multitexturing

I have been re-reading Frank Luna’s DirectX11 book and also doing some of the most interesting exercises that are in end of each chapter. I will post some of the exercise solution snippets so I can review them easier later and use them if I need.

Texture sampler address modes

Chapter 8 discussed texturing and the ex. 1 asked to modify texture coordinates and texture samplers address modes to reproduce some example images of the chapter.
Here is screenshots of the books crate demo with different address modes: border, clamp, mirror and wrap.

Capturech8ex1_border
border
Capturech8ex1_clamp
clamp
Capturech8ex1_mirror
mirror
Capturech8ex1_wrap
wrap

Texture coordinates of a cube where simply modified to range -0.5 to 2.5

vertices[k].Tex.x *= vertices[k].Tex.x*3.0f - 0.5f;
vertices[k].Tex.y = vertices[k].Tex.y*3.0f - 0.5f;

example sampler state in shader

SamplerState samAnisotropic
{
Filter = ANISOTROPIC;
MaxAnisotropy = 4;

AddressU = WRAP;
AddressV = WRAP;
};

Multi texturing with component wise multiplication

ex. 3 was an exercise about multi-texturing. It asked to modify the crate demo by component-wise multiplying two textures in pixel shader. Other texture was a flame texture and another was its alpha map texture.

I added two texture support for the effect by creating another ID3D11ShaderResourceView and  ID3DX11EffectShaderResourceVariable to CrateDemo.cpp and to the Effect.h files and then modified the basic effect Basix.fx hlsl code in pixel shader to actually do the component wise multiplication;

float4 texColor = float4(1, 1, 1, 1);
float4 texAlphaColor = float4(1, 1, 1, 1);

if(gUseTexure)
{
	texColor		= gDiffMap1.Sample(samAnisotropic, pin.Tex);
	texAlphaColor	= gDiffMapAlpha1.Sample(samAnisotropic, pin.Tex);
	texColor *= texAlphaColor;
}

 

componentwisemultip

 

 

 

produces result like this:

Capturech8ex3

 

 

 

 

 

Rotating texture and flipping animation

ex. 4 Asked to rotate the fireball texture of ex 3 by a function over time. I did it like this in vertex shader:

vout.Tex = mul(float4(vin.Tex, 0.0f, 1.0f), gTexTransform).xy;

float x = vout.Tex.x - 0.5;
float y = vout.Tex.y - 0.5;
vout.Tex.x = x*cos(1.0*gTime) - y*sin(1.0*gTime);
vout.Tex.y = x*sin(1.0*gTime) + y*cos(1.0*gTime);

vout.Tex.x += 0.5f;
vout.Tex.y += 0.5f;

ex 5 asked to implement simple fire animation on the cube by flipping textures. It asked to change the fire texture every 1/30 of second. 120 fire textures was given and hint to create array for them, also it was suggested that it would be more efficient to have one texture atlas and change texture coordinates every frame. I was lazy to make those 120 textures to one bigger texture in gimp or apint.net so I used texture array and changed the texture to gpu every frame.
extra member variables

float mLastFlip;
int  mCurrentFrame;

reading textures to array  in init method:

for (int i = 1; i < 121; i++)
{
	std::wstring num = L"0";
	if (i < 10) num = L"00" + std::to_wstring(i); if (i >= 10 & i < 100) num = L"0" + std::to_wstring(i); if (i >= 100)
		num = std::to_wstring(i);

	std::wstring fileName = L"Textures/FireAnim/Fire" + num + L".bmp";
	HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice,
		fileName.c_str(), 0, 0, &mFireTextures[i-1], 0));
}

updating the frame in update

if (mTimer.TotalTime() - mLastFlip > (1.0f / 30.0f))
{
	mLastFlip = mTimer.TotalTime();
	mCurrentFrame++;
	if (mCurrentFrame >= 120)
		mCurrentFrame = 0;
}

and finally inside draw()

Effects::BasicFX->SetDiffuseMap(mFireTextures[mCurrentFrame]);

 

Leave a Reply

Your email address will not be published. Required fields are marked *