d3d中::d3d是什么意思思

 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
D3D 中的模板缓存 3
下载积分:400
内容提示:D3D 中的模板缓存 3
文档格式:PDF|
浏览次数:136|
上传日期: 05:24:39|
文档星级:
该用户还上传了这些文档
D3D 中的模板缓存 3
官方公共微信游戏里的D3D模式和OPENGL模式各是什么意思?工作原理有什么不同?_百度知道D3D中UI界面的制作-海文库
全站搜索:
您现在的位置:&>&&>&互联网
D3D中UI界面的制作
D3D中UI界面的制作案例说明:本案例通过制作D3D中的UI界面,掌握如何在D3D中插入cegui图形界面库,并渲染出来。首先,***CEGui的SDK,***完成后在项目设置中新增include和lib文件夹
设置完成后,修改项目的属性:
修改设置完成后,在项目中加入用于Gui渲染的头文件和文件
接下来新建一个用于GUI渲染的类
实现构造函数
在cpp文件中实现InitGui函数如下: bool CD3DGui::InitGui(){//创建渲染器m_pRender = &CEGUI::Direct3D9Renderer::create(m_pD3DDevice);
//创建CEGUI系统CEGUI::System::create(*m_pRender);
CEGUI::DefaultResourceProvider* rp =static_cast&CEGUI::DefaultResourceProvider*&(CEGUI::System::getSingleton().getResourceProvider());
//设置资源目录
rp-&setResourceGroupDirectory(&schemes&,&cegui/datafiles/schemes/&);rp-&setResourceGroupDirectory(&imagesets&,&cegui/datafiles/imagesets/&);rp-&setResourceGroupDirectory(&fonts&, &cegui/datafiles/fonts/&);
rp-&setResourceGroupDirectory(&layouts&,&cegui/datafiles/layouts/&);rp-&setResourceGroupDirectory(&looknfeels&,&cegui/datafiles/looknfeel/&);
CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
CEGUI::Imageset::setDefaultResourceGroup(&imagesets&);CEGUI::Font::setDefaultResourceGroup(&fonts&);CEGUI::Scheme::setDefaultResourceGroup(&schemes&);CEGUI::WidgetLookManager::setDefaultResourceGroup(&looknfeels&);
CEGUI::WindowManager::setDefaultResourceGroup(&layouts&);//取得窗体管理器CEGUI::WindowManager& winMgr =CEGUI::WindowManager::getSingleton();
//读取字体CEGUI::SchemeManager::getSingleton().create(&TaharezLook.scheme&);CEGUI::System::getSingleton().setDefaultMouseCursor(&TaharezLook&, &MouseArrow&);CEGUI::FontManager::getSingleton().create(&DejaVuSans-10.font&);//设置鼠标的图片CEGUI::MouseCursor::getSingleton().setImage(&TaharezLook&, &MouseArrow&);//显示鼠标CEGUI::MouseCursor::getSingleton().show( );//创建一个根窗口,上面加载各种界面CEGUI::Window* root =winMgr.createWindow(&DefaultWindow&,&root_wnd&);
root-&setFont(&DejaVuSans-10&);//设置为默认的界面install this as the root GUI sheet
CEGUI::System::getSingleton().setGUISheet(root);}继续实现Render函数
在D3DInit中创建CD3DInit的对象指针
Init函数中初始化对象指针
在D3D渲染开始之后先设置D3D渲染的状态
Render函数中最后实现Gui的渲染
将资源文件夹下的cegui文件夹复制到工程目录下,将dll文件夹中的文件全部复制到debug目录下,运行测试。运行后发现有两个鼠标,但是位置不一致,需要修改D3DInit时的参数打开D3DInit.***件,修改原来的InitD3D函数加入一个是否全屏的属性
在D3DInit.cpp文件的函数定义也需要修改
D3D设备创建前的代码修改如下://--创建D3D对象if(NULL == (m_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))return E_FAIL;
//--(E_FAIL)创建失败D3DDISPLAYMODE d3
//--定义一个保存当前适配器属性的结构
//--获得当前适配器的显示模式if(FAILED(m_pD3D-&GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))){return E_FAIL;
//--如果失败则返回(FAILED:失败)}//--设置传递给LPDIRECT3D9::CreateDevice的参数D3DPRESENT_PARAMETERS d3ZeroMemory(&d3dpp, sizeof(d3dpp));
//--把结构体清空
if (fullScreen)
//判断是否全屏显示{//如果全屏显示后台缓冲区高度和高度设为当前显示器的宽度和高度
d3dpp.Windowed
//--指明不是窗口模式,而是全局独占模式d3dpp.BackBufferWidth
//--后缓冲宽度d3dpp.BackBufferHeight = d3ddm.H
//--后缓冲高度
}else{//如果不是全屏显示后台缓冲区高度和高度设为当前窗体的宽度和高度
RECTGetClientRect(hWnd,&rect);
//取得当前窗体的矩形大小d3dpp.Windowed
= TRUE;d3dpp.BackBufferWidth
= rect.right-rect.
//--后缓冲宽度d3dpp.BackBufferHeight = rect.bottom-rect.
//--后缓冲高度}d3dpp.SwapEffect
= D3DSWAPEFFECT_DISCARD;
//--设定换页效果为丢弃后台缓存d3dpp.BackBufferFormat = d3ddm.F
//--色彩深度为桌面的色彩深度
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //--屏幕刷新率d3dpp.PresentationInterval
= D3DPRESENT_INTERVAL_ONE; //--翻转模式
d3dpp.AutoDepthStencilFormat
= D3DFMT_D16;d3dpp.EnableAutoDepthStencil
=最后修改CreateWindows中的函数调用
上面完成了Gui渲染环境的搭建,下面我们使用Gui编辑器制作一个GUI界面,放入游戏中渲染出来。
上一篇: 下一篇:
All rights reserved Powered by
copyright ©right 。文档资料库内容来自网络,如有侵犯请联系***。下次自动登录
现在的位置:
& 综合 & 正文
D3D中用3D方法绘制2D图象的例子
原文出处:
思路与相差不多,主要的区别在顶点的数据结构定义不一样。
2D的VERTEX结构定义为:
// The 2D vertex format and descriptortypedef struct{
float x, y,
// 2D coordinates
// texture coordinates} VERTEX;
#define VERTEX_FVF
(D3DFVF_XYZRHW | D3DFVF_TEX1)
3D的VERTEX结构定义为:
// The 3D vertex format and descriptortypedef struct{
float x, y,
// 3D coordinates
// texture coordinates} VERTEX;
#define VERTEX_FVF
(D3DFVF_XYZ | D3DFVF_TEX1)
其他的区别主要在Do_Init函数,3D模式启用深度蒙板,并且格式为16位z-缓存位深;接着禁用光照,并且启用z缓存;再接着设置透视矩阵和视口矩阵。
present_param.EnableAutoDepthStencil = TRUE;
present_param.AutoDepthStencilFormat = D3DFMT_D16;
// set render state
// disable d3d lighting
g_d3d_device-&SetRenderState(D3DRS_LIGHTING, FALSE);
// enable z-buffer
g_d3d_device-&SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
// create and set the projection matrix
// builds a left-handed perspective projection matrix based on a field of view
D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4.0, 1.33333, 1.0, 1000.0);
// sets a single device transformation-related state
g_d3d_device-&SetTransform(D3DTS_PROJECTION, &mat_proj);
// create and set the view matrix
D3DXMatrixLookAtLH(&mat_view,
&D3DXVECTOR3(0.0, 0.0, -500.0),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_d3d_device-&SetTransform(D3DTS_VIEW, &mat_view);
函数Do_Frame也有一些区别,调用Clear来清除后备缓冲时增加了一个D3DCLEAR_ZBUFFER提示D3D要清除z 缓存,接着设置世界矩阵。
// clear device back buffer
g_d3d_device-&Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 64, 128, 255), 1.0f, 0);
// create and set the world transformation matrix
// rotate object along z-axis
D3DXMatrixRotationZ(&mat_world, (float) (timeGetTime() / 1000.0));
g_d3d_device-&SetTransform(D3DTS_WORLD, &mat_world);
完整源码如下:
/***************************************************************************************PURPOSE:
3D Drawing Demo
Required libraries:
WINMM.LIB, D3D9.LIB, D3DX9.LIB. ***************************************************************************************/
#include &windows.h&#include &stdio.h&#include "d3d9.h"#include "d3dx9.h"
#pragma comment(lib, "winmm.lib")#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx9.lib")
#pragma warning(disable : 4305)
#define WINDOW_WIDTH
400#define WINDOW_HEIGHT
#define Safe_Release(p) if((p)) (p)-&Release();
// window handles, class and caption text.HWND g_HINSTANCE g_static char g_class_name[] = "Draw3DClass";static char g_caption[]
= "Draw3D Demo";
// the Direct3D and device objectIDirect3D9* g_d3d = NULL;IDirect3DDevice9* g_d3d_device = NULL;
// The 3D vertex format and descriptortypedef struct{
float x, y,
// 3D coordinates
// texture coordinates} VERTEX;
#define VERTEX_FVF
(D3DFVF_XYZ | D3DFVF_TEX1)
IDirect3DVertexBuffer9* g_vertex_buffer = NULL;IDirect3DTexture9*
g_texture = NULL;
//--------------------------------------------------------------------------------// Window procedure.//--------------------------------------------------------------------------------long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg)
case WM_DESTROY:
PostQuitMessage(0);
return (long) DefWindowProc(hwnd, msg, wParam, lParam);}
//--------------------------------------------------------------------------------// Initialize d3d, d3d device, vertex buffer, set render state for d3d;// set perspective matrix and view matrix.//--------------------------------------------------------------------------------BOOL Do_Init(){
D3DPRESENT_PARAMETERS present_
D3DDISPLAYMODE
D3DXMATRIX mat_proj, mat_
BYTE* vertex_
// initialize vertex data
VERTEX verts[] = {
{ -100.0f,
100.0f, 0.0f, 0.0f, 0.0f },
100.0f, 0.0f, 1.0f, 0.0f },
{ -100.0f, -100.0f, 0.0f, 0.0f, 1.0f },
100.0f, -100.0f, 0.0f, 1.0f, 1.0f }
// do a windowed mode initialization of Direct3D
if((g_d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return FALSE;
// retrieves the current display mode of the adapter
if(FAILED(g_d3d-&GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode)))
return FALSE;
ZeroMemory(&present_param, sizeof(present_param));
// initialize d3d presentation parameter
present_param.Windowed
present_param.SwapEffect
= D3DSWAPEFFECT_DISCARD;
present_param.BackBufferFormat
= display_mode.F
present_param.EnableAutoDepthStencil = TRUE;
present_param.AutoDepthStencilFormat = D3DFMT_D16;
// creates a device to represent the display adapter
if(FAILED(g_d3d-&CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_param, &g_d3d_device)))
return FALSE;
// set render state
// disable d3d lighting
g_d3d_device-&SetRenderState(D3DRS_LIGHTING, FALSE);
// enable z-buffer
g_d3d_device-&SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
// create and set the projection matrix
// builds a left-handed perspective projection matrix based on a field of view
D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4.0, 1.33333, 1.0, 1000.0);
// sets a single device transformation-related state
g_d3d_device-&SetTransform(D3DTS_PROJECTION, &mat_proj);
// create and set the view matrix
D3DXMatrixLookAtLH(&mat_view,
&D3DXVECTOR3(0.0, 0.0, -500.0),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_d3d_device-&SetTransform(D3DTS_VIEW, &mat_view);
// create the vertex buffer and set data
g_d3d_device-&CreateVertexBuffer(sizeof(VERTEX) * 4, 0, VERTEX_FVF, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);
// locks a range of vertex data and obtains a pointer to the vertex buffer memory
g_vertex_buffer-&Lock(0, 0, (void**)&vertex_ptr, 0);
memcpy(vertex_ptr, verts, sizeof(verts));
// unlocks vertex data
g_vertex_buffer-&Unlock();
// load the texture map
D3DXCreateTextureFromFile(g_d3d_device, "Texture.bmp", &g_texture);
return TRUE;}
//--------------------------------------------------------------------------------// Release all d3d resource.//--------------------------------------------------------------------------------BOOL Do_Shutdown(){
Safe_Release(g_vertex_buffer);
Safe_Release(g_texture);
Safe_Release(g_d3d_device);
Safe_Release(g_d3d);
return TRUE;}
//--------------------------------------------------------------------------------// Render a frame.//--------------------------------------------------------------------------------BOOL Do_Frame(){
D3DXMATRIX mat_
// clear device back buffer
g_d3d_device-&Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 64, 128, 255), 1.0f, 0);
// Begin scene
if(SUCCEEDED(g_d3d_device-&BeginScene()))
// create and set the world transformation matrix
// rotate object along z-axis
D3DXMatrixRotationZ(&mat_world, (float) (timeGetTime() / 1000.0));
g_d3d_device-&SetTransform(D3DTS_WORLD, &mat_world);
// set the vertex stream, shader, and texture.
// binds a vertex buffer to a device data stream
g_d3d_device-&SetStreamSource(0, g_vertex_buffer, 0, sizeof(VERTEX));
// set the current vertex stream declation
g_d3d_device-&SetFVF(VERTEX_FVF);
// assigns a texture to a stage for a device
g_d3d_device-&SetTexture(0, g_texture);
// renders a sequence of noindexed, geometric primitives of the specified type from the current set
// of data input stream.
g_d3d_device-&DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
// release texture
g_d3d_device-&SetTexture(0, NULL);
// end the scene
g_d3d_device-&EndScene();
// present the contents of the next buffer in the sequence of back buffers owned by the device
g_d3d_device-&Present(NULL, NULL, NULL, NULL);
return TRUE;}
//--------------------------------------------------------------------------------// Main function, routine entry.//--------------------------------------------------------------------------------int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show){
WNDCLASSEX
// create window class and register it
win_class.cbSize
= sizeof(win_class);
win_class.style
= CS_CLASSDC;
win_class.lpfnWndProc
= Window_P
win_class.cbClsExtra
win_class.cbWndExtra
win_class.hInstance
win_class.hIcon
= LoadIcon(NULL, IDI_APPLICATION);
win_class.hCursor
= LoadCursor(NULL, IDC_ARROW);
win_class.hbrBackground = NULL;
win_class.lpszMenuName
win_class.lpszClassName = g_class_
win_class.hIconSm
= LoadIcon(NULL, IDI_APPLICATION);
if(! RegisterClassEx(&win_class))
return FALSE;
// create the main window
g_hwnd = CreateWindow(g_class_name, g_caption, WS_CAPTION | WS_SYSMENU, 0, 0,
WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, inst, NULL);
if(g_hwnd == NULL)
return FALSE;
ShowWindow(g_hwnd, SW_NORMAL);
UpdateWindow(g_hwnd);
// initialize game
if(Do_Init() == FALSE)
return FALSE;
// start message pump, waiting for signal to quit.
ZeroMemory(&msg, sizeof(MSG));
while(msg.message != WM_QUIT)
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
TranslateMessage(&msg);
DispatchMessage(&msg);
// draw a frame
if(Do_Frame() == FALSE)
// run shutdown function
Do_Shutdown();
UnregisterClass(g_class_name, inst);
return (int) msg.wP}
效果(图象绕z轴旋转):
&&&&推荐文章:
【上篇】【下篇】WP Cumulus Flash tag cloud by
9 or better.
2016年九月
12131415161718
19202122232425
2627282930
本博客主机
最近访客新浪微博

参考资料

 

随机推荐