/********************************************************/
/*★ マウスでティーポットを操作(fx を使う) 前田 稔 ★*/
/********************************************************/
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.Samples.DirectX.UtilityToolkit;
namespace EmptyProjectSample
{
public class EmptyProject : IFrameworkCallback, IDeviceCreation
{
// Variables
private Framework sampleFramework = null;
private Mesh mesh = null;
private Effect effect = null;
private ModelViewerCamera camera = new ModelViewerCamera();
public EmptyProject(Framework f)
{ sampleFramework = f; }
public bool IsDeviceAcceptable(Caps caps, Format adapterFormat, Format backBufferFormat, bool windowed)
{
if (!Manager.CheckDeviceFormat(caps.AdapterOrdinal, caps.DeviceType, adapterFormat,
Usage.QueryPostPixelShaderBlending, ResourceType.Textures, backBufferFormat))
return false;
return true;
}
public void ModifyDeviceSettings(DeviceSettings settings, Caps caps)
{
if ( (!caps.DeviceCaps.SupportsHardwareTransformAndLight) ||
(caps.VertexShaderVersion < new Version(1,1)) )
{ settings.BehaviorFlags = CreateFlags.SoftwareVertexProcessing; }
else
{ settings.BehaviorFlags = CreateFlags.HardwareVertexProcessing; }
if ( (caps.DeviceCaps.SupportsPureDevice) &&
((settings.BehaviorFlags & CreateFlags.HardwareVertexProcessing) != 0 ) )
settings.BehaviorFlags |= CreateFlags.PureDevice;
if (settings.DeviceType == DeviceType.Reference)
{
Utility.DisplaySwitchingToRefWarning(sampleFramework, "EmptyProject");
}
}
private void OnCreateDevice(object sender, DeviceEventArgs e)
{
ShaderFlags shaderFlags = ShaderFlags.NotCloneable;
// Read the D3DX effect file
string path = Utility.FindMediaFile("EmptyProject.fx");
effect = ResourceCache.GetGlobalInstance().CreateEffectFromFile(e.Device,
path, null, null, shaderFlags, null);
//ティーポットの生成
mesh = Mesh.Teapot(e.Device);
}
private void OnResetDevice(object sender, DeviceEventArgs e)
{
SurfaceDescription desc = e.BackBufferDescription;
// Setup the camera's projection parameters
float aspectRatio = (float)desc.Width / (float)desc.Height;
camera.SetViewParameters(new Vector3(0.0f, 2.0f, -7.0f), Vector3.Empty);
camera.SetProjectionParameters((float)Math.PI / 4, aspectRatio, 1.0f, 500.0f);
camera.SetWindow(desc.Width, desc.Height);
}
private void OnDestroyDevice(object sender, EventArgs e)
{
}
public void OnFrameMove(Device device, double appTime, float elapsedTime)
{
// Update the camera's position based on user input
camera.FrameMove(elapsedTime);
}
public IntPtr OnMsgProc(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam, ref bool noFurtherProcessing)
{
// Pass all remaining windows messages to camera so it can respond to user input
camera.HandleMessages(hWnd, msg, wParam, lParam);
return IntPtr.Zero;
}
public void OnFrameRender(Device device, double appTime, float elapsedTime)
{
bool beginSceneCalled = false;
// Clear the render target and the zbuffer
device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00424B79, 1.0f, 0);
try
{
device.BeginScene();
beginSceneCalled = true;
effect.SetValue("g_mWorldViewProjection", camera.WorldMatrix * camera.ViewMatrix * camera.ProjectionMatrix);
effect.SetValue("g_mWorld", camera.WorldMatrix);
effect.SetValue("g_fTime", (float)appTime);
int passes = effect.Begin(0);
for(int pass = 0; pass < passes; pass++)
{
effect.BeginPass(pass);
mesh.DrawSubset(0);
effect.EndPass();
}
effect.End();
}
finally
{
if (beginSceneCalled) device.EndScene();
}
}
static int Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
using(Framework sampleFramework = new Framework())
{
EmptyProject sample = new EmptyProject(sampleFramework);
sampleFramework.Disposing += new EventHandler(sample.OnDestroyDevice);
sampleFramework.DeviceCreated += new DeviceEventHandler(sample.OnCreateDevice);
sampleFramework.DeviceReset += new DeviceEventHandler(sample.OnResetDevice);
sampleFramework.SetWndProcCallback(new WndProcCallback(sample.OnMsgProc));
sampleFramework.SetCallbackInterface(sample);
try
{
// Show the cursor and clip it when in full screen
sampleFramework.SetCursorSettings(true, true);
sampleFramework.Initialize( true, true, true );
sampleFramework.CreateWindow("EmptyProject");
sampleFramework.CreateDevice( 0, true, Framework.DefaultSizeWidth,
Framework.DefaultSizeHeight, sample);
sampleFramework.MainLoop();
}
catch
{
// Ignore any exceptions here, they would have been handled by other areas
return (sampleFramework.ExitCode == 0) ? 1 : sampleFramework.ExitCode;
}
return sampleFramework.ExitCode;
}
}
}
}
|