6 Replies - 431 Views - Last Post: 27 June 2013 - 05:55 AM Rate Topic: -----

#1 darknessx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 18-June 13

DX11- Some questions about it

Posted 26 June 2013 - 04:07 PM

So i need some help with this again. Now its with DirectX 11 Texture System.
I want to change a character texture to make a 2D animation. 3 Frames of animation.

An example of my texture code.

TextureClass.cpp
bool TextureClass::Initialize(ID3D11Device* device, WCHAR* filename)
{
	HRESULT result;
	result = D3DX11CreateShaderResourceViewFromFile(device, filename, NULL, NULL, &m_texture, NULL);
	if(FAILED(result))
	{
		return false;
	}

	return true;
}

void TextureClass::Shutdown()
{
	if(m_texture)
	{
		m_texture->Release();
		m_texture = 0;
	}

	return;
}

ID3D11ShaderResourceView* TextureClass::GetTexture()
{
	return m_texture;
}



BitmapClass.cpp
bool BitmapClass::Render(ID3D11DeviceContext* deviceContext, int positionX, int positionY)
{
	bool result;


	// Re-build the dynamic vertex buffer for rendering to possibly a different location on the screen.
	result = UpdateBuffers(deviceContext, positionX, positionY);
	if(!result)
	{
		return false;
	}

	// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
	RenderBuffers(deviceContext);

	return true;
}

ID3D11ShaderResourceView* BitmapClass::GetTexture()
{
	return m_Texture->GetTexture();
}


bool BitmapClass::LoadTexture(ID3D11Device* device, WCHAR* filename)
{
	bool result;


	// Create the texture object.
	m_Texture = new TextureClass;
	if(!m_Texture)
	{
		return false;
	}

	// Initialize the texture object.
	result = m_Texture->Initialize(device, filename);
	if(!result)
	{
		return false;
	}

	return true;
}


void BitmapClass::ReleaseTexture()
{
	// Release the texture object.
	if(m_Texture)
	{
		m_Texture->Shutdown();
		delete m_Texture;
		m_Texture = 0;
	}

	return;
}



And another question
How i can use alpha layer to make a no-background image into my program ?.

its dds image format. have mipmaps, and alpha ok when i save with photoshop

Is This A Good Question/Topic? 0
  • +

Replies To: DX11- Some questions about it

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: DX11- Some questions about it

Posted 26 June 2013 - 06:17 PM

You are probably going to be better off asking this question in the Game Programming forum...

Moving now...
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: DX11- Some questions about it

Posted 27 June 2013 - 02:38 AM

What's the question? You said you need help and then showed your code, what's the question?

Secondly I think there is a DDS plugin for photoshop which may allow you to use an alpha channel.
Was This Post Helpful? 0
  • +
  • -

#4 darknessx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 18-June 13

Re: DX11- Some questions about it

Posted 27 June 2013 - 05:14 AM

Yes i know dds have a plugin from nvidia. but idk how to apply this into the code. Seek a printscreen.
The cause its i dont find a way to change the image into the game. like a realtime animation.
The cause i showed the code its if anyone have a sample or tutorial.

In the case what i need to learn now is.

1 - Change the buffer for make an animation like after 2 pixels(based on movespeed algorith) of movement i change buffer image to leg right, other 2 pixels leg left, if side of move is left, change image, if right too... things like this.

2 - Apply transparency into game (now only show a pink background dont have transparency)

- PrintScreen
Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 592
  • View blog
  • Posts: 1,321
  • Joined: 24-April 12

Re: DX11- Some questions about it

Posted 27 June 2013 - 05:32 AM

I haven't actually done what you're trying to do. This is pretty close to what I'm currently working on though. So, I'm just going to guess.

I believe the way to do the alpha channel is in your HLSL. I'm guessing that you mix the background color with the sprite according to the amount in the alpha channel. The alpha channel is going to be probably a number between 1 and 0 which is basically a percentage. And so that percentage is going to determine how much of the background pixel to use for the pixel color and how much of the sprite pixel to use for the color. But after trying to find some examples, I think I may be on the wrong path here.

Actually, I may be totally wrong on that the more I look at this. Maybe you don't directly control the alpha blending at all, but rather its done by DirectX during rasterization or something.

I can see your dilemma as I try to find examples on the web. DX10 examples "might" work. I believe they took out all the 2D sprite rendering stuff after DX9. So I think now, the only way you can do 2D is actually in 3D where you draw a quad (square of two triangle/polygons) and use an orthogonal camera to draw them all at the Z=0 plane. You're actually in 3D even though it looks 2D. Additionally, they took out the fixed function pipeline and so you have to use HLSL for everything.

The only tutorial I really came up with on the web was RasterTek. I think this is actually 3D and may not include the animation and such, but it may get you started in the right direction.

http://www.rastertek.../dx11tut11.html

After looking I think you actually have to set the blend state of both the device and the device context to alpha blend and then DirectX actually does the blending for you. Here's some code from a book example I found:


    D3D11_BLEND_DESC blendDesc;
    ZeroMemory( &blendDesc, sizeof( blendDesc ) );
    blendDesc.RenderTarget[0].BlendEnable = TRUE;
    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
    blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
    blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
    blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
    blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
    blendDesc.RenderTarget[0].RenderTargetWriteMask = 0x0F;

    float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };

    d3dDevice_->CreateBlendState( &blendDesc, &alphaBlendState_ );
    d3dContext_->OMSetBlendState( alphaBlendState_, blendFactor, 0xFFFFFFFF );



This kind of makes sense now that I think about it, because "How are you going to know what's in the background except during rasterization?" At the pixel shader stage, I don't think its really possible to know what to blend with because you would have to go through the whole process of drawing the scene before you could determine Z order and what objects overlap what and such in 3D. And since 2D is really 3D, it makes sense.

Now animation, I'm not sure on that either. I'm guessing you only send the current frame to your shader. In fact, I would think you're only sending half of the current image as its one triangle of the quad the sprite is on. So, you probably have basically an array that holds the sprite sheet with all the different animation images and you load the quad being drawn with just the current animation image you want based on a timer that times the playback of the animation.

I may get into trying this this weekend, and I'll let you know if I figure it out.
Was This Post Helpful? 1
  • +
  • -

#6 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 592
  • View blog
  • Posts: 1,321
  • Joined: 24-April 12

Re: DX11- Some questions about it

Posted 27 June 2013 - 05:42 AM

This may also be helpful:

http://code.msdn.mic...sample-97ae6262

It's for Windows 8, but I would think all the sprite code should work for any version of Windows. So, you should be able to get a lot from it, even if you don't run Windows 8 and can't actually compile it as is.
Was This Post Helpful? 1
  • +
  • -

#7 darknessx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 18-June 13

Re: DX11- Some questions about it

Posted 27 June 2013 - 05:55 AM

I will read all those samples and tutorials now and try to make some changes into my engine code.
Thanks for the help BBeck, i ill return some results soon.
And if i encounter a way to do this into direct code i post a example for others try to use too.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1