Có ai giúp mình cái vấn đề này với :
Mình vẽ một mình hộp 3d sau đó cho nó quay theo tọa độ x,y,z bây giờ có một yêu cầu đặt gia như thế này :
Mình muốn khi nhấn một phím nào mình chỉ định, thì cái hình hộp của mình nó sẽ dừng lại đúng vị chí lúc mình nhấn vào mình bỏ khỏi nút nhấn thì nó tiếp tục quay với vị trí lúc nhấn, mình bị vướng chỗ khi mình nhấn thì nó dừng quay nhưng vị trí của nó thì không dừng đúng vị trí nút nhấn, hai là khi bỏ tay ra khỏi nút nhấn thì nó quay nhưng vị trị của nó cũng không quáy tiếp tục chỗ mình nhấn
Các bạn xem code rồi cho mình ý kiến
còn đây là cả bài đầy đủ :http://www.mediafire.com/?yog1zwhmmeu
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
LRESULT CALLBACK WinProc (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
HWND wndHandle;
//khai bao bien con tro
LPDIRECT3D9 pD3D;//->doi tuong 3D
LPDIRECT3DDEVICE9 pd3dDevice; //->thiet bi ket xuat
LPDIRECT3DVERTEXBUFFER9 buffervertex=NULL;//->vung dem
LPDIRECT3DTEXTURE9 pTexture;
//khai bao mau
bool initWindow(HINSTANCE);
bool initDirect3D();
bool initvertex();
void game_run(HWND);
void game_end(HWND);
void SetCamera(void);
void SetPerspective(void);
void SetRotation(void);
void initLight(void);
#define D3DFVF_MYVERTEX D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1
#define SafeRelease(pObject)if(pObject!=NULL){pObject->Release();pObject=NULL}
#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code)& 0x8000)?1:0)
#define KEY_UP(vk_code)((GetAsynckeyState(vk_code)& 0x8000)?1:0)
D3DXMATRIX matWorld1,matWorld1X,matWorld1Y,matWorld1Z,
matWorld2,matWorld2X,matWorld2Y,matWorld2Z,matWorl dTranslate2,
matWorld3,matWorld3X,matWorld3Y,matWorld3Z,matWorl dTranslate3;
//khai bao cau truc
struct customvertex
{
float x,y,z;
D3DVECTOR normal;
float u,v;
};
//mang kieu cau truc gom cac dinh cua khoi hinh hop se show ra man hinh
customvertex myvertex[]={
//top face
{-5.0f,5.0f,-5.0f,0,1,0,0.0f,1.0f},
{-5.0f,5.0f,5.0f,0,1,0,0.0f,0.0f},
{5.0f,5.0f,-5.0f,0,1,0,1.0f,1.0f},
{5.0f,5.0f,5.0f,0,1,0,1.0f,0.0f},
//face 1
{-5.0f,-5.0f,-5.0f,1,1,1,0.0f,1.0f},
{-5.0f,5.0f,-5.0f,1,1,1,0.0f,0.0f},
{5.0f,-5.0f,-5.0f,1,1,1,1.0f,1.0f},
{5.0f,5.0f,-5.0f,1,1,1,1.0f,0.0f},
//face 2
{5.0f,-5.0f,5.0f,1,0,0,0.0f,1.0f},
{5.0f,5.0f,5.0f,1,0,0,0.0f,0.0f},
//face 3
{-5.0f,-5.0f,5.0f,0,0,1,1.0f,1.0f},
{-5.0f,5.0f,5.0f,0,0,1,1.0f,0.0f},
//face 4
{-5.0f,-5.0f,-5.0f,-1,0,0,0.0f,1.0f},
{-5.0f,5.0f,-5.0f,-1,0,0,0.0f,0.0f},
//face 5
{5.0f,-5.0f,-5.0f,0,-1,0,0.0f,1.0f},
{5.0f,-5.0f,5.0f,0,-1,0,0.0f,0.0f},
{-5.0f,-5.0f,-5.0f,0,-1,0,1.0f,1.0f},
{-5.0f,-5.0f,5.0f,0,-1,0,1.0f,0.0f},
};
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
if (!initWindow(hInstance))
return false;
if (!initDirect3D())
return false;
if(!initvertex())
return false;
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message !=WM_QUIT)
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
{
game_run(wndHandle);
}
return (int) msg.wParam;//ep kiu
}
bool initWindow(HINSTANCE hInstance)
{
WNDCLAS*** wcex;
wcex.cbSize = sizeof(WNDCLAS***);
wcex.style = CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc = WinProc;
wcex.cbWndExtra = 0;
wcex.cbCl***tra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"About Window";
wcex.hIconSm = 0;
RegisterClas*** (&wcex);
wndHandle = CreateWindow(L"ABout Window",
L"The Hello Program",
WS_OVERLAPPEDWINDOW,//->che do cua so
//WS_EX_TOPMOST|WS_POPUP|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL );
if (!wndHandle)
return false;
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
bool initDirect3D(void)
{
pD3D = NULL;
pd3dDevice = NULL;
if(NULL==(pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
{
return false;
}
D3DPRESENT_PARAMETERS d3dpp;
//xoa bo nho con tro
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed =true;//->che do cua so
//d3dpp.Windowed =false;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//->che do cua so
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight =600;
d3dpp.BackBufferWidth =800;
d3dpp.hDeviceWindow = wndHandle;
//cho fep tinh do xau
d3dpp.EnableAutoDepthStencil=true;
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
if (FAILED (pD3D ->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice)))
{
return false;
}
D3DXCreateTextureFromFile(pd3dDevice,L"13.jpg",&pTexture);
//tat anh sang
pd3dDevice->SetRenderState(D3DRS_LIGHTING,true);
pd3dDevice->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(50,50,5 0));
/*cho fep tinh do xau
pd3dDevice->SetRenderState(D3DRS_ZENABLE,true);*/
return true;
}
//ham ve hinh len vung dem
void game_run(HWND wndHandle)
{
pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
pd3dDevice->Clear(0,NULL,D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);//cho fep tinh do xau
pd3dDevice->SetTexture(0,pTexture);
pd3dDevice->BeginScene();
//goi ham
SetRotation();
SetCamera();
SetPerspective();
initLight();
//chỉ định khi nhấn phim a thì hình sẽ dừng lại, khi bỏ ra thì tiếp tục quay
//vấn đề là chỗ này mong chỉ dúp
if(KEY_DOWN(0x41))
{
D3DXMatrixTranslation(&matWorld1,timeGetTime()/800.0f,timeGetTime()/800.0f,timeGetTime()/800.0f);
}
else
{
}
pd3dDevice->SetStreamSource(0,buffervertex,0,sizeof(customvert ex));
pd3dDevice->SetFVF(D3DFVF_MYVERTEX);
pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld1);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);//diem,dt,tamgic,hinh vuong<->point,line,triangle,quad
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,4,8);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,14,2);
pd3dDevice->EndScene();
//xuat file anh ra man hinh
pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
LRESULT CALLBACK WinProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
//ham khai bao & khoi tao vertexbuffer
bool initvertex (void)
{
HRESULT result;
result=pd3dDevice->CreateVertexBuffer(sizeof(customvertex)*sizeof(myv ertex),
0,
D3DFVF_MYVERTEX,//FVF<->Flexible Vertex Format->do co gian
D3DPOOL_DEFAULT,
&buffervertex,//lay dia chi con tro
NULL);
//test
if FAILED(result)
return false;
VOID* pVertices;//tro toi kiu du lieu chua bit truoc
//khoa buffer
result=buffervertex->Lock(0,sizeof(myvertex),(void**)&pVertices,0);
//test
if FAILED(result)
return false;
//memcpy
memcpy(pVertices,myvertex,sizeof (myvertex));
//mo khoa buffer
buffervertex ->Unlock();
//return S_OK;
return true;
}
//ham xoay
void SetRotation()
{
D3DXMatrixRotationX(&matWorld1X, timeGetTime()/800.0f);// toa do x,goc quay
D3DXMatrixRotationY(&matWorld1Y,timeGetTime()/800.0f);// toa do y...
D3DXMatrixRotationZ(&matWorld1Z,timeGetTime()/800.0f);//toa do z
//nhan ma tran xoay
D3DXMatrixMultiply(&matWorld1,&matWorld1X,&matWorld1Y);
D3DXMatrixMultiply(&matWorld1,&matWorld1,&matWorld1Z);
pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld1);
}
//ham view
void SetCamera()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView,&D3DXVECTOR3(0.0f,0.0f,-60.0f),//koag cach giua anh va camera(tam nhin)
&D3DXVECTOR3(0.0f,0.0f,0.0f),//nhin goc toa do
&D3DXVECTOR3(0.0f,1.0f,0.0f));//huong nhin len theo truc y
pd3dDevice->SetTransform(D3DTS_VIEW,&matView);
}
//ham chieu
void SetPerspective()
{
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,500.0f);
pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);
}
void initLight()
{
D3DLIGHT9 light;//create the light struct
D3DMATERIAL9 material;//create the material struct
ZeroMemory (&light,sizeof(light));//clear out the struc for use
light.Type=D3DLIGHT_DIRECTIONAL;//make the light type 'direttional light'
light.Diffuse.r =0.5f;//.5 red
light.Diffuse.g=0.5f;//.5 green
light.Diffuse.b=0.5f;//.5 blue
light.Diffuse.a= 1.0f ;//full alpha (we'll get too that soon)
D3DVECTOR vecDirection ={0.0f,-3.0f,-1.0f};//the direction of the light
light.Direction=vecDirection;//set the direction
pd3dDevice->SetLight(0,&light);//send the light struct properties to light #0
pd3dDevice->LightEnable(0,TRUE);//turn on light #0
ZeroMemory(&material,sizeof(D3DMATERIAL9));//clear out the struc for use
material.Diffuse.r=material.Ambient.r = 1.0f;//set the material to full red
material.Diffuse.g=material.Ambient.g = 1.0f;//set the material to full green
material.Diffuse.b=material.Ambient.b = 1.0f;//set the material to full blue
material.Diffuse.a=material.Ambient.a = 1.0f;//set the material to full alpha
pd3dDevice ->SetMaterial(&material);//set the globably-use
}
[=========> Bổ sung bài viết <=========]
sao không có ai cho mình ý kiến vậy, cho mình hỏi tiếp câu nữa, trong bài trên có sử dụng hàm :
timeGetTime():ý nghĩa của nó để lấy giờ hệ thông, vậy có hàm nào đặt được giờ hệ thống không bạn
Powered by vBulletin® Version 4.2.0 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.