3D Graphics with XNA Game Studio 4.0 - James Sean - E-Book

3D Graphics with XNA Game Studio 4.0 E-Book

James Sean

0,0
23,30 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

In Detail

XNA is a very powerful API using which it's easy to make great games, especially when you have dazzling 3D effects. This book will put you on course to implement the same 3D graphics used in professional games to make your games shine, and get those gamers addicted! If you think 3D graphics is something that limits your games, then this book is for you.

3D Graphics with XNA Game Studio 4.0 is a step by step companion to implement the effects used in professional 3D games in your XNA games. By the time you're done with this book your games would have eye-catching visuals and 3D effects.

The one thing that can make or break a game is its appearance; players will mostly be attracted to a game if it looks good. With this book you will create your 3D objects and models and make them look more interesting by using shadowing and lighting techniques, and make them move in nasty ways using animation. Want to create realistic terrians for your games? Need some place for your 3D models to engage in battle? This book will enable you to do all that and more, by walking you through the implementation of numerous effects and graphics techniques used in professional games so that you can make them look great.

Approach

This book is designed as a step-by-step tutorial that can be read through from beginning to end, with each chapter building on the last. Each section, however, can also be used as a reference for implementing various camera models, special effects, etc. The chapters are filled with illustrations, screenshots, and example code, and each chapter is based around the creation of one or more example projects. By the end of the first chapter you will have created the framework that is used and improved upon for the rest of the book, and by the end of the book you will have implemented dozens of special effects, camera types, lighting models and more using that framework.

Who this book is for

This book is mainly written for those who are familiar with object oriented programming and C# and who are interested in taking 3D graphics of their XNA games to the next level. This book will be useful as learning material for those who are new to graphics and for those who are looking to expand their toolset. Also, it can be used by game developers looking for an implementation guide or reference for effects or techniques they are already familiar with.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 262

Veröffentlichungsjahr: 2010

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

3D Graphics with XNA Game Studio 4.0
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. Getting Started with 3D
Setting up a new project
The 3D coordinate system
Matrices
Loading a model
Drawing a model
Creating a Custom Model class
Creating a Camera class
Creating a target camera
Upgrading the camera to a free camera
Calculating bounding spheres for models
View frustum culling
Additional camera types: Arc-Ball
Additional camera types: chase camera
Example—spaceship simulator
XNA Graphics Profiles
Summary
2. Introduction to HLSL
Getting started
Assigning a shader to a model
Creating a simple effect
Texture mapping
Texture sampling
Diffuse colors
Ambient lighting
Lambertian directional lighting
Phong specular highlights
Creating a Material class to store effect parameters
Summary
3. Advanced Lighting
Implementing a point light with HLSL
Implementing a spot light with HLSL
Drawing multiple lights
Prelighting
Storing depth and normal values
Creating the light map
Drawing models with the light map
Creating the prelighting renderer
Using the prelighting renderer
Summary
4. Projection and Shadowing Effects
Projective texturing
Shadow mapping—drawing the depth map
Shadow mapping—projecting the depth texture onto the scene
Shadow mapping—performing the depth comparison
Variance shadow mapping—soft shadows
Variance shadow mapping—blurring the depth texture
Variance shadow mapping—generating shadows
Summary
5. Shader Effects
Fog
Normal mapping
Generating normal maps with Photoshop
Cube mapping: Making a sky sphere
Cube mapping: Reflections
Rendering sky boxes with Terragen
Creating a reflective water effect
Summary
6. Billboard and Particle Effects
Creating the BillboardSystem class
Drawing Billboards
Creating clouds with spherical billboarding
Non-rotating billboards
Particle effects
Particle fire
Particle smoke
Summary
7. Environmental Effects
Building a terrain from a heightmap
Multitexturing
Adding a detail texture to the terrain
Placing plants on the terrain
Adding the finishing touches
Summary
8. Advanced Materials and Post Processing
Advanced Materials
Post processing
Black and white post processor
Gaussian blur post processor
Depth of field
Glow post processor
Summary
9. Animation
Object animation
Keyframed animation
Curve interpolation
Building a Race Track from a Curve
Moving a car along the track
Hierarchical animation
Skinned animation
Loading a skinned model
Playing a skinned animation
Changing an animation's play speed
Model attachments
Summary
Index

3D Graphics with XNA Game Studio 4.0

3D Graphics with XNA Game Studio 4.0

Copyright © 2010 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2010

Production Reference: 1071210

Published by Packt Publishing Ltd.

32 Lincoln Road

Olton

Birmingham, B27 6PA, UK.

ISBN 978-1-849690-04-1

www.packtpub.com

Cover Image by Charwak A (<[email protected]>)

Credits

Author

Sean James

Reviewers

Zhenyu George Li

Cătălin Zima-Zegreanu

Acquisition Editor

David Barnes

Development Editor

Tariq Rakhange

Technical Editor

Namita Sahni

Copy Editor

Laxmi Subramanian

Indexers

Monica Ajmera Mehta

Rekha Nair

Editorial Team Leader

Aditya Belpathak

Project Team Leader

Ashwin Shetty

Project Coordinator

Poorvi Nair

Proofreader

Lynda Sliwoski

Graphics

Geetanjali Sawant

Production Coordinator

Shantanu Zagade

Cover Work

Shantanu Zagade

About the Author

Sean James is a computer science student who has been programming for many years. He started with web designing, learning HTML, PHP, JavaScript, and so on. Since then he has created many websites including his personal XNA and game development focused blog, www.innovativegames.net. In addition to web designing, he has interests in desktop software development and development for mobile devices such as Android, Windows Mobile, and Zune. However, his passion is for game development with DirectX, OpenGL, and XNA. Sean James lives in Claremont, CA with his family and two dogs.

I would like to thank my family and friends who supported me throughout the writing of this book, and all the people at Packt Publishing who worked hard on the book to support me. I would also like to thank the XNA community for providing such amazing resources, without which this book would not have been possible.

About the Reviewers

Zhenyu George Li has been working as a software engineer in the game industry for more than ten years. In his early years, George really enjoyed playing video games and dreamed to be a game developer, so he started learning Turbo C 3D programming and DirectX6 in 1998. George's article series, The Road to Game Development – DirectX Programming with C++ Builder, was published in the magazine Computer Programming Techniques and Maintenance in China in 2000, and George won the magazine's Best Writer prize of the year. After moving to Canada in 2001, George has been working for some companies on several game titles and tools such as CT Baseball (Taiwan), The Bigs2, Dead Rising 2, online poker games, Heroes of Mythology, Battle of Britain, and Avatar XNA skinned Model Animation Engine. As a video game developer, George has accumulated ample knowledge and experience in computer graphics, game play, game frontend and UI, as well as game engine and tools development.

The first time George used Microsoft XNA in 2005, he realized that XNA had great potential for developers, educators, and learners because of its easy-to-learn programming language, C#, and multi-platform support. In 2007, George's book XNA PC and Xbox360 C# Game Programming was published in Taiwan and promoted by Microsoft Taiwan. George was also invited to translate the book Game Programming Gems 4 that was published in Taiwan in 2006.

I am thankful to Pat McGee for helping me with the Avatar XNA Skinned Mesh Animation Engine project and referring the opportunity of being the technical reviewer of 3D Graphics with XNA Game Studio 4.0. I also want to appreciate Charles Yeh and Dr. Wyn Roberts' support on my works and publications.

Cătălin Zima-Zegreanu has been coding games and graphics as a hobby for over eight years, and is planning on continuing to do so. Starting with Pascal, he continued with OpenGl and DirectX and is now passionately in love with XNA Game Studio.

He got involved with XNA and its community since the first beta version, released back in 2006, and likes to hang around the official Creator's Club Forums, to chime in whenever he feels he can help someone. His activity as well as his articles and samples released for the growing community were rewarded with the Microsoft XNA/DirectX MVP Award. You can follow his activity on his own site (http://catalinzima.com), as well as on "Sgt. Conker" (http://sgtconker.com)—an XNA community site he's managing together with a group of "absolutely fine" men.

I'd like to thank my wife for being a geek. Just like me! :)

www.PacktPub.com

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print and bookmark contentOn demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

Preface

XNA is a very powerful API using which it's easy to make great games, especially when you have dazzling 3D effects. This book will show you how to implement the same 3D graphics used in professional games to make your games shine, and get those gamers addicted! This book will show you, step-by-step, how to implement the effects used in professional 3D games in your XNA games. Upon reaching the end of the book, you would have built an extensible framework for both basic 3D rendering and advanced effects. The one thing that can make or break a game is its appearance; players will mostly be attracted to a game if it looks good. One of the most common stopping points in an XNA game is its graphics, and many independent developers are not sure of how to implement the graphical effects needed to make great looking games. This book will help you avoid this pitfall, by walking you through the implementation of many common effects and graphics techniques used in professional games so that you can make your games look great.

What this book covers

Chapter 1, Getting Started with 3D, introduces the fundamentals of 3D graphics, including coordinate systems, matrices, and so on, which will be used for the rest of the book. We start by learning some simple model drawing code and finish by building a framework to implement a number of camera types. We also take a look at view frustum culling and how it can speed up our game.

Chapter 2, Introduction to HLSL, continues on the first chapter, explaining the graphics pipeline and shaders. We then look at a number of lighting and texturing effects, expanding on the framework built in Chapter 1 and adding a system that will allows us to draw our models with any effect.

Chapter 3, Advanced Lighting, continues our discussion of lighting, implementing more light types. We then look at several ways to increase the number of lights we can draw in a scene at a time.

Chapter 4, Projection and Shadowing Effects, builds on top of the renderer completed in Chapter 3 by adding two new effects: projected textures and shadow mapping.

Chapter 5, Shader Effects, takes a look at some "shader effects" such as normal mapping and reflections. We build a number of useful effects in this chapter such as a sky box and reflective water effect.

Chapter 6, Billboard and Particle Effects, investigates particle and billboarding effects—two effects that take advantage of 2D textures to create some interesting effects in 3D scenes such as foliage, clouds, and efficient trees and particle systems.

Chapter 7, Environmental Effects, discusses several "environmental" effects such as terrain, randomly "grown" foliage, and more. The chapter finishes by combining many effects created in the book thus far to create a spectacular mountainous terrain scene.

Chapter 8, Advanced Materials and Post Processing, expands on the material system created in the earlier chapter to allow for more advanced material types. It then takes a look at "post processing" effects like blurs, glows, and depth of field.

Chapter 9, Animation, takes a look at several different types of animation, including objects animation, keyframed animation, and skinned animation to introduce movement into our scenes.

What you need for this book

All you need for this book is XNA and Visual Studio—the whole list and guide is available at creators.xna.com.

Who this book is for

This book is mainly written for those who are familiar with object-oriented programming and C# and who are interested in improving the visual appearance of their XNA games. This book will be useful as a learning material for those who are new to graphics and for those who are looking to expand their toolset. Also, it can be used by game developers looking for an implementation guide or reference for effects or techniques they are already familiar with.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Note

Downloading the example code for this book

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

The 3D coordinate system

One thing that all 3D systems hold in common is a coordinate system. Coordinate systems are important because they allow us to represent points in 3D space in a consistent manner as distances from a center point called the origin along a number of axes. You're probably used to the idea of a 2D coordinate system from your math classes in school—the origin was at (0, 0) and the X and Y axes grew to the right and up respectively. A 3D coordinate system is very similar, except for the addition of a third axis labeled the Z-axis. XNA uses what is called a "right-handed" coordinate system, meaning that the X and Y axes grow the way you're used to (to the right and up respectively), and the Z-axis grows "towards" you. If the X and Y axes were placed flat on your computer screen, you can imagine the Z-axis as growing out of the screen towards you.

With this coordinate system, we can define points in space. For example, let's assume that our coordinate system uses meters as units. Say for a moment, we were sitting at the origin (0, 0, 0) and were facing down the negative portion of the Z-axis. If we wanted to note the location of an object sitting five meters in front of us, three meters to the right, on a table one meter tall, we would say that the object was at (3, 1, -5).

Matrices

Matrices are mathematical structures that are used in 3D graphics to represent transformations—operations performed on a point to move it in some way. The three common transformations are translation (movement), rotation, and scaling (changing size). When a transformation is applied to a model, each vertex in the model is multiplied by the transformation's matrix until the entire model has been transformed.

Matrices can be combined by multiplying them together. It is worth noting that matrix multiplication is done from right to left, so the last matrix to be multiplied will be the first to affect the model and so on. This means that rotating and then moving a model will not have the same effect as moving and then rotating it. Generally, unless you mean to do otherwise, the matrices should be multiplied in the following order: scaling * rotation * transformation.

In the 3D graphics world, there are usually three matrices that must be calculated to draw an object onto the screen: the world, view, and projection matrices. The world matrix is the result of all of our transformation matrices multiplied together. Once this transformation has been applied, the model has moved from what is called "local" or "object space" to "world space". Each model in a scene has a different world matrix, as they all have different locations, orientations, and so on. It is also possible that each "piece" of a model (or mesh) may have its own world matrix. For example, the head and leg of a human model will likely have their own matrices to offset them from the center of the model (its root). When the model is drawn, each mesh has its transformation multiplied by the entire model's world matrix to calculate the final world matrix.

The view matrix is used to transform the scene from world space into view space: the world as seen by the camera. The world matrix for each model is simply multiplied by the view matrix to transform the scene. The projection matrix then transforms the three-dimensional position of each vertex in the scene into the two-dimensional projection of the scene that is drawn onto the screen. When the 3D world/view matrix combination is multiplied by the projection matrix, the scene is flattened out so that it can be drawn onto a 2D screen.

Creating a Custom Model class