sdk的框架借用一下·
先看一下完整的程序
Code
//-----------------------------------------------------------------------------
// File: CreateDevice.cpp
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
// we are doing is creating a Direct3D device and using it to clear the
// window.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
#define LENGTH 1.0f //立方体边长
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX
{
//坐标
float x,y,z;
//坐标颜色
DWORD color;
// 顶点的纹理坐标(目前还不需要用到)
FLOAT tu, tv;
};
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIB = NULL;
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice. Most parameters are
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
// window, and then set the SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; //生成16 bit的Z缓存
// Create the Direct3D device. Here we are using the default adapter (most
// systems only have one, unless they have multiple graphics hardware cards
// installed) and requesting the HAL (which is saying we want the hardware
// device rather than a software one). Software vertex processing is
// specified since we know it will work on all cards. On cards that support
// hardware vertex processing, though, we would see a big performance gain
// by specifying hardware vertex processing.
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off culling
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// Turn on the zbuffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
return S_OK;
}
HRESULT InitGeometry()
{
// CUSTOMVERTEX vertices[] =
// {{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0,255,0) }, //点A,绿色
// { -1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(255,0,0) }, //点B,红色
// { 1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(0,255,255) }, //点C,浅蓝
// { 0.0f, -1.0f, 1.155f, D3DCOLOR_XRGB(255,0,255) }}; //点D,粉红
//
// WORD indices[] = { 0, 2, 1, 0, 3, 2, 0, 1, 3, 1, 2, 3 };
CUSTOMVERTEX vertices[] =
{
// (0,15,18) (1,17,23) (2,4,13) (3,5,21) (8,19,22) (6,10,14) (9,12,16) (7,11,20)
// 前面
// { LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 0
//
// { -LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 1
//
// { LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 2
//
// { -LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 3
//
//
// // 顶部的面
//
// { LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 1.0 }, // 4
//
// { -LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 1.0 }, // 5
//
// { LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 6
//
// { -LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 7
//
//
//
//
// // 背部的面
//
// { -LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 8
//
// { LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 9
//
// { LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 10
//
// { -LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 11
//
//
// // 左边的面
//
// { LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 12
//
// { LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 13
//
// { LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 14
//
// { LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 15
//
//
// // 底部的面
//
// { LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 16
//
// { -LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 0.0 }, // 17
//
// { LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 0.0 }, // 18
//
// { -LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 19
//
//
// // 右边的面
//
// { -LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 20
//
// { -LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 21
//
// { -LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 22
//
// { -LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 23
//前面
{0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,0,0)},//点A 0
{0.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},//点B 1
{1.0f,1.0f,0.0f,D3DCOLOR_XRGB(255,255,0)},//点C 2
{1.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},//点D 3
//后面
{0.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,0)},//点A' 4
{0.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,0)},//点B' 5
{1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,255,0)},//点C'6
{1.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)},//点D' 7
//
// //左面
// {0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,0,0)},//点A 8
// {0.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,0)},//点A' 9
// {0.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,0)},//点B' 10
// {0.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},//点B 11
//
// //右面
// {1.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},//点D 12
// {1.0f,1.0f,0.0f,D3DCOLOR_XRGB(255,255,0)},//点C 13
// {1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,255,0)},//点C'14
// {1.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)},//点D' 15
//
// //上面
// {0.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},//点B 16
// {0.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,0)},//点B' 17
// {1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,255,0)},//点C'18
// {1.0f,1.0f,0.0f,D3DCOLOR_XRGB(255,255,0)},//点C 19
//
// //下面
// {0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,0,0)},//点A 20
// {1.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},//点D 21
// {1.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)},//点D' 22
// {0.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,0)}//点A' 23
};
// WORD indices[] = { 0, 1, 2, 3, 2, 1,
//
// 4, 5, 6, 5, 7, 6,
//
// 8, 9, 10, 8, 10, 11,
//
// 12, 13, 14, 12, 15, 13,
//
// 16, 17, 18, 16, 19, 17,
//
// 20, 21, 22, 21, 23, 22 };
// WORD indices[] = {
// //前
// 0, 1, 2, 3, 0, 2,
// //后
// 4,5,6,7,4,6,
// //左
// 8,9,10,11,8,10,
// //右
// 12,13,14,15,12,14,
// //上
// 16,17,18,19,16,18,
// //下
// 20,21,22,23,20,22
// };
WORD indices[] = {
//前
0, 1, 2, 3, 0, 2,
//后
4,5,6,7,4,6,
//左
0,4,5,1,0,5,
//右
7,3,2,6,7,2,
//上
2,1,5,6,2,5,
//下
7,4,0,3,7,0
};
//创建顶点缓存 开始
g_pd3dDevice->CreateVertexBuffer(
sizeof(vertices), //缓存区尺寸
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL );
void* pVertices;
g_pVB->Lock(0,sizeof(vertices),(void**)&pVertices,0);
memcpy(pVertices,vertices,sizeof(vertices));
g_pVB->Unlock();
//创建顶点缓存 结束
//创建索引缓存 开始
g_pd3dDevice->CreateIndexBuffer(
sizeof(indices),0,
D3DFMT_INDEX16,D3DPOOL_DEFAULT,
&g_pIB,NULL
);
void* pIndices;
g_pIB->Lock(0,sizeof(indices),(void**)&pIndices,0);
memcpy(pIndices,indices,sizeof(indices));
g_pVB->Unlock();
//创建索引缓存 结束
return S_OK;
}
VOID SetupMatrices()
{
// Set up world matrix
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity( &matWorld );
D3DXMatrixRotationX( &matWorld, timeGetTime() / 1000.0f );
//D3DXMatrixRotationZ( &matWorld, timeGetTime() / 10000.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
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 );
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if (g_pIB != NULL)
g_pIB->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dDevice )
return;
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
SetupMatrices();
// Rendering of scene objects can happen here
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetIndices(g_pIB);
g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
// case WM_PAINT:
// Render();
// ValidateRect( hWnd, NULL );
// return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL
};
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 350, 350,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
if (SUCCEEDED(InitGeometry()))
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
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"D3D Tutorial", wc.hInstance );
return 0;
}
//-----------------------------------------------------------------------------
// File: CreateDevice.cpp
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
// we are doing is creating a Direct3D device and using it to clear the
// window.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
#define LENGTH 1.0f //立方体边长
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX
{
//坐标
float x,y,z;
//坐标颜色
DWORD color;
// 顶点的纹理坐标(目前还不需要用到)
FLOAT tu, tv;
};
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIB = NULL;
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice. Most parameters are
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
// window, and then set the SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; //生成16 bit的Z缓存
// Create the Direct3D device. Here we are using the default adapter (most
// systems only have one, unless they have multiple graphics hardware cards
// installed) and requesting the HAL (which is saying we want the hardware
// device rather than a software one). Software vertex processing is
// specified since we know it will work on all cards. On cards that support
// hardware vertex processing, though, we would see a big performance gain
// by specifying hardware vertex processing.
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off culling
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// Turn on the zbuffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
return S_OK;
}
HRESULT InitGeometry()
{
// CUSTOMVERTEX vertices[] =
// {{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0,255,0) }, //点A,绿色
// { -1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(255,0,0) }, //点B,红色
// { 1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(0,255,255) }, //点C,浅蓝
// { 0.0f, -1.0f, 1.155f, D3DCOLOR_XRGB(255,0,255) }}; //点D,粉红
//
// WORD indices[] = { 0, 2, 1, 0, 3, 2, 0, 1, 3, 1, 2, 3 };
CUSTOMVERTEX vertices[] =
{
// (0,15,18) (1,17,23) (2,4,13) (3,5,21) (8,19,22) (6,10,14) (9,12,16) (7,11,20)
// 前面
// { LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 0
//
// { -LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 1
//
// { LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 2
//
// { -LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 3
//
//
// // 顶部的面
//
// { LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 1.0 }, // 4
//
// { -LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 1.0 }, // 5
//
// { LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 6
//
// { -LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 7
//
//
//
//
// // 背部的面
//
// { -LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 8
//
// { LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 9
//
// { LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 10
//
// { -LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 11
//
//
// // 左边的面
//
// { LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 12
//
// { LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 13
//
// { LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 14
//
// { LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 15
//
//
// // 底部的面
//
// { LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 16
//
// { -LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 0.0 }, // 17
//
// { LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 0.0 }, // 18
//
// { -LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 19
//
//
// // 右边的面
//
// { -LENGTH, LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 0.0, 0.0 }, // 20
//
// { -LENGTH, LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0, 0.0 }, // 21
//
// { -LENGTH, -LENGTH/2, LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 0.0, 1.0 }, // 22
//
// { -LENGTH, -LENGTH/2, -LENGTH, D3DCOLOR_ARGB(255, 127, 127, 127), 1.0, 1.0 }, // 23
//前面
{0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,0,0)},//点A 0
{0.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},//点B 1
{1.0f,1.0f,0.0f,D3DCOLOR_XRGB(255,255,0)},//点C 2
{1.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},//点D 3
//后面
{0.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,0)},//点A' 4
{0.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,0)},//点B' 5
{1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,255,0)},//点C'6
{1.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)},//点D' 7
//
// //左面
// {0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,0,0)},//点A 8
// {0.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,0)},//点A' 9
// {0.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,0)},//点B' 10
// {0.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},//点B 11
//
// //右面
// {1.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},//点D 12
// {1.0f,1.0f,0.0f,D3DCOLOR_XRGB(255,255,0)},//点C 13
// {1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,255,0)},//点C'14
// {1.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)},//点D' 15
//
// //上面
// {0.0f,1.0f,0.0f,D3DCOLOR_XRGB(0,255,0)},//点B 16
// {0.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,0)},//点B' 17
// {1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,255,0)},//点C'18
// {1.0f,1.0f,0.0f,D3DCOLOR_XRGB(255,255,0)},//点C 19
//
// //下面
// {0.0f,0.0f,0.0f,D3DCOLOR_XRGB(0,0,0)},//点A 20
// {1.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,0,0)},//点D 21
// {1.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)},//点D' 22
// {0.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,0)}//点A' 23
};
// WORD indices[] = { 0, 1, 2, 3, 2, 1,
//
// 4, 5, 6, 5, 7, 6,
//
// 8, 9, 10, 8, 10, 11,
//
// 12, 13, 14, 12, 15, 13,
//
// 16, 17, 18, 16, 19, 17,
//
// 20, 21, 22, 21, 23, 22 };
// WORD indices[] = {
// //前
// 0, 1, 2, 3, 0, 2,
// //后
// 4,5,6,7,4,6,
// //左
// 8,9,10,11,8,10,
// //右
// 12,13,14,15,12,14,
// //上
// 16,17,18,19,16,18,
// //下
// 20,21,22,23,20,22
// };
WORD indices[] = {
//前
0, 1, 2, 3, 0, 2,
//后
4,5,6,7,4,6,
//左
0,4,5,1,0,5,
//右
7,3,2,6,7,2,
//上
2,1,5,6,2,5,
//下
7,4,0,3,7,0
};
//创建顶点缓存 开始
g_pd3dDevice->CreateVertexBuffer(
sizeof(vertices), //缓存区尺寸
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL );
void* pVertices;
g_pVB->Lock(0,sizeof(vertices),(void**)&pVertices,0);
memcpy(pVertices,vertices,sizeof(vertices));
g_pVB->Unlock();
//创建顶点缓存 结束
//创建索引缓存 开始
g_pd3dDevice->CreateIndexBuffer(
sizeof(indices),0,
D3DFMT_INDEX16,D3DPOOL_DEFAULT,
&g_pIB,NULL
);
void* pIndices;
g_pIB->Lock(0,sizeof(indices),(void**)&pIndices,0);
memcpy(pIndices,indices,sizeof(indices));
g_pVB->Unlock();
//创建索引缓存 结束
return S_OK;
}
VOID SetupMatrices()
{
// Set up world matrix
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity( &matWorld );
D3DXMatrixRotationX( &matWorld, timeGetTime() / 1000.0f );
//D3DXMatrixRotationZ( &matWorld, timeGetTime() / 10000.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
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 );
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if (g_pIB != NULL)
g_pIB->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dDevice )
return;
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
SetupMatrices();
// Rendering of scene objects can happen here
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetIndices(g_pIB);
g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
// case WM_PAINT:
// Render();
// ValidateRect( hWnd, NULL );
// return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL
};
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 350, 350,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
if (SUCCEEDED(InitGeometry()))
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
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"D3D Tutorial", wc.hInstance );
return 0;
}
立方体共有8个顶点,其中一个点被三个面共用,开始写的代码很麻烦,一共24个顶点,索引也排列到23·
如果按共用顶点计算就很简单了,要注意是按顺时针排列的,要把索引的顺寻排好,要不图形会不完整的
大部分转载 小部分自写