Sign in to follow this  

XNA - NeHe Style (Projects 1 - 10) Source Code

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hail All, Short Form: I've implemented the first NeHe tutorial in XNA. The project and related files are located here. Features: - 800x600 Resizeable Window - Fullscreen Toggleable (F1 Key) - Mouse is visible in Windowed Mode, Hidden in Full-Screen Mode - Update and Draw only if Window has Focus - 16 bit(565) BackBuffer (Same as NeHe's Tutorials) - Clears BackBuffer and DepthBuffer on Draw Call - Terminates with ESC Key Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc... Long Form: Through a series of fortunate events I've suddenly found myself in a situation of needing to learn XNA relatively quickly. For me, the best way to learn something new is to just give it a try, "kick the tires" so to speak, and see what I can do with the framework or language. In the spirit of that, I've decided to implement all of the NeHe tutorials in XNA. I'm sure it has probably been done before already, but that doesn't solve my immediate problem of needing to learn the framework, and learning best through experience. At any rate, I just wanted to post here and make my XNA Framework, as implemented in NeHe's Tutorial 1, available to everyone. Please feel free to provide feedback, and as I work my way through the tutorials I'll continue to post my projects, etc... Once again, the download is located here. Updates/Errata: While working on project #2 I found a bug in the ResetProjection() method. I will update it in the downloadable source. However, if you want to make the changes locally, it should read:
protected void ResetProjection()
{
    Viewport viewport = graphics.GraphicsDevice.Viewport;

    // Set the Projection Matrix.  Make sure to cast one of the viewport sides
    // to a FLOAT.  Otherwise it's truncated, and you dont get a correct aspect ratio.
    projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
        (float)viewport.Width / viewport.Height, 0.1f, 100.0f);
}









Cheers! [Edited by - JWalsh on September 25, 2007 1:03:27 PM]

Share this post


Link to post
Share on other sites
JWalsh, I'm surprised you have time to get anything job-related done with all the things you do around here [lol]. I love the idea of doing some NeHe-style XNA tutorials (though, hopefully with some better code than NeHe). Can't look at the code ATM but I'll provide more feedback when I can

Share this post


Link to post
Share on other sites
Project 2 Completed!

I've implemented the second NeHe tutorial in XNA. The project and related files are located here.

Features:
- Displays two white shapes drawn with TriangeStrips

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Updates/Errata
9/19/2007, 3:00pm PT
I've discovered that you can embed changes to the effect object, such as transforms, etc...within a call to Begin() on the effect and pass, so long as you call CommitChanges() before rendering additional primitives.

Additionally, I reverted the BackBuffer to it's default value - a 32bit ARGB format, rather than a 16bit BGR. The smooth shading on the triangle of project 3 looks MUCH better.

9/20/2007, 2:00pm PT
Uploaded new source which corrected a problem with resources not being correctly restored when a device was lost.



[Edited by - JWalsh on September 20, 2007 3:41:02 PM]

Share this post


Link to post
Share on other sites
Cool. How far do you think you will be going with these tutorials? Just until you know enough to get away with what you were needing to do?

With the increasing popularity of XNA, it might be worth while to continue (or find someone else to continue) the tutorials...

That, and I wouldn't worry about setting it to a 16 bit back buffer. If XNA handles it, let XNA do it. Besides, when is the last time that a video card was released that didn't support at least a 24 bit depth buffer?

Share this post


Link to post
Share on other sites
Project 3 Completed!

I've implemented the third NeHe tutorial in XNA. The project and related files are located here.

Features:
- The Triangle is drawn with Vertex Coloring enabled
- The Rectangle is drawn with the VC Disabled, but with a specified diffuse material

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Updates/Errata
9/20/2007, 2:00pm PT
Uploaded new source which corrected a problem with resources not being correctly restored when a device was lost.



[Edited by - JWalsh on September 20, 2007 3:45:51 PM]

Share this post


Link to post
Share on other sites
Project 4 Completed!

I've implemented the fourth NeHe tutorial in XNA. The project and related files are located here.

Features:
- The Triangle is rotated CCW about the Y-Axis
- The Rectangle is rotated CW about the X-Axis

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Updates/Errata
9/20/2007, 2:00pm PT
Uploaded new source which corrected a problem with resources not being correctly restored when a device was lost.



[Edited by - JWalsh on September 20, 2007 3:10:18 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Moe
JWalsh - you must be a mad man! If you keep churning these out at this rate, it'll only take a week to match NeHe!


Hehe, Thanks Moe.

Unfortunately, the speed at which I'm able to pump these out has more to do with XNA's ease of use than my sanity or mastery of the technology. I'm really just learning as I go along.

As an example, the OpenGL implementation of Project 4 was approximately 425 LoC. XNA, only 230 LoC.

While everyone now realizes "Lines-of-Code" isn't an effective measurement on which to base the efficiency or complexity of an algorithm or API, it does suggest that even with having to create a vertex declaration, a BasicEffect, and Transforms for both the triangle and rectangle (none of which is required in OpenGL), XNA is still a very powerful framework, and does a lot of the heavy lifting for you.

Finally we've got a DirectX technology that matches OpenGL in ease of use.

Cheers!

Share this post


Link to post
Share on other sites
Project 5 Completed!

I've implemented the fifth NeHe tutorial in XNA. The project and related files are located here.

Features:
- Backface Culling has been re-enabled
- The triangle and square Triangle Strips have been replaced with Pyramid and Cube Triangle Lists
- The cube rotates on all 3 axes to make more sides visible

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Updates/Errata
9/20/2007, 2:00pm PT
Uploaded new source which corrected a problem with resources not being correctly restored when a device was lost.



[Edited by - JWalsh on September 20, 2007 3:04:54 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
1) I thought the general consensus here was that NeHe is bad for you.


I always thought that NeHe's implementation was bad for you, in principle there's nothing wrong with it (except, perhaps, in creating more copy & paste beginners).

Share this post


Link to post
Share on other sites
Project 6 Completed!

I've implemented the sixth NeHe tutorial in XNA. The project and related files are located here.

Features:
- Pyramid and Cube are now rendered with a texture map
- Pyramid and Cube no longer have vertex colors
- Fixed a bug causing resources to not be re-allocated on device lost (previous source code has been updated)

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...



[Edited by - JWalsh on September 20, 2007 3:30:43 PM]

Share this post


Link to post
Share on other sites
Project 7 Completed!

I've implemented the seventh NeHe tutorial in XNA. The project and related files are located here.

Features:
- Pyramid was removed, cube is now transformed to the center of the screen
- Cube is textured with a Crate texture using the Mipmapped Texture Importer
- "F" key toggles 3 different filtering modes: None, Linear, and Linear Mipmapped
- "L" key toggles on/off lighting
- "Up", "Down", "Left" and "Right" modifies rotation speed about local X and Y Axes
- "PageUp" and "PageDown" translates the cube in/out of the screen
- Fixed a bug causing previous key state not to be cached for later comparison

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Share this post


Link to post
Share on other sites
Project 8 Completed!

I've implemented the eighth NeHe tutorial in XNA. The project and related files are located here.

Features:
- Press "B" to toggle on/off Frame Buffer Alpha Blending
- Fixed a bug causing lighting to be reset if the device was lost

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Share this post


Link to post
Share on other sites
I'm going to be out of town for the weekend, so I wont be able to contribute to the XNA NeHe projects. But please feel free to provide any comments you might have about the source code, style, etc...

If you'd like to see projects completed based around a specific XNA topic, please speak up. I dont need to stick to NeHe's tutorials precisely. This is just a way for me to become familiar with the API, so I'm happy implement stuff beyond the NeHe tutorials.

Cheers!

Share this post


Link to post
Share on other sites
Checking out some of the projects, I noticed you use the BasicEffect class rather than writing your own (simple) shaders. You could argue that mimicking the fixed pipeline like this is okay for very small demos like this, but since XNA really depends of the use of shaders, I would introduce them even in the early tutorials. Before things get more complicated and the use of shaders can realistically no longer be avoided, at least anyone who has used these projects as a learning tool will hopefully have a basic understanding of HLSL and the programmable pipeline by then.

Share this post


Link to post
Share on other sites
Quote:
Original post by WanMaster
Checking out some of the projects, I noticed you use the BasicEffect class rather than writing your own (simple) shaders. You could argue that mimicking the fixed pipeline like this is okay for very small demos like this, but since XNA really depends of the use of shaders, I would introduce them even in the early tutorials. Before things get more complicated and the use of shaders can realistically no longer be avoided, at least anyone who has used these projects as a learning tool will hopefully have a basic understanding of HLSL and the programmable pipeline by then.


Wan,

Thanks for your suggestion. I'd considered making all of them use HLSL, rather than relying on the built in shader which is part of BasicEffect, however I couldn't justify writing a simple shader for use by all these beginning tutorials when there is a perfectly good one included in XNA.

Also, I wanted to try and fit the XNA implementations as closely as possible with the original OpenGL versions, and you'll note those weren't written in GLSL.

Finally, I follow a strict programming paradigm which is made up of several "rules." One of which is "Code by Necessity." Which simply means, if I dont have a reason to write it, I dont. Writing my own shader for these while a simple task, unnecessarily increases the lines of code, the development time, and the debug and maintenance time. Meanwhile, using the built in shader for the basic effects comes for free.

As soon as shaders are a necessity, they will be used. Even with that, however, I may go back and plug in a basic shader for the earlier projects, just so people can see how a simple shader is implemented.

Thanks again for the suggestion!

Cheers!

Share this post


Link to post
Share on other sites
Quote:
Original post by WanMaster
Checking out some of the projects, I noticed you use the BasicEffect class rather than writing your own (simple) shaders. You could argue that mimicking the fixed pipeline like this is okay for very small demos like this, but since XNA really depends of the use of shaders, I would introduce them even in the early tutorials. Before things get more complicated and the use of shaders can realistically no longer be avoided, at least anyone who has used these projects as a learning tool will hopefully have a basic understanding of HLSL and the programmable pipeline by then.


BasicEffect doesn't mimic the fixed-funtion pipeline. It's just a pre-written effect that makes perfect sense to use. There is no reason, at this stage, that anyone would justify learning and writing HLSL. If these are the top graphics for someone's game, they would be angered to have taken the time to learn HLSL only to find out the BasicEffect is a perfectly good replacement. Not to mention with BasicEffect you have properties to modify instead of setting effect parameters, something that is very prone to programmer error.

I love all the tutorials so far. I haven't checked out the code yet, but did you do any sorting on the translucent cube to ensure backfaces were drawn first?

Share this post


Link to post
Share on other sites
Quote:
I love all the tutorials so far. I haven't checked out the code yet, but did you do any sorting on the translucent cube to ensure backfaces were drawn first?


No. I cheated like NeHe. [grin] My focus was more on enabling alpha blending and playing with the source and dest blend modes than on determining which triangle needed to be drawn in which order.

This of course has side effects, but I wanted to focus on the more XNA specific details, and less on the more algorithmic topic. However, you're welcome to modify that project and submit your updates.

Cheers!

Share this post


Link to post
Share on other sites
Project 9 Completed!

I've implemented the ninth NeHe tutorial in XNA. The project and related files are located here.

Features:
- Renders 50x star textures to an animated 3D position in world-space
- Renders each star with alpha blending against a randomly generated color
- Press "T" to toggle on/off Twinkling
- Up/Down arrow keys tilts the nebula on the X-Axis
- PageUp/PageDown zooms in and out

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Share this post


Link to post
Share on other sites
Project 10 Completed!

I've implemented the tenth NeHe tutorial in XNA. The project and related files are located here.

Features:
- Loads a 3D "room" from a file using System.IO classes
- Renders the room with Linear Filtering and Mipmapping
- 'F' toggles through the three Texture Filter Modes previously explored
- A,W,S,D allows FPS-style movement around the room
- PageUp/PageDown Tilts the camera up/down
- Has a little head-bob animation while moving to add realism

Anyone interested in downloading the zip file, checking out the code, running the program etc...are encouraged. Please provide feedback about coding style, implementation details, etc...

Share this post


Link to post
Share on other sites
Okay, I just took the opportunity to go through the first four lessons. They are pretty good, but there are a few things that I found that bothered me.

First, I prefer to have all my member variables in the Game class up at the top. It lets me see what I am going to use before using it. I know this isn't much of an issue with Visual C# 2005, as you can just mouse over a variable to see what type it is, and right click to go to the definition if you want. I just prefer to see what I am going to be working with in a tutorial before I use it.

One thing that threw me for a loop when I first saw it was the IsFullScreen property. It took me a little while to realize what was happening. I personally would have made a separate function for it, but the property does work nicely. Again, just personal preference.

The one thing that I did see that might be a cause of problems is how you are handling your input. You do set the value of prevKeyboardState when the variables are first initialized, but you never set it after that. Should you be setting to to something each time your Update() is called, like so?


//member variable...
KeyboardState currentKeyboardState;

//in the Update() function...
prevKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();

Either that, or I am missing something altogether (a likely scenario, as I haven't done much programming in the past few months).

Other than that, I would say they are pretty darn good. I like the logical progression of steps thus far. I will most definitely have to find the time and go through the other ones as well.

Share this post


Link to post
Share on other sites

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this