Mauskoordinaten ind 3d

  • #1
G

Goso

Guest
Ich brauche die richtige Mauskoordinaten in meine
directx3dfenster

sodass ich auf die meshboxen zugreifen kann,

wie geht das ?

/************************************************************************************************
* Das Programm soll einen Würfel von 5 * 5 * 5 Würfelelemente in 3D darstellen *
* Deshalb wurde es in Directx erstellt. Um die Würfelelemente zu erstellen, *
* wurde das Mesh MeshBox benutzt. *
* *
* Wenn die Maus auf ein Würfellement der aktiven ebene Kommt, wird ein geladener *
* Text angezeigt. *
* *
* Wird auf ein Würfelelement mit der Maus geklickt, wird ein Frage-Antwortfenster geöffnet. *
************************************************************************************************/

// Benutzte Verweise
using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
using System.Threading;

// Namspace für den Würfel
namespace Wuerfel
{
// Klasse für den Würfel
public class Form1 : System.Windows.Forms.Form
{

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static Microsoft.DirectX.Direct3D.Device device = null;
private Vector3 v3CurrMeshPos = new Vector3();
static float xAngle,yAngle,zAngle;
PresentParameters presentParams;
private int px,py,pz,dx,dy,dz;
delegate void UIDelegate();
static int i = 0;
private Point mouseStart;
private Matrix world;
private Vector3 v3MouseStartPos;// the mouse position as we press down the mouse button
private Vector3 v3MouseNewPos; // the new mouse position while dragging the mesh


// Deklaration der Würfelmatrix
static Mesh[] meshBox = new Mesh[126];
static Matrix[] mBox = new Matrix[126];
//============================================================================================

static void Main()
{
Application.Run(new Form1());

}
//============================================================================================

// Hier wird durch eine Schleife der Würfel mit den einzelnen Würfelelementen gezeichnet
public static void mBoxebene()
{
// Variable für die Y-Koordinate
float e = -1.6f;
// Variable für die Z-Koordinate
float f = 0.0f;
// Laufvariablen für die Schleifen
i=0;
int l=0;

// Die äußere Schleife ist zuständig für den gesammten würfel
while(i<125)
{
// Die innere Schleife zeichnet jeweils eine Ebene des Würfels
while(l<25)
{
// Aufruf zum zeichen des Würfelelements
mBox=Matrix.Translation( new Vector3(-1.6f,e,f));
i++;
mBox=Matrix.Translation( new Vector3(-0.8f,e,f));
i++;
mBox=Matrix.Translation( new Vector3( 0.0f,e,f));
i++;
mBox=Matrix.Translation( new Vector3( 0.8f,e,f));
i++;
mBox=Matrix.Translation( new Vector3( 1.6f,e,f));
// Schrittweite für den Abstand in Y-Richtung
e = e + 0.8f;
i++;
// Springen zur nächsten Reihe
l=l+5;
}
// Variablen werden wieder auf Standartwert gesetzt.
l=0;
e = -1.6f;
// die Variable für die Z-Koordinate wird um 1,5 erhöht
f=f+1.5f;
}
}
//============================================================================================

// Zeichnen des Fensters
public Form1()
{
// Fenstertext
Text = KDE Würfel ;
// Aufruf für den Timer
myTimer.Tick += new EventHandler( OnTimer );
myTimer.Interval = 2;
//Fenstergröße
ClientSize = new Size( 800, 600 );
// Aufruf der Mausfunktionen
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);


}
//============================================================================================

protected override void OnResize( System.EventArgs e )
{
myTimer.Stop();
try
{
i=0;
presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;

if ( device != null ) device.Dispose();
device = new Microsoft.DirectX.Direct3D.Device( 0, Microsoft.DirectX.Direct3D.DeviceType.Hardware,
this,CreateFlags.SoftwareVertexProcessing, presentParams );

while(i<125)
{
if ( meshBox != null ) meshBox.Dispose();
i++;
}
//Die Form vom Würfel
i=0;
while(i<=125)
{
meshBox = Mesh.Box ( device, 0.5f, 0.5f, 0.5f );
i++;
}

// Optic des Würfels
Material myMaterial = new Material();
myMaterial.Diffuse = myMaterial.Ambient = Color.Gold;
device.Material = myMaterial;

//Lichtverhältnisse des Würfels
device.Lights[0].Type = LightType.Spot;
device.Lights[0].Diffuse = Color.Green;
device.Lights[0].Direction = new Vector3( 0, 0, 7 );
device.Lights[0].Enabled = true;

// Einstellug des Blickwinkels
device.Transform.View = Matrix.LookAtLH(
new Vector3( 0f, 0f, -6.0f ), //Augenpunkt 5.0 vor dem canvas
new Vector3( 0f, 0f, -1f ), //Kammera sieht auf 0,0,0
new Vector3( 0f, 1.0f, 0f ) ); //world's up direction is the y-axis
device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI/4, 1f, 1f, 100f );
device.RenderState.CullMode = Cull.None;
device.RenderState.Lighting = true;
xAngle = yAngle = zAngle = 1;
myTimer.Start();
}
catch (DirectXException) { MessageBox.Show(Could not initialize Direct3D. ); return; }
}
//============================================================================================

protected static void OnTimer( Object myObject, EventArgs myEventArgs )
{
mBoxebene();

if (device == null) return;
device.Clear( ClearFlags.Target | ClearFlags.ZBuffer, Color.Honeydew, 5f, 0 );
//In alle drei Richtungen rotieren
Matrix m = Matrix.RotationYawPitchRoll( xAngle += 0.05f, yAngle += 0.05f, zAngle += 0.05f );
//Würfel zeichnen
device.BeginScene();
i=0;
// Zeichnen des Würfels
while (i<125)
{
device.Transform.World = m * mBox; meshBox.DrawSubset( 0 );
i++;
}
device.EndScene();
device.Present();
}
//============================================================================================
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Vector3 v = new Vector3(e.X, e.Y, 0);
v.Unproject(device.Viewport, device.Transform.Projection,
device.Transform.View, device.Transform.World);
if ((e.Button == MouseButtons.Left))
{
MessageBox.Show((mouse) X= + e.X+(mouse) Y= + e.Y);

MessageBox.Show((3D) X= + v.X+(3D) Y= + v.Y+(3D) Z= + v.Z);
}
if ((e.Button == MouseButtons.Right))
{

}

}
}// Ende der Klasse
}// Ende des Namespaces
 
Thema:

Mauskoordinaten ind 3d

ANGEBOTE & SPONSOREN

Statistik des Forums

Themen
113.838
Beiträge
707.961
Mitglieder
51.491
Neuestes Mitglied
haraldmuc
Oben