Archive

Posts Tagged ‘c#’

Golly…Macs Really Do “Just Work”!

2011/03/13 Leave a comment

I was recently lucky enough to grab myself an iMac; in that time it has solved more problems than the time I have spent trying to solve a single problem on Windows.

Computer Specs

Memory Windows= iMac =4GB
CPU Windows=Intel Core 2 Extreme Quad @ 3GHz,  iMac=2.8GHz Quad-Core Intel Core i5
Video Windows=dual nVidia 8800 GTX,   iMac=ATI Radeon HD 5670


Before

Here is a list of my prior issues:

  • iTunes on PC and generation 1 Apple TV would “forget” their network connection
    • meaning my Apple TV could no longer sync/stream TV shows and movies from my PC
    • typically you buy on the Apple TV and after watching it is “archived” on your PC to make room for other purchases
    • I had lost a few purchases because of this.  Apple does not allow you to download to Apple TV again
  • Could not connect to a Canon MX850 printer via the network if it was connected to one PC via USB
    • basically allows you to share the printer over a single USB cable instead of a network cable
    • USB allows you to do certain things network cable cant
  • Lacie 4big Quadra 4TB RAID “big blue button” not recognized
    • allows for quick backup
    • Windows Backup is inefficient, slow and poorly designed
  • Access to Apple’s MobileMe email via Microsoft Outlook/Windows Live Essentials IMAP is incredibly slow – 5 seconds to delete a message
  • Opening the majority of any document takes considerable time on Windows
    • PDF
    • videos
    • Word/Excel documents
  • MSN Messenger takes considerable time to load; consumes excessive computer resources
  • Games such as the recent Civilization V consume excessive resources; takes considerable time to load considering nature of application – “its no Crysis!”
  • Windows Phone developer registration is more complex than applying for a home mortgage.  Took approximately 7 days to complete including various to and throw e-mails to Microsoft
  • Visual Studio has historically shown that with each new version requires a level of magnitude more computer memory and CPU resources.   VS is very expensive to buy for the Pro version. Express as a tool for serious development is questionable

After

  • Apple TV and iTunes on iMac just work; do not forget each other
  • Canon MX850 was automatically detected over wi-fi when plugged into Airport Extreme Base Station USB socket
  • Lacie 4big Quadra 4TB RAID was automatically detected and iMac kindly asked if I wished to use it for TimeMachine
    • Pressing big blue button on Lacie launched the brilliant TimeMachine app
    • TimeMachine is fast, intuitive, reliable
  • Apple’s iMail automatically configured e-mail server settings just by me typing in my @me.com address
    • iMail is fast
    • Like the central inbox
  • iMacs have a Preview application that very quickly opens a document for reading rather than loading bloatware Acrobat, Microsoft Word, Excel or similar
  • MS Messenger for iMac (via Office Mac) is much like pre-Windows Live Essentials meaning it doesn’t have the annoying ads but more importantly, uses less computer resources like memory and CPU in wlcomm.exe
  • Civilization V for iMac loads considerably quicker and exits to mac desktop much faster than Windows
  • Apple’s Mac Developer MacOS/iOS registration completed in 12 hours
  • Apple’s Xcode IDE is only $5.00 and uses minimal CPU and memory
  • Though Apple may make use of seemingly vertical standards such as Bonjour, historically it can be shown that Apple is more likely to adopt more popular standards with more realized use cases than say MS.  Calendering is a good example.

In summary, in only a short time does one really appreciate the high quality of the iMac, but also comes to the realization that pretty much everything PC is by way of poorly designed, badly integrating bloatware produced by capitalist-corporate-hugging software houses.   Take Microsoft Office 2010 is not only bloated but is also unintuitive and bloated; similarly for VS2010 as mentioned.

Tutorial: On the Fly Effect, Material and Model Mapping

2009/08/22 15 comments

UPDATE: be sure to check out my YouTube channel where you can see live-action of content such as this article as well is other subjects.

 

By using a ModelProcessor custom content pipeline processor you can import 3D assets and automatically have the XNA Model class linked to the appropriate textures and HLSL FX code.  No more individual code for loading effects, models and no more horrid manual mapping (or should I say re-mapping) of effects to models mesh parts.

LoadContent() now looks as clean as:

_stoneyBox = Content.Load<Model>(“models/stoneybox”);

 

So How is This Done?

In the case of FBX file formats, there is a wonderful amount of metadata that is sadly being ignored by our old friend BasicEffect and the standard model import pipeline.  By doing some of our own magic we can access this data and automatically connect shaders, textures and models together.

Add your model to your Content folder, this should be a FBX, at this stage X does not not seem to have all the smarts in it.

Right-click your Solution and choose Add.New Project…

Then in the appearing window, select Content Pipeline Extension Library and do the usual filename stuff, adding it to your solution.

image

This will create a new project with a default class.  Delete what is there and replace with something like the following.

[ContentProcessor(DisplayName = “Custom Effect Model Processor – MickyD”)]
public class CustomEffectModelProcessor : ModelProcessor

{

protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context)
{
EffectMaterialContent myMaterial = new EffectMaterialContent();

if (material is BasicMaterialContent)
{
Log(context, “Material is basic”);

// do appropriate basic stuff here
}
else if (material is EffectMaterialContent)
{
EffectMaterialContent effectMaterialContent = (EffectMaterialContent) material;

//
// remap effect
//
myMaterial.Effect = new ExternalReference<EffectContent>(effectMaterialContent.Effect.Filename);

// textures
foreach (KeyValuePair<string, ExternalReference<TextureContent>> pair in effectMaterialContent.Textures)
{
string textureKey = pair.Key;
ExternalReference<TextureContent> textureContent = pair.Value;

if (!string.IsNullOrEmpty( textureContent.Filename))
{
myMaterial.Textures.Add(textureKey, material.Textures[textureKey]);
Log(context, “Set texture ‘{0}’ = {1}”, textureKey, textureContent.Filename);
}
}

}

return base.ConvertMaterial(myMaterial, context);
}

Compile your solution making sure everything is fine. OK, now we have a custom pipeline in the solution but nothing is actually using it yet.  We do have that FBX we added before, wouldn’t it be schmick to have it use the pipeline?

Well first we need to add a reference to the pipeline project to our main project.  Once done we get a cool new drop-down option on model’s Content Processor property.  In your main project, right-click Content (NOTE it’s the Content references we want NOT project references), choose Add reference and add a reference to your pipeline project on the Projects tab.

image

If all goes well it should look something like this:

image

Back in your main project (the one with your Content folder holding your assets anyway), right-click your FBX model to bring up the Properties window.  If you look there should be a new choice in the Content Processor field. Fill it out as follows with particular attention to:

  • Content Processor = (whatever your custom effect model processor is)
  • Generate Tangent Frames = true
  • Scale = … (depends on your model)
  • Swap Winding Order = true EDIT: actually leave this to false

image

Now we can load the model in our LoadContent() as follows:

_stoneyBox = Content.Load<Model>(“models/stoneybox”);

Note though that we still need a reference to the effect so that we can pass parameters.  (unless of course your shader has none)

_stoneyEffect = _stoneyBox.Meshes[0].Effects[0];

Note this is the complete reverse of the usual tutorials – which load the model AND effect and then re-apply the effect to the model!

In my draw function I do need to pass though the light position.  I guess if it wasn’t moving (which it wasn’t) I could just do it during LoadContent().  Oh well.

_stoneyEffect.Parameters[“light1Pos”].SetValue(light1Pos);

Here you can see our friend the cube, but with a:

  • diffuse texture map
  • normal bump map
  • diffuse color
  • specular highlights

image

And here’s a shoot of a few buildings.  Each building is a single FBX model with multiple FX (or materials) in XNA (glass, bricks, tar roof)

image

That’s it.  With this technique, there is minimal code required to load your models with the correct textures and effects.  What’s more it is only necessary to add your model to your project – not the textures and effects.  I like to export my models out of 3DS Max directly into my Content/Models folder.  I don’t tell Max to export the textures but rather have it just put the path into the FBX so that the pipeline can load my textures directly out of material library folder at compile time.  This works for me, but understandably it may not suit others.  That is fine.

 

EDIT: The draw code for drawing a XNA model with one or more custom effects is as below:

/// <summary>
///     Draws the with custom effect.
/// </summary>
/// <param name = “model”>The model.</param>
/// <param name = “world”>The world.</param>
/// <param name = “viewMatrix”>The view matrix.</param>
/// <param name = “projectionMatrix”>The projection matrix.</param>
/// <param name = “offset”>The offset.</param>
public static void DrawWithCustomEffect(Model model,
Matrix world,
Matrix viewMatrix,
Matrix projectionMatrix,
Vector3 offset)
{
var transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);

var translation = Matrix.CreateTranslation(offset);
foreach (var mesh in model.Meshes)
{
foreach (var effect in mesh.Effects)
{
var w = transforms[mesh.ParentBone.Index] * world * translation;

effect.CurrentTechnique = effect.Techniques[“Complete”];
effect.Parameters[“wvp”].SetValue(w * viewMatrix * projectionMatrix);
effect.Parameters[“worldI”].SetValue(Matrix.Invert(w));
effect.Parameters[“worldIT”].SetValue(Matrix.Transpose(Matrix.Invert(w)));

effect.Parameters[“viewInv”].SetValue(Matrix.Invert(viewMatrix));

effect.Parameters[“world”].SetValue(w);

foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
mesh.Draw();
}
}
}
}

Categories: Development Tags: , , , ,

XNA Sanity Check

2009/08/22 Leave a comment

Over the past few years I’ve had an on-again off-again affair with the now defunct Microsoft Managed DirectX (MDX) then XNA (no there is no acronym).  My interest has generally been making a modeller for editing all things 3D but usually resulted in me getting stuck on something or other or leaving in a huff to play an equally frustrating pastime – WoW.

Recently I’ve come back to XNA, XNA 3.1 to be exact and have found it to be rather good with lots of useful features to allow novices such as myself to create something pretty darn good. That’s the theory anyway.  After one gets tired of creating spinning teapots, you discover how ridiculously hard it is to say:

  • Create a model in either XSI Mod Tool or 3DS Max (my preference)
  • Create model materials such as plastics, metals, enviro maps and so on
  • Export it in a format that XNA supports
  • Find out why the imported model in XNA has lost all materials!
  • Find the best model file format

Case in point – importing a model with custom HLSL effects usually results in surface-challenged blank model.  One idea is to make a custom model processor pipeline component that can read all the effect and material metadata.  If you are keen on the X file format (no I don’t mean Mulder and Scully), well stiff bickies because X apparently does not include string names for materials.  FBX does!  At this stage of my feeble knowledge, I can not fathom why many people keep pushing X over FBX in XNA (maybe it’s because X is Microsoft?).  Then again, many of the tutorials from the XNA website use FBX anyway. Hmm.  Maybe I’ll learn later.  I would have thought materials to be pretty important though.

So, as I write this down, I have discovered after many frustrating weekends, how mind-bogglingly difficult it has been to start with something in say 3DS Max and end up with a wonderful looking model in my XNA application.

Now there are many so called XNA tutorials out there, but in my opinion they are so basic as to offer no use in a realistic game, or far too complicated that during the explanation of solving a problem they mention far too many other things that just leaves you even more confused.

Stay tuned.

Categories: Development Tags: , , ,