關於如何在DirecX中爲Windows 8開發人員預覽繪製一個簡單的三角形,有沒有什麼好的教程?遊戲示例太複雜,無法理解新DirectX版本的「基本」操作(這與DirectX 9完全不同)。適用於Windows 8的任何優秀的簡單DirectX示例(開發人員預覽版)?
2
A
回答
6
該示例的要點是Windows 8,而不是DirectX。您需要在使用Windows 8之前在之前學習DirectX11 。您正在嘗試閱讀兩種技術之間的互操作示例,這兩種技術都不是您理解的。 Windows 8 Dev Preview的重點不在於教DirectX。
1
想通了。見下面的代碼。只有不起作用的是紋理(它顯示爲白色而不是顯示圖像,但至少顯示三角形!)(這個代碼顯然不是整個程序,原來它涉及到很多複雜代碼只是繪製一個三角形這些天!)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ManagedDirectX;
using Windows.Storage;
using Windows.Storage.Streams;
namespace TestProgram
{
public sealed class EntryPoint
{
public void NtfyExecutionAbrt()
{
}
public EntryPoint()
{
beginexecblock();
}
void onrenderframe()
{
if(vertcount>0) {
maincontext.Draw(vertcount);
}
}
int vertcount = 0;
Shader defaultshader;
RenderContext maincontext;
async void beginexecblock()
{
if ((await Windows.Storage.ApplicationData.Current.RoamingFolder.GetFilesAsync()).Count == 0)
{
await ApplicationData.Current.RoamingFolder.CreateFileAsync("testfile.txt");
ApplicationData.Current.SignalDataChanged();
Windows.UI.Popups.MessageDialog tdlg = new Windows.UI.Popups.MessageDialog("Roaming file creation success", "Sync status");
await tdlg.ShowAsync();
}
try
{
DateTime started = DateTime.Now;
RenderContext mtext = new RenderContext();
maincontext = mtext;
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync("DXInteropLib\\VertexShader.cso");
var stream = (await file.OpenAsync(FileAccessMode.Read));
Windows.Storage.Streams.DataReader mreader = new Windows.Storage.Streams.DataReader(stream.GetInputStreamAt(0));
byte[] dgram = new byte[file.Size];
await mreader.LoadAsync((uint)dgram.Length);
mreader.ReadBytes(dgram);
file = await folder.GetFileAsync("DXInteropLib\\PixelShader.cso");
stream = await file.OpenAsync(FileAccessMode.Read);
mreader = new Windows.Storage.Streams.DataReader(stream.GetInputStreamAt(0));
byte[] mgram = new byte[file.Size];
await mreader.LoadAsync((uint)file.Size);
mreader.ReadBytes(mgram);
try
{
defaultshader = mtext.CreateShader(dgram, mgram);
mtext.InitializeLayout(dgram);
defaultshader.Apply();
mtext.OnRenderFrame += onrenderframe;
}
catch (Exception er)
{
Windows.UI.Popups.MessageDialog mdlg = new Windows.UI.Popups.MessageDialog(er.ToString(),"Fatal error");
mdlg.ShowAsync().Start();
}
IStorageFile[] files = (await folder.GetFilesAsync()).ToArray();
bool founddata = false;
foreach (IStorageFile et in files)
{
if (et.FileName.Contains("rawimage.dat"))
{
stream = await et.OpenAsync(FileAccessMode.Read);
founddata = true;
}
}
int width;
int height;
byte[] rawdata;
if (!founddata)
{
file = await folder.GetFileAsync("TestProgram\\test.png");
stream = await file.OpenAsync(FileAccessMode.Read);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
var pixeldata = await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Rgba8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, new Windows.Graphics.Imaging.BitmapTransform(), Windows.Graphics.Imaging.ExifOrientationMode.IgnoreExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.DoNotColorManage);
width = (int)decoder.PixelWidth;
height = (int)decoder.PixelHeight;
rawdata = pixeldata.DetachPixelData();
file = await folder.CreateFileAsync("rawimage.dat");
stream = (await file.OpenAsync(FileAccessMode.ReadWrite));
var realstream = stream.GetOutputStreamAt(0);
DataWriter mwriter = new DataWriter(realstream);
mwriter.WriteInt32(width);
mwriter.WriteInt32(height);
mwriter.WriteBytes(rawdata);
await mwriter.StoreAsync();
await realstream.FlushAsync();
}
else
{
DataReader treader = new DataReader(stream.GetInputStreamAt(0));
await treader.LoadAsync((uint)stream.Size);
rawdata = new byte[stream.Size-(sizeof(int)*2)];
width = treader.ReadInt32();
height = treader.ReadInt32();
treader.ReadBytes(rawdata);
}
Texture2D mtex = maincontext.createTexture2D(rawdata, width, height);
List<VertexPositionNormalTexture> triangle = new List<VertexPositionNormalTexture>();
triangle.Add(new VertexPositionNormalTexture(new Vector3(-.5f,-.5f,0),new Vector3(1,1,1),new Vector2(0,0)));
triangle.Add(new VertexPositionNormalTexture(new Vector3(0,0.5f,0),new Vector3(1,1,1),new Vector2(1,0)));
triangle.Add(new VertexPositionNormalTexture(new Vector3(.5f,-0.5f,0),new Vector3(1,1,1),new Vector2(1,1)));
byte[] gpudata = VertexPositionNormalTexture.Serialize(triangle.ToArray());
VertexBuffer mbuffer = maincontext.createVertexBuffer(gpudata,VertexPositionNormalTexture.Size);
mbuffer.Apply(VertexPositionNormalTexture.Size);
vertcount = 3;
Windows.UI.Popups.MessageDialog tdlg = new Windows.UI.Popups.MessageDialog("Unit tests successfully completed\nShader creation: Success\nTexture load: Success\nVertex buffer creation: Success\nTime:"+(DateTime.Now-started).ToString(), "Results");
tdlg.ShowAsync().Start();
}
catch (Exception er)
{
Windows.UI.Popups.MessageDialog tdlg = new Windows.UI.Popups.MessageDialog(er.ToString(), "Fatal error");
tdlg.ShowAsync().Start();
}
}
}
}
相關問題
- 1. 在Windows 8上的WPF ribbonwindow桌面開發人員預覽
- 2. Microsoft.Data.Entity.Design版本= 10.0.0.0適用於Visual Studio開發人員預覽版11
- 3. Windows 8預覽示例
- 4. Windows 8開發人員ID
- 5. Windows開發人員預覽C#版本 - 功能缺失?
- 6. openCV僅適用於C開發人員嗎?...適用於VB開發人員的任何openCV?
- 7. 適用於.NET開發人員的本地Windows開發平臺
- 8. 如何在Windows 10開發人員預覽中啓用Bash?
- 9. 適用於C#開發人員的Java
- 10. Windows 8 Consumer Preview中的套接字BUG + Visual Studio 11開發人員預覽
- 11. WPF功能區在Visual Studio 11開發人員預覽版中?
- 12. WebGL的不Chrome會在Windows 8開發者預覽版
- 13. 適用於獨立開發人員的Visual Studio 2010版本
- 14. PC上的谷歌瀏覽器(適用於開發人員)
- 15. Facebook開發人員時間線預覽
- 16. 適用於iOS的任何優秀的崩潰報告服務
- 17. 適用於Android的任何優秀的社區翻譯工具?
- 18. 開發人員的排版
- 19. 錯誤安裝Visual Studio 11開發人員預覽版
- 20. 適用於Java EE開發人員的Java ME提示
- 21. 卸載Xcode 4.5開發人員預覽版
- 22. 適用於iOS開發人員的Android開發
- 23. 適用於視覺工作室開發人員的任何示例Cordova應用程序
- 24. 適用於Windows 8的SAPI版本
- 25. VS2012/Windows 8中的F#3.1預覽版
- 26. 針對瀏覽器開發的Windows Phone開發人員
- 27. 我應該學會如何成爲一名優秀的Web開發人員?
- 28. Alpha不適用於此簡單示例
- 29. Xcode 4.3開發人員預覽版2:與舊版iOS SDK兼容
他們完全改變DirectX,雖然在Windows 8中。它不完全相同以前的版本;所以DirectX是Windows 8的一部分。他們在DirectX上似乎也沒有很好的文檔(DirectX for Windows 8與DirectX for Windows 7不同) – IDWMaster
@IDWMaster我從來沒有聽說過任何DirectX 12。 Windows 8的DirectX 11.1應該與Windows 7中已經可以使用的DirectX 11不同。當然,它與DirectX 9不同,但這不是您可以在Windows Vista(DX10)中使用的最後一個版本,或者7(DX11)。 –
以爲DX11是Windows 8專用的,但事實證明它與現有的Windows 7 DirectX 11非常相似。 – IDWMaster