XNA 4.0 Workshop - Week 2 (Ch. 4)

Welcome to Week 2 of the XNA 4.0 Winter Workshop. This week we're going to move past Part I of the textbook and into Part 2 - Understanding XNA Framework Basics.

This week's reading assignment is Chapter 4 from the textbook: "Creating 3D Objects". While there are less assigned pages for reading from the book this week (about 30), I will be providing a lot more supplemental material. Although the textbook does a great job giving an overview of the topics, and I generally like the order the information is presented in, it still only does a cursory job covering a lot of the necessary information.

When talking about "3D Graphics API's" we need to focus on all three elements at once. That is, we need to focus on the 3D Math, the Graphics Hardware, and the Programming API which exposes the other two elements.

3D Mathematics

The textbook fails in effectively covering 3D Mathematics. As with many authors of the genre, Carter chooses to emphasize the fact that the XNA Framework Library already provides classes for Vectors, Matrices, etc... and trusts the reader to either already know the fundamental mathematics or to go and learn more on their own time. As with nearly all books on Graphics API's, in this book we "do not discuss 3D math ... because there are many texts available that cover it." This is unfortunate but understandable. What Carter really means is, 3D mathematics is a difficult, time consuming topic to write about as it requires lots of visual aids (which authors generally have to make themselves) and could fill the page count of the entire book if done well.

With all that said, this is a workshop, not a textbook and understanding 3D mathematics is a pre-requisite to working effectively in 3D. When talking about "3D Mathematics" we're talking about the math required to manipulate, view, and render 3D primitives. This involves Vectors, Matrices, and potentially quaternions if you're looking for more advanced ways of storing rotations.

Ultimately, we'll use these mathematical concepts to represent an object's position in the world, it's position relative to our virtual camera, and what it looks like when projected from 3D space onto a 2D surface. While, I, like Chad do not have the time to write a textbook on 3D mathematics, there are a number of great tutorials on the web and we should take this week to provide and discuss those resources. Any and all questions on 3D mathematics is welcome. I challenge you to discover the best 3D tutorials on the web and provide links to them this week.

The Graphics Hardware

One of the reasons I chose Chad Carter's book as the textbook for this course is that it covers 3D primitives before covering 2D sprites. This may seem counter-intuitive but in reality it makes a lot of sense. While the mathematics involved in 3D rendering is more complex than 2D, most 2D API's we use today leverage the fact that our GPU's are built for 3D rendering. Gone are the days when we could simply copy byte arrays to video memory and expect them to show up on screen. Today, video cards (and drivers) have something called a 3D Graphics Pipeline, which is a series of stages data you wish to be drawn must go through before being shown on your screen.

When using the SpriteBatch class in XNA, you're really just creating a ton of pre-transformed triangles which are then being rendered as quads by the GPU hardware, using your "sprite" as the texture for the triangles. With that in mind, understanding the Graphics Pipeline makes you not only a capable 3D graphics programmer, but also a more efficient 2D programmer.

The other useful observation here is that all 2D "sprites" go through the same stages as your 3D triangles. This means that any and all effects we learn to do in 3D can also be used in 2D.

The Graphics API

XNA represents physical hardware components and data with a fairly complete set of classes, structs, and interfaces. These range from simple Color structs, to VertexBuffers which represent a collection of 3D points, all the way to the fairly complex GraphicsDevice class. The textbook takes a cursory look at the most important of these types, which are all related to 3D primitives. We'll take a closer look at some additional ones in the supplemental material, with the rest of them coming in Weeks 4, 6, 7, and 13.

Supplemental ReadingBelow is a list of supplemental reading that might help this week's reading make more sense to you. I am likely to add to this list over the course of the week (or even the workshop) and will make an announcement when I've added additional reading or resources.

Direct3D 9.0 Graphics Pipeline Diagram - This is the most comprehensive diagram I've seen for the DirectX 9.0 Graphics Pipeline. As XNA is largely a managed wrapper around DirectX 9.0 this is almost all relevant. Note: This is not entirely accurate for Xbox 360, but should be 99% accurate for PC

Recently working on a project of my own, I realized that while I have taken Algebra and Geometric analysis, I still had a couple of concepts rather loose, there is also the issue that most formal math classes don't much care for the performance issues of complex functions like roots, sin, cosine, etc.

There is a great leap of knowledge between "Knowing math" and "Knowing 3D Games Math"

The best aproach I've found for my position is the book "3D Math Primer For Graphics And Game Development - Fletcher Dunn and Ian Parberry".
If you have the time to squeeze in another book in your schedule, it is more than worth it.

This is a free site with a TON of mathematical videos, including Geometry, Linear Algebra, etc. I found this site really useful when I was trying to learn (or relearn in some cases) some of the 3D mathematical concepts.

I have read your Tutorial #2 and now I'm going to read these chapters in the book, because I read them once and I didn't understand some things like VertexBuffer and VertexList (I don't know very well why I have to use them); let's see if now I understand more things.

As great as the content in this book is, I often have trouble following along with his coding. For example, the author tends to tell you to use variables before he tells you that you need to create them, too (see the paragraphs surrounding the InitializeVertices() function for a good example of this), which wreaks havoc upon anyone using an intellisense-enabled IDE -- which should be just about everyone -- and makes me think that I missed something somewhere prior to that section. This seems to be a very sloppy way of coding that can cause a lot of problems in the long run if people get used to shotgunning a bunch of code into the file and trying to go back and make sure they declared all their variables and set all their properties in the proper places.
He also does not make it very clear WHERE to put a lot of his code samples -- are they members of the Game1 class, part of a function, inside a loop?

I dont know how much time you guys have, but I have been reading the whole chapter first, and then going back and doing the coding. It helps with situations like that and also gives me a "big picture" prespective which I think is helpful.

External Articulation of Concepts Materializes Innate Knowledge of One's Craft and ScienceBeginner in Game Development? Read here. And read here.Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

How on earth can you go around telling lies, be shown that they are lies, then continue to ignore the evidence to the contrary. I'm not qualified to deal with this, sorry. - HodgmanNever crash to desktop. That's the equivalent of a Starbucks rep punching you in the face when you try to order a coffee with soymilk when they run out of soymilk. - KoobazaurFor programmers, R & D means "Research & Duplicate".Just remember... XML is like violence. If it doesn't work, you just aren't using enough of it... - MoeEvery time I go into my account settings I feel like I've never used the internet before. - booleanPerson X: Do you want to live forever?
Person Y: No, but I want to live a long life.
Person X: Only cowards live that long.
Person Y: And yet, we travel to seek the counsel of these "cowards".