Приложение Б - using System, страница 2

ap.changeScreenSize(SCREEN_WIDTH, SCREEN_HEIGHT);

this._processor = new MarkerProcessor(ap, this._raster.getBufferType());

NyARCode[] codes = new NyARCode[2];

codes[0] = new NyARCode(16, 16);

codes[1] = new NyARCode(16, 16);

codes[0].loadARPatt(new StreamReader(AR_CODE_FILE1));

codes[1].loadARPatt(new StreamReader(AR_CODE_FILE2));

this._processor.setARCodeTable(codes,16,80.0);

this._device = PrepareD3dDevice(topLevelForm);

this._device.RenderState.ZBufferEnable = true;

this._device.RenderState.Lighting = false;

this._device.RenderState.CullMode = Cull.CounterClockwise;

Viewport vp = new Viewport();

vp.X = 0;

vp.Y = 0;

vp.Height = ap.getScreenSize().h;

vp.Width = ap.getScreenSize().w;

vp.MaxZ = 1.0f;

this._device.Viewport = vp;

this._text = new TextPanel(this._device, 1);

Matrix tmp = new Matrix();

NyARD3dUtil.toCameraFrustumRH(ap.getPerspectiveProjectionMatrix(), ap.getScreenSize(), 1, 10, 10000, ref tmp);

this._device.Transform.Projection = tmp;

this._device.Transform.View = Matrix.LookAtLH(

new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, 1.0f, 0.0f));

this._surface = new NyARD3dSurface(this._device, SCREEN_WIDTH, SCREEN_HEIGHT);

return true;

}

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

public void MainLoop()

{

lock (this)

{

Surface dest_surface = this._device.GetBackBuffer(0, 0, BackBufferType.Mono);

Rectangle src_dest_rect = new Rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

this._device.StretchRectangle((Surface)this._surface, src_dest_rect, dest_surface, src_dest_rect, TextureFilter.None);

this._device.BeginScene();

this._device.Clear(ClearFlags.ZBuffer, Color.DarkBlue, 1.0f, 0);

if (this._processor.current_id!=-1)

{

long d = DateTime.Now.Ticks;

float r =  (d / 500000) % 360;

Matrix transform_mat2 = Matrix.Scaling(20.0f, 20.0f, 20.0f);

transform_mat2 *= Matrix.RotationX((float)(90.0 * 3.14 / 180.0));

transform_mat2 *= Matrix.Translation(-50f, 0, 50.0f);

transform_mat2 *= Matrix.RotationZ((float)(r * Math.PI / 180));

transform_mat2 *= this._processor.transmat;

this._device.SetTransform(TransformType.World, transform_mat2);

this._text.draw("MarkerId:" + this._processor.current_id, 1);

this._device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);

}

this._device.EndScene();

this._device.Present();

}

return;

}

public void Dispose()

{

lock (this)

{

if (this._surface != null)

{

this._surface.Dispose();

}

if (this._device != null)

{

this._device.Dispose();

}

}

}

}

}

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

using System;

using System.Drawing;

using System.Collections.Generic;

using System.Text;

using jp.nyatla.nyartoolkit.cs.core;

using jp.nyatla.nyartoolkit.cs.markersystem;

using NyARToolkitCSUtils;

using NyARToolkitCSUtils.Direct3d;

using NyARToolkitCSUtils.Capture;

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

namespace SimpleLite

{

class Sketch : D3dSketch

{

private const int SCREEN_WIDTH = 640;

private const int SCREEN_HEIGHT = 480;

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

private NyARD3dMarkerSystem _ms;

private NyARDirectShowCamera _ss;

private NyARD3dRender _rs;

private int mid;

public override void setup(CaptureDevice i_cap)

{

Device d3d=this.size(SCREEN_WIDTH, SCREEN_HEIGHT);

i_cap.PrepareCapture(SCREEN_WIDTH, SCREEN_HEIGHT, 30.0f);

INyARMarkerSystemConfig cf = new NyARMarkerSystemConfig(SCREEN_WIDTH, SCREEN_HEIGHT);

d3d.RenderState.ZBufferEnable = true;

d3d.RenderState.Lighting = false;

d3d.RenderState.CullMode = Cull.CounterClockwise;

this._ms = new NyARD3dMarkerSystem(cf);

this._ss = new NyARDirectShowCamera(i_cap);

this._rs = new NyARD3dRender(d3d, this._ms);

//            this.mid = this._ms.addARMarker(AR_CODE_FILE, 16, 25, 80);

this.mid = this._ms.addARMarker(new Bitmap("../../../../../data/hiro.png"), 16, 25, 80); // you can use PNG style marker too.

//this.mid = this._ms.addNyIdMarker(0, 80);// For Id  marker #0

//set View mmatrix

this._rs.loadARViewMatrix(d3d);

//set Viewport matrix

this._rs.loadARViewPort(d3d);

//setD3dProjectionMatrix

this._rs.loadARProjectionMatrix(d3d);

this._ss.start();

}

public override void loop(Device i_d3d)

{

lock (this._ss)

{

this._ms.update(this._ss);

this._rs.drawBackground(i_d3d, this._ss.getSourceImage());

i_d3d.BeginScene();

i_d3d.Clear(ClearFlags.ZBuffer, Color.DarkBlue, 1.0f, 0);

if (this._ms.isExistMarker(this.mid))

{

//立方体を20mm上(マーカーの上)にずらしておく

Matrix transform_mat2 = Matrix.Translation(0, 0, 20.0f);

//変換行列を掛ける

transform_mat2 *= this._ms.getD3dMarkerMatrix(this.mid);

// 計算したマトリックスで座標変換

i_d3d.SetTransform(TransformType.World, transform_mat2);

// レンダリング(描画)

this._rs.colorCube(i_d3d,40);

}

i_d3d.EndScene();

}

i_d3d.Present();

}

public override void cleanup()

{

this._rs.Dispose();

}

static void Main(string[] args)

{

new Sketch();

}

}

}