XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   September 24: 2D-19: Particle engine
My Book: Out Now!
      
       Go to section on this site

Další informace


Poslední príspevky na fóru

 Next Chapter
  Posted by: jasonbarry
  When: 15/10/2008 at 15:55:47

 HLSL: compilation fails
  Posted by: aguacate
  When: 15/10/2008 at 12:03:19

 my collision sucks!
  Posted by: orgad
  When: 15/10/2008 at 11:25:49

 HLSL: compilation fails
  Posted by: aguacate
  When: 15/10/2008 at 11:16:55

 Flickering/glitches
  Posted by: Danzence
  When: 15/10/2008 at 09:53:00

 Next Chapter
  Posted by: samer
  When: 15/10/2008 at 06:40:32

 Flickering/glitches
  Posted by: samer
  When: 15/10/2008 at 06:39:17

 Texture terrian c++
  Posted by: ruudriem
  When: 15/10/2008 at 05:55:16

 1.7 and 2.8 problem.
  Posted by: riemer
  When: 15/10/2008 at 03:36:34

 Next Chapter
  Posted by: riemer
  When: 15/10/2008 at 03:34:29


Ads



Vykreslení prvního trojúhelníku

Tato cást vám ukáže základy vykreslování. První z nekolik vecí, které byste meli znát.

Každý objekt vykreslený ve 3 je složen z trojúhelníku. Každá koule muže být zastoupena trojúhelníky, pokud jich použijete dost. Trojúhelník je definován 3 body, což je prekvapive dost. Každý bod je definován vektorem, který má specifickou polohu podle os X,Y a Z. Prosté informace o poloze bodu nestací. Napríklad mužete chtít definovat barvu pro body. A tady prichází na radu vertex (mn.c. vertexy). Vertex je seznam nastavení dávající budu pozici, barvu a další nastavení.

XNA má strukturu, která se presne hodí pro informace o vertexech: the VertexPositionColor struct. Vertex tohoto typu muže obsahovat pozici a barvu, což se skvele hodí do zacátku. K definování trojúhelníku potrebujeme 3 tyto vertexy, které budeme mít v poli. Takže jdeme deklarovat tyto promenné, takže vložte tento kód navrch vaší trídy:

 VertexPositionColor[] vertices;

Dále pridáme metodu SetUpVertices do našeho kódu, která bude obsahovat pole se 3 vertexy:

 private void SetUpVertices()
 {
     vertices = new VertexPositionColor[3];
 
     vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
     vertices[0].Color = Color.Red;
     vertices[1].Position = new Vector3(0, 0.5f, 0f);
     vertices[1].Color = Color.Green;
     vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
     vertices[2].Color = Color.Yellow;
 }

Pole je inicializováno s obsahem 3 vertexu, které poté budou využity. Prozatím použijeme souradnice, kterou jsou relativní vuci oknu, takže bod (0,0,0) bude uprostred obrazovky, bod (-1,-1,0) dole vlevo a bod (1,1,0) nahore vpravo. Takže v príkladu nahore, první bod je v polovine vzdálenosti od stredu k dolnímu levému rožku a druhý bod je v polovine vzdálenosti od stredu k horní lište okna (Toto nejsou pravé 3D souradnice, protože nepotrebují být transformovány do 2D souradnici a proto se jmenuje tato technika „Pretransformed“).

„f“ za nekterými císly jednoduše zkonvertuje hodnoty integer (celá císla) do float(desetinná c.), což je potrebný formát. Nastavíme každý vertex na jinou barvu. Ješte musíme zavolat tuto metodu z naší inicializacní metody:

 SetUpVertices();

Ted už stací jenom, aby device vykreslilo trojúhelník. Bežte k metode Draw, kde musíme vykreslit trojúhelník mezi voláním cesty Begin a End:

 device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
 device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);

První rádek ukazuje, který formát vertexu má grafická karta ocekávat. Naše vertexy obsahují informaci o pozici a barve. Druhý rádek už skutecne vykreslí trojúhelník. Chceme vykreslit 1 trojúhelník z pole vertexu zacínající vertexem 0. TriangleList znamená, že vertexy obsahují seznam trojúhelníku (v našem prípade seznam jenom 1 trojúhelníku). Pokud byste chteli vykreslit 4 trojúhelníky potrebujete pole o 12 vertexech. Jiná možnost je použití TriangleStrip, což je rychlejší, ale je to jenom využitelné k vykreslení trojúhelníku, které jsou spojeny k sobe. Více o TriangleStrip v C# Sérii 3: „TriangleStrip“.

Spuštením kód byste meli videt toto okno:




DirectX Tutorial 3 - Vykreslení prvního trojúhelníku

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/riemers/public_html/chz/Tutorials/XNA/Csharp/Series1/The_first_triangle.php(9) : eval()'d code(35) : eval()'d code on line 292


That's all there is to it! Running this code will already give you a colorful triangle on a blue background. Feel free to experiment with the colors and the coordinates.

I've listed the total code below :

 
 #region Using Statements
 using System;
 using System.Collections.Generic;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Audio;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Input;
 using Microsoft.Xna.Framework.Storage;
 #endregion
 
 namespace XNAtutorial
 {
     public class Game1 : Microsoft.Xna.Framework.Game
     {
         GraphicsDeviceManager graphics;
         ContentManager content;
         GraphicsDevice device;
         Effect effect;
         VertexPositionColor[] vertices;
 
         public Game1()
         {
             graphics = new GraphicsDeviceManager(this);
             content = new ContentManager(Services);
         }
 
         protected override void Initialize()
         {
             base.Initialize();
             SetUpXNADevice();
             SetUpVertices();
         }
 
         private void SetUpXNADevice()
         {
             device = graphics.GraphicsDevice;
 
             graphics.PreferredBackBufferWidth = 500;
             graphics.PreferredBackBufferHeight = 500;
             graphics.IsFullScreen = false;
             graphics.ApplyChanges();
             Window.Title = "Riemer's XNA Tutorials -- Series 1";
 
             CompiledEffect compiledEffect = Effect.CompileEffectFromFile("@/../../../../effects.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
             effect = new Effect(graphics.GraphicsDevice, compiledEffect.GetEffectCode(), CompilerOptions.None, null);
         }
 
         private void SetUpVertices()
         {
             vertices = new VertexPositionColor[3];
 
             vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
             vertices[0].Color = Color.Red;
             vertices[1].Position = new Vector3(0, 0.5f, 0f);
             vertices[1].Color = Color.Green;
             vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
             vertices[2].Color = Color.Yellow;
         }
         
         protected override void LoadGraphicsContent(bool loadAllContent)
         {
             if (loadAllContent)
             {        
             }
         }
 
         protected override void UnloadGraphicsContent(bool unloadAllContent)
         {
             if (unloadAllContent == true)
             {
                 content.Unload();
             }
         }
 
         protected override void Update(GameTime gameTime)
         {
             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                 this.Exit();
             base.Update(gameTime);
         }
 
         protected override void Draw(GameTime gameTime)
         {
             device.Clear(Color.DarkSlateBlue);
 
             effect.CurrentTechnique = effect.Techniques["Pretransformed"];
             effect.Begin();
             foreach (EffectPass pass in effect.CurrentTechnique.Passes)
             {
                 pass.Begin();
 
                 device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
                 device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);
 
                 pass.End();
             }
             effect.End();
 
             base.Draw(gameTime);
         }
     }
 }


Google
 
Webwww.riemers.net


If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2008 Riemer Grootjans
Translations

This site in English
This site in Korean
This site in Czech

Microsoft MVP Award



2007 - 2008 MVP Award
DirectX - XNA

Contents

Tutoriály
XNA používající C#
Series 1: Terrain
Začátek projektu
Soubor Effect
Vykreslení prvního trojúhelníku
-- Expand all --


Thank you!

Podporte tuto stránku
jakákoliv cástka je vítaná!

Zustante informováni

I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!