also visual studio gibt kein fehler aus
aber sobald programm gestartet wird kommt fehlermeldung
der code:
/*---------------------------------------------------------------------------- CGA-Kuer: WS 06/07 Jassamin Ghzavini, 726663 ----------------------------------------------------------------------------*/ using System; //wie Objact in Java using System.Drawing; //2D Objekte zeichnen using System.Windows.Forms; //home of the Form-class + method Application.Run using Microsoft.DirectX; //Sammlung von mehreren APIs using Microsoft.DirectX.Direct3D; //um auf dem Bildschirm zu zeichnen(2D und 3D) using System.Collections; using System.ComponentModel; using System.Data; /*---------------------------------------------------------------------------- Klasse Form1 ----------------------------------------------------------------------------*/ public class Form1 : Form { /*---------------------------------------------------------------------------- Methode Main ----------------------------------------------------------------------------*/ static void Main() { Application.Run(new Form1()); } /*---------------------------------------------------------------------------- Variablendeklaration ----------------------------------------------------------------------------*/ static Device device = null; //3D Raum mit null initialisieren PresentParameters presentParams; //bestimmen wie sich das Device grundlegend verhalten soll static Mesh meshbox; //Var meshbox: Mesh ist eine Klasse in der man Vertexinformationen zur Darstellung //von 3D-Objekten leicht speichern u anzeigen kann static float xAngle, yAngle, zAngle = 0; //Kantenwinkel/Aktuelle Winkel unseres Objektes static Vector3 xAxis = new Vector3(1, 0, 0); // x EinheitsVektor static Vector3 yAxis = new Vector3(0, 1, 0); // y EinheitsVektor static Vector3 zAxis = new Vector3(0, 0, 1); // z EinheitsVektor static float moveX = 0; // X = Bewegung oben/unten //anfangs keine Bewegung static float moveY = 0; // Y = Bewegung links/rechts static float moveZ = 0; // Z = Bewegung in den Bildschirm rein/raus static float konst = 0.2f; //Bewegungskonstante um 0.2f static float cameraY = 0.0f; //Blickrichtung der Kamera nach oben/unten static float cameraX = -0.3f; //Blickrichtung der Kamera nach rechts/links float big = 0.6f; //Konstante z.B. vom bigger-Button Timer myTimer = new Timer(); //Timer generieren /*---------------------------------------------------------------------------- Buttondeklaration ----------------------------------------------------------------------------*/ private System.Windows.Forms.Button oben; private System.Windows.Forms.Button links; private System.Windows.Forms.Button unten; private System.Windows.Forms.Button rechts; private System.Windows.Forms.Button freeze; private System.Windows.Forms.Button depth; private System.Windows.Forms.Button bigger; private System.Windows.Forms.Button smaller; private System.Windows.Forms.Button color; private System.Windows.Forms.Button stop; private System.Windows.Forms.Button goUp; private System.Windows.Forms.Button goDown; private System.Windows.Forms.Button goLeft; private System.Windows.Forms.Button goRight; /*---------------------------------------------------------------------------- Methode Form1 ----------------------------------------------------------------------------*/ public Form1() { /*---------------------------------------------------------------------------- Dialogfenster ----------------------------------------------------------------------------*/ ClientSize = new Size(1000, 460); SetStyle(ControlStyles.ResizeRedraw, true); //Fenstergröße veränderbar Text = "Jassamin Ghazvini 726663"; //Titel StartPosition = FormStartPosition.Manual; Top = 100; //100 vom oberen Rand entfernt (y-Achse) Left = 100; //100 vom linken Rand entfernt (x-Achse) /*---------------------------------------------------------------------------- Timer set-up ----------------------------------------------------------------------------*/ myTimer.Tick += new EventHandler(OnTimer); //Der ontimer-Event tritt ein, wenn //ein vordefinierter Timer ( Stopuhr ) abgelaufen ist myTimer.Interval = 60; //je höher die Zahl desto langsamer dreht sich die Box OnResize(null); // Direct3D wird initialisiert } /*---------------------------------------------------------------------------- Methode OnResize ----------------------------------------------------------------------------*/ protected override void OnResize(System.EventArgs e) { //Jedesmal wenn sich das Fenster ändert, muss Direct3D neu initialisiert werden try { presentParams = new PresentParameters(); //bestimmen wie sich das Device grundlegend verhalten soll, presentParams.Windowed = true; //wollen Fensteranwendung presentParams.SwapEffect = SwapEffect.Discard; //wie wir die Bilder auf den Bildschirm bringen wollen //Discard: effizienste Darstellung, Copy: immer gesammter Screen neugzeichnet presentParams.EnableAutoDepthStencil = true; // Aktivierung der Matrizentiefe presentParams.AutoDepthStencilFormat = DepthFormat.D16; // 16bit Matrizenformat device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams); /*Manager.Adapters.Default: gibt an auf welchem Adapter(Bildschirm) wir zeichnen wollen //DeviceType.Hardware: alle Operationen von Direct3D verwenden die Grafikkarte(schneller als CPU) //this: zeigt an daß das Device dieses Fenster zum zeichnen verwenden soll //CreateFlags.HardwareVertexProcessing: ähnlich wie oben, Operationen über die Grafikkarte presentParams: zeigt auf die ausgefüllte PresentParameters Struktur*/ meshbox = Mesh.Box(device, big, big, big); //Funktion Box wird berechnet aus 3 Parametern //1.device, 2.Ausdehnung auf der x-Achse, 3.Ausdehnung auf der y-Achse, 4.Ausdehnung auf der z-Achse Material mtrl = new Material(); //Material um z.B Reflexion zu kontrollieren mtrl.Diffuse = Color.White; //Umgebungsfarbe auf ein diffuses/metalic Weiß setzen device.Material = mtrl; device.RenderState.Lighting = true; //Lighting einschalten //es gibt vier unterschiedliche Lichttypen: Ambient-, Directional-, Point- und Spot-Lighting device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.Red; //Anfangsfarbe ROT device.Lights[0].Direction = new Vector3(0, 1, 1); //direktes Licht device.Lights[0].Enabled = true; //Aktivierung //device.Transform.View: View-Matrix //device.Transform.World: World-Matrix //device.Transform.Projection: Projektions-Matrix device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -6.0f), // Augenpunkt/Ausgangspunkt des Betrachters new Vector3(-0.3f, 0.0f, 0.0f), // Punkt, zu dem der Betrachter schaut new Vector3(0.0f, 1.0f, 0.0f)); // Ausrichtung auf der y-Achse ((0,1,0)=>Standard) //View: definiert wie eine 3-dimensionale Szene 2-dimensional dargestellt wird (Position des Objekts) device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 6.0f); /* Verantwortlich dafür,daß auf dem 2D-Monitor eine 3D-Szene dargestellt werden kann. * Sorgt dafür, daß Objekte,die weiter weg sind, kleiner sind u umgekehrt * 1. Parameter: beschreibt das Sichtfeld in Y-Richtung (PI/4 ist ein üblicher Wert) * 2. Parameter: Verhältnis Höhe zu Breite, 1 = viereckiges Fenster * 3. Parameter: znearPlane //bei 0 nur einzelne linien sichtbar * 4. Parameter: zfarPlane geben an, von wo bis wo die Szene dargestellt ist*/ device.RenderState.CullMode = Cull.None; // culling entscheidet welche Objekte gezeichnet werden /*warum Culling ausschalten? Wir rotieren ja das Dreieck. Und beim rotieren sind die Vertices ja einmal gegen den Uhrzeigersinn angeordnet und einmal im Uhrzeigersinn. Wenn wir jetzt das Culling einschalten würden, würden wir je nach gesetzen Wert eine Seite des Dreiecks nicht sehen*/ //FillMode.WireFrame:nur Linien angezeigt, Solid:Standard, Point:Eckpunkte DeleteComponent(); InitializeComponent(); myTimer.Start(); //Timer starten bzw. Ontimer Methode aufrufen } catch (DirectXException) { MessageBox.Show("Could not initialize Direct3D."); return; } } /*---------------------------------------------------------------------------- DeleteComponent: Controls loeschen ----------------------------------------------------------------------------*/ private void DeleteComponent() { this.Controls.Clear(); } /*---------------------------------------------------------------------------- Buttoninitialisierung ----------------------------------------------------------------------------*/ private void InitializeComponent() { this.oben = new System.Windows.Forms.Button(); this.links = new System.Windows.Forms.Button(); this.unten = new System.Windows.Forms.Button(); this.rechts = new System.Windows.Forms.Button(); this.freeze = new System.Windows.Forms.Button(); this.stop = new System.Windows.Forms.Button(); this.depth = new System.Windows.Forms.Button(); this.bigger = new System.Windows.Forms.Button(); this.smaller = new System.Windows.Forms.Button(); this.color = new System.Windows.Forms.Button(); this.goUp = new System.Windows.Forms.Button(); this.goDown = new System.Windows.Forms.Button(); this.goRight = new System.Windows.Forms.Button(); this.goLeft = new System.Windows.Forms.Button(); this.SuspendLayout(); Rectangle cr = ClientRectangle; //Inhalt des Fensters Int32 w = cr.Width; Int32 h = cr.Height; //------------reset------------------------------------------------- this.stop.Location = new System.Drawing.Point(0, w / 30); //Position des Buttons this.stop.Size = new System.Drawing.Size(w / 6, h / 20); //Groesse des Buttons this.stop.Text = " reset"; this.stop.Click += new System.EventHandler(this.stop_Click); this.stop.TextAlign = System.Drawing.ContentAlignment.BottomLeft; //Text links-unten-bündig this.stop.BackColor = System.Drawing.Color.DarkGray; //grauer Button this.stop.Font = new System.Drawing.Font("Arial", 10); //------------freeze------------------------------------------------ this.freeze.Location = new System.Drawing.Point(0, w / 30 + 1 * (h / 20)); this.freeze.Size = new System.Drawing.Size(w / 6, h / 20); this.freeze.Text = " freeze"; this.freeze.Click += new System.EventHandler(this.freeze_Click); this.freeze.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.freeze.BackColor = System.Drawing.Color.DarkGray; this.freeze.Font = new System.Drawing.Font("Arial", 10); //----------depth----------------------------------------------- this.depth.Location = new System.Drawing.Point(0, w / 30 + 2 * (h / 20)); this.depth.Size = new System.Drawing.Size(w / 6, h / 20); this.depth.Text = " Tiefe"; this.depth.Click += new System.EventHandler(this.depth_Click); this.depth.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.depth.BackColor = System.Drawing.Color.DarkGray; this.depth.Font = new System.Drawing.Font("Arial", 10); //----------smaller---------------------------------------------- this.smaller.Location = new System.Drawing.Point(0, w / 30 + 3 * (h / 20)); this.smaller.Size = new System.Drawing.Size(w / 6, h / 20); this.smaller.Text = " kleiner"; this.smaller.Click += new System.EventHandler(this.smaller_Click); this.smaller.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.smaller.BackColor = System.Drawing.Color.DarkGray; this.smaller.Font = new System.Drawing.Font("Arial", 10); //----------bigger---------------------------------------------- this.bigger.Location = new System.Drawing.Point(0, w / 30 + 4 * (h / 20)); this.bigger.Size = new System.Drawing.Size(w / 6, h / 20); this.bigger.Text = " größer"; this.bigger.Click += new System.EventHandler(this.bigger_Click); this.bigger.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.bigger.BackColor = System.Drawing.Color.DarkGray; this.bigger.Font = new System.Drawing.Font("Arial", 10); //-----------color------------------------------------------------- this.color.Location = new System.Drawing.Point(0, w / 30 + 5 * (h / 20)); this.color.Size = new System.Drawing.Size(w / 6, h / 20); this.color.Text = " Farbwechsel"; this.color.Click += new System.EventHandler(this.color_Click); this.color.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.color.BackColor = System.Drawing.Color.DarkGray; this.color.Font = new System.Drawing.Font("Arial", 10); //---------------goUp---------------------------------------------- this.goUp.Location = new System.Drawing.Point(0, w / 30 + 7 * (h / 20)); this.goUp.Size = new System.Drawing.Size(w / 6, h / 20); this.goUp.Text = " hoch"; this.goUp.Click += new System.EventHandler(this.goUp_Click); this.goUp.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.goUp.BackColor = System.Drawing.Color.DarkGray; this.goUp.Font = new System.Drawing.Font("Arial", 10); //-----------------goRight----------------------------------------- this.goRight.Location = new System.Drawing.Point(0, w / 30 + 8 * (h / 20)); this.goRight.Size = new System.Drawing.Size(w / 6, h / 20); this.goRight.Text = " rechts ->"; this.goRight.Click += new System.EventHandler(this.goRight_Click); this.goRight.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.goRight.BackColor = System.Drawing.Color.DarkGray; this.goRight.Font = new System.Drawing.Font("Arial", 10); //-------------goDown----------------------------------------------- this.goDown.Location = new System.Drawing.Point(0, w / 30 + 9 * (h / 20)); this.goDown.Size = new System.Drawing.Size(w / 6, h / 20); this.goDown.Text = " runter"; this.goDown.Click += new System.EventHandler(this.goDown_Click); this.goDown.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.goDown.BackColor = System.Drawing.Color.DarkGray; this.goDown.Font = new System.Drawing.Font("Arial", 10); //------------goLeft---------------------------------------------- this.goLeft.Location = new System.Drawing.Point(0, w / 30 + 10 * (h / 20)); this.goLeft.Size = new System.Drawing.Size(w / 6, h / 20); this.goLeft.Text = " links <-"; this.goLeft.Click += new System.EventHandler(this.goLeft_Click); this.goLeft.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.goLeft.BackColor = System.Drawing.Color.DarkGray; this.goLeft.Font = new System.Drawing.Font("Arial", 10); //------------oben------------------------------------------------- this.oben.Location = new System.Drawing.Point(0, w / 30 + 12 * (h / 20)); this.oben.Size = new System.Drawing.Size(w / 6, h / 20); this.oben.Text = " roll up"; this.oben.Click += new System.EventHandler(this.oben_Click); this.oben.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.oben.BackColor = System.Drawing.Color.DarkGray; this.oben.Font = new System.Drawing.Font("Arial", 10); //------------rechts-------------------------------------------- this.rechts.Location = new System.Drawing.Point(0, w / 30 + 13 * (h / 20)); this.rechts.Size = new System.Drawing.Size(w / 6, h / 20); this.rechts.Text = " roll right"; this.rechts.Click += new System.EventHandler(this.rechts_Click); this.rechts.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.rechts.BackColor = System.Drawing.Color.DarkGray; this.rechts.Font = new System.Drawing.Font("Arial", 10); //----------unten--------------------------------------------------- this.unten.Location = new System.Drawing.Point(0, w / 30 + 14 * (h / 20)); this.unten.Size = new System.Drawing.Size(w / 6, h / 20); this.unten.Text = " roll down"; this.unten.Click += new System.EventHandler(this.unten_Click); this.unten.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.unten.BackColor = System.Drawing.Color.DarkGray; this.unten.Font = new System.Drawing.Font("Arial", 10); //-------------links------------------------------------------------ this.links.Location = new System.Drawing.Point(0, w / 30 + 15 * (h / 20)); this.links.Size = new System.Drawing.Size(w / 6, h / 20); this.links.Text = " roll left"; this.links.Click += new System.EventHandler(this.links_Click); this.links.TextAlign = System.Drawing.ContentAlignment.BottomLeft; this.links.BackColor = System.Drawing.Color.DarkGray; this.links.Font = new System.Drawing.Font("Arial", 10); /*---------------------------------------------------------------------------- Buttonaufruf ----------------------------------------------------------------------------*/ this.Controls.Add(this.stop); this.Controls.Add(this.freeze); this.Controls.Add(this.depth); this.Controls.Add(this.bigger); this.Controls.Add(this.smaller); this.Controls.Add(this.color); this.Controls.Add(this.rechts); this.Controls.Add(this.unten); this.Controls.Add(this.links); this.Controls.Add(this.oben); this.Controls.Add(this.goUp); this.Controls.Add(this.goDown); this.Controls.Add(this.goLeft); this.Controls.Add(this.goRight); this.ResumeLayout(false); } /*---------------------------------------------------------------------------- Bewegung des Objekts ----------------------------------------------------------------------------*/ private void goUp_Click(object sender, System.EventArgs e) { cameraY = cameraY - 0.4f; //Schrittgröße des Objekts (um 0,4 nach oben) if (cameraY > 2.5f || cameraY < -2.5f) //wenn das Objekt außerhalb der Bühne ist cameraY = 2.2f; //taucht es unten (Pos. 2.2) wieder auf device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -6.0f), //Position des Betrachters new Vector3(cameraX, cameraY, 0.0f), //Bickpunkt des Betrachters new Vector3(0.0f, 1.0f, 0.0f)); //wo oben für den Betrachter ist } private void goDown_Click(object sender, System.EventArgs e) { cameraY = cameraY + 0.4f; //Schrittgröße des Objekts (um 0,4 nach unten) if (cameraY > 2.5f || cameraY < -2.5f) cameraY = -2.2f; device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -6.0f), new Vector3(cameraX, cameraY, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); } private void goRight_Click(object sender, System.EventArgs e) { cameraX = cameraX - 0.4f; if (cameraX > 4.0f || cameraX < -4.0f) cameraX = 2.0f; device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -6.0f), new Vector3(cameraX, cameraY, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); } private void goLeft_Click(object sender, System.EventArgs e) { cameraX = cameraX + 0.4f; if (cameraX > 4.0f || cameraX < -4.0f) cameraX = -2.0f; device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -6.0f), new Vector3(cameraX, cameraY, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); } private void oben_Click(object sender, System.EventArgs e) { //roll up moveX = konst; //positive = hoch } private void unten_Click(object sender, System.EventArgs e) { //roll down moveX = -konst; //negativ = runter } private void rechts_Click(object sender, System.EventArgs e) { //roll right moveY = -konst; //negativ = rechts } private void links_Click(object sender, System.EventArgs e) { //roll left moveY = konst; //positive = links } private void depth_Click(object sender, System.EventArgs e) { zAxis = new Vector3(0, 0, 0); moveZ = konst; } private void bigger_Click(object sender, System.EventArgs e) { if (big < 3.0f) //darf nicht größer als 3,0 werden, damits nicht zu groß wird big += 0.2f; //Vergrösserung des Objekts um 0,2 else big = 3.0f; meshbox = Mesh.Box(device, big, big, big); //x-Size,y-Size,z-Size } private void smaller_Click(object sender, System.EventArgs e) { if (big > 0.3f) //darf nicht kleiner als 0,3 werden, damit mans noch sieht big -= 0.2f; //Verkleinerung um 0,2 else big = 0.2f; meshbox = Mesh.Box(device, big, big, big); } private void stop_Click(object sender, System.EventArgs e) { //wieder auf default-Werte xAngle = yAngle = zAngle = 0; moveX = moveY = moveZ = 0; myTimer.Interval = 120; cameraX = -0.3f; cameraY = 0; device.Lights[0].Diffuse = Color.Red; //Anfangsfarbe ROT device.Lights[0].Enabled = true; //Aktivierung device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -6.0f), new Vector3(-0.3f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); big = 0.6f; meshbox = Mesh.Box(device, big, big, 2 * big); //xSize, ySize, zSize myTimer.Start(); } private void freeze_Click(object sender, System.EventArgs e) { moveX = moveY = moveZ = 0; //Bewegungsvariablen wieder auf Null } private void color_Click(object sender, System.EventArgs e) { Random random = new Random(); Byte red = (Byte)random.Next(Byte.MaxValue); // ein ZufallsByte fuer Rot generieren Byte green = (Byte)random.Next(Byte.MaxValue); // ein ZufallsByte fuer Grün generieren Byte blue = (Byte)random.Next(Byte.MaxValue); // ein ZufallsByte fuer Blau generieren device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.FromArgb(red, green, blue); device.Lights[0].Enabled = true;//turn it on } protected static void OnTimer(Object myObject, EventArgs myEventArgs) {// Koordinaten Aktualisieren if (device == null) return; //throw the old image away device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.WhiteSmoke, 2.0f, 0); //Das alte Inhalt wegwerfen & das Fenster(Target)mit einem weißem BG gerendern // neue Winkel Berechnen xAngle += moveX; // mit moveX um die X-Achse Rotieren yAngle += moveY; // mit moveY um die Y-Achse Rotieren zAngle += moveZ; // mit moveZ um die Z-Achse Rotieren //World.Matrix device.Transform.World = Matrix.Identity; //stillstehen device.Transform.World *= Matrix.RotationAxis(xAxis, xAngle); device.Transform.World *= Matrix.RotationAxis(yAxis, yAngle); device.Transform.World *= Matrix.RotationAxis(zAxis, zAngle); //1:Ursprungsgröße des Objekts //zum Rendern device.BeginScene(); //Szene beginnen meshbox.DrawSubset(0);//Box rendern device.EndScene(); //Szene beenden device.Present();//veranlasst Direct3D alles wirklich zu rendern } }
Dieser Beitrag wurde von Witi bearbeitet: 28. Oktober 2007 - 17:41
Änderungsgrund: Ich war mal so frei...