tom

tom

  • NA
  • 1
  • 0

directx c# fixed camera location

Jun 15 2007 12:08 PM
Hello,

I am trying to write and copy paste a very limited flight sim together. I am allmost where I want to be except for one thing ; a fixed camera. I can speed up and down with my airplane, steer in all directions, put the camera right behind my airplane (third person) and inside the plane (1st person). But now I want to be able to put it in a fixed location so I can fly away from it and towards it. If anyone could help me, I would grately appreciate it. Here is the code I have so far :

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

using D3D = Microsoft.DirectX.Direct3D;

using Microsoft.DirectX.DirectInput;

using DI = Microsoft.DirectX.DirectInput;

using Microsoft.DirectX.DirectSound;

using DS = Microsoft.DirectX.DirectSound;

using Microsoft.DirectX.AudioVideoPlayback;

namespace FlightSim

{

public class WinForm : System.Windows.Forms.Form

{

private int[,] int_Floorplan;

private int WIDTH;

private int HEIGHT;

private float gamespeed = 0f;

private System.ComponentModel.Container components = null;

private D3D.Device device;

private Texture scenerytexture;

private Material material;

private CustomVertex.PositionNormalTextured[] verticesarray;

ArrayList verticeslist = new ArrayList();

private Mesh spacemesh;

private Material[] spacemeshmaterials;

private Texture[] spacemeshtextures;

private float spacemeshradius;

private float scaling = 0.03f;

private Vector3 spacemeshposition = new Vector3(8, 2, 0.03f);

private Vector3 spacemeshangles = new Vector3(0, 0, 0);

private DI.Device keyb;

private double score = 0;

private Mesh skyboxmesh;

private Material[] skyboxmaterials;

private Texture[] skyboxtextures;

private DS.Device sounddevice;

private ArrayList soundlist = new ArrayList();

private SecondaryBuffer explosionsound;

private Buffer3D explosion3D;

private Listener3D listener;

private DS.Buffer primary;

private Audio backmusic;

private double lastmusicposition;

private D3D.Font text;

 

 

public WinForm()

{

InitializeComponent();

this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

}

public void InitializeDevice()

{

PresentParameters presentParams = new PresentParameters();

presentParams.BackBufferWidth = 800;

presentParams.BackBufferHeight = 600;

presentParams.BackBufferFormat = Format.R5G6B5;

presentParams.Windowed = false;

presentParams.SwapEffect = SwapEffect.Discard;

presentParams.AutoDepthStencilFormat = DepthFormat.D16;

presentParams.EnableAutoDepthStencil = true;

device = new D3D.Device(0, D3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

device.RenderState.Lighting = true;

device.Lights[0].Type = LightType.Directional;

device.Lights[0].Diffuse = Color.White;

device.Lights[0].Direction = new Vector3(1, 1, -1);

device.Lights[0].Update();

device.Lights[0].Enabled = true;

device.Lights[1].Type = LightType.Directional;

device.Lights[1].Diffuse = Color.White;

device.Lights[1].Direction = new Vector3(-1, -1, -1);

device.Lights[1].Update();

device.Lights[1].Enabled = true;

device.SamplerState[0].MinFilter = TextureFilter.Anisotropic;

device.SamplerState[0].MagFilter = TextureFilter.Anisotropic;

device.SamplerState[0].AddressU = TextureAddress.Mirror;

device.SamplerState[0].AddressV = TextureAddress.Mirror;

device.RenderState.PointSpriteEnable = true;

device.RenderState.PointScaleEnable = true;

device.RenderState.PointScaleA = 0f;

device.RenderState.PointScaleB = 0f;

device.RenderState.PointScaleC = 100f;

device.RenderState.SourceBlend = Blend.One;

device.RenderState.DestinationBlend = Blend.One;

}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

{

ReadUserInput(gamespeed);

UpdatePosition(ref spacemeshposition, spacemeshangles, gamespeed);

UpdateListenerPosition();

if (CheckCollision(spacemeshposition, spacemeshradius) > 0)

{

spacemeshposition = new Vector3(8, 2, 0.03f);

spacemeshangles = new Vector3(0, 0, 0);

gamespeed = 0;

score /= 2;

}

device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);

device.BeginScene();

text.DrawText(null, string.Format("speed : {0}", this.gamespeed * 10000), new Point(10, 20), Color.White);

device.RenderState.Ambient = Color.DarkGray;

device.Transform.World = Matrix.Translation(-(float)spacemeshposition.X, -(float)spacemeshposition.Y, -(float)spacemeshposition.Z) * Matrix.RotationYawPitchRoll((float)spacemeshangles.X, (float)spacemeshangles.Y, (float)spacemeshangles.Z);

device.VertexFormat = CustomVertex.PositionNormalTextured.Format;

device.Material = material;

device.SetTexture(0, scenerytexture);

device.DrawUserPrimitives(PrimitiveType.TriangleList, verticeslist.Count / 3, verticesarray);

device.Transform.World = Matrix.Scaling(scaling, scaling, scaling) * Matrix.RotationX((float)Math.PI / 2);

DrawMesh(spacemesh, spacemeshmaterials, spacemeshtextures);

device.RenderState.Ambient = Color.White;

device.Transform.World = Matrix.RotationX((float)Math.PI / 2) * Matrix.RotationYawPitchRoll((float)spacemeshangles.X, (float)spacemeshangles.Y, (float)spacemeshangles.Z);

DrawMesh(skyboxmesh, skyboxmaterials, skyboxtextures);

device.EndScene();

device.Present();

this.Invalidate();

SoundAffairs();

}

private void SetUpCamera()

{

device.Transform.Projection =

Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 0.003f, 500f);

device.Transform.View = Matrix.LookAtLH(new Vector3(0, -1f, 0.2f), new Vector3(0, 0, 0), new Vector3(0, 0, 1));

}

private void CockpitView()

{

device.Transform.Projection =

Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 0.003f, 500f);

device.Transform.View =

Matrix.LookAtLH(new Vector3(0, 0.06199063f, 0.02000003f), new Vector3(0, 2, 0), new Vector3(0, 0, 1));

}

private void RearView()

{

device.Transform.Projection =

Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 0.003f, 500f);

device.Transform.View =

Matrix.LookAtLH(new Vector3(0, -1f, 0.2f), new Vector3(0, 0, 0), new Vector3(0, 0, 1));

}

private void LoadTexturesAndMaterials()

{

material = new Material();

material.Diffuse = Color.White;

material.Ambient = Color.White;

scenerytexture = TextureLoader.FromFile(device, "texturemap03.jpg");

}

private void LoadFloorplan()

{

WIDTH = 20;

HEIGHT = 15;

int_Floorplan = new int[,]

{

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},

{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

};

}

private void LoadMesh(string filename, ref Mesh mesh, ref Material[] meshmaterials, ref Texture[] meshtextures, ref float meshradius)

{

ExtendedMaterial[] materialarray;

mesh = Mesh.FromFile(filename, MeshFlags.Managed, device, out materialarray);

if ((materialarray != null) && (materialarray.Length > 0))

{

meshmaterials = new Material[materialarray.Length];

meshtextures = new Texture[materialarray.Length];

for (int i = 0; i < materialarray.Length; i++)

{

meshmaterials[i] = materialarray[i].Material3D;

meshmaterials[i].Ambient = meshmaterials[i].Diffuse;

if ((materialarray[i].TextureFilename != null) && (materialarray[i].TextureFilename != string.Empty))

{

meshtextures[i] = TextureLoader.FromFile(device, materialarray[i].TextureFilename);

}

}

}

mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, device);

mesh.ComputeNormals();

VertexBuffer vertices = mesh.VertexBuffer;

GraphicsStream stream = vertices.Lock(0, 0, LockFlags.None);

Vector3 meshcenter;

meshradius = Geometry.ComputeBoundingSphere(stream, mesh.NumberVertices, mesh.VertexFormat, out meshcenter) * scaling;

}

private void LoadMeshes()

{

float dummy = 0;

LoadMesh("airplane 2.x", ref spacemesh, ref spacemeshmaterials, ref spacemeshtextures, ref spacemeshradius);

LoadMesh("skybox2.x", ref skyboxmesh, ref skyboxmaterials, ref skyboxtextures, ref dummy);

}

private void DrawMesh(Mesh mesh, Material[] meshmaterials, Texture[] meshtextures)

{

for (int i = 0; i < meshmaterials.Length; i++)

{

device.Material = meshmaterials[i];

device.SetTexture(0, meshtextures[i]);

mesh.DrawSubset(i);

}

}

private void UpdatePosition(ref Vector3 position, Vector3 angles, float speed)

{

Vector3 addvector = new Vector3();

addvector.X += (float)(Math.Sin(angles.Z));

addvector.Y += (float)(Math.Cos(angles.Z));

addvector.Z -= (float)(Math.Tan(angles.Y));

addvector.Normalize();

position += addvector * speed;

}

private void InitializeInputDevices()

{

keyb = new DI.Device(SystemGuid.Keyboard);

keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);

keyb.Acquire();

}

private void ReadUserInput(float speed)

{

KeyboardState keys = keyb.GetCurrentKeyboardState();

if (keys[Key.F5])

{

this.CockpitView();

}

if (keys[Key.F6])

{

this.RearView();

}

if (keys[Key.Add])

{

if (gamespeed < 0.03f)

{

gamespeed = gamespeed + 0.001f;

}

}

if (keys[Key.Subtract])

{

if (gamespeed >= 0.001f)

{

gamespeed = gamespeed - 0.001f;

}

}

if (keys[Key.Right])

{

spacemeshangles.X += 2.5f * speed;

}

if (keys[Key.Left])

{

spacemeshangles.X -= 2.5f * speed;

}

if (keys[Key.Down])

{

spacemeshangles.Z -= (float)(speed * Math.Sin(spacemeshangles.X));

spacemeshangles.Y -= (float)(speed * Math.Cos(spacemeshangles.X));

}

if (keys[Key.Up])

{

spacemeshangles.Z += (float)(speed * Math.Sin(spacemeshangles.X));

spacemeshangles.Y += (float)(speed * Math.Cos(spacemeshangles.X));

}

}

private int CheckCollision(Vector3 position, float radius)

{

if (position.Z < 0.02f) return 1;

return 0;

}

private void SoundAffairs()

{

if (soundlist.Count > 0)

{

for (int i = soundlist.Count - 1; i > -1; i--)

{

SecondaryBuffer currentsound = (SecondaryBuffer)soundlist[i];

if (!currentsound.Status.Playing)

{

currentsound.Dispose();

soundlist.RemoveAt(i);

}

}

}

if (backmusic.CurrentPosition == lastmusicposition)

{

backmusic.SeekCurrentPosition(0, SeekPositionFlags.AbsolutePositioning);

}

lastmusicposition = backmusic.CurrentPosition;

}

private void InitializeSound()

{

sounddevice = new DS.Device();

sounddevice.SetCooperativeLevel(this, CooperativeLevel.Normal);

BufferDescription description = new BufferDescription();

description.ControlEffects = false;

BufferDescription description3D = new BufferDescription();

description3D.ControlEffects = false;

description3D.Control3D = true;

explosionsound = new SecondaryBuffer("explosion.wav", description3D, sounddevice);

explosion3D = new Buffer3D(explosionsound);

BufferDescription descriptionlistener = new BufferDescription();

descriptionlistener.ControlEffects = false;

descriptionlistener.Control3D = true;

descriptionlistener.PrimaryBuffer = true;

primary = new DS.Buffer(descriptionlistener, sounddevice);

listener = new Listener3D(primary);

backmusic = new Audio("music.mp3");

backmusic.Play();

}

private void UpdateListenerPosition()

{

listener.Position = spacemeshposition;

Listener3DOrientation orientation = new Listener3DOrientation();

orientation.Front = new Vector3((float)Math.Sin(spacemeshangles.Z), (float)Math.Cos(spacemeshangles.Z), (float)Math.Sin(spacemeshangles.Y));

orientation.Top = new Vector3((float)Math.Sin(spacemeshangles.Z), (float)Math.Sin(spacemeshangles.X), (float)Math.Cos(spacemeshangles.X));

listener.Orientation = orientation;

}

private void InitializeFont()

{

System.Drawing.Font systemfont = new System.Drawing.Font("Arial", 12f, FontStyle.Regular);

text = new D3D.Font(device, systemfont);

}

private void VertexDeclaration()

{

float imagesintexture = 2;

for (int x = 0; x < WIDTH; x++)

{

for (int y = 0; y < HEIGHT; y++)

{

if (int_Floorplan[x, y] == 0)

{

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 1f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0), new Vector3(0, 0, 1), 0f, 1f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, 0), new Vector3(0, 0, 1), 0f, 0f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, 0), new Vector3(0, 0, 1), 0f, 0f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 0f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 1f));

}

if (int_Floorplan[x, y] == 1)

{

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0), new Vector3(0, 0, 1), 2f / imagesintexture, 1f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 1f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 0f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 0f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0), new Vector3(0, 0, 1), 2f / imagesintexture, 0f));

verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0), new Vector3(0, 0, 1), 2f / imagesintexture, 1f));

}

}

}

verticesarray = (CustomVertex.PositionNormalTextured[])verticeslist.ToArray(typeof(CustomVertex.PositionNormalTextured));

}

protected override void Dispose(bool disposing)

{

if (disposing)

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.Size = new System.Drawing.Size(800, 600);

this.Text = "programmeren05 - praktische proef";

}

static void Main()

{

using (WinForm our_directx_form = new WinForm())

{

our_directx_form.InitializeDevice();

our_directx_form.SetUpCamera();

our_directx_form.LoadFloorplan();

our_directx_form.VertexDeclaration();

our_directx_form.LoadTexturesAndMaterials();

our_directx_form.LoadMeshes();

our_directx_form.InitializeInputDevices();

our_directx_form.InitializeSound();

our_directx_form.InitializeFont();

Application.Run(our_directx_form);

}

}

}

}