Jump to content
  • Advertisement
Sign in to follow this  
GAFO

DX11 [Help] From 2D Filled Color Rect to 2D Filled Texture Rect

This topic is 851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Community,

I was reading through the rastertek tutorials and got quite confused how complicated stuff is explained.

Basicly I am trying to map a texture (image) into a rect.

 

If I want draw a simple colored rect, I use this code

void G::Base::DX11Renderer::DrawFilledBox(C2D _at, DIM _dim, Color _BoxColor)
{
	if (ptr_dx11Manager->ptr_Context == nullptr)
		return;

	int a = _BoxColor.A & 0xff;
	int r = _BoxColor.R & 0xff;
	int g = _BoxColor.G & 0xff;
	int b = _BoxColor.B & 0xff;
	D3DXCOLOR* col = new D3DXCOLOR(((float)r / 255.0f), ((float)g / 255.0f), ((float)b / 255.0f), ((float)a / 255.0f));

	UINT viewportNumber = 1;
	D3D11_VIEWPORT vp;

	float blendFactor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
	ptr_dx11Manager->ptr_Context->OMSetBlendState(ptr_transparency, blendFactor, 0xffffffff);
	ptr_dx11Manager->ptr_Context->RSGetViewports(&viewportNumber, &vp);

	
	C2D loc_f = _at;
	C2D loc_t = C2D(_at.x + _dim.w, _at.y + _dim.h);
	DIM loc_view(vp.Width, vp.Height);
	NormC(&loc_f, loc_view);
	NormC(&loc_t, loc_view);
	

	COLOR_VERTEX* v = NULL;
	D3D11_MAPPED_SUBRESOURCE mapData;

	if (FAILED(ptr_dx11Manager->ptr_Context->Map(ptr_VertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mapData)))
		return;

	v = (COLOR_VERTEX*)mapData.pData;
	// position = [0] 
	//
	//  [0]------[1]
	//   |        |
	//   |        |
	//   |        |
	//  [2]------[3]

	v[0].Position.x = loc_f.x;
	v[0].Position.y = loc_f.y;
	v[0].Position.z = 1.0f;
	v[0].Color = *col;

	v[1].Position.x = loc_t.x;
	v[1].Position.y = loc_f.y;
	v[1].Position.z = 1.0f;
	v[1].Color = *col;

	v[2].Position.x = loc_f.x;
	v[2].Position.y = loc_t.y;
	v[2].Position.z = 1.0f;
	v[2].Color = *col;

	v[3].Position.x = loc_t.x;
	v[3].Position.y = loc_t.y;
	v[3].Position.z = 1.0f;
	v[3].Color = *col;
	
	ptr_dx11Manager->ptr_Context->Unmap(ptr_VertexBuffer, NULL);

	UINT Stride = sizeof(COLOR_VERTEX);
	UINT Offset = 0;

	ptr_dx11Manager->ptr_Context->IASetVertexBuffers(0, 1, &ptr_VertexBuffer, &Stride, &Offset);
	ptr_dx11Manager->ptr_Context->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
	ptr_dx11Manager->ptr_Context->IASetInputLayout(ptr_InputLayout);

	ptr_dx11Manager->ptr_Context->VSSetShader(ptr_VS, 0, 0);
	ptr_dx11Manager->ptr_Context->PSSetShader(ptr_PS, 0, 0);
	ptr_dx11Manager->ptr_Context->GSSetShader(NULL, 0, 0);
	ptr_dx11Manager->ptr_Context->Draw(4, 0);
		
	delete col;
}

and the costum COLOR_VERTEX is defined as:

struct COLOR_VERTEX
{
   D3DXVECTOR3	Position;
   D3DXCOLOR    Color;
};

So first of all is what I am doing here:

 

1) I load what is drawn on the screen into 

D3D11_MAPPED_SUBRESOURCE mapData;

and cast its pData into my custom vertex.

 

2) I overwrite the vertexes needed for "D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP"  to get a square and put the result

back into the ptr_VertexBuffer

 

Well then the rest is following ..

 

 

So my question is now how I would set a image into it instead of a color. 

I do have the following two things:

1) ID3D11Resource* ptr_Resource;

Got throught:

D3DX11CreateTextureFromFile(_ptr_Device, _file.c_str(), NULL, NULL, &ptr_Resource, NULL);

 

2) ID3D11ShaderResourceView* ptr_RenderResourceView;
Got throught:
D3DX11CreateShaderResourceViewFromFile(_ptr_Device, _file.c_str(), NULL, NULL, &ptr_RenderResourceView, NULL);
 
As described here:
Sadly the drawing examble is missing since ebook got removed/the link posted there doesnt work anymore :/
Any Ideas how I would use those 2 things to get a image into my square ? Remember that I only want it to be 2D.
 
I gues just replacing D3DXCOLOR inside the costum vertex wont help, or would it ?
 
,greetings
 
 
 
 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!