- UID
- 38919
- 帖子
- 308
- 积分
- 2457
- 阅读权限
- 50
- 注册时间
- 2006-12-20
- 最后登录
- 2011-2-20
- 在线时间
- 459 小时
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Collections;
namespace VisualPoint
{
public partial class MainForm : Form
{
private Device device = null;
private ArrayList ObstacleList;
private Camera camera;
//private bool pause=false;
public MainForm()
{
InitializeComponent();
InitializeGraphics();
}
public bool InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
//Now let's setup our D3D stuff
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
//LoadDefaultOptions();
// Create the D3DDevice
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
OnResetDevice(device, null);
return true;
}
catch (DirectXException)
{
return false;
}
}
private void OnResetDevice(object sender, EventArgs e)
{
System.IO.Directory.SetCurrentDirectory(Application.StartupPath + @"\..\..\");
Device dev = (Device)sender;
// Turn on the zbuffer
device.RenderState.ZBufferEnable = true;
//device.RenderState.Ambient = System.Drawing.Color.White;
ObstacleList = new ArrayList();
camera = new Camera();
//SetupMatrices();
//SetupLights();
//LoadDefaultOptions();
}
private void SetupMatrices()
{
//device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 1000.0f);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 1.0f, 1.0f, 100.0f);
device.Transform.View = camera.GetViewMatrix();
}
private void SetupLights()
{
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = System.Drawing.Color.White;
device.Lights[0].Direction = new Vector3((float)Math.Cos(Environment.TickCount / 1000.0f), 1.0f, (float)Math.Sin(Environment.TickCount / 1000.0f));
device.Lights[0].Direction = new Vector3(1f, 1f, 1f);
device.Lights[0].Enabled = true;//turn it on
}
protected override void OnPaint(PaintEventArgs e)
{
this.Render();
}
private void Render()//绘制场景
{
//OnFrameUpdate();
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
//Begin the scene
device.BeginScene();
SetupMatrices();
SetupLights();
foreach (PointObject obj in ObstacleList)
{
obj.Draw(this.device);
}
//End the scene
device.EndScene();
device.Present();
}
private void LoadDefaultOptions()
{
}
private void creatToolStripMenuItem_Click_1(object sender, EventArgs e)
{
ObstacleList.Add(new PointObject());
}
}
}
显示是没有问题,但是移动窗口几下就报错在 device.Present(); 在移动窗口的过程中,内存占用不断增大。为什么?有人能帮忙回答下么?
[ 本帖最后由 suisui2006 于 2007-12-16 23:38 编辑 ] |
|