Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU.

Similar presentations


Presentation on theme: "Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU."— Presentation transcript:

1 Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU

2 Processes in XNA

3 XNA Project Template Program Class  Main() method Game1 Class  Game1() method(constructor)  Initialize() method  LoadGraphicsContent() method  UnloadGraphicsContent() method  Update() method  Draw() method  GraphicsDeviceManager object  ContentManager object

4 GraphicsDeviceManager object Required by every XNA application. Handles the configuration and management of the graphics device. GraphicsDevice class is used for drawing primitive-based objects. Declared as a member of the game class.  GraphicsDeviceManager graphics; Created in the game class constructor.  graphics = new GraphicsDeviceManager(this);

5 ContentManager Object Used to load, manage, and dispose of binary media content through the content pipeline. Graphics and media content can be loaded with this object when it is referenced in the game project. Declared as a member of the game class.  ContentManager content; Created in the game class constructor.  content = new ContentManager(Services);

6 Initialize() method Traps the one-time game startup event. Natural place to trigger basic setup activities such as the following:  Setting window properties such as the title or full screen options  Setting the perspective and view to define how a user sees the 3D game  Initializing image objects for displaying textures  Initializing vertices for storing color, and image coordinates to be used throughout the program

7 Initialize() method (cont.)  Initializing vertex shaders to convert your primitive objects to pixel output  Initializing audio objects  Setting up other game objects

8 LoadGraphicsContent() method For loading binary image and model content through the graphics pipeline.

9 UnloadGraphicsContent() method For loading binary image and model content loaded in LoadGraphicsContent method the program exit.

10 Draw() method Handles the drawing (also known as rendering) for the game program. Starts by clearing the screen background, setting the screen color, and then drawing the graphics onto the screen.

11 Update() method Checks and handles game-time events.  mouse clicks, keyboard presses, game-pad control events, and timers Also a place for many other activities that require continuous checks or updates  advancing animations, detecting collisions, and tracking and modifying game scores

12 XNA Vertex types A vertex stores information, which could include X, Y, and Z positions, image coordinates, a normal vector, and color.

13 XNA Primitive Objects

14 XNA Primitive Objects (cont.) Total triangle list vertices = N triangles * 3 vertices Total triangle strip vertices = N triangles + 2 vertices Total line list vertices = N lines * 2 vertices Total line strip vertices = N lines + 1 vertex

15 Drawing 3D in XNA uses shader-based rendering to convert vertex data into pixel output must use a shader to draw 3D graphics Shaders can be used to manipulate all vertex properties (e.g.,color, position, and texture) Built-in shader class name BasicEffect you will need to write your own shader to implement the effect

16 Using BasicEffect class Declared as member variable of the Game class  BasicEffect effect; Created in Initialize() method  effect = new BasicEffect(graphics.GraphicDevice,null);

17 Using BasicEffect class (cont.) Initialized in Initialize() method // Calculate the effect aspect ratio, world, projection, and view matrix GraphicsDevice device = graphics.GraphicsDevice; float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height; // Set the World Matrix effect.World = Matrix.Identity; // Set the Viewing Matrix effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 2.0f), Vector3.Zero, Vector3.Up); // Set the Projection Matrix effect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(60.0f), aspectRatio, 1.0f, 10.0f);

18 Using BasicEffect class (cont.) Used in Draw() method effect.Begin(); foreach (EffectPass CurrentPass in effect.CurrentTechnique.Passes) { CurrentPass.Begin(); // draw here device.DrawPrimitives(PrimitiveType.LineList, 0, 3); CurrentPass.End(); } effect.End();

19 Model construction 1. Creates an array of vertex and assigns value to them 2. Represents a list of 3D vertices to be streamed (VertexBuffer object)

20 Create and assign value to Vertex private void CreateModel() { // size of 3D Axis float axisLength = 1f; // Number of vertices we´ll use int vertexCount = 6; VertexPositionColor[] vertices = new VertexPositionColor[vertexCount]; // X axis vertices[0] = new VertexPositionColor(new Vector3(-axisLength, 0.0f, 0.0f), Color.White); vertices[1] = new VertexPositionColor(new Vector3(axisLength, 0.0f, 0.0f), Color.White); // Y axis vertices[2] = new VertexPositionColor(new Vector3(0.0f, -axisLength, 0.0f), Color.White); vertices[3] = new VertexPositionColor(new Vector3(0.0f, axisLength, 0.0f), Color.White); // Z axis vertices[4] = new VertexPositionColor(new Vector3(0.0f, 0.0f, -axisLength), Color.White); vertices[5] = new VertexPositionColor(new Vector3(0.0f, 0.0f, axisLength), Color.White); // fill the vertex buffer with the vertices vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, vertexCount * VertexPositionColor.SizeInBytes, ResourceUsage.WriteOnly); vertexBuffer.SetData (vertices); }

21 VertexBuffer object Declared as member variable of the Game class VertexBuffer vertexBuffer; Created and filled with the vertex array vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, vertexCount * VertexPositionColor.SizeInBytes, ResourceUsage.WriteOnly); vertexBuffer.SetData (vertices);

22 GraphicDevice Preparation for Drawing Declare a correct vertex type to the graphic device Set the vertex source of the graphic device to the game vertex buffer GraphicsDevice device = graphics.GraphicsDevice; // Create a vertex declaration to be used when drawing the vertices device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements); // Set the vertex source device.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);


Download ppt "Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU."

Similar presentations


Ads by Google