Game Engineering 2 Assignment 11

Objective:

1. Learn how to draw 2D sprite.

2. Learn how to use texture atlas.

Sprite

In this assignment we are asked to create our own sprite class instead of using the ID3DXSprite interface. A sprite can be seen as a quad mesh with a texture. However there are some differences between a sprite and a mesh:

1) A sprite is just 2 triangles so we don’t need an index buffer and we can draw it with DrawPrimitive instead of DrawIndexedPrimitive.

2) We normally use a image which has transparent or semi-transparent pixels on a sprite. This means we need to enable alpha blending and disable Z-buffer when we render a sprite.

3) When we use sprites as GUIs in our game, we use screen coordinates and we don’t want the the light to affect sprites. This means we need a different vertex shader and fragment shader for sprites.

In my implementation I used normalized device coordinates and here is how I store sprites in my scene:

sprite_scene

 

My position and texcoord is stored as {left, top, width, height}.

The 2D screen coordinates I used is like this (assume the screen resolution is 1024×768):

pic0031

 

Here are 2 screenshot of my program running in different resolution:

normal_size

scaled_size

If you look at my screenshots you will notice that my sprites remain the same no matter how big the window is.

Here is the code to normalize the screen coordinates (make the point between -1 and 1):

screen_code

Texture Atlas

When we have several textures that apply on one mesh, we usually packed them into one large texture to speed up rendering. In this assignment all the numbers are on one texture and we change the texture coordinates to show different part of the texture.

You can press 0-9 to change the numbers.

PIX capture

Here is the PIX capture to show the status of Z buffer and alpha blending:

depth_info

blending_info

 

You can also notice that I created separate events for drawing meshes and drawing sprites.

Time Estimates

  • Reading – 3 hours
  • Coding – 4 hours
  • Write up – 1 hours
  • Total – 8 hours

Download Executable

Leave a Reply