Tuesday, November 28, 2006

Rendering a textured cube


XNA Game Studio Express provides a number of helpful how-to guides. One such guide demonstrates the use of the framework's default material class "BasicEffect" to render a cube using 3D primitives.

When you run the sample code, you will see a colored cube rendered on screen (seen on the right).

That great. So now how do you render a bitmap textured cube?

Initially, I got a black cube instead of the expected rendered one. After tinkering around, I have managed to load a bitmap and assigned it to the basic effect material.

Here are a couple of learning points:
  1. To use bitmap textures, you need to :
    • Assign a loaded texture to the BasicEffect.Texture property
    • Enable texture rendering, set BasicEffect.TextureEnabled to true
  2. The framework calls Initialize() before LoadGraphicsContent(). Hence you should only assign your texture reference to the basic-effect material after you have loaded the bitmap.
Here is a screenshot of modded cube on the right. You can also download the project files here (UseBasicEffect.zip).

Please let me know if the link works and other comments.

Monday, November 20, 2006

Getting Mouse Input for the XNA 3D Tutorial

As mentioned in my previous post, there is support for mouse input in the XNA framework. I have added code to check for mouse input in the UpdateInput() method of Game1.cs as shown below:


protected void UpdateInput()
{
//get the gamepad state
GamePadState current_state = GamePad.GetState(PlayerIndex.One);
if (current_state.IsConnected)
{
//original gamepad checking goes here
}
else
{
MouseState mouseState = Mouse.GetState();
Rectangle rt = this.Window.ClientBounds;
if (mouseState.LeftButton == ButtonState.Pressed)
{
modelrotation -= (mouseState.X - rt.Width/2) * 0.01f;
//create some velocity if the right trigger is down
Vector3 modelvelocity_add = Vector3.Zero;
//find out what direction we should be thrusting, using rotation
modelvelocity_add.X = -(float)Math.Sin(modelrotation);
modelvelocity_add.Z = -(float)Math.Cos(modelrotation);
//finally, add this vector to our velocity.
modelvelocity += modelvelocity_add;
}
//in case you get lost, warp back to the center
if (mouseState.RightButton == ButtonState.Pressed)
{
modelposition = Vector3.Zero;
modelvelocity = Vector3.Zero;
modelrotation = 0.0f;
}
//set some audio based on whether we're pressing trigger
if (mouseState.LeftButton == ButtonState.Pressed)
{
if (engine_sound == null)
{
engine_sound = sound_bank.GetCue("engine_2");
engine_sound.Play();
}
else if (engine_sound.IsPaused)
{
engine_sound.Resume();
}
}
else
{
if (engine_sound != null && engine_sound.IsPlaying)
{
engine_sound.Pause();
}
}
//in case you get lost, press A to warp back to the center
if (mouseState.RightButton == ButtonState.Pressed)
{
//make a sound when we warp
sound_bank.PlayCue("hyperspace_activate");
}
Mouse.SetPosition(rt.Width / 2, rt.Height / 2);
}
}

Hold down the left button of the mouse adds thrust to the space ship. Turning the mouse while holding down the left button rotates the ship. Right click to warp the ship back to the center of the screen.

The first part of the code computes the ship's velocity based on the mouse's position and button states. The second added the appropriate audio cues based on the mouse events.

Sunday, November 19, 2006

Getting Keyboard Input for the XNA 3D Tutorial

If you tried out the 3D tutorial, you will notice that it is geared towards the Xbox. The tutorial introduces the framework's support for Xbox controllers.

That's ok, except that I am trying to learn how to use XNA to develop Windows games and applications. It turns out that there is support for keyboard and mouse as well.

I have added the following lines of code into the beginning of the Update() method of the Game1.cs class file.



KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
this.Exit();


Now, you can terminate the runtime window by hitting the Escape key.

Spacewar (Windows) Full Screen Tip

Just a tip, you can toggle between full screen and windowed mode of the Spacewar demo by hitting the Alt-Enter combo.

Don't think there is any help documentation published but you can figure this out by looking at the following code fragment from SpacewarGame.cs:



protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
XInputHelper.Update(keyState);

if ((keyState.IsKeyDown(Keys.RightAlt) ||
keyState.IsKeyDown(Keys.LeftAlt)) &&
keyState.IsKeyDown(Keys.Enter) &&
!justWentFullScreen)
{
ToggleFullScreen();
justWentFullScreen = true;
}
}


Btw, I am looking at the windows code for Spacewar. So whatever Spacewar post will refer to just that.

Thursday, November 16, 2006

Space War Class Relationship Diagram

I was pleasantly suprised that Microsoft has released such a complete example to illustrate the capability of the XNA framework. Enuff said.


As my first look at the Space War example, I have mapped out the class relationships. Specifically, the inheritance relationships between the classes. I have outlined a few 'containment' relationships as well where they don't clutter up the diagram.

Let me know if it is of any help to you.