如何在D3D9中清除纹理上移动线的痕迹 [英] How Clean trace of mobile line on the texture in D3D9

查看:149
本文介绍了如何在D3D9中清除纹理上移动线的痕迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个由四个不同的.png文件组成的多纹理正方形顶点.
我将它们与"Alpha Blendig"混合.

Hello

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

//-------------------------------------------------------------------------//
        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;

}



我想在此表面上画一条可变长度的线.线条的绘制由鼠标移动事件触发,并且线条将曲面上的预定义点连接到当前鼠标位置.

我知道可以重绘背景以清除旧线的痕迹,然后再绘制新线,但是我想使用一种与背景纹理无关的方法.

总之,我想像画图"中的画线一样完成这项工作,我不想再调用setTexture()或clear()函数.我该怎么做?



关于您的帮助.

添加了< pre>代码片段周围的标签可提高可读性-Code-o-mat [/EDIT]



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.

Added <pre> tags around code sniplet for better readability - Code-o-mat [/EDIT]

推荐答案

我可以想到3种解决方案.不知道哪种最适合/最适合您的需求.

1.使用逻辑XOR操作以非破坏性的方式画线.重复删除行.

2.加载图像,在其上绘制,在不再需要线条时重新加载

3.加载图像,在绘制线条时保存所有修改像素的位置和颜色,恢复修改后的像素. (或者保存包含所有修改后的像素的矩形块)



1是最快的,尽管线条颜色很难控制,并且随基础内容的不同而变化

2是最简单(也是最浪费)的

3将很容易给出单一的实线颜色(或与此相关的阴影颜色)-也将比2更快-尤其是在使用较大的压缩图像而更改了很小的部分以显示线条时.


我不记得现在在哪里-但我很确定自己已经看过模拟并显示在立方体侧面的生命游戏".我会尝试查找贴图和生活游戏. g.o.l是理想的,因为通常使用静态背景,并在此背景上绘制不断演变的生命"菌落.

另外,不知道是否可以从gpu/directX获取纹理的句柄,然后对其进行修改并显示修改,或者是否需要将修改后的纹理重新发送到gpu/directX并获取纹理的新句柄.如果您不知道我在说什么,最好得到一把舒适的椅子和一杯咖啡-您可能需要阅读一些内容...
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...


替代方法解决方案,如果您无权访问/不想更改纹理...

1.多种纹理-第一是您的地图,第二是您的线
2.模具阴影"(Shadows)-在地图上方"渲染线,然后从中投射阴影,将阴影贴在地图上

再次,舒适的椅子,咖啡
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


这篇关于如何在D3D9中清除纹理上移动线的痕迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆