Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello

I have a multi-texture square vertex composed of four different .png files.
I blending them with“Alpha Blendig”.

C++
//-------------------------------------------------------------------------//
        g_pQuad[0].colDiffuse = D3DCOLOR_ARGB(10, 255, 255, 255);
	g_pQuad[0].rhw  = 1;
	g_pQuad[0].vPos = D3DXVECTOR3(0  , 0   , 0);
	g_pQuad[0].vUV1 = D3DXVECTOR2(0.0f, 0.0f);
	g_pQuad[0].vUV2 = D3DXVECTOR2(0.0f, 0.0f);
	g_pQuad[0].vUV3 = D3DXVECTOR2(0.0f, 0.0f);
	g_pQuad[0].vUV4 = D3DXVECTOR2(0.0f, 0.0f);

	g_pQuad[1].colDiffuse = D3DCOLOR_ARGB(10, 255, 255, 255);
	g_pQuad[1].rhw  = 1;
	g_pQuad[1].vPos = D3DXVECTOR3(0  , 1000 , 0);
	g_pQuad[1].vUV1 = D3DXVECTOR2(0.0f, 1.0f);
	g_pQuad[1].vUV2 =  D3DXVECTOR2(0.0f, 1.0f);
	g_pQuad[1].vUV3 =  D3DXVECTOR2(0.0f, 1.0f);
	g_pQuad[1].vUV4 =  D3DXVECTOR2(0.0f, 1.0f);
	


	g_pQuad[2].colDiffuse = D3DCOLOR_ARGB(10, 255, 255, 255);
	g_pQuad[2].rhw  = 1;
	g_pQuad[2].vPos = D3DXVECTOR3(1000, 0   , 0);
	g_pQuad[2].vUV1 = D3DXVECTOR2(1.0f, 0.0f);
	g_pQuad[2].vUV2 = D3DXVECTOR2(1.0f, 0.0f);
	g_pQuad[2].vUV3 = D3DXVECTOR2(1.0f, 0.0f);
	g_pQuad[2].vUV4 = D3DXVECTOR2(1.0f, 0.0f);
	
	g_pQuad[3].colDiffuse = D3DCOLOR_ARGB(10, 255, 255, 255);
	g_pQuad[3].rhw  = 1;
	g_pQuad[3].vPos = D3DXVECTOR3(1000, 1000 , 0);
	g_pQuad[3].vUV1 = D3DXVECTOR2(1.0f, 1.0f);
	g_pQuad[3].vUV2 = D3DXVECTOR2(1.0f, 1.0f);
	g_pQuad[3].vUV3 = D3DXVECTOR2(1.0f, 1.0f);
	g_pQuad[3].vUV4 = D3DXVECTOR2(1.0f, 1.0f);


//----------------------------------------------------------------------------//

bool MyD3DClass::InitD3D(HWND hWnd)
{

	HRESULT Res;

	//Create the device,
	if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) 
	{
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp;



	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = true;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	if (FAILED(Res = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,  D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
        {
		return false;
	}

	
	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); 
	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); 
	
	for(unsigned i = 0;i < 8;++i)
	{
		g_pd3dDevice->SetSamplerState(i,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
		g_pd3dDevice->SetSamplerState(i,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
		g_pd3dDevice->SetSamplerState(i,D3DSAMP_MIPFILTER,D3DTEXF_ANISOTROPIC);
	}


	g_pd3dDevice->SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,0);
	g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);

	g_pd3dDevice->SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,1);
	g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_CURRENT);
	g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_MODULATE);
	g_pd3dDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2,D3DTA_CURRENT);
	g_pd3dDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_MODULATE);

	g_pd3dDevice->SetTextureStageState(2,D3DTSS_TEXCOORDINDEX,2);
	g_pd3dDevice->SetTextureStageState(2,D3DTSS_COLORARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(2,D3DTSS_COLORARG2,D3DTA_CURRENT);
	g_pd3dDevice->SetTextureStageState(2,D3DTSS_COLOROP,D3DTOP_MODULATE);
	g_pd3dDevice->SetTextureStageState(2,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(2,D3DTSS_ALPHAARG2,D3DTA_CURRENT);
	g_pd3dDevice->SetTextureStageState(2,D3DTSS_ALPHAOP,D3DTOP_MODULATE);

	g_pd3dDevice->SetTextureStageState(3,D3DTSS_TEXCOORDINDEX,3);
	g_pd3dDevice->SetTextureStageState(3,D3DTSS_COLORARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(3,D3DTSS_COLORARG2,D3DTA_CURRENT);
	g_pd3dDevice->SetTextureStageState(3,D3DTSS_COLOROP,D3DTOP_MODULATE);
	g_pd3dDevice->SetTextureStageState(3,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(3,D3DTSS_ALPHAARG2,D3DTA_CURRENT);
	g_pd3dDevice->SetTextureStageState(3,D3DTSS_ALPHAOP,D3DTOP_MODULATE);

	
	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);


	return true;

}


I want to draw a variable length line on this surface. Drawing of line is triggered by mouse move event and the line connects a predefined point on the surface to the current mouse position.

I know that it is possible to redraw the background to clear the trace of the old line and then draw a new line but I want to use a different method being independent from the background texture.

In a word,I want to do this work like the draw line in the “paint”,I don’t want to have to call setTexture() or clear() functions again.how I can do this?



Regards from your help.

[EDIT] Added <pre> tags around code sniplet for better readability - Code-o-mat [/EDIT]
Posted
Updated 29-Apr-12 23:31pm
v5

I can think of 3 solutions for this task. Don't know which would be the best/most appropriate for your needs.

1. Use the logical XOR operation to non-destructively draw the line. Repeat to remove line.

2. Load the image, draw over it, re-load when line is no longer needed

3. Load the image, save position and color of all modified pixels while drawing the line, restore the modified pixels. (or perhaps save a rectangular block that contains all modified pixels)



1 is the quickest, though the line color is hard to control AND it varies with the underlying content

2 is the easiest (and most wasteful)

3 will give single, solid line colours easily (or shaded ones for that matter) - it will also be faster than 2 - especially when using large, compressed images that have a small portion changed to display the line.


Can't remember where right now - but I'm pretty sure I've seen the 'game of life' simulated and displayed on the side(s) of a cube. I'd try looking up draw-to-texture and the game-of-life. The g.o.l would be ideal, since often a static background is used with continually evolving colonies of 'life' draw atop this background.

Also, don't know if you can get a handle to a texture(from the gpu/directX), then modify it and have the modifications displayed, OR if you need to resend the modified texture to the gpu/directX and obtain a new handle to the texture. If you don't know what I'm talking about, best you get a comfy chair and a coffee - you may have a little reading to do...
 
Share this answer
 
Alternatives to enhzflep's solutions, if you don't have access to/want to change the texture ...

1. Multiple textures - first is your map, second is your line
2. Stencil 'Shadows' - render the line 'above' the map, then cast a shadow from it, perp'l to the map

again, comfy chair, coffee
 
Share this answer
 
Comments
Espen Harlinn 19-Feb-12 18:25pm    
Good reply, my 5 :)
zoltrix61 8-Apr-12 1:52am    
I have a multi-texture square vertex composed of three different .png files. I want to draw a variable length line on this surface. Drawing of line is triggered by mouse move event and the line connects a predefined point on the surface to the current mouse position. I know that it is possible to redraw the background to clear the trace of the old line and then draw a new line but I want to use a different method being independent from the background texture.

How can I write this program with multi-texture technique?
Can you put on example for me,please...
barneyman 13-Apr-12 3:58am    
https://www.google.com.au/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1#hl=en&safe=off&output=search&sclient=psy-ab&q=directx%20texture%20stages&oq=&aq=&aqi=&aql=&gs_l=&pbx=1&fp=37b244dfe41e981b&ion=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1280&bih=909

Toymaker and Two Kings have great tutorials (items #4 and #5 in the list) - it's where I learned most of my DX info from

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900