
Explorer
   
Профиль
Группа: Завсегдатай
Сообщений: 2735
Регистрация: 1.4.2006
Где: Н.Новгород
Репутация: нет Всего: 56
|
Всем привет, есть следующий элементарный исходник. | Код | // Direct3D.cpp : Defines the entry point for the application. //
#include "stdafx.h" #include "Direct3D.h" #include <atlbase.h> #include <d3d9.h> #include <d3dx9math.h> #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX //формат вершин { FLOAT x, y, z, rhw; DWORD color; };
#define MAX_LOADSTRING 100
// Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name HWND hWnd; IDirect3D9* m_pD3D; IDirect3DDevice9* m_pd3dDevice; IDirect3DVertexBuffer9* m_pVertexBuffer;
D3DXMATRIX matView; // the view matrix D3DXMATRIX matProj; // the projection matrix D3DXVECTOR3 cameraPosition; // the position of the camera D3DXVECTOR3 cameraLook; // where the camera is pointing
// Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); bool InitDirect3D(); bool createCube(void);
void createCamera(float nearClip, float farClip); void moveCamera(D3DXVECTOR3 vec); void pointCamera(D3DXVECTOR3 vec); void Init(); bool Rendering();
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here. MSG msg; HACCEL hAccelTable;
// Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_DIRECT3D, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance);
// Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; }
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DIRECT3D)); Init();
// Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
return (int) msg.wParam; }
// // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DIRECT3D)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_DIRECT3D); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex); }
// // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) { return FALSE; }
ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
InitDirect3D(); Init();
return TRUE; }
// // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc;
switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... Rendering(); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
// Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE;
case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }
bool InitDirect3D() { m_pD3D = Direct3DCreate9( D3D_SDK_VERSION); if(!m_pD3D) return false; D3DPRESENT_PARAMETERS d3dpp = {0}; d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.BackBufferCount = 1; d3dpp.BackBufferHeight = 200; d3dpp.BackBufferWidth = 200; d3dpp.hDeviceWindow = hWnd; /* d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16;*/ HRESULT hrResult = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pd3dDevice ); if(FAILED(hrResult)) return false; m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; } bool Rendering() { // Clear the backbuffer to a black color m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
m_pd3dDevice->BeginScene();
m_pd3dDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(CUSTOMVERTEX) ); m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4, 2 ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 8, 2 ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );
m_pd3dDevice->EndScene();
// Present the backbuffer contents to the display m_pd3dDevice->Present( NULL, NULL, NULL, NULL ); return true; }
bool createCube(void) { // Initialize three vertices for rendering a triangle CUSTOMVERTEX g_Vertices[] = { // 1 { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 2 { -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 3 { -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 4 { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 5 { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 6 {-64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, {-64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)}, {-64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,255)}, {-64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)}, };
// Create the vertex buffer. HRESULT hr;
hr = m_pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL ); if FAILED (hr) return false;
// prepare to copy the vertices into the vertex buffer VOID* pVertices; // lock the vertex buffer hr = m_pVertexBuffer->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0);
// check to make sure the vertex buffer can be locked if FAILED (hr) return false;
// copy the vertices into the buffer memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
// unlock the vertex buffer m_pVertexBuffer->Unlock();
return true; }
void createCamera(float nearClip, float farClip) { //Here we specify the field of view, aspect ration and near and far clipping planes. D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 800/600, nearClip, farClip); m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
/************************************************************************* * moveCamera * moves the camera to a position specified by the vector passed as a * parameter *************************************************************************/ void moveCamera(D3DXVECTOR3 vec) { cameraPosition = vec; }
/************************************************************************* * pointCamera * points the camera a location specified by the passed vector *************************************************************************/ void pointCamera(D3DXVECTOR3 vec) { cameraLook = vec;
D3DXMatrixLookAtLH(&matView, &cameraPosition, //Camera Position &cameraLook, //Look At Position &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView); }
void Init() { createCube();
createCamera(1.0f, 600.0f); // near clip plane, far clip plane moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f)); pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f)); D3DXMATRIX meshMat, meshScale, meshRotate; // set the rotation D3DXMatrixRotationY(&meshRotate, D3DXToRadian(45)); // set the scaling D3DXMatrixScaling(&meshScale, 1.0f, 1.0f, 1.0f); // multiple the scaling and rotation matrices to create the meshMat matrix D3DXMatrixMultiply(&meshMat, &meshScale, &meshRotate);
// transform the object in world space m_pd3dDevice->SetTransform(D3DTS_WORLD, &meshMat);
}
|
который в результате рисует:  Я так понимаю не срабатывает ни одна ф-ия по работе с установкой матриц. Подскажите в чем может быть проблема, весь мозг уже сломал Добавлено через 3 минуты и 10 секунда вот как должно рисовать
|