用VB.NET实现DirectX

用VB.NET实现DirectX,directx,Directx,是否有一套关于DirectX(9或更高版本)的文档,其中显示了在VB.NET中使用DirectX(9或更高版本)的对象、方法、属性、事件和示例代码? 最新的SDK包括C++的详细信息和“Windows DirectX图形文档”文件不包含这些细节。 < P>您可能想检查这是一个非常好的重新编码时,管理托管DirectX和XNA! 一个快速的directx谷歌搜索可以在上找到。 快速浏览一下这里的库页面。我认为这里最明智的想法是使用SlimDX(一个用户编写的非常好的包装器)。原因请记住,micro

是否有一套关于DirectX(9或更高版本)的文档,其中显示了在VB.NET中使用DirectX(9或更高版本)的对象、方法、属性、事件和示例代码?
最新的SDK包括C++的详细信息和“Windows DirectX图形文档”文件不包含这些细节。

< P>您可能想检查这是一个非常好的重新编码时,管理托管DirectX和XNA! 一个快速的directx谷歌搜索可以在上找到。
快速浏览一下这里的库页面。

我认为这里最明智的想法是使用SlimDX(一个用户编写的非常好的包装器)。原因请记住,microsoft不再支持或更新托管DirectX。(这种支持在几年前就停止了)


去看看吧。它支持DirectX11。

看看XNA game studio,它目前支持所有.NET语言。 在这里下载
它附带示例。

我建议您使用SharpDX。这很像SlimDX,但速度更快,几乎完全基于头文件创建。您应该能够在VB下使用该库,即使它的名称中有“Sharp”指向C-Sharp。恐怕这是WinRT唯一可以使用的解决方案,它几乎是DirectX的1:1采用-因此您可以使用原始的MSDN DirectX文档。它具有DX9、DX10和DX11的功能

也可以在这里查看,以便在SlimDX和SharpDX之间做出决定:


我强烈建议至少选择其中一个,如果你只想“管理DirectX”,而不需要高级API。

< P>这是一个在Visual Studio 2017中使用VB.NET和C++ DLL的例子。 特点:

  • VB中使用XAML控件的通用Windows项目(UWP) SwapChainPanel
  • 调用标准win32 dll。dll初始化 DirectX,创建一个线程并从dll呈现SwapChainPanel C++编写的代码.
  • 在Visual Studio 2017中编译
在Visual Basic中,将SwapChainPanel添加到MainPage.xaml

<SwapChainPanel x:Name="swapChainPanel1">
        <TextBlock x:Name="txt1" Text="Hello from XAML!"
               HorizontalAlignment="Right"
               VerticalAlignment="Top"
               FontSize="30" />
        <Button Content="Button" HorizontalAlignment="Left" Height="30" Margin="812,484,0,0" VerticalAlignment="Top" Width="97" Click="Button_Click"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="79" Margin="851,561,0,0" VerticalAlignment="Top" Width="126" Click="Button_Click_1"/>

</SwapChainPanel>
头文件Game.h(此代码基于。Microsoft代码过于膨胀

#pragma once

using namespace Microsoft::WRL;
using namespace DirectX;

class CGame
{


public:
    ComPtr<ID3D11Device1> dev;              // the device interface
    ComPtr<ID3D11DeviceContext1> devcon;    // the device context interface
    ComPtr<IDXGISwapChain1> swapchain;      // the swap chain interface
    ComPtr<ID3D11RenderTargetView> rendertarget;    

    ComPtr<ID3D11Texture2D> Backbuffer;

    UINT32 Initialize();
    void Update();
    UINT32 Render();
    void InitGraphics();

private:
    float ScreenColour;
    UINT ScreenColourI;

    UINT Width, Height;
    UINT32 * TextureData;

};
#pragma一次
使用名称空间Microsoft::WRL;
使用名称空间DirectX;
类CGame
{
公众:
ComPtr dev;//设备接口
ComPtr devcon;//设备上下文接口
ComPtr swapchain;//交换链接口
ComPtr渲染目标;
ComPtr Backbuffer;
UINT32初始化();
无效更新();
UINT32 Render();
void InitGraphics();
私人:
浮动屏幕颜色;
银幕色彩;
单元宽度、高度;
UINT32*纹理数据;
};
和一个Game.cpp文件

#include "stdafx.h"
#include <wrl/client.h>
#include <d3d11_1.h>
#include <DirectXMath.h>
#include <windows.ui.xaml.media.dxinterop.h>
#include "Game.h"

DWORD WINAPI renderThread(LPVOID lpParameter);

CGame* pGame;
ComPtr<ISwapChainPanelNative> pSwapChainNative;

UINT32 __stdcall D3DInit(PULONG ptrSwapChainNative)
{
    UINT r;
    pGame = new CGame;
    pSwapChainNative = reinterpret_cast<ISwapChainPanelNative*> (ptrSwapChainNative);
    r = pGame->Initialize();

    return r;
}
UINT32 __stdcall D3DRender(void)
{

    CreateThread(NULL, NULL, renderThread, NULL, 0, NULL);
    return 0;
}


DWORD WINAPI renderThread(LPVOID lpParameter)
{

    for (int i = 0; i < 500; i++)
    {
        //pGame->Update();
        pGame->Render();
    }
    return 1;
}


// this function initializes and prepares Direct3D for use
UINT32 CGame::Initialize()
{
    HRESULT hr;
    // Define temporary pointers to a device and a device context
    ComPtr<ID3D11Device> dev11;
    ComPtr<ID3D11DeviceContext> devcon11;

    // Create the device and device context objects
    D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        0,
        nullptr,
        0,
        D3D11_SDK_VERSION,
        &dev11,
        nullptr,
        &devcon11);

    // Convert the pointers from the DirectX 11 versions to the DirectX 11.1 versions
    dev11.As(&dev);
    devcon11.As(&devcon);


    // First, convert our ID3D11Device1 into an IDXGIDevice1
    ComPtr<IDXGIDevice1> dxgiDevice;
    dev.As(&dxgiDevice);

    // Second, use the IDXGIDevice1 interface to get access to the adapter
    ComPtr<IDXGIAdapter> dxgiAdapter;
    dxgiDevice->GetAdapter(&dxgiAdapter);

    // Third, use the IDXGIAdapter interface to get access to the factory
    ComPtr<IDXGIFactory2> dxgiFactory;
    dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);


    /*
    // set up the swap chain description
    DXGI_SWAP_CHAIN_DESC1 scd = {0};
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    // how the swap chain should be used
    scd.BufferCount = 2;                                  // a front buffer and a back buffer
    scd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;              // the most common swap chain format
    scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;    // the recommended flip mode
    scd.SampleDesc.Count = 1;                             // disable anti-aliasing



    CoreWindow^ Window = CoreWindow::GetForCurrentThread();    // get the window pointer

    hr = dxgiFactory->CreateSwapChainForCoreWindow(
    dev.Get(),                                  // address of the device
    reinterpret_cast<IUnknown*>(Window),        // address of the window
    &scd,                                       // address of the swap chain description
    nullptr,                                    // advanced
    &swapchain);
    */

    DXGI_SWAP_CHAIN_DESC1 scd = { 0 };
    scd.Width = 1000;           //BUG setting it arbitarily. Needs cleanup. by Ravi
    scd.Height = 800;
    scd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;           // this is the most common swapchain format
    scd.Stereo = false;
    scd.SampleDesc.Count = 1;                          // don't use multi-sampling
    scd.SampleDesc.Quality = 0;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scd.BufferCount = 2;
    scd.Scaling = DXGI_SCALING_STRETCH;
    scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // we recommend using this swap effect for all applications
    scd.Flags = 0;


    hr = dxgiFactory->CreateSwapChainForComposition(
        dev.Get(),
        &scd,
        nullptr,                                    // allow on any display 
        &swapchain);


    if (FAILED(hr))
    {
        return  204;
    }

    pSwapChainNative->SetSwapChain(swapchain.Get());


    // get a pointer directly to the back buffer
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), &Backbuffer);


    // create a render target pointing to the back buffer
    dev->CreateRenderTargetView(Backbuffer.Get(), nullptr, &rendertarget);


    // Width and Height
    swapchain->GetDesc1(&scd);
    Width = scd.Width;
    Height = scd.Height;


    TextureData = new UINT32[Width * Height];


    // initialize graphics and the pipeline
    InitGraphics();


    return 0;
}

// this function performs updates to the state of the game
void CGame::Update()
{
}

// this function renders a single frame of 3D graphics
UINT32 CGame::Render()
{
    UINT n, i;
    UINT  * psrc;
/*
    // set our new render target object as the active render target
    devcon->OMSetRenderTargets(1, rendertarget.GetAddressOf(), nullptr);

    ScreenColour += 0.005f;
    if (ScreenColour >0.9f)
        ScreenColour = 0.0f;

    // clear the back buffer to a deep blue
    float color[4] = {ScreenColour, ScreenColour, ScreenColour, 1.0f};
    devcon->ClearRenderTargetView(rendertarget.Get(), color);
*/
    psrc = TextureData;
    i = ScreenColourI & 255;

    for (n=0; n<Width*Height; n++)
    {
        *psrc++ = (i<<16) | (i<<8) | i ;
        i++;
        i &= 255;
    }


    ScreenColourI += 1;

    devcon->UpdateSubresource (Backbuffer.Get(), 0, NULL, TextureData, Width*4, 0);

    // switch the back buffer and the front buffer
    swapchain->Present(1, 0);

    return 0;

}

// this function loads and initializes all graphics data
void CGame::InitGraphics()
{
    ScreenColour = 0.0f;
    ScreenColourI = 0;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括“Game.h”
DWORD WINAPI renderThread(LPVOID lpParameter);
CGame*pGame;
ComPtr pswapchainnactive;
UINT32标准呼叫D3DInit(PULONG ptrSwapChainNative)
{
UINT-r;
pGame=新的CGame;
pSwapChainNative=重新解释铸件(ptrSwapChainNative);
r=pGame->Initialize();
返回r;
}
UINT32标准球D3雨淋器(无效)
{
CreateThread(NULL,NULL,renderThread,NULL,0,NULL);
返回0;
}
DWORD WINAPI渲染读取(LPVOID lpParameter)
{
对于(int i=0;i<500;i++)
{
//pGame->Update();
pGame->Render();
}
返回1;
}
//此函数用于初始化和准备Direct3D以供使用
UINT32 CGame::Initialize()
{
HRESULT-hr;
//定义指向设备和设备上下文的临时指针
ComPtr dev11;
ComPtr devcon11;
//创建设备和设备上下文对象
D3D11CreateDevice(
nullptr,
D3D_驱动器_类型_硬件,
nullptr,
0,
nullptr,
0,
D3D11_SDK_版本,
&dev11,
nullptr,
&devcon11);
//将指针从DirectX 11版本转换为DirectX 11.1版本
dev11.As&dev;
devcon11.As(&devcon);
//首先,将ID3D11Device1转换为IDXGIDevice1
ComPtr-dxgiDevice;
开发组件(&dxgiDevice);
//其次,使用IDXGIDevice1接口访问适配器
ComPtr-dxgiAdapter;
dxgiDevice->GetAdapter(&dxgiAdapter);
//第三,使用IDXGIAdapter接口访问工厂
ComPtr-dxgiFactory;
dxgiAdapter->GetParent(uu uuidof(IDXGIFactory2),&dxgiFactory);
/*
//设置交换链描述
DXGI_SWAP_CHAIN_DESC1 scd={0};
scd.BufferUsage=DXGI\u USAGE\u RENDER\u TARGET\u OUTPUT;//如何使用交换链
scd.BufferCount=2;//前缓冲区和后缓冲区
scd.Format=DXGI_Format_B8G8R8A8_UNORM;//最常见的交换链格式
scd.SwapEffect=DXGI\u SWAP\u EFFECT\u FLIP\u SEQUENTIAL;//建议的翻转模式
scd.SampleDesc.Count=1;//禁用抗锯齿
CoreWindow^Window=CoreWindow::GetForCurrentThread();//获取窗口指针
hr=dxgiFactory->CreateSwapChainForCoreWindow(
dev.Get(),//设备的地址
重新解释\u cast(窗口),//窗口的地址
&scd,//交换链描述的地址
nullptr,//高级
&交换链);
*/
DXGI_SWAP_CHAIN_DESC1 scd={0};
scd.Width=1000;//错误任意设置。需要清理。由Ravi
scd.高度=800;
scd.Format=DXGI_Format_B8G8R8A8_UNORM;//这是最常见的交换链格式
scd.立体声=假;
scd.SampleDesc.Count=1;//不使用多重采样
scd.SampleDesc.Quality=0;
scd.BufferUsage=DXGI\u USAGE\u RENDER\u TARGET\u输出;
scd.BufferCount=2;
scd.Scaling=DXGI\u Scaling\u STRETCH;
scd.SwapEffect=DXGI\u SWAP\u EFFECT\u FLIP\u SEQUENTIAL;//我们建议对所有应用程序使用此交换效果
scd.Flags=0;
hr=dxgiFactory->CreateSwapChainForComposition(
#pragma once

using namespace Microsoft::WRL;
using namespace DirectX;

class CGame
{


public:
    ComPtr<ID3D11Device1> dev;              // the device interface
    ComPtr<ID3D11DeviceContext1> devcon;    // the device context interface
    ComPtr<IDXGISwapChain1> swapchain;      // the swap chain interface
    ComPtr<ID3D11RenderTargetView> rendertarget;    

    ComPtr<ID3D11Texture2D> Backbuffer;

    UINT32 Initialize();
    void Update();
    UINT32 Render();
    void InitGraphics();

private:
    float ScreenColour;
    UINT ScreenColourI;

    UINT Width, Height;
    UINT32 * TextureData;

};
#include "stdafx.h"
#include <wrl/client.h>
#include <d3d11_1.h>
#include <DirectXMath.h>
#include <windows.ui.xaml.media.dxinterop.h>
#include "Game.h"

DWORD WINAPI renderThread(LPVOID lpParameter);

CGame* pGame;
ComPtr<ISwapChainPanelNative> pSwapChainNative;

UINT32 __stdcall D3DInit(PULONG ptrSwapChainNative)
{
    UINT r;
    pGame = new CGame;
    pSwapChainNative = reinterpret_cast<ISwapChainPanelNative*> (ptrSwapChainNative);
    r = pGame->Initialize();

    return r;
}
UINT32 __stdcall D3DRender(void)
{

    CreateThread(NULL, NULL, renderThread, NULL, 0, NULL);
    return 0;
}


DWORD WINAPI renderThread(LPVOID lpParameter)
{

    for (int i = 0; i < 500; i++)
    {
        //pGame->Update();
        pGame->Render();
    }
    return 1;
}


// this function initializes and prepares Direct3D for use
UINT32 CGame::Initialize()
{
    HRESULT hr;
    // Define temporary pointers to a device and a device context
    ComPtr<ID3D11Device> dev11;
    ComPtr<ID3D11DeviceContext> devcon11;

    // Create the device and device context objects
    D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        0,
        nullptr,
        0,
        D3D11_SDK_VERSION,
        &dev11,
        nullptr,
        &devcon11);

    // Convert the pointers from the DirectX 11 versions to the DirectX 11.1 versions
    dev11.As(&dev);
    devcon11.As(&devcon);


    // First, convert our ID3D11Device1 into an IDXGIDevice1
    ComPtr<IDXGIDevice1> dxgiDevice;
    dev.As(&dxgiDevice);

    // Second, use the IDXGIDevice1 interface to get access to the adapter
    ComPtr<IDXGIAdapter> dxgiAdapter;
    dxgiDevice->GetAdapter(&dxgiAdapter);

    // Third, use the IDXGIAdapter interface to get access to the factory
    ComPtr<IDXGIFactory2> dxgiFactory;
    dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);


    /*
    // set up the swap chain description
    DXGI_SWAP_CHAIN_DESC1 scd = {0};
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    // how the swap chain should be used
    scd.BufferCount = 2;                                  // a front buffer and a back buffer
    scd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;              // the most common swap chain format
    scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;    // the recommended flip mode
    scd.SampleDesc.Count = 1;                             // disable anti-aliasing



    CoreWindow^ Window = CoreWindow::GetForCurrentThread();    // get the window pointer

    hr = dxgiFactory->CreateSwapChainForCoreWindow(
    dev.Get(),                                  // address of the device
    reinterpret_cast<IUnknown*>(Window),        // address of the window
    &scd,                                       // address of the swap chain description
    nullptr,                                    // advanced
    &swapchain);
    */

    DXGI_SWAP_CHAIN_DESC1 scd = { 0 };
    scd.Width = 1000;           //BUG setting it arbitarily. Needs cleanup. by Ravi
    scd.Height = 800;
    scd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;           // this is the most common swapchain format
    scd.Stereo = false;
    scd.SampleDesc.Count = 1;                          // don't use multi-sampling
    scd.SampleDesc.Quality = 0;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scd.BufferCount = 2;
    scd.Scaling = DXGI_SCALING_STRETCH;
    scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // we recommend using this swap effect for all applications
    scd.Flags = 0;


    hr = dxgiFactory->CreateSwapChainForComposition(
        dev.Get(),
        &scd,
        nullptr,                                    // allow on any display 
        &swapchain);


    if (FAILED(hr))
    {
        return  204;
    }

    pSwapChainNative->SetSwapChain(swapchain.Get());


    // get a pointer directly to the back buffer
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), &Backbuffer);


    // create a render target pointing to the back buffer
    dev->CreateRenderTargetView(Backbuffer.Get(), nullptr, &rendertarget);


    // Width and Height
    swapchain->GetDesc1(&scd);
    Width = scd.Width;
    Height = scd.Height;


    TextureData = new UINT32[Width * Height];


    // initialize graphics and the pipeline
    InitGraphics();


    return 0;
}

// this function performs updates to the state of the game
void CGame::Update()
{
}

// this function renders a single frame of 3D graphics
UINT32 CGame::Render()
{
    UINT n, i;
    UINT  * psrc;
/*
    // set our new render target object as the active render target
    devcon->OMSetRenderTargets(1, rendertarget.GetAddressOf(), nullptr);

    ScreenColour += 0.005f;
    if (ScreenColour >0.9f)
        ScreenColour = 0.0f;

    // clear the back buffer to a deep blue
    float color[4] = {ScreenColour, ScreenColour, ScreenColour, 1.0f};
    devcon->ClearRenderTargetView(rendertarget.Get(), color);
*/
    psrc = TextureData;
    i = ScreenColourI & 255;

    for (n=0; n<Width*Height; n++)
    {
        *psrc++ = (i<<16) | (i<<8) | i ;
        i++;
        i &= 255;
    }


    ScreenColourI += 1;

    devcon->UpdateSubresource (Backbuffer.Get(), 0, NULL, TextureData, Width*4, 0);

    // switch the back buffer and the front buffer
    swapchain->Present(1, 0);

    return 0;

}

// this function loads and initializes all graphics data
void CGame::InitGraphics()
{
    ScreenColour = 0.0f;
    ScreenColourI = 0;
}