D3D绘制2D图像例子
2014/09/25
(转载自:http://blog.csdn.net/pizi0475/article/details/6268240)
原文出处:
http://www.cppblog.com/lovedday/archive/2007/07/02/27379.html
思路与一个用D3D绘制2D图形的例子相差不多,主要的区别在顶点的数据结构定义不一样。
2D的VERTEX结构定义为:
typedef struct
{
float x, y, z; // 2D coordinates
float rhw; // rhw
float u, v; // texture coordinates
} VERTEX;
#define VERTEX_FVF (D3DFVF_XYZRHW | D3DFVF_TEX1)
3D的VERTEX结构定义为:
typedef struct
{
float x, y, z; // 3D coordinates
float u, v; // texture coordinates
} VERTEX;
#define VERTEX_FVF (D3DFVF_XYZ | D3DFVF_TEX1)
其他的区别主要在Do_Init函数,3D模式启用深度蒙板,并且格式为16位z-缓存位深;接着禁用光照,并且启用z缓存;再接着设置透视矩阵和视口矩阵。
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 缓存,接着设置世界矩阵。
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 400
#define Safe_Release(p) if((p)) (p)->Release();
// window handles, class and caption text.
HWND g_hwnd;
HINSTANCE g_inst;
static char g_class_name[] = "Draw3DClass";
static char g_caption[] = "Draw3D Demo";
// the Direct3D and device object
IDirect3D9* g_d3d = NULL;
IDirect3DDevice9* g_d3d_device = NULL;
// The 3D vertex format and descriptor
typedef struct
{
float x, y, z; // 3D coordinates
float u, v; // 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 0;
}
return ( long) DefWindowProc(hwnd, msg, wParam, lParam);
}
// --------------------------------------------------------------------------------
// Initialize d3d, d3d device, vertex buffer, texutre; set render state for d3d;
// set perspective matrix and view matrix.
// --------------------------------------------------------------------------------
BOOL Do_Init()
{
D3DPRESENT_PARAMETERS present_param;
D3DDISPLAYMODE display_mode;
D3DXMATRIX mat_proj, mat_view;
BYTE* vertex_ptr;
// initialize vertex data
VERTEX verts[] = {
{ -100.0f, 100.0f, 0.0f, 0.0f, 0.0f },
{ 100.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 = TRUE;
present_param.SwapEffect = D3DSWAPEFFECT_DISCARD;
present_param.BackBufferFormat = display_mode.Format;
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_world;
// 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 win_class;
MSG msg;
g_inst = inst;
// create window class and register it
win_class.cbSize = sizeof(win_class);
win_class.style = CS_CLASSDC;
win_class.lpfnWndProc = Window_Proc;
win_class.cbClsExtra = 0;
win_class.cbWndExtra = 0;
win_class.hInstance = inst;
win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win_class.hbrBackground = NULL;
win_class.lpszMenuName = NULL;
win_class.lpszClassName = g_class_name;
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)
break;
}
// run shutdown function
Do_Shutdown();
UnregisterClass(g_class_name, inst);
return ( int) msg.wParam;
}
效果(图象绕z轴旋转):
D3D绘制2D图像例子相关推荐
- [SheRO]用D3D绘制2D图像
置顶声明:本文版权归shallway所有,如有转载,请按如下方式于文章明显位置标明原创作者及原文出处,以示尊重!! ========================================== ...
- (转)一个用D3D绘制2D图形的例子
一个用D3D绘制2D图形的例子 思路如下: (1)函数Do_Init初始化D3D,D3D设备,顶点缓冲,纹理:主要调用这几个函数: Direct3DCreate9,GetAdapter ...
- dx绘制2d图像_在DirectX 中进行2D渲染
http://flcstudio.blog.163.com/blog/static/756035392008115111123672/ 最近,我看到很多关于DirectX8在最新的API中摒弃Dire ...
- stl文件 python_用Python从STL文件绘制2D图像
我想加载一个STL文件,并在不同的旋转产生一组2D图像.在 我得到了基于this示例的numpy stl的基本操作,最后得到以下代码-from stl import mesh from mpl_too ...
- dx绘制2d图像_【教程】使用DX9做一个2D游戏(1)
本文最先发表在贴吧,现在整理到此处,之后所有更新将在这里进行. by Chu @ XDU 2012/11/25 版权所有,禁止用于商业用途. 转载请注明出处. 用DX9做一个2D游戏显然不是一件容易的 ...
- dx绘制2d图像_Direct2D教程II——绘制基本图形和线型(StrokeStyle)的设置详解
目前,在博客园上,相对写得比较好的两个关于Direct2D的教程系列,分别是万一的Direct2D系列和zdd的Direct2D系列.有兴趣的网友可以去看看.本系列也是介绍Direct2D的教程,是基 ...
- 使用 Matplotlib 绘制 2D 和 3D 图形
文章目录 1. 绘制2D图像 1.1. 与MATLAB相似的用法 1.2. 面向对象的方法 1.3. 图的属性设置 1.4. 其他二维图 2. 绘制3D图 2.1. 在三维空间中绘制散点 2.2. 在 ...
- Boost:GPU上的2D图像中绘制最终的随机“walk”,并使用OpenCV进行显示
Boost:GPU上的2D图像中绘制最终的随机"walk",并使用OpenCV进行显示 实现功能 C++实现代码 实现功能 Boost的compute模块,Boost:GPU上的2 ...
- Mac系统中怎么绘制函数图像?附绘制函数图像教程~
学数学常常要自己画图?画不对,画得慢,画的丑?Mac系统中怎么绘制函数图像?福利来了,mac系统下有非常方便的画函数图像的工具,可以快速地画出很多简单的,复杂的,2D的,3D的函数图像.简直就是学习数 ...
最新文章
- HTML+CSS布局技巧及兼容问题【阅读季】
- 全国大学生智能汽车竞赛-百度线下赛道报名开始!
- 【转】了解SQL Server触发器及触发器中的事务
- arduino串口监视器显示nan_Arduino常用的三种通信协议UART, I2C和SPI
- 启示录2:打造优秀的产品团队
- 安静模式 运行 reg注册表文件
- CSS 幻术 | 抗锯齿
- 服务器的mdf文件怎么打开,在没SQL Server数据库情况下怎么打开.MDF文件?
- 宋九九:怎么做好网站搜索引擎优化,企业网站如何seo优化?
- 基于Jeecg的权限获取
- 仪表放大器和运算放大器优缺点对比
- 微信小程序产品定位及功能介绍
- 计算机网络基础知识点快速复习手册
- python 情感分析实例_基于Python的情感分析案例
- 精心推荐8款实用国产软件,非常强大
- openfoam计算旋转体滑移网格方法和MRF方法(附案例代码)
- flightgear 光标消失 卡死
- 使用pandas清洗数据(中文字符串的正则使用)
- 安卓系统与ADB详解
- 申宝资讯虚拟现实等概念表现抢眼