DirectX如何让鼠标点中矩形移动呢?
现在我用DirectX创建了4个顶点,然后画了一个矩形,想让鼠标点矩形的时候,矩形向右移动,鼠标点中矩形之外,则矩形不移动,改如何修改下面的程序呢?
另外,我看顶点的坐标 CUSTOMVERTEX g_Vertices[]={
{ -3, -3, 0, 0.0f, 1.0f},
{ -3, -0.2, 0, 0.0f, 0.5f},
{ 0-0.1, -3, 0, 0.5f, 1.0f},
{ 0-0.1, -0.2, 0, 0.5f, 0.5f },
};
它与窗口 HWND hWnd = CreateWindow( L"ClassName", L"纹理影射基础",
WS_OVERLAPPEDWINDOW, 200, 100, 600, 500,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
是如何一一对应的呢?
比如(-3,-3)点,好像大概对应窗口中(0,450),这两者是怎么映射的?
初学DirectX,基础也非常差,求高人指点迷津!
//=============================================================================
// Desc: 鼠标点击矩形,让矩形移动
//=============================================================================
#include <d3dx9.h>
//-----------------------------------------
// Desc: 全局变量
//-----------------------------------------
LPDIRECT3D9 g_pD3D = NULL; //Direct3D对象
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; //Direct3D设备对象
LPDIRECT3DTEXTURE9 g_pTexture = NULL; //纹理对象
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; //顶点缓冲区对象
FLOAT lrMoveDistance =0; // 左右移动的距离
//-----------------------------------------
// Desc: 顶点结构
//-----------------------------------------
struct CUSTOMVERTEX
{
FLOAT x, y, z; //顶点位置
FLOAT u,v ; //顶点纹理坐标
};
CUSTOMVERTEX g_Vertices[]={
{ -3, -3, 0, 0.0f, 1.0f},
{ -3, -0.2, 0, 0.0f, 0.5f},
{ 0-0.1, -3, 0, 0.5f, 1.0f},
{ 0-0.1, -0.2, 0, 0.5f, 0.5f },
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)
//-----------------------------------------
// Desc: 设置变换矩阵
//-----------------------------------------
VOID SetupMatrices()
{
//创建并设置世界矩阵
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity( &matWorld );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
//创建并设置观察矩阵
D3DXVECTOR3 vEyePt( 0.0f, 0.0f, -10 );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
//创建并设置投影矩阵
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------
// Desc: 初始化Direct3D
//-----------------------------------------
HRESULT InitD3D( HWND hWnd )
{
//创建Direct3D对象, 该对象用于创建Direct3D设备对象
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
//设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
//创建Direct3D设备对象
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
//禁用照明效果
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
//设置变换矩阵
SetupMatrices();
return S_OK;
}
//-----------------------------------------
// Desc: 创建场景图形
//-----------------------------------------
HRESULT InitGriphics()
{
//创建纹理对象
/*if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, L"texture.jpg", &g_pTexture ) ) )
{
MessageBox(NULL, L"创建纹理失败", L"Texture.exe", MB_OK);
return E_FAIL;
}*/
//创建顶点缓冲区
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &g_pVB,NULL ) ) )
{
return E_FAIL;
}
//填充顶点缓冲区
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------
// Desc: 释放创建的对象
//-----------------------------------------
VOID Cleanup()
{
//释放纹理对象
if( g_pTexture != NULL )
g_pTexture->Release();
//释放顶点缓冲区对象
if( g_pVB != NULL )
g_pVB->Release();
//释放Direct3D设备对象
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
//释放Direct3D对象
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------
// Desc: 更新顶点缓冲区
//-----------------------------------------
HRESULT RenewVB(FLOAT xx,FLOAT yy)
{
if (xx==NULL)xx=0;
if (yy==NULL)yy=0;
CUSTOMVERTEX p[4];
for (int j=0;j<4;j++)
{
p[j].x=g_Vertices[j].x+xx;
p[j].y=g_Vertices[j].y+yy;
p[j].z=g_Vertices[j].z;
p[j].u=g_Vertices[j].u;
p[j].v=g_Vertices[j].v;
}
//填充顶点缓冲区
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(p), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, p, sizeof(p));
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------
// Desc: 渲染图形
//-----------------------------------------
VOID Render()
{
//清空后台缓冲区
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(40,50, 170), 1.0f, 0 );
//开始在后台缓冲区绘制图形
RenewVB(lrMoveDistance,0);
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
g_pd3dDevice->SetTexture( 0, g_pTexture ); //设置纹理
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
//结束在后台缓冲区绘制图形
g_pd3dDevice->EndScene();
}
//将在后台缓冲区绘制的图形提交到前台缓冲区显示
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------
// Desc: 消息处理
//-----------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_RIGHT:
lrMoveDistance+=0.1f;
break;
case VK_LEFT:
lrMoveDistance-=0.1f;
break;
}
return 0;
case WM_LBUTTONDOWN: // 鼠标左键点矩形,矩形向右移动,如果点中矩形之外,则不移动
// 现在的情况是不管鼠标点哪里,矩形都在移动,如何解决呢?
lrMoveDistance+=0.1f;
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------
// Desc: 入口函数
//-----------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
//注册窗口类
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"ClassName", NULL };
RegisterClassEx( &wc );
//创建窗口
HWND hWnd = CreateWindow( L"ClassName", L"纹理影射基础",
WS_OVERLAPPEDWINDOW, 200, 100, 600, 500,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
//初始化Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
//创建场景图形
if( SUCCEEDED( InitGriphics() ) )
{
//显示窗口
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
//进入消息循环
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render(); //渲染图形
}
}
}
}
UnregisterClass( L"ClassName", wc.hInstance );
return 0;
}
[解决办法]
我这两天刚把这个弄明白,视角高度变换
[解决办法]
LZ貌似没有判断鼠标点击时的坐标是否在矩形内啊
[解决办法]
LS正解
http://blog.csdn.net/qq45000/article/details/8117348
看下这篇文章怎么去拾取。