DirectX11-Sample-Tutorial02-初始化窗口-利用d3d绘制旋转立方体

这篇教程详细介绍了如何使用DirectX11创建一个窗口,并在其中绘制一个旋转的立方体。通过main.cpp和Tutorial04.fx文件的代码实现,读者将学习到DirectX11的基本操作和图形渲染技巧。
摘要由CSDN通过智能技术生成

完整代码看这里

Tutorial04.fx


//--------------------------------------------------------------------------------------
//常缓存结构变量
//--------------------------------------------------------------------------------------
cbuffer MatrixCoord : register( b0 )
{
	matrix world;
	matrix view;
	matrix projection;
}

//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR0;
};

//--------------------------------------------------------------------------------------
// 顶点着色器输出结构
//--------------------------------------------------------------------------------------
VS_OUTPUT VS( float4 Pos : POSITION, float4 Color : COLOR )
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.Pos = mul( Pos, world );
    output.Pos = mul( output.Pos, view );
    output.Pos = mul( output.Pos, projection );
    output.Color = Color;<pre name="code" class="cpp">#include <Windows.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <D3Dcompiler.h>
#include <xnamath.h>
#include <tchar.h>

//顶点结构
struct SimpleVertex
{
	XMFLOAT3 Pos;//位置
	XMFLOAT4 Color;//颜色
};

//相机矩阵
struct MatrixCoord 
{
	XMMATRIX world;
	XMMATRIX view;
	XMMATRIX projection;
};

//全局变量
HINSTANCE					g_hInstance		=	NULL;
HWND						g_hWnd			=	NULL;

D3D_DRIVER_TYPE				g_driverType	=	D3D_DRIVER_TYPE_HARDWARE;
D3D_FEATURE_LEVEL			g_featureLevel	=	D3D_FEATURE_LEVEL_11_0;
ID3D11Device*				g_device		=	NULL;
ID3D11DeviceContext*		g_deviceContext		=	NULL;
IDXGISwapChain*				g_swapChain		=	NULL;
ID3D11RenderTargetView*		g_renderTargetView	=	NULL;

ID3D11VertexShader*			g_vertexShader	=	NULL;
ID3D11PixelShader*			g_pixelShader	=	NULL;
ID3D11InputLayout*			g_inputLayout	=	NULL;
ID3D11Buffer*				g_vertexBuffer	=	NULL;
ID3D11Buffer*				g_indexBuffer	=	NULL;

ID3D11Buffer*				g_constantBuffer	=	NULL;
XMMATRIX					g_world;
XMMATRIX					g_view;
XMMATRIX					g_projection;

//全局函数
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
HRESULT InitDevice();
HRESULT CompileShaderFromFile( WCHAR* filename, LPCSTR entrypoint, LPCSTR shaderModel, ID3DBlob** pBlob);
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
void Render();
void ShutdownDevice();

//主函数
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPreInstance, LPWSTR nCmdLine, int nCmdShow )
{
	HRESULT hResult;
	g_hInstance	=	hInstance;
	//初始化窗口
	hResult	=	InitWindow( g_hInstance, nCmdShow );
	if ( FAILED(hResult) )
	{
		return 0;
	}

	//初始化设备
	hResult	=	InitDevice();
	if ( FAILED(hResult) )
	{
		return 0;
	}

	//消息循环
	MSG msg	=	{ 0 };
	while ( WM_QUIT != msg.message )
	{
		if ( PeekMessage( &msg, g_hWnd, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			Render();
		}
	}

	return ( int )msg.wParam;
}

//初始化窗口
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{
	HRESULT hResult;

	//创建窗口类
	WNDCLASSEX wcex;
	wcex.cbClsExtra				=	0;
	wcex.cbSize					=	sizeof( wcex );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值