Friday, May 14, 2010

BasicEffect in XNA

All right, so as I'll probably elaborate at some point in the future I've been working on learning programming for just over a year now and have been working on a game project in C# using Microsoft's XNA framework. Well, to be more exact I've been learning how to use it which has been going on since about January.

I'll talk more about the project in another post, but I just finished spending several hours (and several more flipping to other things since I couldn't figure out was wrong and figured I needed to step back for a bit) trying to draw a simple little triangle to the screen. More specifically, I'm working with primitives in a 3D environment and was trying to get a simple triangle with colored vertices.

It's simple enough to draw basic sprites, or even 3D models to the screen (though the models require matrix transformations, but even if you haven't done linear algebra or worked with matrices they're not that hard to implement if you follow a straight forward tutorial and try to grasp the concept). Primitives shouldn't be that much harder, even with having to work with implementing shader effects (in XNA you can use their BasicEffect class which isn't too complex if you don't want it to be), but it still proved to be frustrating - though this was more due to (my) human error than anything about the task itself.

I've been using this site, and I highly recommend his tutorials for anyone interested in working with XNA. He doesn't really get into object-oriented approaches, which start to get important when you're working on a full game and your code gets long and complex, but if you're jumping into XNA you should have a decent understanding of C# already. Anyway, I implemented his code pretty much exactly as he shows it - the difference being that I created my own primitives class since I'm going to be working with primitives in my project.

I combed through the code and didn't see anything, so then tried copying his implementation into a new project (he provides the source code) and it worked fine. From there, I cross-referenced everything that was going on to compare it to my code, tried putting parts of my code into the main class to test it, etc; but nothing worked. I just got a screen with the default blue background. Turns out it was something simple.
vertices = new VertexPositionColor[3];
vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Red);
vertices[0] = new VertexPositionColor(new Vector3(0.5f, 0, 0), Color.Green);
vertices[0] = new VertexPositionColor(new Vector3(-0.5f, 0, 0), Color.Blue);
I created and recreated the same vertice three times instead of creating 3 different vertices. Just goes to show how debugging can be (and from what I've experienced, usually is) the most frustrating part of the process. Of course, it felt great to figure out what the problem was (and I suppose that I gained a much more thorough understanding of the GraphicsDevice and SimpleEffect classes since I was getting pretty certain that the problem lied with one of those two).

No comments:

Post a Comment