|
求大神帮我看看是哪里出了问题,编译没出错~运行就是不出三角形~跪求大神们了
- /////MyDirect3d.h
- #pragma once
- //
- // Our custom FVF, which describes our custom vertex structure.
- #define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
- class CMyDirect3d
- {
- public:
- //
- private:
- //
- HRESULT hr;
- HWND d3dhwnd;
- bool d3dfullscreen;
- //
- D3DDISPLAYMODE displayMode;
- LPDIRECT3D9 d3d ;
- LPDIRECT3DDEVICE9 d3dDevice;
- D3DPRESENT_PARAMETERS d3dpp;
- //
- LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
- D3DXMATRIX World;
- struct stD3DVertex
- {
- float x, y, z,rhw;
- DWORD color;
- };
- //
- public:
- CMyDirect3d(void);
- ~CMyDirect3d(void);
- CMyDirect3d(HWND hwnd, bool fullscreen);
- //
- bool Initialize();
- bool RenderScreen();
- bool InitializeObjects();
- };
复制代码- ////MyDirect3d.cpp
- #include "stdafx.h"
- #include "MyDirect3d.h"
- CMyDirect3d::CMyDirect3d(void)
- {
- }
- CMyDirect3d::CMyDirect3d(HWND hwnd, bool fullscreen)
- {
- d3dhwnd = hwnd;
- d3dfullscreen = fullscreen;
- }
- CMyDirect3d::~CMyDirect3d(void)
- {
- if(d3dDevice != NULL) d3dDevice->Release();
- if(d3d != NULL) d3d->Release();
- if(vertexBuffer != NULL) vertexBuffer->Release();
- //
- d3dDevice = NULL;
- d3d = NULL;
- vertexBuffer = NULL;
- //MessageBox(NULL, TEXT("hfaf"), TEXT("he"), 0);
- }
- //////////////////////////////////////////////////////////////////////////
- bool CMyDirect3d::Initialize()
- {
- // Create the D3D object.
- d3d = Direct3DCreate9(D3D_SDK_VERSION);
- if (d3d == NULL)
- {
- return false;
- }
- // Get the desktop display mode.
- if (FAILED(d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
- {
- return false;
- }
- // Set up the structure used to create the D3Device
- ZeroMemory(&d3dpp, sizeof(d3dpp));
- if (d3dfullscreen)
- {
- d3dpp.Windowed = FALSE;
- d3dpp.BackBufferWidth = 640;
- d3dpp.BackBufferHeight = 480;
- }
- else
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = displayMode.Format;
- hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dhwnd,
- D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3dDevice);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- //
- if (!InitializeObjects())
- {
- return false;
- }
- return true;
- }//end Initialize
- bool CMyDirect3d::InitializeObjects()
- {
- //ColorVertex t;
- // Create the vertex buffer.
- hr = d3dDevice->CreateVertexBuffer(sizeof(stD3DVertex), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT,
- &vertexBuffer, NULL);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- //
- stD3DVertex triangle[]=
- {
- 420.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0),
- 420.0f,350.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0),
- 300.0f,350.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,0,255),
- };
- // Fill the vertex buffer
- void* p;
- hr = vertexBuffer->Lock(0, sizeof(stD3DVertex), (void**)&p, 0);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- memcpy(p, triangle, sizeof(stD3DVertex));
- vertexBuffer->Unlock();
- //
- //D3DXMATRIX proj;
- //D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, 1.3333f, 1.0f, 1000.0f);
- //d3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
- //d3dDevice->SetRenderState(D3DRS_LIGHTING, false);
- //
- return true;
- }
- //
- bool CMyDirect3d::RenderScreen()
- {
- // Clear the back-buffer
- hr = d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET,
- D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- // Begin the screen start rendering
- d3dDevice->BeginScene();
- //Render objDate
- hr = d3dDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(stD3DVertex));
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- hr = d3dDevice->SetFVF(D3DFVF_VERTEX);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- //D3DXMatrixTranslation(&World, -1.25f, 0.0f, 0.0f);
- //hr = d3dDevice->SetTransform(D3DTS_WORLD, &World);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- //hr = d3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- hr = d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
- if (FAILED(hr))
- {
- DXTRACE_ERR_MSGBOX(DXGetErrorStringW(hr),hr);
- return false;
- }
- // End the screen stop rendering
- d3dDevice->EndScene();
- // Display the screen.
- d3dDevice->Present(NULL, NULL, NULL, NULL);
- return true;
- }
复制代码- // DrawTriangle.cpp : 定义应用程序的入口点。
- //
- #include "stdafx.h"
- #include "DrawTriangle.h"
- #include "MyDirect3d.h"
- #define MAX_LOADSTRING 100
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
- CMyDirect3d* pmyd3d;
- // 此代码模块中包含的函数的前向声明:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPTSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
- ZeroMemory(&msg, sizeof(msg));
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_DRAWTRIANGLE, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DRAWTRIANGLE));
- // 主消息循环:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- pmyd3d->RenderScreen();
- }
- }
- delete pmyd3d;
- return (int) msg.wParam;
- }
- //
- // 函数: MyRegisterClass()
- //
- // 目的: 注册窗口类。
- //
- 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_DRAWTRIANGLE));
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = NULL;
- wcex.lpszMenuName = MAKEINTRESOURCE(IDC_DRAWTRIANGLE);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassEx(&wcex);
- }
- //
- // 函数: InitInstance(HINSTANCE, int)
- //
- // 目的: 保存实例句柄并创建主窗口
- //
- // 注释:
- //
- // 在此函数中,我们在全局变量中保存实例句柄并
- // 创建和显示主程序窗口。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // 将实例句柄存储在全局变量中
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, GetDesktopWindow(), NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- pmyd3d = new CMyDirect3d(hWnd, false);
- if (!pmyd3d->Initialize())
- {
- MessageBox(NULL, TEXT("ha"), TEXT("CMyDirect3d::Initialize is false"), 0);
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // 目的: 处理主窗口的消息。
- //
- // WM_COMMAND - 处理应用程序菜单
- // WM_PAINT - 绘制主窗口
- // WM_DESTROY - 发送退出消息并返回
- //
- //
- 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);
- // 分析菜单选择:
- 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_DESTROY:
- PostQuitMessage(0);
- return 0;
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // “关于”框的消息处理程序。
- 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;
- }
复制代码
|
|