Tuesday, September 27, 2011

Clamping Efficiency

Been poking around the XNA 4.0 framework, trying to figure out it all works together.
(I finally realized that Vector3 themselves are not absolute positions in a 3D spatial volume, but rather relative values until Transformed to absolutes. Right?)

I came across this line of code in an example: turnAmount = MathHelper.Clamp( turnAmount, -1, 1 );
So I dug into the Clamp function.

What I saw was this:
public static float Clamp(float value, float min, float max) {
      value = (double) value > (double) max ? max : value;
      value = (double) value < (double) min ? min : value;
      return value;
    }

Isn't this function possibly inefficient? First of all, it is casting the floats to doubles.
Why? Does the compiler ignore this 'feature' and optimize it away?

And also, if the value is between the min and max, it ends up setting the value to value.
Why? Does the compiler ignore this 'feature' and optimize it away?

I would change this Clamp function to this:
public static float Clamp( float value, float min, float max ) {
            return value > max ? max : ( value < min ? min : value );
        }

If these functions are used even once per Game.Update(), and that Game.Update is called at 60 times per second.. and each Model might use it..

I'm sure the efficiency could add up to a better performing game.

Friday, September 23, 2011

XNA oddity with VertexPositionNormalColored

Getting my feet wet on the XNA 4.0 framework.
Going along on the tutorial over at Reimers.

When I got to the lighting section, my game form started going blank.
No errors, just a big black background.

So after many (many) recopies and re-pastes from the webpage, I narrowed down the silent bug to the ordering of the fields of the struct "VertexPositionNormalColored" (which inherits from the interface "IVertexType") Position, Color, and Normal.

Here is the corrected version that doesn't go funky:


        [ StructLayout( LayoutKind.Explicit ) ]
        public struct VertexPositionNormalColored : IVertexType {
            public static readonly int SizeInBytes = 28;

            public static readonly VertexDeclaration VertexDeclaration =
                new VertexDeclaration( new[] {
                    new VertexElement( 0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0 ),
                    new VertexElement( sizeof ( float ) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0 ),
                    new VertexElement( sizeof ( float ) * 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0 )
                } );

            [ FieldOffset( 0 ) ]
            public Vector3 Position;

            [ FieldOffset( 12 ) ]
            public Color Color;

            [ FieldOffset( 16 ) ]
            public Vector3 Normal;

            public VertexPositionNormalColored( Vector3 p, Vector3 n, Color c ) {
                this.Position = p;
                this.Normal = n;
                this.Color = c;
            }

            VertexDeclaration IVertexType.VertexDeclaration { get { return VertexDeclaration; } }
        }

Thursday, September 22, 2011

"Programming" versus "Coding"

Reposted for posterity with a linkback.

Coding is about how you should write,
Programming is about how you SHOULDN'T!
Coding as about the Language and Syntax,
Programming is about the Paradigm, the Thought Process
Coding is represented by the 'Writing' Metaphor,
Programming is epitomized by the 'Construction' metaphor
Coding defines the Solution,
Programming defines the Problem too!
Coding creates the Implementation,
Programming creates the Interface
Coding is about "Getting it done...",
Programming is about "Getting to know it!"
Coding gets you Paid,
Programming makes you Satisfied.
Coding is what we HAVE to do,
Programming is what we SHOULD be doing!

Tuesday, September 6, 2011

C#.NET Script – Associative Array | Xhanch Studio

C#.NET Script – Associative Array | Xhanch Studio: " http://xhanch.com/" I don't understand why this class simply wraps a dictionary.. I don't get it.
What am I missing?

Thursday, September 1, 2011

Crossword Puzzle Generator in C#

Crossword Puzzle Generator – in C# — Pierich Consulting:
My comment on a comment in the code on that site: "// This next line is a hack that just makes the code work."
When I see comments like that I just have to /shudder.