Subscribe

RSS Feed (xml)

Powered By

Powered by Blogger

Google
 
xnahelp.blogspot.com

Kamis, 03 April 2008

Effects

Effects are used to get anything in our XNA 3D game to actually show up on the screen.
They handle things like lights, textures, and even position of the points. We will talk
about effects extensively in Part V, “High Level Shading Language (HLSL).” For now, we
can utilize the BasicEffect class that XNA provides. This keeps us from having to actually
create an effect file so we can get started quickly.
The first thing to notice is that we create a new variable to hold our effect. We do this by
passing in the graphics device as our first parameter and we are passing in null as the
effect pool because we are only using one effect and don’t need a pool to share among
66 CHAPTER 4 Creating 3D Objects
multiple effects. After creating our effect, we want to set some of the properties so we can
use it. Notice we set the world, view, and projection matrices for the effect as well as
telling the effect to turn on the default lighting. We discuss lighting in detail in the HLSL
part of our book, but for now, this will light up the 3D scene so we can see our objects.
TIP
It is a good idea to leave the background color set to Color.CornflowerBlue or some
other nonblack color. The reason for this is if the lights are not set up correctly, the
object will render in black (no light is shining on it). So if the background color is
black, we might think that the object didn’t render at all.
Now back to our code. Notice that we call the Begin method on our effect and we also
call the End method. Anything we draw on the screen in between these two calls will
have that effect applied to them. The next section of code is our foreach loop. This loop
iterates through all of the passes of our effect. Effects will have one or more techniques. A
technique will have one or more passes. For this basic effect, we have only one technique
and one pass. We will learn about techniques and passes in more detail in Part V. At this
point we have another begin and end pair, but this time it is for the pass of the current
(only) technique in our effect. Inside of this pass is where we finally get to draw our triangle
onto the screen. This is done using the DrawUserPrimitives method in the graphics
device object:
graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);
We are passing in the type of primitive we will be rendering. The primitives we are
drawing are triangles so we are going to pass in a triangle list. This is the most common
primitive type used in modern games. Refer to Table 4.1 for a list of different primitive
types and how they can be used. The second parameter we pass in is the actual vertex
data we created in our InitializeVertices method. The third parameter is the offset of
the point data where we want to start drawing—in our case we want to start with the first
point, so that is 0. Finally, we need to pass in the number of triangles we are drawing on
the screen. We can calculate this by taking the number of points we have stored and
dividing it by 3 (as there are 3 points in a triangle). For our example, this will return one
triangle. If we compile and run the code at this point we should see a triangle drawn on
our screen. It is not very pretty, as it is a dull shade of gray, but it is a triangle nonetheless.
To see a screen shot, refer to Figure 4.3.
Effects 67
4
Member Name Description
LineList Renders the vertices as a list of isolated straight-line segments.
LineStrip Renders the vertices as a single polyline.
PointList Renders the vertices as a collection of isolated points. This value is
unsupported for indexed primitives.
TriangleFan Renders the vertices as a triangle fan.
TriangleList Renders the specified vertices as a sequence of isolated triangles. Each
group of three vertices defines a separate triangle. Back-face culling is
affected by the current winding-order render state.
TriangleStrip Renders the vertices as a triangle strip. The back-face culling flag is
flipped automatically on even-numbered triangles.

0 komentar: