主にプログラミングに関して。Python, .NET Framework(C#), JavaScript, その他いくらか。
記事にあるサンプルやコードは要検証。使用に際しては責任を負いかねます

スポンサーサイト

                
tags:
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

IronPython: Direct3Dで三角形を表示

                
 SlimDXをとおしてDirect3Dを使い、三角形を表示する。SlimDXにある同タイトルのサンプルコードをIronPythonに置き換える。ついでに背景色を時間経過で変化させてみる。
http://slimdx.org/tutorials/SimpleTriangle.php

import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
clr.AddReference("SlimDX")

from System import Array
from System.Windows.Forms import *
from SlimDX import *
from SlimDX.D3DCompiler import *
from SlimDX.Direct3D11 import *
from SlimDX.DXGI import *
from SlimDX.Windows import *
from SlimDX.Direct3D11 import Device
from SlimDX.Direct3D11 import Resource
from System.Diagnostics import Stopwatch
from System.Threading.Thread import CurrentThread

class IpyD3D11():
def __init__(self):
self.form = RenderForm("Tutorial 3: Triangle")
self.description = SwapChainDescription(BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = self.form.Handle,
IsWindowed = True,
ModeDescription = ModeDescription(0, 0, Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard)

deviceOut = clr.Reference[Device]()
swapChainOut = clr.Reference[SwapChain]()
Device.CreateWithSwapChain(DriverType.Hardware,
DeviceCreationFlags.None,
self.description,
deviceOut,
swapChainOut)
self.device = deviceOut.Value
self.swapChain = swapChainOut.Value

# create a view of our render target, which is the backbuffer of the swap chain we just created
resource = Resource.FromSwapChain[Texture2D](self.swapChain, 0)
self.renderTarget = RenderTargetView(self.device, resource)

# setting a viewport is required if you want to actually see anything
self.context = self.device.ImmediateContext
self.viewport = Viewport(0.0, 0.0, self.form.ClientSize.Width, self.form.ClientSize.Height)
self.context.OutputMerger.SetTargets(self.renderTarget)
self.context.Rasterizer.SetViewports(self.viewport)

# load and compile the vertex shader
bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None)
inputSignature = ShaderSignature.GetInputSignature(bytecode)
vertexShader = VertexShader(self.device, bytecode)

# load and compile the pixel shader
bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None)
pixelShader = PixelShader(self.device, bytecode);

# create test vertex data, making sure to rewind the stream afterward
vertices = DataStream(12 * 3, True, True);
vertices.Write(Vector3(0.0, 0.5, 0.5))
vertices.Write(Vector3(0.5, -0.5, 0.5))
vertices.Write(Vector3(-0.5, -0.5, 0.5))
vertices.Position = 0

# create the vertex layout and buffer
self.elements = Array[InputElement]([InputElement("POSITION", 0, Format.R32G32B32_Float, 0)])
layout = InputLayout(self.device, inputSignature, self.elements);
vertexBuffer = Buffer(self.device,
vertices,
12 * 3,
ResourceUsage.Default,
BindFlags.VertexBuffer,
CpuAccessFlags.None,
ResourceOptionFlags.None,
0)

# configure the Input Assembler portion of the pipeline with the vertex data
self.context.InputAssembler.InputLayout = layout
self.context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList
self.context.InputAssembler.SetVertexBuffers(0, VertexBufferBinding(vertexBuffer, 12, 0))

# set the shaders
self.context.VertexShader.Set(vertexShader)
self.context.PixelShader.Set(pixelShader)

# prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
self.factory = self.swapChain.GetParent[Factory]()
self.factory.SetWindowAssociation(self.form.Handle, WindowAssociationFlags.IgnoreAltEnter)

self.blue = 0
self.i = 0.001

self.sw = Stopwatch()
self.sw.Start()
self.t = 0
self.form.KeyDown += self.KeyEvent
MessagePump.Run(self.form, self.main)

self.renderTarget.Dispose()
self.swapChain.Dispose()
self.device.Dispose()

# handle alt+enter ourselves
def KeyEvent(self, o, e):
if (e.Alt and e.KeyCode == Keys.Enter):
self.swapChain.IsFullScreen = not self.swapChain.IsFullScreen;

def main(self):
# clear the render target to a soothing blue
if self.blue > 1.0:
self.i = -0.001
elif self.blue < 0.0:
self.i = 0.001
self.blue += self.i
self.context.ClearRenderTargetView(self.renderTarget, Color4(0.5, 0.5, self.blue))

# draw the triangle
self.context.Draw(3, 0)
self.swapChain.Present(0, PresentFlags.None)

# diff = self.sw.Elapsed.TotalSeconds - self.t
# self.t = self.sw.Elapsed.TotalSeconds
# print diff
# sleep = int(16)
# CurrentThread.Join(sleep)

a = IpyD3D11()


triangle.fx
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}

float4 PShader(float4 position : SV_POSITION) : SV_Target
{
return float4(1.0f, 1.0f, 0.0f, 1.0f);
}
            

コメントの投稿

非公開コメント

プロフィール

hMatoba

Author:hMatoba
Github

最新記事
リンク
作ったものなど
月別アーカイブ
カテゴリ
タグリスト

検索フォーム
Amazon
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。