Приложение Б - using System

Страницы работы

Содержание работы

Приложение Б

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Threading;

using NyARToolkitCSUtils.Capture;

namespace SingleARMarker

{

static class Program

{

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

CaptureDeviceList capture_device_list = new CaptureDeviceList();

if (capture_device_list.count < 1)

{

MessageBox.Show("Capture Device not detected");

return;

}

int cdevice_number = 0;

using (CameraSelectDialog frm2 = new CameraSelectDialog())

{

frm2.ShowDialog(capture_device_list, out cdevice_number);

}

using (CaptureDevice capture_device = capture_device_list[cdevice_number])            {

using (Form1 frm = new Form1())

using (SimpleLiteD3d sample = new SimpleLiteD3d())

{

if (sample.InitializeApplication(frm, capture_device))

{

frm.Show();

sample.StartCap();         

while (frm.Created)

{          

sample.MainLoop();             

Thread.Sleep(1);               

Application.DoEvents();

}          

sample.StopCap();

}                   

}

}

}

}

}

/************/

using System;

using System.IO;

using System.Collections.Generic;

using System.Diagnostics;

using System.Drawing;

using System.Threading;

using System.Windows.Forms;

using jp.nyatla.nyartoolkit.cs.core;

using jp.nyatla.nyartoolkit.cs.nyidmarker;

using jp.nyatla.nyartoolkit.cs.processor;

using NyARToolkitCSUtils.Capture;

using NyARToolkitCSUtils.Direct3d;

using NyARToolkitCSUtils;

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

namespace SingleARMarker

{

class MarkerProcessor : SingleARMarkerProcesser

{

public Matrix transmat = new Matrix();

public int current_id = -1;

public MarkerProcessor(NyARParam i_cparam, int i_raster_format)

{

initInstance(i_cparam);

return;

}

protected override void onEnterHandler(int i_code)        {

this.current_id = i_code;

}

protected override void onLeaveHandler()        {

this.current_id = -1;

return;

}

protected override void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result)

{

NyARD3dUtil.toD3dCameraView(result,1f, ref this.transmat);

}

}

/************/

public partial class SimpleLiteD3d : IDisposable, CaptureListener

{

private const int SCREEN_WIDTH = 640;

private const int SCREEN_HEIGHT = 480;

private const String AR_CODE_FILE1 = "../../../../../data/patt.hiro";

private const String AR_CODE_FILE2 = "../../../../../data/patt.kanji";

private const String AR_CAMERA_FILE = "../../../../../data/camera_para.dat";

private CaptureDevice _cap;

private MarkerProcessor _processor;

//NyAR

private DsRgbRaster _raster;

private NyARD3dSurface _surface;

/// Direct3D

private Device _device = null;

private TextPanel _text;

private NyARTransMatResult __OnBuffer_nyar_transmat = new NyARTransMatResult();

public void OnBuffer(CaptureDevice i_sender, double i_sample_time, IntPtr i_buffer, int i_buffer_len)

{

int w = i_sender.video_width;

int h = i_sender.video_height;

int s = w * (i_sender.video_bit_count / 8);

lock (this)

{               

this._raster.setBuffer(i_buffer,i_buffer_len, i_sender.video_vertical_flip);               

this._processor.detectMarker(this._raster);               

this._surface.setRaster(this._raster);

}

return;

}

public void StartCap()

{

this._cap.StartCapture();

return;

}

public void StopCap()

{

this._cap.StopCapture();

return;

}

/************/

private Device PrepareD3dDevice(Control i_window)

{

PresentParameters pp = new PresentParameters();

pp.Windowed = true;

pp.SwapEffect = SwapEffect.Flip;

pp.BackBufferFormat = Format.X8R8G8B8;

pp.BackBufferCount = 1;

pp.EnableAutoDepthStencil = true;

pp.AutoDepthStencilFormat = DepthFormat.D16;

CreateFlags fl_base = CreateFlags.FpuPreserve;

try

{

return new Device(0, DeviceType.Hardware, i_window.Handle, fl_base | CreateFlags.HardwareVertexProcessing, pp);

}

catch (Exception ex1)

{

Debug.WriteLine(ex1.ToString());

try

{

return new Device(0, DeviceType.Hardware, i_window.Handle, fl_base | CreateFlags.SoftwareVertexProcessing, pp);

}

catch (Exception ex2)

{

Debug.WriteLine(ex2.ToString());

try

{

return new Device(0, DeviceType.Reference, i_window.Handle, fl_base | CreateFlags.SoftwareVertexProcessing, pp);

}

catch (Exception ex3)

{

throw ex3;

}

}

}

}

/************/

public bool InitializeApplication(Form1 topLevelForm, CaptureDevice i_cap_device)

{

topLevelForm.ClientSize = new Size(SCREEN_WIDTH, SCREEN_HEIGHT);

i_cap_device.SetCaptureListener(this);

i_cap_device.PrepareCapture(SCREEN_WIDTH, SCREEN_HEIGHT, 30);

this._cap = i_cap_device;

this._raster = new DsRgbRaster(i_cap_device.video_width, i_cap_device.video_height,NyARBufferType.BYTE1D_B8G8R8X8_32);

NyARParam ap = new NyARParam();

ap.loadARParam(new StreamReader(AR_CAMERA_FILE));

Похожие материалы

Информация о работе