3,881
社区成员
发帖
与我相关
我的任务
分享
#include "d3dUtility.h"
#define WIDTH 800 //屏幕宽
#define HEIGHT 600 //屏幕高
IDirect3DDevice9*device = 0;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DVertexBuffer9 *VB; //定义顶点缓存
IDirect3DIndexBuffer9 *IB; //定义索引缓存
struct Vertex
{
float _x, _y, _z;
Vertex(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
bool D3D::Setup()
{
device->CreateVertexBuffer(8 * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &VB, 0);
device->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &IB, 0);
Vertex *vertices;
VB->Lock(0, 0, (void **)&vertices, 0);
vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
vertices[2] = Vertex(1.0f, 1.0f, -1.0f);
vertices[3] = Vertex(1.0f, -1.0f, -1.0f);
vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
vertices[6] = Vertex(1.0f, 1.0f, 1.0f);
vertices[7] = Vertex(1.0f, -1.0f, 1.0f);
VB->Unlock();
WORD *indices;
IB->Lock(0, 0, (void **)&indices, 0);
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 0; indices[4] = 2; indices[5] = 3;
// 背面
indices[6] = 4; indices[7] = 6; indices[8] = 5;
indices[9] = 4; indices[10] = 7; indices[11] = 6;
// 左面
indices[12] = 4; indices[13] = 5; indices[14] = 1;
indices[15] = 4; indices[16] = 1; indices[17] = 0;
// 右面
indices[18] = 3; indices[19] = 2; indices[20] = 6;
indices[21] = 3; indices[22] = 6; indices[23] = 7;
// 顶部
indices[24] = 1; indices[25] = 5; indices[26] = 6;
indices[27] = 1; indices[28] = 6; indices[29] = 2;
// 底部
indices[30] = 4; indices[31] = 0; indices[32] = 3;
indices[33] = 4; indices[34] = 3; indices[35] = 7;
IB->Unlock();
D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
device->SetTransform(D3DTS_VIEW, &V);
// 投影矩阵
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)WIDTH / (float)HEIGHT,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION, &proj);
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
device->BeginScene();// 开始绘制场景
device->SetStreamSource(0, VB, 0, sizeof(Vertex));// 设置资源流
device->SetIndices(IB); // 设置索引缓存
device->SetFVF(Vertex::FVF); // 设置顶点格式
// 利用索引缓存绘制
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
device->EndScene();// 结束绘制场景
return true;
}
#include "d3dUtility.h"
#define WIDTH 800 //屏幕宽
#define HEIGHT 600 //屏幕高
IDirect3DDevice9*device = 0;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DVertexBuffer9 *VB; //定义顶点缓存
IDirect3DIndexBuffer9 *IB; //定义索引缓存
struct Vertex
{
float _x, _y, _z;
Vertex(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
bool D3D::Setup()
{
device->CreateVertexBuffer(
3 * sizeof(Vertex), // size in bytes
D3DUSAGE_WRITEONLY, // flags
Vertex::FVF, // vertex format
D3DPOOL_MANAGED, // managed memory pool
&VB, // return create vertex buffer
0); // not used - set to 0
Vertex *vertices;
VB->Lock(0, 0, (void **)&vertices, 0);
vertices[0] = Vertex(-1.0f, 0.0f, 2.0f);
vertices[1] = Vertex(0.0f, 1.0f, 2.0f);
vertices[2] = Vertex(1.0f, 0.0f, 2.0f);
VB->Unlock();
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)WIDTH / (float)HEIGHT,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION, &proj);
device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
device->BeginScene();
device->SetStreamSource(0, VB, 0, sizeof(Vertex));
device->SetFVF(Vertex::FVF);
// Draw one triangle.
device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
device->EndScene();
device->Present(0, 0, 0, 0);
return true;
}
bool D3D::initD3D(D3DDEVTYPE DeviceType, HWND hwnd)
{
IDirect3D9 * d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
// 填充主显示设备的能力( D3DCAPS9结构)
D3DCAPS9 caps;
d3d9->GetDeviceCaps(
D3DADAPTER_DEFAULT, // 主显示设备
DeviceType, // 设备类型,一般是D3DDEVTYPE_HAL.
&caps); // 返回填充后的D3DCAPS9 结构,包含主显示设备的能力
// 是否可以使用硬件顶点处理?
int vp = 0;
if (caps.DevCaps& D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
// 是,支持硬件顶点处理
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
// 不,只能用软件顶点处理
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //像素格式
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = true; // fullscreen
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // depth format
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
int hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT, //primary adapter
D3DDEVTYPE_HAL,//devicetype
hwnd,//window associated withdevice
D3DCREATE_HARDWARE_VERTEXPROCESSING, //vertexprocessing type
&d3dpp,//presentparameters
&device);//returned created device
if (FAILED(hr))
{
::MessageBox(0, "CreateDevice() -FAILED", 0, 0);
return 0;
}
else
{
return 1;
}
}
大哥 你看看 我把立方体改成三角形了 什么东西都没有出来。。 我窗口背景颜色是白色[/quote]
你贴出来的代码是没问题的额,你程序能出来窗口吗?还是有窗口没内容?[/quote]
大哥 谢谢你的耐心 群里面的朋友说要循环调用 我就把函数放到WM_PAINI里面调用就有东西出来了。
#include "d3dUtility.h"
#define WIDTH 800 //屏幕宽
#define HEIGHT 600 //屏幕高
IDirect3DDevice9*device = 0;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DVertexBuffer9 *VB; //定义顶点缓存
IDirect3DIndexBuffer9 *IB; //定义索引缓存
struct Vertex
{
float _x, _y, _z;
Vertex(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
bool D3D::Setup()
{
device->CreateVertexBuffer(
3 * sizeof(Vertex), // size in bytes
D3DUSAGE_WRITEONLY, // flags
Vertex::FVF, // vertex format
D3DPOOL_MANAGED, // managed memory pool
&VB, // return create vertex buffer
0); // not used - set to 0
Vertex *vertices;
VB->Lock(0, 0, (void **)&vertices, 0);
vertices[0] = Vertex(-1.0f, 0.0f, 2.0f);
vertices[1] = Vertex(0.0f, 1.0f, 2.0f);
vertices[2] = Vertex(1.0f, 0.0f, 2.0f);
VB->Unlock();
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)WIDTH / (float)HEIGHT,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION, &proj);
device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
device->BeginScene();
device->SetStreamSource(0, VB, 0, sizeof(Vertex));
device->SetFVF(Vertex::FVF);
// Draw one triangle.
device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
device->EndScene();
device->Present(0, 0, 0, 0);
return true;
}
bool D3D::initD3D(D3DDEVTYPE DeviceType, HWND hwnd)
{
IDirect3D9 * d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
// 填充主显示设备的能力( D3DCAPS9结构)
D3DCAPS9 caps;
d3d9->GetDeviceCaps(
D3DADAPTER_DEFAULT, // 主显示设备
DeviceType, // 设备类型,一般是D3DDEVTYPE_HAL.
&caps); // 返回填充后的D3DCAPS9 结构,包含主显示设备的能力
// 是否可以使用硬件顶点处理?
int vp = 0;
if (caps.DevCaps& D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
// 是,支持硬件顶点处理
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
// 不,只能用软件顶点处理
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //像素格式
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = true; // fullscreen
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // depth format
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
int hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT, //primary adapter
D3DDEVTYPE_HAL,//devicetype
hwnd,//window associated withdevice
D3DCREATE_HARDWARE_VERTEXPROCESSING, //vertexprocessing type
&d3dpp,//presentparameters
&device);//returned created device
if (FAILED(hr))
{
::MessageBox(0, "CreateDevice() -FAILED", 0, 0);
return 0;
}
else
{
return 1;
}
}
大哥 你看看 我把立方体改成三角形了 什么东西都没有出来。。 我窗口背景颜色是白色[/quote]
你贴出来的代码是没问题的额,你程序能出来窗口吗?还是有窗口没内容?
#include "d3dUtility.h"
#define WIDTH 800 //屏幕宽
#define HEIGHT 600 //屏幕高
IDirect3DDevice9*device = 0;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DVertexBuffer9 *VB; //定义顶点缓存
IDirect3DIndexBuffer9 *IB; //定义索引缓存
struct Vertex
{
float _x, _y, _z;
Vertex(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
bool D3D::Setup()
{
device->CreateVertexBuffer(
3 * sizeof(Vertex), // size in bytes
D3DUSAGE_WRITEONLY, // flags
Vertex::FVF, // vertex format
D3DPOOL_MANAGED, // managed memory pool
&VB, // return create vertex buffer
0); // not used - set to 0
Vertex *vertices;
VB->Lock(0, 0, (void **)&vertices, 0);
vertices[0] = Vertex(-1.0f, 0.0f, 2.0f);
vertices[1] = Vertex(0.0f, 1.0f, 2.0f);
vertices[2] = Vertex(1.0f, 0.0f, 2.0f);
VB->Unlock();
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)WIDTH / (float)HEIGHT,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION, &proj);
device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
device->BeginScene();
device->SetStreamSource(0, VB, 0, sizeof(Vertex));
device->SetFVF(Vertex::FVF);
// Draw one triangle.
device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
device->EndScene();
device->Present(0, 0, 0, 0);
return true;
}
bool D3D::initD3D(D3DDEVTYPE DeviceType, HWND hwnd)
{
IDirect3D9 * d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
// 填充主显示设备的能力( D3DCAPS9结构)
D3DCAPS9 caps;
d3d9->GetDeviceCaps(
D3DADAPTER_DEFAULT, // 主显示设备
DeviceType, // 设备类型,一般是D3DDEVTYPE_HAL.
&caps); // 返回填充后的D3DCAPS9 结构,包含主显示设备的能力
// 是否可以使用硬件顶点处理?
int vp = 0;
if (caps.DevCaps& D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
// 是,支持硬件顶点处理
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
// 不,只能用软件顶点处理
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //像素格式
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = true; // fullscreen
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // depth format
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
int hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT, //primary adapter
D3DDEVTYPE_HAL,//devicetype
hwnd,//window associated withdevice
D3DCREATE_HARDWARE_VERTEXPROCESSING, //vertexprocessing type
&d3dpp,//presentparameters
&device);//returned created device
if (FAILED(hr))
{
::MessageBox(0, "CreateDevice() -FAILED", 0, 0);
return 0;
}
else
{
return 1;
}
}
大哥 你看看 我把立方体改成三角形了 什么东西都没有出来。。 我窗口背景颜色是白色bool Display(float timeDelta)
{
if( Device )
{
//
// spin the cube:
//
D3DXMATRIX Rx, Ry;
// rotate 45 degrees on x-axis
D3DXMatrixRotationX(&Rx, 3.14f / 4.0f);
// incremement y-rotation angle each frame
static float y = 0.0f;
D3DXMatrixRotationY(&Ry, y);
y += timeDelta;
// reset angle to zero when angle reaches 2*PI
if( y >= 6.28f )
y = 0.0f;
// combine x- and y-axis rotation transformations.
D3DXMATRIX p = Rx * Ry;
Device->SetTransform(D3DTS_WORLD, &p);
//
// draw the scene:
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetIndices(IB);
Device->SetFVF(Vertex::FVF);
// Draw cube.
Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}