D3D 线列 小样例

画两条线

#pragma once

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")

#include<d3d9.h>
#include<d3dx9.h>

//TODO: -1 custom vertex
struct CUSTOMVERTEX
{
	float x;
	float y;
	float z;
	float rhw;
};

#define D3DFVF_CUSTOMVERTEX D3DFVF_XYZRHW

HRESULT InitD3D(HWND hWnd);
void CleanUp();
void Render();
LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
HRESULT InitD3DVertexBuffer();

//TODO: 0 declare d3d variables.
LPDIRECT3D9 g_pd3d = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice9 = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pd3dVB = NULL;

//application entry point.
INT WINAPI wWinMain(
	HINSTANCE,
	HINSTANCE,
	LPWSTR,
	INT)
{
	//initialize wnd class.
	WNDCLASSEX wcex;
	ZeroMemory(&wcex,sizeof(wcex));
	wcex.cbSize=sizeof(wcex);
	wcex.hInstance=GetModuleHandle(NULL);
	wcex.lpfnWndProc=MsgProc;
	wcex.lpszClassName=L"Self001";
	wcex.style=CS_CLASSDC;

	//register wnd class.
	RegisterClassEx(&wcex);

	//create window.
	HWND hWnd=CreateWindowEx(
		WS_EX_OVERLAPPEDWINDOW,
		L"Self001",
		L"Self001 Window",
		WS_OVERLAPPEDWINDOW,
		100,
		100,
		300,
		300,
		NULL,
		NULL,
		wcex.hInstance,
		NULL);

	//init d3d
	if(SUCCEEDED(InitD3D(hWnd)))
	{
		//show window.
		ShowWindow(hWnd,SW_SHOWDEFAULT);
		UpdateWindow(hWnd);

		//message loop.
		MSG msg;
		ZeroMemory(&msg,sizeof(msg));
		while(msg.message != WM_QUIT)
		{
			if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
			{
				Render();
			}
		}
	}

	//unregister wnd class.
	UnregisterClass(L"Self001",wcex.hInstance);

	return 0;
}

//init d3d.
HRESULT InitD3D(HWND hWnd)
{
	//create d3d.
	g_pd3d=Direct3DCreate9(D3D_SDK_VERSION);
	if(g_pd3d == NULL)
	{
		return E_FAIL;
	}

	//initialize d3d present parameters.
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.Windowed=TRUE;
	d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;

	//create d3d device.
	if(FAILED(g_pd3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&g_pd3dDevice9)))
	{
		return E_FAIL;
	}

	//init vertex buffer.
	if(FAILED(InitD3DVertexBuffer()))
	{
		return E_FAIL;
	}

	return S_OK;
}

//clean up d3d.
void CleanUp()
{
	//TODO: 3 clean up d3d.
	if(g_pd3dVB != NULL)
	{
		g_pd3dVB->Release();
	}

	//release device.
	if(g_pd3dDevice9 != NULL)
	{
		g_pd3dDevice9->Release();
	}

	//release d3d.
	if(g_pd3d != NULL)
	{
		g_pd3d->Release();
	}
}

//render the scene.
void Render()
{
	//clear target.
	g_pd3dDevice9->Clear(
		0,
		NULL,
		D3DCLEAR_TARGET,
		D3DCOLOR_XRGB(0,0,0),
		1.0f,
		0);

	//draw primitive.
	if(SUCCEEDED(g_pd3dDevice9->BeginScene()))
	{
		//TODO: 2 render d3d.
		g_pd3dDevice9->SetStreamSource(0,g_pd3dVB,0,sizeof(CUSTOMVERTEX));
		g_pd3dDevice9->SetFVF(D3DFVF_CUSTOMVERTEX);
		g_pd3dDevice9->DrawPrimitive(D3DPT_LINELIST,0,2);

		g_pd3dDevice9->EndScene();
	}

	//present sence.
	g_pd3dDevice9->Present(NULL,NULL,NULL,NULL);
}

//window message handler.
LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		CleanUp();
		return 0L;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}

//init d3d vertex buffer.
HRESULT InitD3DVertexBuffer()
{
	//TODO: 1 init vertex buffer data.
	//create vertex data.
	CUSTOMVERTEX vertices[4]=
	{
		{50.0f,50.0f,1.0f,1.0f},
		{50.0f,250.0f,1.0f,1.0f},
		{250.0f,50.0f,1.0f,1.0f},
		{250.0f,250.0f,1.0f,1.0f}
	};

	//create vertex buffer.
	if(FAILED(g_pd3dDevice9->CreateVertexBuffer(
		sizeof(vertices),
		0,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT,
		&g_pd3dVB,
		NULL)))
	{
		return E_FAIL;
	}

	//fill vertex buffer.
	void* pVertices=NULL;
	if(FAILED(g_pd3dVB->Lock(0,sizeof(vertices),&pVertices,0)))
	{
		return E_FAIL;
	}

	memcpy(pVertices,vertices,sizeof(vertices));

	g_pd3dVB->Unlock();

	return S_OK;
}

程序结果

选择图元小心别误选。

时间: 2024-09-30 16:13:59

D3D 线列 小样例的相关文章

SpringMVC+Spring+Hibernate的小样例

Strusts2+Spring+Hibernate尽管是主流的WEB开发框架,可是SpringMVC有越来越多的人使用了.确实也很好用.用得爽! 这里实现了一个SpringMVC+Spring+Hibernate的小样例.凝视都在代码里面. 项目各包的结构例如以下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlhbnR1amF2YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve

C Tips:显示点阵汉字的小样例

非常简陋的一段小程序,演示怎样显示点阵字库.有时间的时候再详解. #include <stdio.h> #include <stdlib.h> struct HzkInfoStruct { int HzkSelect; int HzkSquare; char * fileName; FILE * file; int martixBytesCount; unsigned char *pMatrix; }; typedef struct HzkInfoStruct HzkInfo; v

D3D 线列 小例子

画两条线 #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #include<d3d9.h> #include<d3dx9.h> //TODO: -1 custom vertex struct CUSTOMVERTEX { float x; float y; float z; float rhw; }; #define D3DFVF_CU

D3D 光照和材料 小样例

1.实现一个旋转的圆柱体,体现d3d光照效果 2.程序实现 #pragma once #pragma comment(lib,"winmm.lib") #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #include<d3d9.h> #include<d3dx9.h> //自己定义顶点格式 struct CUSTOMVERTEX { D3DX

简单的ADO.NET连接数据小样例

ADO.NET连接数据库的样例如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Confi

用两个小样例来解释单例模式中的“双重锁定”

学习单例模式时,好多人都不太理解双重锁定. 学完后突然想到一个非常有趣的样例. 单例模式结构图: 代码: Singleton类 class Singleton { private static Singleton instance; private static readonly object syncRoot = new object(); //程序执行时创建一个静态仅仅读的进程辅助对象 private Singleton() { } //用private修饰构造方法,防止外界利用new创建此

android:json解析的两个工具:Gson和Jackson的使用小样例

1.简单介绍 json是android与server通信过程中经常使用的数据格式,比如.例如以下是一个json格式的字符串: {"address":"Nanjing","name":"NUPT","students":[{"name":"stu1","id":"10000","age":20},{"

时间戳转化为时间小样例

        NSString *[email protected]"1439965800";//时间戳         /*因为时间差问题所以要加8个小时==28800sec*/         NSTimeInterval time=[str doubleValue]+28800;         NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time];         NSLog(@"detailD

Java实现串口通信的小样例

用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME/jre/lib/ext; 2.win32com.dll放置到 JAVA_HOME/bin; 3.javax.comm.properties 两个地方都要放 jre/lib(也就是在JAVA目录下的jre) JAVA_HOME/jre/lib 说一下我应用的环境.电子秤称重时,计算机通过串口给称重控